group.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. var apis = require( './apis' );
  2. var rongrequest = require( './rongrequest' );
  3. var util = require( './util' );
  4. var rcResult = require( '../const' ).result;
  5. exports.sync = function( userId, groupIdNamePairs, format, callback ) {
  6. var params = { userId : userId };
  7. var validGroupId = true;
  8. for( var k in groupIdNamePairs ) {
  9. if( !util.validateId( k ) ) {
  10. validGroupId = false;
  11. break;
  12. }
  13. params[ 'group[' + k + ']' ] = groupIdNamePairs[k];
  14. }
  15. if( validGroupId ) {
  16. rongrequest.request( apis['group']['sync'], params, format, function( err, resultText ) {
  17. return callback( err, resultText );
  18. } );
  19. }
  20. else {
  21. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  22. }
  23. }
  24. exports.quit = function( userIDs, groupId, format, callback ) {
  25. if( !util.validateId( groupId ) ) {
  26. return callback( 'Invalid group id', null );
  27. }
  28. var params = { groupId : groupId };
  29. if( typeof userIDs == 'object' && userIDs.length ) {
  30. rongrequest.requestWithSameFields( apis['group']['quit'], params, [ { field : 'userId', values : userIDs } ], format, callback );
  31. }
  32. else if( typeof userIDs == 'string' || typeof userIDs == 'number' ) {
  33. params.userId = userIDs;
  34. rongrequest.request( apis['group']['quit'], params, format, callback );
  35. }
  36. else {
  37. return callback( "Invalid userId! The userId should be either a string, a number, or an non-empty array!" );
  38. }
  39. }
  40. exports.dismiss = function( userId, groupId, format, callback ) {
  41. if( !util.validateId( groupId ) ) {
  42. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  43. }
  44. rongrequest.request( apis['group']['dismiss'], {
  45. userId : userId,
  46. groupId : groupId
  47. }, format, function( err, resultText ) {
  48. return callback( err, resultText );
  49. } );
  50. }
  51. exports.refresh = function( groupId, groupName, format, callback ) {
  52. if( !util.validateId( groupId ) ) {
  53. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  54. }
  55. rongrequest.request( apis['group']['refresh'], {
  56. groupId : groupId,
  57. groupName : groupName
  58. }, format, function( err, resultText ) {
  59. return callback( err, resultText );
  60. } );
  61. }
  62. function createGroup( userIDs, groupId, groupName, format, callback ) {
  63. if( !util.validateId( groupId ) ) {
  64. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  65. }
  66. var params = { groupId : groupId, groupName : groupName };
  67. if( typeof userIDs == 'object' && userIDs.length ) {
  68. rongrequest.requestWithSameFields( apis['group']['create'], params, [ { field : 'userId', values : userIDs } ], format, callback );
  69. }
  70. else if( typeof userIDs == 'string' || typeof userIDs == 'number' ) {
  71. params.userId = userIDs;
  72. rongrequest.request( apis['group']['create'], params, format, callback );
  73. }
  74. else {
  75. return callback( "Invalid userId! The userId should be either a string, a number, or an non-empty array!" );
  76. }
  77. }
  78. exports.create = createGroup;
  79. exports.join = createGroup;
  80. exports.user = {};
  81. exports.user.query = function( groupId, format, callback ) {
  82. if( !util.validateId( groupId ) ) {
  83. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  84. }
  85. var params = { groupId : groupId };
  86. rongrequest.request( apis['group']['user']['query'], params, format, function( err, result ) {
  87. return callback( err, result );
  88. } );
  89. }
  90. exports.user.gag = {};
  91. exports.user.gag.add = function( userId, groupId, minute, format, callback ) {
  92. if( !util.validateId( groupId ) ) {
  93. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  94. }
  95. var params = { userId : userId, groupId : groupId, minute : minute };
  96. rongrequest.request( apis['group']['user']['gag']['add'], params, format, function( err, result ) {
  97. return callback( err, result );
  98. } );
  99. }
  100. exports.user.gag.rollback = function( userId, groupId, format, callback ) {
  101. if( !util.validateId( groupId ) ) {
  102. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  103. }
  104. var params = { userId : userId, groupId : groupId };
  105. rongrequest.request( apis['group']['user']['gag']['rollback'], params, format, function( err, result ) {
  106. return callback( err, result );
  107. } );
  108. }
  109. exports.user.gag.list = function( groupId, format, callback ) {
  110. if( !util.validateId( groupId ) ) {
  111. return callback( { code : rcResult.INVALID_GROUPID, message : rcResult.INVALID_GROUPID_MSG }, null );
  112. }
  113. var params = { groupId : groupId };
  114. rongrequest.request( apis['group']['user']['gag']['list'], params, format, function( err, result ) {
  115. return callback( err, result );
  116. } );
  117. }