Skip to content

Commit 4a29a25

Browse files
committed
feat(leaderboard): allow to specify a user to get results around
1 parent 687baee commit 4a29a25

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

demo/leaderboard/leaderboard.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,14 @@ var app = new Vue({
117117
limit: MAX_RESULTS_COUNT,
118118
selectUserKeys: ['username'],
119119
}),
120-
Promise.resolve(
121-
AV.User.current()
122-
? leaderboard.getResultsAroundUser({
123-
limit: 3,
124-
selectUserKeys: ['username'],
125-
})
126-
: []
120+
Promise.resolve(AV.User.current()).then(
121+
currentUser =>
122+
currentUser
123+
? leaderboard.getResultsAroundUser(currentUser, {
124+
limit: 3,
125+
selectUserKeys: ['username'],
126+
})
127+
: []
127128
),
128129
])
129130
.then(([topRankings, [beforeUserRankings, userRanking]]) => {

src/leaderboard.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,12 @@ _.extend(
241241
_getResults(
242242
{ skip, limit, selectUserKeys, includeStatistics, version },
243243
authOptions,
244-
self
244+
userId
245245
) {
246246
return request({
247247
method: 'GET',
248248
path: `/leaderboard/leaderboards/${this.statisticName}/ranks${
249-
self ? '/self' : ''
249+
userId ? `/${userId}` : ''
250250
}`,
251251
query: {
252252
skip,
@@ -298,7 +298,8 @@ _.extend(
298298
);
299299
},
300300
/**
301-
* Retrieve a list of ranked users for this Leaderboard, centered on the current user.
301+
* Retrieve a list of ranked users for this Leaderboard, centered on the specified user.
302+
* @param {AV.User} user The specified AV.User pointer.
302303
* @param {Object} [options]
303304
* @param {number} [options.limit] The limit of the number of results.
304305
* @param {string[]} [options.selectUserKeys] Specify keys of the users to include
@@ -307,14 +308,16 @@ _.extend(
307308
* @param {AuthOptions} [authOptions]
308309
* @return {Promise<Ranking[]>}
309310
*/
310-
getResultsAroundUser(
311-
{ limit, selectUserKeys, includeStatistics, version } = {},
312-
authOptions
313-
) {
311+
getResultsAroundUser(user, options = {}, authOptions) {
312+
// getResultsAroundUser(options, authOptions)
313+
if (user && typeof user.id !== 'string') {
314+
return this.getResultsAroundUser(undefined, user, options);
315+
}
316+
const { limit, selectUserKeys, includeStatistics, version } = options;
314317
return this._getResults(
315318
{ limit, selectUserKeys, includeStatistics, version },
316319
authOptions,
317-
true
320+
user ? user.id : 'self'
318321
);
319322
},
320323
_update(data, authOptions) {

storage.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,16 @@ export class Leaderboard {
897897
},
898898
authOptions?: AuthOptions
899899
): Promise<Ranking[]>;
900+
getResultsAroundUser(
901+
user: User,
902+
options?: {
903+
limit?: number;
904+
selectUserKeys?: string | string[];
905+
includeStatistics?: string | string[];
906+
version?: number;
907+
},
908+
authOptions?: AuthOptions
909+
): Promise<Ranking[]>;
900910
getResultsAroundUser(
901911
options?: {
902912
limit?: number;

test/leaderboard.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe('Leaderboard', () => {
170170
})
171171
.should.be.rejected();
172172
});
173-
it('getResultsAroundUser', function() {
173+
it('get results around user', function() {
174174
const leaderboard = this.leaderboard;
175175
return leaderboard
176176
.getResultsAroundUser({ limit: 3 }, { user: currentUser })
@@ -183,6 +183,19 @@ describe('Leaderboard', () => {
183183
rankings.map(ranking => ranking.rank).should.eql([1, 2, 3]);
184184
});
185185
});
186+
it('get results around a specified user', function() {
187+
const leaderboard = this.leaderboard;
188+
return leaderboard
189+
.getResultsAroundUser(users[1], { limit: 3 }, { user: currentUser })
190+
.then(rankings => {
191+
rankings.should.be.an.Array();
192+
rankings.should.be.length(3);
193+
rankings[1].value.should.eql(1);
194+
expect(rankings[1].user.get('username')).to.be(undefined);
195+
rankings[1].includedStatistics.should.be.an.Array();
196+
rankings.map(ranking => ranking.rank).should.eql([0, 1, 2]);
197+
});
198+
});
186199
});
187200

188201
after(function() {

0 commit comments

Comments
 (0)