chatroom.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.destroy = function( chatRoomIDs, format, callback ) {
  26. if( 'object' == typeof chatRoomIDs && chatRoomIDs.length ) {
  27. if( !util.validateIDs( chatRoomIDs ) ) {
  28. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  29. }
  30. rongrequest.requestWithSameFields( apis['chatroom']['destroy'], {}, [{field:'chatroomId', values:chatRoomIDs}], format, callback );
  31. }
  32. else {
  33. if( !util.validateId( chatRoomIDs ) ) {
  34. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  35. }
  36. rongrequest.request( apis['chatroom']['destroy'], { chatroomId : chatRoomIDs }, format, callback );
  37. }
  38. }
  39. exports.query = function( chatRoomIDs, format, callback ) {
  40. if( 'object' == typeof chatRoomIDs && chatRoomIDs.length ) {
  41. // Check the IDs.
  42. var valid = util.validateIDs( chatRoomIDs );
  43. if( !valid ) {
  44. callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  45. }
  46. else {
  47. rongrequest.requestWithSameFields( apis['chatroom']['query'], {}, [{field:'chatroomId', values:chatRoomIDs}], format, callback );
  48. }
  49. }
  50. else {
  51. if( !util.validateId( chatRoomIDs ) ) {
  52. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  53. }
  54. rongrequest.request( apis['chatroom']['query'], { chatroomId : chatRoomIDs }, format, callback );
  55. }
  56. }
  57. exports.user = {};
  58. exports.user.query = function( chatRoomId, format, callback ) {
  59. if( !util.validateId( chatRoomId ) ) {
  60. return callback( { code : rcResult.INVALID_CHATROOMID, message : rcResult.INVALID_CHATROOMID_MSG }, null );
  61. }
  62. rongrequest.request( apis['chatroom']['user']['query'], {
  63. chatroomId : chatRoomId
  64. }, format, function( err, resultText ) {
  65. return callback( err, resultText );
  66. } );
  67. }
  68. exports.queryAll = function( format, callback ) {
  69. rongrequest.requestWithSameFields( apis['chatroom']['query'], {}, [], format, callback );
  70. }