summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/aggregation/notes/reply.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-04-08 02:30:37 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-04-08 02:30:37 +0900
commita1b490afa756a71b9cef4afa424575bc223bc612 (patch)
tree06de4d839e17b1e08e0891542af7360c701a154a /src/server/api/endpoints/aggregation/notes/reply.ts
parentMerge pull request #1392 from syuilo/greenkeeper/element-ui-2.3.3 (diff)
downloadmisskey-a1b490afa756a71b9cef4afa424575bc223bc612.tar.gz
misskey-a1b490afa756a71b9cef4afa424575bc223bc612.tar.bz2
misskey-a1b490afa756a71b9cef4afa424575bc223bc612.zip
Post --> Note
Closes #1411
Diffstat (limited to 'src/server/api/endpoints/aggregation/notes/reply.ts')
-rw-r--r--src/server/api/endpoints/aggregation/notes/reply.ts75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/server/api/endpoints/aggregation/notes/reply.ts b/src/server/api/endpoints/aggregation/notes/reply.ts
new file mode 100644
index 0000000000..42df95a9a5
--- /dev/null
+++ b/src/server/api/endpoints/aggregation/notes/reply.ts
@@ -0,0 +1,75 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Note from '../../../../../models/note';
+
+/**
+ * Aggregate reply of a note
+ *
+ * @param {any} params
+ * @return {Promise<any>}
+ */
+module.exports = (params) => new Promise(async (res, rej) => {
+ // Get 'noteId' parameter
+ const [noteId, noteIdErr] = $(params.noteId).id().$;
+ if (noteIdErr) return rej('invalid noteId param');
+
+ // Lookup note
+ const note = await Note.findOne({
+ _id: noteId
+ });
+
+ if (note === null) {
+ return rej('note not found');
+ }
+
+ const datas = await Note
+ .aggregate([
+ { $match: { reply: note._id } },
+ { $project: {
+ createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST
+ }},
+ { $project: {
+ date: {
+ year: { $year: '$createdAt' },
+ month: { $month: '$createdAt' },
+ day: { $dayOfMonth: '$createdAt' }
+ }
+ }},
+ { $group: {
+ _id: '$date',
+ count: { $sum: 1 }
+ }}
+ ]);
+
+ datas.forEach(data => {
+ data.date = data._id;
+ delete data._id;
+ });
+
+ const graph = [];
+
+ for (let i = 0; i < 30; i++) {
+ const day = new Date(new Date().setDate(new Date().getDate() - i));
+
+ const data = datas.filter(d =>
+ d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate()
+ )[0];
+
+ if (data) {
+ graph.push(data);
+ } else {
+ graph.push({
+ date: {
+ year: day.getFullYear(),
+ month: day.getMonth() + 1, // In JavaScript, month is zero-based.
+ day: day.getDate()
+ },
+ count: 0
+ });
+ }
+ }
+
+ res(graph);
+});