From bb7452f3f75cfb4148af4a14a9f9debe647d8e4b Mon Sep 17 00:00:00 2001 From: Ayumi Sarah Date: Tue, 23 Aug 2022 15:09:38 +0800 Subject: [PATCH] feat(friendship): prefer AV.User.currentAsync (#671) --- src/friendship.js | 98 +++++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 32 deletions(-) diff --git a/src/friendship.js b/src/friendship.js index 78adce509..503103526 100644 --- a/src/friendship.js +++ b/src/friendship.js @@ -3,6 +3,31 @@ const { request: LCRequest } = require('./request'); const { getSessionToken } = require('./utils'); module.exports = function(AV) { + const getUserWithSessionToken = authOptions => { + if (authOptions.user) { + if (!authOptions.user._sessionToken) { + throw new Error('authOptions.user is not signed in.'); + } + return Promise.resolve(authOptions.user); + } + if (authOptions.sessionToken) { + return AV.User._fetchUserBySessionToken(authOptions.sessionToken); + } + return AV.User.currentAsync(); + }; + + const getSessionTokenAsync = authOptions => { + const sessionToken = getSessionToken(authOptions); + if (sessionToken) { + return Promise.resolve(sessionToken); + } + return AV.User.currentAsync().then(user => { + if (user) { + return user.getSessionToken(); + } + }); + }; + /** * Contains functions to deal with Friendship in LeanCloud. * @class @@ -14,33 +39,38 @@ module.exports = function(AV) { * @param {String | AV.User | Object} options if an AV.User or string is given, it will be used as the friend. * @param {AV.User | string} options.friend The friend (or friend's objectId) to follow. * @param {Object} [options.attributes] key-value attributes dictionary to be used as conditions of followeeQuery. - * @param {*} [authOptions] + * @param {AuthOptions} [authOptions] * @return {Promise} */ - request: function(options, authOptions) { - if (!AV.User.current()) { - throw new Error('Please signin an user.'); - } + request: function(options, authOptions = {}) { let friend; let attributes; + if (options.friend) { friend = options.friend; attributes = options.attributes; } else { friend = options; } - const friendObject = _.isString(friend) + + const friendObj = _.isString(friend) ? AV.Object.createWithoutData('_User', friend) : friend; - return LCRequest({ - method: 'POST', - path: '/users/friendshipRequests', - data: AV._encode({ - user: AV.User.current(), - friend: friendObject, - friendship: attributes, - }), - authOptions, + + return getUserWithSessionToken(authOptions).then(userObj => { + if (!userObj) { + throw new Error('Please signin an user.'); + } + return LCRequest({ + method: 'POST', + path: '/users/friendshipRequests', + data: { + user: userObj._toPointer(), + friend: friendObj._toPointer(), + friendship: attributes, + }, + authOptions, + }); }); }, @@ -54,9 +84,6 @@ module.exports = function(AV) { * @return {Promise} */ acceptRequest: function(options, authOptions = {}) { - if (!getSessionToken(authOptions) && !AV.User.current()) { - throw new Error('Please signin an user.'); - } let request; let attributes; if (options.request) { @@ -66,13 +93,18 @@ module.exports = function(AV) { request = options; } const requestId = _.isString(request) ? request : request.id; - return LCRequest({ - method: 'PUT', - path: '/users/friendshipRequests/' + requestId + '/accept', - data: { - friendship: AV._encode(attributes), - }, - authOptions, + return getSessionTokenAsync(authOptions).then(sessionToken => { + if (!sessionToken) { + throw new Error('Please signin an user.'); + } + return LCRequest({ + method: 'PUT', + path: '/users/friendshipRequests/' + requestId + '/accept', + data: { + friendship: AV._encode(attributes), + }, + authOptions, + }); }); }, @@ -83,14 +115,16 @@ module.exports = function(AV) { * @return {Promise} */ declineRequest: function(request, authOptions = {}) { - if (!getSessionToken(authOptions) && !AV.User.current()) { - throw new Error('Please signin an user.'); - } const requestId = _.isString(request) ? request : request.id; - return LCRequest({ - method: 'PUT', - path: '/users/friendshipRequests/' + requestId + '/decline', - authOptions, + return getSessionTokenAsync(authOptions).then(sessionToken => { + if (!sessionToken) { + throw new Error('Please signin an user.'); + } + return LCRequest({ + method: 'PUT', + path: '/users/friendshipRequests/' + requestId + '/decline', + authOptions, + }); }); }, };