message.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 : 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']['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 : 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 : 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.group = {};
  61. exports.group.publish = function( fromUserId, toGroupId, objectName, content, pushContent, pushData, format, callback ) {
  62. var requestParams = {
  63. fromUserId : fromUserId,
  64. toGroupId : toGroupId,
  65. objectName : objectName,
  66. content : content
  67. }
  68. var f = arguments[ arguments.length - 2 ];
  69. var formatSpecified = f === 'json' || f === 'xml';
  70. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  71. callback = arguments[arguments.length - 1];
  72. var optionalArgs = { '4' : 'pushContent', '5' : 'pushData' };
  73. for( var i=4; i<=indexOfLastParam; ++i ) {
  74. if( optionalArgs[i] ) {
  75. requestParams[ optionalArgs[i] ] = arguments[i];
  76. }
  77. }
  78. rongrequest.request( apis[ 'message']['group']['publish'], requestParams, formatSpecified ? f : 'json', function( err, resultText ) {
  79. return callback( err, resultText );
  80. } );
  81. }
  82. exports.chatroom = {};
  83. exports.chatroom.publish = function( fromUserId, toChatroomId, objectName, content, pushContent, pushData, format, callback ) {
  84. var requestParams = {
  85. fromUserId : fromUserId,
  86. toChatroomId : toChatroomId,
  87. objectName : objectName,
  88. content : content
  89. }
  90. var f = arguments[ arguments.length - 2 ];
  91. var formatSpecified = f === 'json' || f === 'xml';
  92. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  93. callback = arguments[arguments.length - 1];
  94. var optionalArgs = { '4' : 'pushContent', '5' : 'pushData' };
  95. for( var i=4; i<=indexOfLastParam; ++i ) {
  96. if( optionalArgs[i] ) {
  97. requestParams[ optionalArgs[i] ] = arguments[i];
  98. }
  99. }
  100. rongrequest.request( apis[ 'message']['chatroom']['publish'], requestParams, formatSpecified ? f : 'json', function( err, resultText ) {
  101. return callback( err, resultText );
  102. } );
  103. }
  104. /**
  105. *
  106. */
  107. exports.broadcast = function( fromUserId, objectName, content, pushContent, pushData, format, callback ) {
  108. var requestParams = {
  109. fromUserId : fromUserId,
  110. objectName : objectName,
  111. content : content
  112. }
  113. var f = arguments[ arguments.length - 2 ];
  114. var formatSpecified = f === 'json' || f === 'xml';
  115. var indexOfLastParam = formatSpecified ? arguments.length - 3 : arguments.length - 2;
  116. callback = arguments[arguments.length - 1];
  117. var optionalArgs = { '3' : 'pushContent', '4' : 'pushData' };
  118. for( var i=4; i<=indexOfLastParam; ++i ) {
  119. if( optionalArgs[i] ) {
  120. requestParams[ optionalArgs[i] ] = arguments[i];
  121. }
  122. }
  123. rongrequest.request( apis['message']['broadcast'], requestParams, formatSpecified ? f : 'json', function( err, resultText ) {
  124. return callback( err, resultText );
  125. } );
  126. }
  127. exports.history = function( dateString, format, callback ) {
  128. rongrequest.request( apis['message']['history'], {
  129. date : dateString
  130. }, format, function( err, resultText ) {
  131. return callback( err, resultText );
  132. } );
  133. }