chatroom.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. var apis = require( './apis' );
  2. var util = require( './util' );
  3. var rongrequest = require( './rongrequest' );
  4. var rcResult = require( '../const' ).result;
  5. exports.create = function( chatRoomIDNamePairs, format, callback ) {
  6. var params = {};
  7. var chatroomId;
  8. var validChatRoomId = true;
  9. for( var i=0; i<chatRoomIDNamePairs.length; ++i ) {
  10. chatroomId = chatRoomIDNamePairs[i].id;
  11. if( !util.validateId( chatroomId ) ) {
  12. validChatRoomId = false;
  13. break;
  14. }
  15. var key = 'chatroom[' + chatroomId + ']';
  16. params[ key ] = chatRoomIDNamePairs[i].name;
  17. }
  18. if( validChatRoomId ) {
  19. rongrequest.request( apis['chatroom']['create'], params, format, callback );
  20. }
  21. else {
  22. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  23. }
  24. }
  25. exports.join = function(chatroomId,userId,format,callback){
  26. util.check(['string','string','string','function'],[chatroomId,userId,format,callback],'chatroom.join');
  27. rongrequest.request(apis['chatroom']['join'],
  28. {
  29. chatroomId : chatroomId,
  30. userId : userId
  31. },format,callback);
  32. }
  33. exports.destroy = function( chatRoomIDs, format, callback ) {
  34. if( 'object' == typeof chatRoomIDs && chatRoomIDs.length ) {
  35. if( !util.validateIDs( chatRoomIDs ) ) {
  36. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  37. }
  38. rongrequest.requestWithSameFields( apis['chatroom']['destroy'], {}, [{field:'chatroomId', values:chatRoomIDs}], format, callback );
  39. }
  40. else {
  41. if( !util.validateId( chatRoomIDs ) ) {
  42. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  43. }
  44. rongrequest.request( apis['chatroom']['destroy'], { chatroomId : chatRoomIDs }, format, callback );
  45. }
  46. }
  47. exports.query = function( chatRoomIDs, format, callback ) {
  48. if( 'object' == typeof chatRoomIDs && chatRoomIDs.length ) {
  49. // Check the IDs.
  50. var valid = util.validateIDs( chatRoomIDs );
  51. if( !valid ) {
  52. callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  53. }
  54. else {
  55. rongrequest.requestWithSameFields( apis['chatroom']['query'], {}, [{field:'chatroomId', values:chatRoomIDs}], format, callback );
  56. }
  57. }
  58. else {
  59. if( !util.validateId( chatRoomIDs ) ) {
  60. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  61. }
  62. rongrequest.request( apis['chatroom']['query'], { chatroomId : chatRoomIDs }, format, callback );
  63. }
  64. }
  65. exports.user = {};
  66. exports.message = {};
  67. exports.user.query = function( chatroomId, count, order, format, callback ) {
  68. if( !util.validateId( chatroomId ) ) {
  69. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  70. }
  71. rongrequest.request( apis['chatroom']['user']['query'], {
  72. chatroomId : chatroomId,
  73. count:count,
  74. order:order
  75. }, format, function( err, resultText ) {
  76. return callback( err, resultText );
  77. } );
  78. }
  79. exports.user.gagAdd = function(chatroomId,userId,minute,format,callback){
  80. util.check(['string','string','number','string','function'],[chatroomId,userId,minute,format,callback],'chatroom.user.gag.add');
  81. rongrequest.request(apis['chatroom']['user']['gagAdd'],
  82. {
  83. chatroomId : chatroomId,
  84. userId:userId,
  85. minute:minute
  86. },format,callback);
  87. }
  88. exports.user.gagRollback = function(chatroomId,userId,format,callback){
  89. util.check(['string','string','string','function'],[chatroomId,userId,format,callback],'chatroom.user.gag.rollback');
  90. rongrequest.request(apis['chatroom']['user']['gagRollback'],
  91. {
  92. chatroomId : chatroomId,
  93. userId:userId
  94. },format,function(err,resultText){
  95. return callback(err,resultText);
  96. });
  97. }
  98. exports.user.gagList = function(chatroomId,format,callback){
  99. util.check(['string','string','function'],[chatroomId,format,callback],'chatroom.user.gag.list');
  100. rongrequest.request(apis['chatroom']['user']['gagList'],
  101. {
  102. chatroomId : chatroomId
  103. },format,function(err,resultText){
  104. return callback(err,resultText);
  105. });
  106. }
  107. exports.user.blockAdd = function(chatroomId,userId,minute,format,callback){
  108. util.check(['string','string','number','string','function'],[chatroomId,userId,minute,format,callback],'chatroom.user.block.add');
  109. rongrequest.request(apis['chatroom']['user']['blockAdd'],
  110. {
  111. chatroomId : chatroomId,
  112. userId:userId,
  113. minute:minute
  114. },format,function(err,resultText){
  115. return callback(err,resultText);
  116. });
  117. }
  118. exports.user.blockRollback = function(chatroomId,userId,format,callback){
  119. util.check(['string','string','string','function'],[chatroomId,userId,format,callback],'chatroom.user.block.rollback');
  120. rongrequest.request(apis['chatroom']['user']['blockRollback'],
  121. {
  122. chatroomId : chatroomId,
  123. userId:userId
  124. },format,function(err,resultText){
  125. return callback(err,resultText);
  126. });
  127. }
  128. exports.user.blockList = function(chatroomId,format,callback){
  129. util.check(['string','string','function'],[chatroomId,format,callback],'chatroom.user.block.list');
  130. rongrequest.request(apis['chatroom']['user']['blockList'],
  131. {
  132. chatroomId : chatroomId
  133. },format,function(err,resultText){
  134. return callback(err,resultText);
  135. });
  136. }
  137. exports.message.stopDistribution = function(chatroomId,format,callback){
  138. util.check(['string','string','function'],[chatroomId,format,callback],'chatroom.message.stopDistribution');
  139. rongrequest.request(apis['chatroom']['message']['stop'],
  140. {
  141. chatroomId : chatroomId
  142. },
  143. format,function(err,resultText){
  144. return callback(err,resultText);
  145. });
  146. }
  147. exports.message.resumeDistribution = function(chatroomId,format,callback){
  148. util.check(['string','string','function'],[chatroomId,format,callback],'chatroom.message.resumeDistribution');
  149. rongrequest.request(apis['chatroom']['message']['resume'],
  150. {
  151. chatroomId : chatroomId
  152. },
  153. format,function(err,resultText){
  154. return callback(err,resultText);
  155. });
  156. }
  157. exports.queryAll = function( format, callback ) {
  158. rongrequest.requestWithSameFields( apis['chatroom']['query'], {}, [], format, callback );
  159. }