group.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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;