@@ -3,6 +3,31 @@ const { request: LCRequest } = require('./request');
33const { getSessionToken } = require ( './utils' ) ;
44
55module . exports = function ( AV ) {
6+ const getUserWithSessionToken = authOptions => {
7+ if ( authOptions . user ) {
8+ if ( ! authOptions . user . _sessionToken ) {
9+ throw new Error ( 'authOptions.user is not signed in.' ) ;
10+ }
11+ return Promise . resolve ( authOptions . user ) ;
12+ }
13+ if ( authOptions . sessionToken ) {
14+ return AV . User . _fetchUserBySessionToken ( authOptions . sessionToken ) ;
15+ }
16+ return AV . User . currentAsync ( ) ;
17+ } ;
18+
19+ const getSessionTokenAsync = authOptions => {
20+ const sessionToken = getSessionToken ( authOptions ) ;
21+ if ( sessionToken ) {
22+ return Promise . resolve ( sessionToken ) ;
23+ }
24+ return AV . User . currentAsync ( ) . then ( user => {
25+ if ( user ) {
26+ return user . getSessionToken ( ) ;
27+ }
28+ } ) ;
29+ } ;
30+
631 /**
732 * Contains functions to deal with Friendship in LeanCloud.
833 * @class
@@ -14,33 +39,38 @@ module.exports = function(AV) {
1439 * @param {String | AV.User | Object } options if an AV.User or string is given, it will be used as the friend.
1540 * @param {AV.User | string } options.friend The friend (or friend's objectId) to follow.
1641 * @param {Object } [options.attributes] key-value attributes dictionary to be used as conditions of followeeQuery.
17- * @param {* } [authOptions]
42+ * @param {AuthOptions } [authOptions]
1843 * @return {Promise<void> }
1944 */
20- request : function ( options , authOptions ) {
21- if ( ! AV . User . current ( ) ) {
22- throw new Error ( 'Please signin an user.' ) ;
23- }
45+ request : function ( options , authOptions = { } ) {
2446 let friend ;
2547 let attributes ;
48+
2649 if ( options . friend ) {
2750 friend = options . friend ;
2851 attributes = options . attributes ;
2952 } else {
3053 friend = options ;
3154 }
32- const friendObject = _ . isString ( friend )
55+
56+ const friendObj = _ . isString ( friend )
3357 ? AV . Object . createWithoutData ( '_User' , friend )
3458 : 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,
59+
60+ return getUserWithSessionToken ( authOptions ) . then ( userObj => {
61+ if ( ! userObj ) {
62+ throw new Error ( 'Please signin an user.' ) ;
63+ }
64+ return LCRequest ( {
65+ method : 'POST' ,
66+ path : '/users/friendshipRequests' ,
67+ data : {
68+ user : userObj . _toPointer ( ) ,
69+ friend : friendObj . _toPointer ( ) ,
70+ friendship : attributes ,
71+ } ,
72+ authOptions,
73+ } ) ;
4474 } ) ;
4575 } ,
4676
@@ -54,9 +84,6 @@ module.exports = function(AV) {
5484 * @return {Promise<void> }
5585 */
5686 acceptRequest : function ( options , authOptions = { } ) {
57- if ( ! getSessionToken ( authOptions ) && ! AV . User . current ( ) ) {
58- throw new Error ( 'Please signin an user.' ) ;
59- }
6087 let request ;
6188 let attributes ;
6289 if ( options . request ) {
@@ -66,13 +93,18 @@ module.exports = function(AV) {
6693 request = options ;
6794 }
6895 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,
96+ return getSessionTokenAsync ( authOptions ) . then ( sessionToken => {
97+ if ( ! sessionToken ) {
98+ throw new Error ( 'Please signin an user.' ) ;
99+ }
100+ return LCRequest ( {
101+ method : 'PUT' ,
102+ path : '/users/friendshipRequests/' + requestId + '/accept' ,
103+ data : {
104+ friendship : AV . _encode ( attributes ) ,
105+ } ,
106+ authOptions,
107+ } ) ;
76108 } ) ;
77109 } ,
78110
@@ -83,14 +115,16 @@ module.exports = function(AV) {
83115 * @return {Promise<void> }
84116 */
85117 declineRequest : function ( request , authOptions = { } ) {
86- if ( ! getSessionToken ( authOptions ) && ! AV . User . current ( ) ) {
87- throw new Error ( 'Please signin an user.' ) ;
88- }
89118 const requestId = _ . isString ( request ) ? request : request . id ;
90- return LCRequest ( {
91- method : 'PUT' ,
92- path : '/users/friendshipRequests/' + requestId + '/decline' ,
93- authOptions,
119+ return getSessionTokenAsync ( authOptions ) . then ( sessionToken => {
120+ if ( ! sessionToken ) {
121+ throw new Error ( 'Please signin an user.' ) ;
122+ }
123+ return LCRequest ( {
124+ method : 'PUT' ,
125+ path : '/users/friendshipRequests/' + requestId + '/decline' ,
126+ authOptions,
127+ } ) ;
94128 } ) ;
95129 } ,
96130 } ;
0 commit comments