Skip to content

Commit 54400cc

Browse files
authored
feat: friendship (#626)
* feat: friendship * feat(user): getFollowersAndFollowees
1 parent ff3db71 commit 54400cc

File tree

5 files changed

+164
-1
lines changed

5 files changed

+164
-1
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Master
22

3+
### Features
4+
5+
- 好友
6+
- 添加 `AV.User#getFollowersAndFollowees` 方法用于查询指定用户的 followers 和 followees 。
7+
38
### Internal Changes
49

510
- 现在保存通过 `AV.File.withURL` 方法创建的文件等同于直接在 \_File 中添加一行数据,不再自动生成 `mime_type`

src/friendship.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
};

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require('./push')(AV);
3434
require('./status')(AV);
3535
require('./search')(AV);
3636
require('./insight')(AV);
37+
require('./friendship')(AV);
3738

3839
AV.Conversation = require('./conversation');
3940
require('./leaderboard');

src/user.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,34 @@ module.exports = function(AV) {
787787
return request;
788788
},
789789

790+
/**
791+
* Get the user's followers and followees.
792+
* @since 4.8.0
793+
* @param {Object} [options]
794+
* @param {Number} [options.skip]
795+
* @param {Number} [options.limit]
796+
* @param {AuthOptions} [authOptions]
797+
*/
798+
getFollowersAndFollowees: function(options, authOptions) {
799+
if (!this.id) {
800+
throw new Error('Please signin.');
801+
}
802+
return request({
803+
method: 'GET',
804+
path: `/users/${this.id}/followersAndFollowees`,
805+
query: {
806+
skip: options && options.skip,
807+
limit: options && options.limit,
808+
include: 'follower,followee',
809+
keys: 'follower,followee',
810+
},
811+
authOptions,
812+
}).then(({ followers, followees }) => ({
813+
followers: followers.map(({ follower }) => AV._decode(follower)),
814+
followees: followees.map(({ followee }) => AV._decode(followee)),
815+
}));
816+
},
817+
790818
/**
791819
*Create a follower query to query the user's followers.
792820
* @since 0.3.0

storage.d.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,33 @@ export class User extends Object {
881881
): Promise<void>;
882882
followerQuery(): Query<this>;
883883
followeeQuery(): Query<this>;
884+
getFollowersAndFollowees(
885+
options?: { skip?: number; limit?: number },
886+
authOptions?: AuthOptions
887+
): Promise<{ followers: User[]; followees: User[] }>;
888+
}
889+
890+
export class Friendship {
891+
static request(
892+
friend: User | string,
893+
authOptions?: AuthOptions
894+
): Promise<void>;
895+
static request(
896+
options: { friend: User | string; attributes?: object },
897+
authOptions?: AuthOptions
898+
): Promise<void>;
899+
static acceptRequest(
900+
request: Object | string,
901+
authOptions?: AuthOptions
902+
): Promise<void>;
903+
static acceptRequest(
904+
options: { request: Object | string; attributes?: object },
905+
authOptions?: AuthOptions
906+
): Promise<void>;
907+
static declineRequest(
908+
request: Object | string,
909+
authOptions?: AuthOptions
910+
): Promise<void>;
884911
}
885912

886913
export class Captcha {
@@ -1222,7 +1249,12 @@ export namespace Cloud {
12221249
function requestSmsCode(
12231250
data:
12241251
| string
1225-
| { mobilePhoneNumber: string; template?: string; sign?: string; smsType?: 'sms' | 'voice' },
1252+
| {
1253+
mobilePhoneNumber: string;
1254+
template?: string;
1255+
sign?: string;
1256+
smsType?: 'sms' | 'voice';
1257+
},
12261258
options?: SMSAuthOptions
12271259
): Promise<void>;
12281260
function verifySmsCode(code: string, phone: string): Promise<void>;

0 commit comments

Comments
 (0)