message.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. var apis = require( './apis' );
  2. var rongrequest = require( './rongrequest' );
  3. exports.private = {};
  4. exports.private.publish = exports.publish = function( fromUserId, toUserId, objectName, content, pushContent, pushData, format, callback ) {
  5. var requestParams = {
  6. fromUserId : fromUserId,
  7. toUserId : toUserId,
  8. objectName : objectName,
  9. content : typeof content === 'object' ? JSON.stringify(content) : content
  10. }
  11. var f = arguments[ arguments.length - 2 ];
  12. var formatSpecified = f === 'json' || f === 'xml';
  13. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  14. callback = arguments[arguments.length - 1];
  15. var optionalArgs = { '4' : 'pushContent', '5' : 'pushData' };
  16. for( var i=4; i<=indexOfLastParam; ++i ) {
  17. if( optionalArgs[i] ) {
  18. requestParams[ optionalArgs[i] ] = arguments[i];
  19. }
  20. }
  21. rongrequest.request( apis['message']['private']['publish'], requestParams, formatSpecified ? f : 'json', function( err, resultText ) {
  22. return callback( err, resultText );
  23. } );
  24. }
  25. exports.private.publish_template = function( fromUserId, toUserIDs, objectName, content, values, pushContent, pushData, format, callback ) {
  26. var requestParams = {
  27. fromUserId : fromUserId,
  28. objectName : objectName,
  29. content : typeof content === 'object' ? JSON.stringify(content) : content,
  30. toUserId : toUserIDs,
  31. values : values,
  32. pushContent : pushContent,
  33. pushData : pushData
  34. }
  35. rongrequest.requestWithHeaders( apis[ 'message']['private']['publish_template'], requestParams, format, { 'Content-Type' : 'application/json' }, function( err, resultText ) {
  36. return callback( err, resultText );
  37. } );
  38. }
  39. exports.system = {};
  40. exports.system.publish = function( fromUserId, toUserIDs, objectName, content, pushContent, pushData, format, callback ) {
  41. var requestParams = {
  42. fromUserId : fromUserId,
  43. objectName : objectName,
  44. content : typeof content === 'object' ? JSON.stringify(content) : content
  45. }
  46. var f = arguments[ arguments.length - 2 ];
  47. var formatSpecified = f === 'json' || f === 'xml';
  48. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  49. callback = arguments[arguments.length - 1];
  50. var optionalArgs = { '4' : 'pushContent', '5' : 'pushData' };
  51. for( var i=4; i<=indexOfLastParam; ++i ) {
  52. if( optionalArgs[i] ) {
  53. requestParams[ optionalArgs[i] ] = arguments[i];
  54. }
  55. }
  56. rongrequest.requestWithSameFields( apis[ 'message']['system']['publish'], requestParams, [ { field : 'toUserId', values : toUserIDs } ], formatSpecified ? f : 'json', function( err, resultText ) {
  57. return callback( err, resultText );
  58. } );
  59. }
  60. //exports.system.publish_template = function( fromUserId, toUserIDs, objectName, values, content, pushContent, pushData, format, callback ) {
  61. //
  62. //}
  63. exports.group = {};
  64. exports.group.publish = function( fromUserId, toGroupId, objectName, content, pushContent, pushData, format, callback ) {
  65. var requestParams = {
  66. fromUserId : fromUserId,
  67. toGroupId : toGroupId,
  68. objectName : objectName,
  69. content : typeof content === 'object' ? JSON.stringify(content) : content
  70. }
  71. var f = arguments[ arguments.length - 2 ];
  72. var formatSpecified = f === 'json' || f === 'xml';
  73. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  74. callback = arguments[arguments.length - 1];
  75. var optionalArgs = { '4' : 'pushContent', '5' : 'pushData' };
  76. for( var i=4; i<=indexOfLastParam; ++i ) {
  77. if( optionalArgs[i] ) {
  78. requestParams[ optionalArgs[i] ] = arguments[i];
  79. }
  80. }
  81. rongrequest.request( apis[ 'message']['group']['publish'], requestParams, formatSpecified ? f : 'json', function( err, resultText ) {
  82. return callback( err, resultText );
  83. } );
  84. }
  85. exports.chatroom = {};
  86. exports.chatroom.publish = function( fromUserId, toChatroomId, objectName, content, pushContent, pushData, format, callback ) {
  87. var requestParams = {
  88. fromUserId : fromUserId,
  89. toChatroomId : toChatroomId,
  90. objectName : objectName,
  91. content : typeof content === 'object' ? JSON.stringify(content) : content
  92. }
  93. var f = arguments[ arguments.length - 2 ];
  94. var formatSpecified = f === 'json' || f === 'xml';
  95. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  96. callback = arguments[arguments.length - 1];
  97. var optionalArgs = { '4' : 'pushContent', '5' : 'pushData' };
  98. for( var i=4; i<=indexOfLastParam; ++i ) {
  99. if( optionalArgs[i] ) {
  100. requestParams[ optionalArgs[i] ] = arguments[i];
  101. }
  102. }
  103. rongrequest.request( apis[ 'message']['chatroom']['publish'], requestParams, formatSpecified ? f : 'json', function( err, resultText ) {
  104. return callback( err, resultText );
  105. } );
  106. }
  107. //exports.discussion = {};
  108. //exports.discussion.publish = function( fromUserId, toDiscussionId, objectName, content, pushContent, pushData, format, callback ) {
  109. //
  110. //}
  111. /**
  112. *
  113. */
  114. exports.broadcast = function( fromUserId, objectName, content, pushContent, pushData, format, callback ) {
  115. var requestParams = {
  116. fromUserId : fromUserId,
  117. objectName : objectName,
  118. content : typeof content === 'object' ? JSON.stringify(content) : content
  119. }
  120. var f = arguments[ arguments.length - 2 ];
  121. var formatSpecified = f === 'json' || f === 'xml';
  122. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  123. callback = arguments[arguments.length - 1];
  124. var optionalArgs = { '3' : 'pushContent', '4' : 'pushData' };
  125. for( var i=4; i<=indexOfLastParam; ++i ) {
  126. if( optionalArgs[i] ) {
  127. requestParams[ optionalArgs[i] ] = arguments[i];
  128. }
  129. }
  130. rongrequest.request( apis['message']['broadcast'], requestParams, formatSpecified ? f : 'json', function( err, resultText ) {
  131. return callback( err, resultText );
  132. } );
  133. }
  134. exports.history = function( dateString, format, callback ) {
  135. rongrequest.request( apis['message']['history'], {
  136. date : dateString
  137. }, format, function( err, resultText ) {
  138. return callback( err, resultText );
  139. } );
  140. }