From a1b490afa756a71b9cef4afa424575bc223bc612 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 8 Apr 2018 02:30:37 +0900 Subject: Post --> Note Closes #1411 --- .../api/endpoints/aggregation/notes/reaction.ts | 76 ++++++++++++++++++++++ .../api/endpoints/aggregation/notes/reactions.ts | 72 ++++++++++++++++++++ .../api/endpoints/aggregation/notes/reply.ts | 75 +++++++++++++++++++++ .../api/endpoints/aggregation/notes/repost.ts | 75 +++++++++++++++++++++ 4 files changed, 298 insertions(+) create mode 100644 src/server/api/endpoints/aggregation/notes/reaction.ts create mode 100644 src/server/api/endpoints/aggregation/notes/reactions.ts create mode 100644 src/server/api/endpoints/aggregation/notes/reply.ts create mode 100644 src/server/api/endpoints/aggregation/notes/repost.ts (limited to 'src/server/api/endpoints/aggregation/notes') diff --git a/src/server/api/endpoints/aggregation/notes/reaction.ts b/src/server/api/endpoints/aggregation/notes/reaction.ts new file mode 100644 index 0000000000..586e8c2d85 --- /dev/null +++ b/src/server/api/endpoints/aggregation/notes/reaction.ts @@ -0,0 +1,76 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import Note from '../../../../../models/note'; +import Reaction from '../../../../../models/note-reaction'; + +/** + * Aggregate reaction of a note + * + * @param {any} params + * @return {Promise} + */ +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 Reaction + .aggregate([ + { $match: { noteId: 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); +}); diff --git a/src/server/api/endpoints/aggregation/notes/reactions.ts b/src/server/api/endpoints/aggregation/notes/reactions.ts new file mode 100644 index 0000000000..ff9491292e --- /dev/null +++ b/src/server/api/endpoints/aggregation/notes/reactions.ts @@ -0,0 +1,72 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import Note from '../../../../../models/note'; +import Reaction from '../../../../../models/note-reaction'; + +/** + * Aggregate reactions of a note + * + * @param {any} params + * @return {Promise} + */ +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 startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); + + const reactions = await Reaction + .find({ + noteId: note._id, + $or: [ + { deletedAt: { $exists: false } }, + { deletedAt: { $gt: startTime } } + ] + }, { + sort: { + _id: -1 + }, + fields: { + _id: false, + noteId: false + } + }); + + const graph = []; + + 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 = reactions.filter(r => + r.createdAt < day && (r.deletedAt == null || r.deletedAt > day) + ).length; + + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: count + }); + } + + res(graph); +}); 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} + */ +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); +}); diff --git a/src/server/api/endpoints/aggregation/notes/repost.ts b/src/server/api/endpoints/aggregation/notes/repost.ts new file mode 100644 index 0000000000..feb3348a7e --- /dev/null +++ b/src/server/api/endpoints/aggregation/notes/repost.ts @@ -0,0 +1,75 @@ +/** + * Module dependencies + */ +import $ from 'cafy'; +import Note from '../../../../../models/note'; + +/** + * Aggregate renote of a note + * + * @param {any} params + * @return {Promise} + */ +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: { renoteId: 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); +}); -- cgit v1.2.3-freya