Skip to content

Commit 825cc90

Browse files
committed
feat(Leaderboard): add includeUserKeys option
for #getResults
1 parent edea8ca commit 825cc90

File tree

3 files changed

+78
-38
lines changed

3 files changed

+78
-38
lines changed

src/leaderboard.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,14 @@ _.extend(
279279
}).then(({ count }) => count);
280280
},
281281
_getResults(
282-
{ skip, limit, selectUserKeys, includeStatistics, version },
282+
{
283+
skip,
284+
limit,
285+
selectUserKeys,
286+
includeUserKeys,
287+
includeStatistics,
288+
version,
289+
},
283290
authOptions,
284291
userId
285292
) {
@@ -291,8 +298,13 @@ _.extend(
291298
query: {
292299
skip,
293300
limit,
294-
includeUser: selectUserKeys
295-
? ensureArray(selectUserKeys).join(',')
301+
selectUserKeys:
302+
_.union(
303+
ensureArray(selectUserKeys),
304+
ensureArray(includeUserKeys)
305+
).join(',') || undefined,
306+
includeUser: includeUserKeys
307+
? ensureArray(includeUserKeys).join(',')
296308
: undefined,
297309
includeStatistics: includeStatistics
298310
? ensureArray(includeStatistics).join(',')
@@ -322,18 +334,33 @@ _.extend(
322334
* @param {Object} [options]
323335
* @param {number} [options.skip] The number of results to skip. This is useful for pagination.
324336
* @param {number} [options.limit] The limit of the number of results.
325-
* @param {string[]} [options.selectUserKeys] Specify keys of the users to include
337+
* @param {string[]} [options.selectUserKeys] Specify keys of the users to include in the Rankings
338+
* @param {string[]} [options.includeUserKeys] If the value of a selected user keys is a Pointer, use this options to include its value.
326339
* @param {string[]} [options.includeStatistics] Specify other statistics to include in the Rankings
327340
* @param {number} [options.version] Specify the version of the leaderboard
328341
* @param {AuthOptions} [authOptions]
329342
* @return {Promise<Ranking[]>}
330343
*/
331344
getResults(
332-
{ skip, limit, selectUserKeys, includeStatistics, version } = {},
345+
{
346+
skip,
347+
limit,
348+
selectUserKeys,
349+
includeUserKeys,
350+
includeStatistics,
351+
version,
352+
} = {},
333353
authOptions
334354
) {
335355
return this._getResults(
336-
{ skip, limit, selectUserKeys, includeStatistics, version },
356+
{
357+
skip,
358+
limit,
359+
selectUserKeys,
360+
includeUserKeys,
361+
includeStatistics,
362+
version,
363+
},
337364
authOptions
338365
);
339366
},
@@ -342,7 +369,8 @@ _.extend(
342369
* @param {AV.User} user The specified AV.User pointer.
343370
* @param {Object} [options]
344371
* @param {number} [options.limit] The limit of the number of results.
345-
* @param {string[]} [options.selectUserKeys] Specify keys of the users to include
372+
* @param {string[]} [options.selectUserKeys] Specify keys of the users to include in the Rankings
373+
* @param {string[]} [options.includeUserKeys] If the value of a selected user keys is a Pointer, use this options to include its value.
346374
* @param {string[]} [options.includeStatistics] Specify other statistics to include in the Rankings
347375
* @param {number} [options.version] Specify the version of the leaderboard
348376
* @param {AuthOptions} [authOptions]
@@ -353,9 +381,15 @@ _.extend(
353381
if (user && typeof user.id !== 'string') {
354382
return this.getResultsAroundUser(undefined, user, options);
355383
}
356-
const { limit, selectUserKeys, includeStatistics, version } = options;
384+
const {
385+
limit,
386+
selectUserKeys,
387+
includeUserKeys,
388+
includeStatistics,
389+
version,
390+
} = options;
357391
return this._getResults(
358-
{ limit, selectUserKeys, includeStatistics, version },
392+
{ limit, selectUserKeys, includeUserKeys, includeStatistics, version },
359393
authOptions,
360394
user ? user.id : 'self'
361395
);

storage.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ export class Leaderboard {
953953
skip?: number;
954954
limit?: number;
955955
selectUserKeys?: string | string[];
956+
includeUserKeys?: string | string[];
956957
includeStatistics?: string | string[];
957958
version?: number;
958959
},
@@ -963,6 +964,7 @@ export class Leaderboard {
963964
options?: {
964965
limit?: number;
965966
selectUserKeys?: string | string[];
967+
includeUserKeys?: string | string[];
966968
includeStatistics?: string | string[];
967969
version?: number;
968970
},
@@ -972,6 +974,7 @@ export class Leaderboard {
972974
options?: {
973975
limit?: number;
974976
selectUserKeys?: string | string[];
977+
includeUserKeys?: string | string[];
975978
includeStatistics?: string | string[];
976979
version?: number;
977980
},

test/leaderboard.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const statisticName = `score_${Date.now()}`;
22
const statisticName2 = `score_${Date.now()}_2`;
33

4+
const Company = AV.Object.extend('Company');
5+
46
describe('Leaderboard', () => {
5-
before(function setUpLeaderboard() {
6-
return Promise.all([
7+
before(async function setUpLeaderboard() {
8+
const [leaderboard, leaderboard2] = await Promise.all([
79
AV.Leaderboard.createLeaderboard(
810
{
911
statisticName,
@@ -26,10 +28,9 @@ describe('Leaderboard', () => {
2628
useMasterKey: true,
2729
}
2830
),
29-
]).then(([leaderboard, leaderboard2]) => {
30-
this.leaderboard = leaderboard;
31-
this.leaderboard2 = leaderboard2;
32-
});
31+
]);
32+
this.leaderboard = leaderboard;
33+
this.leaderboard2 = leaderboard2;
3334
});
3435

3536
function validateLeaderboard(leaderboard) {
@@ -90,31 +91,29 @@ describe('Leaderboard', () => {
9091
let users;
9192
let currentUser;
9293
let stats;
93-
before(() =>
94-
Promise.all(
94+
before(async () => {
95+
const company = await new Company({ name: 'LeanCloud' }).save();
96+
users = await Promise.all(
9597
['0', '1', '2', '3'].map(value =>
96-
AV.User.signUp(Date.now() + value, Date.now() + value)
98+
new AV.User({ company })
99+
.setUsername(Date.now() + value)
100+
.setPassword(Date.now() + value)
101+
.signUp()
97102
)
98-
)
99-
.then(result => {
100-
users = result;
101-
currentUser = users[2];
102-
return Promise.all(
103-
users.map((user, index) =>
104-
AV.Leaderboard.updateStatistics(
105-
user,
106-
{ [statisticName]: index, [statisticName2]: -index },
107-
{
108-
user,
109-
}
110-
)
111-
)
112-
);
113-
})
114-
.then(result => {
115-
stats = result[2];
116-
})
117-
);
103+
);
104+
currentUser = users[2];
105+
stats = (await Promise.all(
106+
users.map((user, index) =>
107+
AV.Leaderboard.updateStatistics(
108+
user,
109+
{ [statisticName]: index, [statisticName2]: -index },
110+
{
111+
user,
112+
}
113+
)
114+
)
115+
))[2];
116+
});
118117

119118
after(() =>
120119
Promise.all(users.map(user => user.destroy({ useMasterKey: true })))
@@ -152,6 +151,7 @@ describe('Leaderboard', () => {
152151
return leaderboard
153152
.getResults({
154153
selectUserKeys: ['username'],
154+
includeUserKeys: 'company',
155155
includeStatistics: [statisticName2],
156156
})
157157
.then(rankings => {
@@ -162,6 +162,9 @@ describe('Leaderboard', () => {
162162
rankings[2].user
163163
.get('username')
164164
.should.be.eql(currentUser.get('username'));
165+
const company = rankings[2].user.get('company');
166+
company.should.be.instanceof(Company);
167+
company.get('name').should.be.eql('LeanCloud');
165168
rankings[2].includedStatistics.should.be.an.Array();
166169
rankings[2].includedStatistics[0].name.should.be.eql(statisticName2);
167170
});

0 commit comments

Comments
 (0)