Skip to content

Commit 38efdfb

Browse files
authored
feat(Status): Follower/Followee support attributes (#485)
1 parent 54baa5c commit 38efdfb

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

src/user.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -376,44 +376,56 @@ module.exports = function(AV) {
376376
/**
377377
* Follow a user
378378
* @since 0.3.0
379-
* @param {AV.User | String} target The target user or user's objectId to follow.
380-
* @param {AuthOptions} options
379+
* @param {Object | AV.User | String} options if an AV.User or string is given, it will be used as the target user.
380+
* @param {AV.User | String} options.user The target user or user's objectId to follow.
381+
* @param {Object} [options.attributes] key-value attributes dictionary to be used as
382+
* conditions of followerQuery/followeeQuery.
383+
* @param {AuthOptions} [authOptions]
381384
*/
382-
follow: function(target, options){
385+
follow: function(options, authOptions){
383386
if(!this.id){
384387
throw new Error('Please signin.');
385388
}
386-
if(!target){
387-
throw new Error('Invalid target user.');
389+
let user;
390+
let attributes;
391+
if (options.user) {
392+
user = options.user;
393+
attributes = options.attributes;
394+
} else {
395+
user = options;
388396
}
389-
var userObjectId = _.isString(target) ? target: target.id;
397+
var userObjectId = _.isString(user) ? user: user.id;
390398
if(!userObjectId){
391399
throw new Error('Invalid target user.');
392400
}
393401
var route = 'users/' + this.id + '/friendship/' + userObjectId;
394-
var request = AVRequest(route, null, null, 'POST', null, options);
402+
var request = AVRequest(route, null, null, 'POST', AV._encode(attributes), authOptions);
395403
return request;
396404
},
397405

398406
/**
399407
* Unfollow a user.
400408
* @since 0.3.0
401-
* @param {AV.User | String} target The target user or user's objectId to unfollow.
402-
* @param {AuthOptions} options
409+
* @param {Object | AV.User | String} options if an AV.User or string is given, it will be used as the target user.
410+
* @param {AV.User | String} options.user The target user or user's objectId to unfollow.
411+
* @param {AuthOptions} [authOptions]
403412
*/
404-
unfollow: function(target, options){
413+
unfollow: function(options, authOptions){
405414
if(!this.id){
406415
throw new Error('Please signin.');
407416
}
408-
if(!target){
409-
throw new Error('Invalid target user.');
417+
let user;
418+
if (options.user) {
419+
user = options.user;
420+
} else {
421+
user = options;
410422
}
411-
var userObjectId = _.isString(target) ? target: target.id;
423+
var userObjectId = _.isString(user) ? user : user.id;
412424
if(!userObjectId){
413425
throw new Error('Invalid target user.');
414426
}
415427
var route = 'users/' + this.id + '/friendship/' + userObjectId;
416-
var request = AVRequest(route, null, null, 'DELETE', null, options);
428+
var request = AVRequest(route, null, null, 'DELETE', null, authOptions);
417429
return request;
418430
},
419431

storage.d.ts

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,8 @@ declare namespace AV {
478478
interface GetOptions extends AuthOptions { }
479479
}
480480

481+
class FriendShipQuery extends Query {}
482+
481483
/**
482484
* Represents a Role on the AV server. Roles represent groupings of
483485
* Users for the purposes of granting permissions (e.g. specifying an ACL
@@ -513,28 +515,32 @@ declare namespace AV {
513515
export class User extends Object {
514516

515517
static current(): User;
516-
static signUp<T>(username: string, password: string, attrs: any, options?: AuthOptions): Promise<T>;
517-
static logIn<T>(username: string, password: string, options?: AuthOptions): Promise<T>;
518-
static logOut<T>(): Promise<T>;
519-
static become<T>(sessionToken: string, options?: AuthOptions): Promise<T>;
520-
521-
static loginWithWeapp<T>(): Promise<T>;
522-
static logInWithMobilePhone<T>(mobilePhone: string, password: string, options?: AuthOptions): Promise<T>;
523-
static logInWithMobilePhoneSmsCode<T>(mobilePhone: string, smsCode: string, options?: AuthOptions): Promise<T>;
524-
static signUpOrlogInWithAuthData<T>(data: any, platform: string, options?: AuthOptions): Promise<T>;
525-
static signUpOrlogInWithMobilePhone<T>(mobilePhoneNumber: string, smsCode: string, attributes?: any, options?: AuthOptions): Promise<T>;
526-
static requestEmailVerify<T>(email: string, options?: AuthOptions): Promise<T>;
518+
static signUp(username: string, password: string, attrs: any, options?: AuthOptions): Promise<User>;
519+
static logIn(username: string, password: string, options?: AuthOptions): Promise<User>;
520+
static logOut(): Promise<User>;
521+
static become(sessionToken: string, options?: AuthOptions): Promise<User>;
522+
523+
static loginWithWeapp(): Promise<User>;
524+
static logInWithMobilePhone(mobilePhone: string, password: string, options?: AuthOptions): Promise<User>;
525+
static logInWithMobilePhoneSmsCode(mobilePhone: string, smsCode: string, options?: AuthOptions): Promise<User>;
526+
static signUpOrlogInWithAuthData(data: any, platform: string, options?: AuthOptions): Promise<User>;
527+
static signUpOrlogInWithMobilePhone(mobilePhoneNumber: string, smsCode: string, attributes?: any, options?: AuthOptions): Promise<User>;
528+
static requestEmailVerify(email: string, options?: AuthOptions): Promise<User>;
527529
static requestLoginSmsCode(mobilePhoneNumber: string, options?: SMSAuthOptions): Promise<void>;
528530
static requestMobilePhoneVerify(mobilePhoneNumber: string, options?: SMSAuthOptions): Promise<void>;
529-
static requestPasswordReset<T>(email: string, options?: AuthOptions): Promise<T>;
531+
static requestPasswordReset(email: string, options?: AuthOptions): Promise<User>;
530532
static requestPasswordResetBySmsCode(mobilePhoneNumber: string, options?: SMSAuthOptions): Promise<void>;
531-
static resetPasswordBySmsCode<T>(code: string, password: string, options?: AuthOptions): Promise<T>;
532-
static verifyMobilePhone<T>(code: string, options?: AuthOptions): Promise<T>;
533-
signUp<T>(attrs?: any, options?: AuthOptions): Promise<T>;
534-
logIn<T>(options?: AuthOptions): Promise<T>;
535-
linkWithWeapp<T>(): Promise<T>;
536-
fetch<T>(options?: AuthOptions): Promise<T>;
537-
save<T>(arg1?: any, arg2?: any, arg3?: any): Promise<T>;
533+
static resetPasswordBySmsCode(code: string, password: string, options?: AuthOptions): Promise<User>;
534+
static verifyMobilePhone(code: string, options?: AuthOptions): Promise<User>;
535+
536+
static followerQuery(userObjectId: string): FriendShipQuery;
537+
static followeeQuery(userObjectId: string): FriendShipQuery;
538+
539+
signUp(attrs?: any, options?: AuthOptions): Promise<User>;
540+
logIn(options?: AuthOptions): Promise<User>;
541+
linkWithWeapp(): Promise<User>;
542+
fetch(options?: AuthOptions): Promise<User>;
543+
save(arg1?: any, arg2?: any, arg3?: any): Promise<User>;
538544
isAuthenticated(): Promise<boolean>;
539545
isCurrent(): boolean;
540546

@@ -553,7 +559,13 @@ declare namespace AV {
553559
refreshSessionToken(options?: AuthOptions): Promise<User>;
554560

555561
getRoles(options?: AuthOptions): Promise<Role>;
556-
562+
563+
follow(user: User|string, authOptions?: AuthOptions): Promise<void>;
564+
follow(options: { user: User|string, attributes?: Object}, authOptions?: AuthOptions): Promise<void>;
565+
unfollow(user: User|string, authOptions?: AuthOptions): Promise<void>;
566+
unfollow(options: { user: User|string }, authOptions?: AuthOptions): Promise<void>;
567+
followerQuery(): FriendShipQuery;
568+
followeeQuery(): FriendShipQuery;
557569
}
558570

559571
export class Captcha {

test/status.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,27 @@ describe("AV.Status",function(){
6464

6565
describe("Status guide test.", function(){
6666
it("should follow/unfollow successfully.", function(){
67-
return AV.User.current().follow(targetUser).then(function(){
67+
return AV.User.current().follow({
68+
user: targetUser,
69+
attributes: {
70+
group: 1,
71+
position: new AV.GeoPoint(0,0),
72+
},
73+
}).then(function(){
6874
var query = AV.User.current().followeeQuery();
75+
query.equalTo('group', 1);
6976
query.include('followee');
7077
return query.find();
7178
}).then(function(followees){
7279
debug(followees);
7380
expect(followees.length).to.be(1);
7481
expect(followees[0].id).to.be(targetUser);
7582
expect(followees[0].get('username')).to.be('leeyeh');
83+
var query = AV.User.current().followeeQuery();
84+
query.equalTo('group', 0);
85+
return query.find();
86+
}).then(function(followees){
87+
expect(followees.length).to.be(0);
7688
return AV.User.current().unfollow(targetUser);
7789
}).then(function(){
7890
var query = AV.User.current().followeeQuery();

0 commit comments

Comments
 (0)