Skip to content

Commit

Permalink
feat(friendship): prefer AV.User.currentAsync (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdjdd authored Aug 23, 2022
1 parent ff64149 commit bb7452f
Showing 1 changed file with 66 additions and 32 deletions.
98 changes: 66 additions & 32 deletions src/friendship.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<void>}
*/
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,
});
});
},

Expand All @@ -54,9 +84,6 @@ module.exports = function(AV) {
* @return {Promise<void>}
*/
acceptRequest: function(options, authOptions = {}) {
if (!getSessionToken(authOptions) && !AV.User.current()) {
throw new Error('Please signin an user.');
}
let request;
let attributes;
if (options.request) {
Expand All @@ -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,
});
});
},

Expand All @@ -83,14 +115,16 @@ module.exports = function(AV) {
* @return {Promise<void>}
*/
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,
});
});
},
};
Expand Down

0 comments on commit bb7452f

Please sign in to comment.