Skip to content

Commit c850935

Browse files
committed
feat(Status): Follower/Followee support attributes (#485)
1 parent e926aa0 commit c850935

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
@@ -491,6 +491,8 @@ declare namespace AV {
491491
interface GetOptions extends AuthOptions { }
492492
}
493493

494+
class FriendShipQuery extends Query {}
495+
494496
/**
495497
* Represents a Role on the AV server. Roles represent groupings of
496498
* Users for the purposes of granting permissions (e.g. specifying an ACL
@@ -526,28 +528,32 @@ declare namespace AV {
526528
export class User extends Object {
527529

528530
static current(): User;
529-
static signUp<T>(username: string, password: string, attrs: any, options?: AuthOptions): Promise<T>;
530-
static logIn<T>(username: string, password: string, options?: AuthOptions): Promise<T>;
531-
static logOut<T>(): Promise<T>;
532-
static become<T>(sessionToken: string, options?: AuthOptions): Promise<T>;
533-
534-
static loginWithWeapp<T>(): Promise<T>;
535-
static logInWithMobilePhone<T>(mobilePhone: string, password: string, options?: AuthOptions): Promise<T>;
536-
static logInWithMobilePhoneSmsCode<T>(mobilePhone: string, smsCode: string, options?: AuthOptions): Promise<T>;
537-
static signUpOrlogInWithAuthData<T>(data: any, platform: string, options?: AuthOptions): Promise<T>;
538-
static signUpOrlogInWithMobilePhone<T>(mobilePhoneNumber: string, smsCode: string, attributes?: any, options?: AuthOptions): Promise<T>;
539-
static requestEmailVerify<T>(email: string, options?: AuthOptions): Promise<T>;
531+
static signUp(username: string, password: string, attrs: any, options?: AuthOptions): Promise<User>;
532+
static logIn(username: string, password: string, options?: AuthOptions): Promise<User>;
533+
static logOut(): Promise<User>;
534+
static become(sessionToken: string, options?: AuthOptions): Promise<User>;
535+
536+
static loginWithWeapp(): Promise<User>;
537+
static logInWithMobilePhone(mobilePhone: string, password: string, options?: AuthOptions): Promise<User>;
538+
static logInWithMobilePhoneSmsCode(mobilePhone: string, smsCode: string, options?: AuthOptions): Promise<User>;
539+
static signUpOrlogInWithAuthData(data: any, platform: string, options?: AuthOptions): Promise<User>;
540+
static signUpOrlogInWithMobilePhone(mobilePhoneNumber: string, smsCode: string, attributes?: any, options?: AuthOptions): Promise<User>;
541+
static requestEmailVerify(email: string, options?: AuthOptions): Promise<User>;
540542
static requestLoginSmsCode(mobilePhoneNumber: string, options?: SMSAuthOptions): Promise<void>;
541543
static requestMobilePhoneVerify(mobilePhoneNumber: string, options?: SMSAuthOptions): Promise<void>;
542-
static requestPasswordReset<T>(email: string, options?: AuthOptions): Promise<T>;
544+
static requestPasswordReset(email: string, options?: AuthOptions): Promise<User>;
543545
static requestPasswordResetBySmsCode(mobilePhoneNumber: string, options?: SMSAuthOptions): Promise<void>;
544-
static resetPasswordBySmsCode<T>(code: string, password: string, options?: AuthOptions): Promise<T>;
545-
static verifyMobilePhone<T>(code: string, options?: AuthOptions): Promise<T>;
546-
signUp<T>(attrs?: any, options?: AuthOptions): Promise<T>;
547-
logIn<T>(options?: AuthOptions): Promise<T>;
548-
linkWithWeapp<T>(): Promise<T>;
549-
fetch<T>(options?: AuthOptions): Promise<T>;
550-
save<T>(arg1?: any, arg2?: any, arg3?: any): Promise<T>;
546+
static resetPasswordBySmsCode(code: string, password: string, options?: AuthOptions): Promise<User>;
547+
static verifyMobilePhone(code: string, options?: AuthOptions): Promise<User>;
548+
549+
static followerQuery(userObjectId: string): FriendShipQuery;
550+
static followeeQuery(userObjectId: string): FriendShipQuery;
551+
552+
signUp(attrs?: any, options?: AuthOptions): Promise<User>;
553+
logIn(options?: AuthOptions): Promise<User>;
554+
linkWithWeapp(): Promise<User>;
555+
fetch(options?: AuthOptions): Promise<User>;
556+
save(arg1?: any, arg2?: any, arg3?: any): Promise<User>;
551557
isAuthenticated(): Promise<boolean>;
552558
isCurrent(): boolean;
553559

@@ -566,7 +572,13 @@ declare namespace AV {
566572
refreshSessionToken(options?: AuthOptions): Promise<User>;
567573

568574
getRoles(options?: AuthOptions): Promise<Role>;
569-
575+
576+
follow(user: User|string, authOptions?: AuthOptions): Promise<void>;
577+
follow(options: { user: User|string, attributes?: Object}, authOptions?: AuthOptions): Promise<void>;
578+
unfollow(user: User|string, authOptions?: AuthOptions): Promise<void>;
579+
unfollow(options: { user: User|string }, authOptions?: AuthOptions): Promise<void>;
580+
followerQuery(): FriendShipQuery;
581+
followeeQuery(): FriendShipQuery;
570582
}
571583

572584
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)