diff options
| author | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-04-02 21:57:36 +0900 |
|---|---|---|
| committer | Akihiko Odaki <nekomanma@pixiv.co.jp> | 2018-04-02 22:00:40 +0900 |
| commit | 319e905bf9e5398cb62eaeee7da80ff6e942c094 (patch) | |
| tree | 5d0edf01e29d7904bf4b82d57ff60754741ce100 /src/server/api/endpoints/aggregation/users | |
| parent | Implement Follow activity (diff) | |
| download | sharkey-319e905bf9e5398cb62eaeee7da80ff6e942c094.tar.gz sharkey-319e905bf9e5398cb62eaeee7da80ff6e942c094.tar.bz2 sharkey-319e905bf9e5398cb62eaeee7da80ff6e942c094.zip | |
Introduce followed log and following log
Diffstat (limited to 'src/server/api/endpoints/aggregation/users')
| -rw-r--r-- | src/server/api/endpoints/aggregation/users/followers.ts | 64 | ||||
| -rw-r--r-- | src/server/api/endpoints/aggregation/users/following.ts | 63 |
2 files changed, 54 insertions, 73 deletions
diff --git a/src/server/api/endpoints/aggregation/users/followers.ts b/src/server/api/endpoints/aggregation/users/followers.ts index dda34ed7be..580d31a3f2 100644 --- a/src/server/api/endpoints/aggregation/users/followers.ts +++ b/src/server/api/endpoints/aggregation/users/followers.ts @@ -2,8 +2,9 @@ * Module dependencies */ import $ from 'cafy'; +import { ObjectID } from 'mongodb'; import User from '../../../../../models/user'; -import Following from '../../../../../models/following'; +import FollowedLog from '../../../../../models/followed-log'; /** * Aggregate followers of a user @@ -29,47 +30,36 @@ module.exports = (params) => new Promise(async (res, rej) => { return rej('user not found'); } - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); + const today = new Date(); + const graph = []; - const following = await Following - .find({ - followeeId: user._id, - $or: [ - { deletedAt: { $exists: false } }, - { deletedAt: { $gt: startTime } } - ] - }, { - sort: { createdAt: -1 }, - fields: { - _id: false, - followerId: false, - followeeId: false - } - }); + today.setMinutes(0); + today.setSeconds(0); + today.setMilliseconds(0); - const graph = []; + let cursorDate = new Date(today.getTime()); + let cursorTime = cursorDate.setDate(new Date(today.getTime()).getDate() + 1); for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - // day = day.getTime(); - - const count = following.filter(f => - f.createdAt < day && (f.deletedAt == null || f.deletedAt > day) - ).length; + graph.push(FollowedLog.findOne({ + _id: { $lt: ObjectID.createFromTime(cursorTime / 1000) }, + userId: user._id + }, { + sort: { _id: -1 }, + }).then(log => { + cursorDate = new Date(today.getTime()); + cursorTime = cursorDate.setDate(today.getDate() - i); - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); + return { + date: { + year: cursorDate.getFullYear(), + month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based. + day: cursorDate.getDate() + }, + count: log ? log.count : 0 + }; + })); } - res(graph); + res(await Promise.all(graph)); }); diff --git a/src/server/api/endpoints/aggregation/users/following.ts b/src/server/api/endpoints/aggregation/users/following.ts index cd08d89e49..3ac0e3a539 100644 --- a/src/server/api/endpoints/aggregation/users/following.ts +++ b/src/server/api/endpoints/aggregation/users/following.ts @@ -2,8 +2,9 @@ * Module dependencies */ import $ from 'cafy'; +import { ObjectID } from 'mongodb'; import User from '../../../../../models/user'; -import Following from '../../../../../models/following'; +import FollowingLog from '../../../../../models/following-log'; /** * Aggregate following of a user @@ -29,46 +30,36 @@ module.exports = (params) => new Promise(async (res, rej) => { return rej('user not found'); } - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); + const today = new Date(); + const graph = []; - const following = await Following - .find({ - followerId: user._id, - $or: [ - { deletedAt: { $exists: false } }, - { deletedAt: { $gt: startTime } } - ] - }, { - sort: { createdAt: -1 }, - fields: { - _id: false, - followerId: false, - followeeId: false - } - }); + today.setMinutes(0); + today.setSeconds(0); + today.setMilliseconds(0); - const graph = []; + let cursorDate = new Date(today.getTime()); + let cursorTime = cursorDate.setDate(new Date(today.getTime()).getDate() + 1); for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - - const count = following.filter(f => - f.createdAt < day && (f.deletedAt == null || f.deletedAt > day) - ).length; + graph.push(FollowingLog.findOne({ + _id: { $lt: ObjectID.createFromTime(cursorTime / 1000) }, + userId: user._id + }, { + sort: { _id: -1 }, + }).then(log => { + cursorDate = new Date(today.getTime()); + cursorTime = cursorDate.setDate(today.getDate() - i); - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); + return { + date: { + year: cursorDate.getFullYear(), + month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based. + day: cursorDate.getDate() + }, + count: log ? log.count : 0 + }; + })); } - res(graph); + res(await Promise.all(graph)); }); |