summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/aggregation
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2018-04-02 22:06:24 +0900
committerGitHub <noreply@github.com>2018-04-02 22:06:24 +0900
commit5b80b6e901b3618b4cbce56e9f78fc8c11a0adb3 (patch)
tree15bd5adf6a54ef6bd77ca87d2c5d94ed4802e415 /src/server/api/endpoints/aggregation
parentMerge pull request #1373 from akihikodaki/misc (diff)
parentIntroduce followed log and following log (diff)
downloadsharkey-5b80b6e901b3618b4cbce56e9f78fc8c11a0adb3.tar.gz
sharkey-5b80b6e901b3618b4cbce56e9f78fc8c11a0adb3.tar.bz2
sharkey-5b80b6e901b3618b4cbce56e9f78fc8c11a0adb3.zip
Merge pull request #1374 from akihikodaki/log
Introduce followed log and following log
Diffstat (limited to 'src/server/api/endpoints/aggregation')
-rw-r--r--src/server/api/endpoints/aggregation/users/followers.ts64
-rw-r--r--src/server/api/endpoints/aggregation/users/following.ts63
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));
});