|
| 1 | +const _ = require('underscore'); |
| 2 | +const { request: LCRequest } = require('./request'); |
| 3 | +const { getSessionToken } = require('./utils'); |
| 4 | + |
| 5 | +module.exports = function(AV) { |
| 6 | + /** |
| 7 | + * Contains functions to deal with Friendship in LeanCloud. |
| 8 | + * @class |
| 9 | + */ |
| 10 | + AV.Friendship = { |
| 11 | + /** |
| 12 | + * Request friendship. |
| 13 | + * @since 4.8.0 |
| 14 | + * @param {String | AV.User | Object} options if an AV.User or string is given, it will be used as the friend. |
| 15 | + * @param {AV.User | string} options.friend The friend (or friend's objectId) to follow. |
| 16 | + * @param {Object} [options.attributes] key-value attributes dictionary to be used as conditions of followeeQuery. |
| 17 | + * @param {*} [authOptions] |
| 18 | + * @return {Promise<void>} |
| 19 | + */ |
| 20 | + request: function(options, authOptions) { |
| 21 | + if (!AV.User.current()) { |
| 22 | + throw new Error('Please signin an user.'); |
| 23 | + } |
| 24 | + let friend; |
| 25 | + let attributes; |
| 26 | + if (options.friend) { |
| 27 | + friend = options.friend; |
| 28 | + attributes = options.attributes; |
| 29 | + } else { |
| 30 | + friend = options; |
| 31 | + } |
| 32 | + const friendObject = _.isString(friend) |
| 33 | + ? AV.Object.createWithoutData('_User', friend) |
| 34 | + : friend; |
| 35 | + return LCRequest({ |
| 36 | + method: 'POST', |
| 37 | + path: '/users/friendshipRequests', |
| 38 | + data: AV._encode({ |
| 39 | + user: AV.User.current(), |
| 40 | + friend: friendObject, |
| 41 | + friendship: attributes, |
| 42 | + }), |
| 43 | + authOptions, |
| 44 | + }); |
| 45 | + }, |
| 46 | + |
| 47 | + /** |
| 48 | + * Accept a friendship request. |
| 49 | + * @since 4.8.0 |
| 50 | + * @param {AV.Object | string | Object} options if an AV.Object or string is given, it will be used as the request in _FriendshipRequest. |
| 51 | + * @param {AV.Object} options.request The request (or it's objectId) to be accepted. |
| 52 | + * @param {Object} [options.attributes] key-value attributes dictionary to be used as conditions of {@link AV#followeeQuery}. |
| 53 | + * @param {AuthOptions} [authOptions] |
| 54 | + * @return {Promise<void>} |
| 55 | + */ |
| 56 | + acceptRequest: function(options, authOptions = {}) { |
| 57 | + if (!getSessionToken(authOptions) && !AV.User.current()) { |
| 58 | + throw new Error('Please signin an user.'); |
| 59 | + } |
| 60 | + let request; |
| 61 | + let attributes; |
| 62 | + if (options.request) { |
| 63 | + request = options.request; |
| 64 | + attributes = options.attributes; |
| 65 | + } else { |
| 66 | + request = options; |
| 67 | + } |
| 68 | + const requestId = _.isString(request) ? request : request.id; |
| 69 | + return LCRequest({ |
| 70 | + method: 'PUT', |
| 71 | + path: '/users/friendshipRequests/' + requestId + '/accept', |
| 72 | + data: { |
| 73 | + friendship: AV._encode(attributes), |
| 74 | + }, |
| 75 | + authOptions, |
| 76 | + }); |
| 77 | + }, |
| 78 | + |
| 79 | + /** |
| 80 | + * Decline a friendship request. |
| 81 | + * @param {AV.Object | string} request The request (or it's objectId) to be declined. |
| 82 | + * @param {AuthOptions} [authOptions] |
| 83 | + * @return {Promise<void>} |
| 84 | + */ |
| 85 | + declineRequest: function(request, authOptions = {}) { |
| 86 | + if (!getSessionToken(authOptions) && !AV.User.current()) { |
| 87 | + throw new Error('Please signin an user.'); |
| 88 | + } |
| 89 | + const requestId = _.isString(request) ? request : request.id; |
| 90 | + return LCRequest({ |
| 91 | + method: 'PUT', |
| 92 | + path: '/users/friendshipRequests/' + requestId + '/decline', |
| 93 | + authOptions, |
| 94 | + }); |
| 95 | + }, |
| 96 | + }; |
| 97 | +}; |
0 commit comments