summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/aggregation/users/following.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/api/endpoints/aggregation/users/following.ts')
-rw-r--r--src/server/api/endpoints/aggregation/users/following.ts64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/server/api/endpoints/aggregation/users/following.ts b/src/server/api/endpoints/aggregation/users/following.ts
new file mode 100644
index 0000000000..45e246495b
--- /dev/null
+++ b/src/server/api/endpoints/aggregation/users/following.ts
@@ -0,0 +1,64 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import User from '../../../../../models/user';
+import FollowingLog from '../../../../../models/following-log';
+
+/**
+ * Aggregate following of a user
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = (params) => new Promise(async (res, rej) => {
+ // Get 'userId' parameter
+ const [userId, userIdErr] = $(params.userId).id().$;
+ if (userIdErr) return rej('invalid userId param');
+
+ // Lookup user
+ const user = await User.findOne({
+ _id: userId
+ }, {
+ fields: {
+ _id: true
+ }
+ });
+
+ if (user === null) {
+ return rej('user not found');
+ }
+
+ const today = new Date();
+ const graph = [];
+
+ today.setMinutes(0);
+ today.setSeconds(0);
+ today.setMilliseconds(0);
+
+ let cursorDate = new Date(today.getTime());
+ let cursorTime = cursorDate.setDate(new Date(today.getTime()).getDate() + 1);
+
+ for (let i = 0; i < 30; i++) {
+ graph.push(FollowingLog.findOne({
+ createdAt: { $lt: new Date(cursorTime / 1000) },
+ userId: user._id
+ }, {
+ sort: { createdAt: -1 },
+ }).then(log => {
+ cursorDate = new Date(today.getTime());
+ cursorTime = cursorDate.setDate(today.getDate() - i);
+
+ return {
+ date: {
+ year: cursorDate.getFullYear(),
+ month: cursorDate.getMonth() + 1, // In JavaScript, month is zero-based.
+ day: cursorDate.getDate()
+ },
+ count: log ? log.count : 0
+ };
+ }));
+ }
+
+ res(await Promise.all(graph));
+});