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 +++++++++++++++++++++ src/server/api/endpoints/aggregation/posts.ts | 22 +++---- .../api/endpoints/aggregation/posts/reaction.ts | 76 ---------------------- .../api/endpoints/aggregation/posts/reactions.ts | 72 -------------------- .../api/endpoints/aggregation/posts/reply.ts | 75 --------------------- .../api/endpoints/aggregation/posts/repost.ts | 75 --------------------- .../api/endpoints/aggregation/users/activity.ts | 20 +++--- src/server/api/endpoints/aggregation/users/post.ts | 22 +++---- .../api/endpoints/aggregation/users/reaction.ts | 2 +- 12 files changed, 331 insertions(+), 331 deletions(-) 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 delete mode 100644 src/server/api/endpoints/aggregation/posts/reaction.ts delete mode 100644 src/server/api/endpoints/aggregation/posts/reactions.ts delete mode 100644 src/server/api/endpoints/aggregation/posts/reply.ts delete mode 100644 src/server/api/endpoints/aggregation/posts/repost.ts (limited to 'src/server/api/endpoints/aggregation') 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); +}); diff --git a/src/server/api/endpoints/aggregation/posts.ts b/src/server/api/endpoints/aggregation/posts.ts index f4d401eda0..cc2a48b53d 100644 --- a/src/server/api/endpoints/aggregation/posts.ts +++ b/src/server/api/endpoints/aggregation/posts.ts @@ -2,10 +2,10 @@ * Module dependencies */ import $ from 'cafy'; -import Post from '../../../../models/post'; +import Note from '../../../../models/note'; /** - * Aggregate posts + * Aggregate notes * * @param {any} params * @return {Promise} @@ -15,10 +15,10 @@ module.exports = params => new Promise(async (res, rej) => { const [limit = 365, limitErr] = $(params.limit).optional.number().range(1, 365).$; if (limitErr) return rej('invalid limit param'); - const datas = await Post + const datas = await Note .aggregate([ { $project: { - repostId: '$repostId', + renoteId: '$renoteId', replyId: '$replyId', createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST }}, @@ -30,13 +30,13 @@ module.exports = params => new Promise(async (res, rej) => { }, type: { $cond: { - if: { $ne: ['$repostId', null] }, - then: 'repost', + if: { $ne: ['$renoteId', null] }, + then: 'renote', else: { $cond: { if: { $ne: ['$replyId', null] }, then: 'reply', - else: 'post' + else: 'note' } } } @@ -59,8 +59,8 @@ module.exports = params => new Promise(async (res, rej) => { data.date = data._id; delete data._id; - data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; - data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; + data.notes = (data.data.filter(x => x.type == 'note')[0] || { count: 0 }).count; + data.renotes = (data.data.filter(x => x.type == 'renote')[0] || { count: 0 }).count; data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; delete data.data; @@ -79,8 +79,8 @@ module.exports = params => new Promise(async (res, rej) => { graph.push(data); } else { graph.push({ - posts: 0, - reposts: 0, + notes: 0, + renotes: 0, replies: 0 }); } diff --git a/src/server/api/endpoints/aggregation/posts/reaction.ts b/src/server/api/endpoints/aggregation/posts/reaction.ts deleted file mode 100644 index e622745337..0000000000 --- a/src/server/api/endpoints/aggregation/posts/reaction.ts +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../../../models/post'; -import Reaction from '../../../../../models/post-reaction'; - -/** - * Aggregate reaction of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'postId' parameter - const [postId, postIdErr] = $(params.postId).id().$; - if (postIdErr) return rej('invalid postId param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Reaction - .aggregate([ - { $match: { postId: post._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/posts/reactions.ts b/src/server/api/endpoints/aggregation/posts/reactions.ts deleted file mode 100644 index 5f23e296fd..0000000000 --- a/src/server/api/endpoints/aggregation/posts/reactions.ts +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../../../models/post'; -import Reaction from '../../../../../models/post-reaction'; - -/** - * Aggregate reactions of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'postId' parameter - const [postId, postIdErr] = $(params.postId).id().$; - if (postIdErr) return rej('invalid postId param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); - - const reactions = await Reaction - .find({ - postId: post._id, - $or: [ - { deletedAt: { $exists: false } }, - { deletedAt: { $gt: startTime } } - ] - }, { - sort: { - _id: -1 - }, - fields: { - _id: false, - postId: 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/posts/reply.ts b/src/server/api/endpoints/aggregation/posts/reply.ts deleted file mode 100644 index c76191e86b..0000000000 --- a/src/server/api/endpoints/aggregation/posts/reply.ts +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../../../models/post'; - -/** - * Aggregate reply of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'postId' parameter - const [postId, postIdErr] = $(params.postId).id().$; - if (postIdErr) return rej('invalid postId param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Post - .aggregate([ - { $match: { reply: post._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/posts/repost.ts b/src/server/api/endpoints/aggregation/posts/repost.ts deleted file mode 100644 index a203605ebf..0000000000 --- a/src/server/api/endpoints/aggregation/posts/repost.ts +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Module dependencies - */ -import $ from 'cafy'; -import Post from '../../../../../models/post'; - -/** - * Aggregate repost of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => new Promise(async (res, rej) => { - // Get 'postId' parameter - const [postId, postIdErr] = $(params.postId).id().$; - if (postIdErr) return rej('invalid postId param'); - - // Lookup post - const post = await Post.findOne({ - _id: postId - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Post - .aggregate([ - { $match: { repostId: post._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/users/activity.ts b/src/server/api/endpoints/aggregation/users/activity.ts index cef0072296..318cce77a5 100644 --- a/src/server/api/endpoints/aggregation/users/activity.ts +++ b/src/server/api/endpoints/aggregation/users/activity.ts @@ -3,7 +3,7 @@ */ import $ from 'cafy'; import User from '../../../../../models/user'; -import Post from '../../../../../models/post'; +import Note from '../../../../../models/note'; // TODO: likeやfollowも集計 @@ -35,11 +35,11 @@ module.exports = (params) => new Promise(async (res, rej) => { return rej('user not found'); } - const datas = await Post + const datas = await Note .aggregate([ { $match: { userId: user._id } }, { $project: { - repostId: '$repostId', + renoteId: '$renoteId', replyId: '$replyId', createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST }}, @@ -51,13 +51,13 @@ module.exports = (params) => new Promise(async (res, rej) => { }, type: { $cond: { - if: { $ne: ['$repostId', null] }, - then: 'repost', + if: { $ne: ['$renoteId', null] }, + then: 'renote', else: { $cond: { if: { $ne: ['$replyId', null] }, then: 'reply', - else: 'post' + else: 'note' } } } @@ -80,8 +80,8 @@ module.exports = (params) => new Promise(async (res, rej) => { data.date = data._id; delete data._id; - data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; - data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; + data.notes = (data.data.filter(x => x.type == 'note')[0] || { count: 0 }).count; + data.renotes = (data.data.filter(x => x.type == 'renote')[0] || { count: 0 }).count; data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; delete data.data; @@ -105,8 +105,8 @@ module.exports = (params) => new Promise(async (res, rej) => { month: day.getMonth() + 1, // In JavaScript, month is zero-based. day: day.getDate() }, - posts: 0, - reposts: 0, + notes: 0, + renotes: 0, replies: 0 }); } diff --git a/src/server/api/endpoints/aggregation/users/post.ts b/src/server/api/endpoints/aggregation/users/post.ts index 13617cf639..e6170d83e2 100644 --- a/src/server/api/endpoints/aggregation/users/post.ts +++ b/src/server/api/endpoints/aggregation/users/post.ts @@ -3,10 +3,10 @@ */ import $ from 'cafy'; import User from '../../../../../models/user'; -import Post from '../../../../../models/post'; +import Note from '../../../../../models/note'; /** - * Aggregate post of a user + * Aggregate note of a user * * @param {any} params * @return {Promise} @@ -29,11 +29,11 @@ module.exports = (params) => new Promise(async (res, rej) => { return rej('user not found'); } - const datas = await Post + const datas = await Note .aggregate([ { $match: { userId: user._id } }, { $project: { - repostId: '$repostId', + renoteId: '$renoteId', replyId: '$replyId', createdAt: { $add: ['$createdAt', 9 * 60 * 60 * 1000] } // Convert into JST }}, @@ -45,13 +45,13 @@ module.exports = (params) => new Promise(async (res, rej) => { }, type: { $cond: { - if: { $ne: ['$repostId', null] }, - then: 'repost', + if: { $ne: ['$renoteId', null] }, + then: 'renote', else: { $cond: { if: { $ne: ['$replyId', null] }, then: 'reply', - else: 'post' + else: 'note' } } } @@ -74,8 +74,8 @@ module.exports = (params) => new Promise(async (res, rej) => { data.date = data._id; delete data._id; - data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; - data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; + data.notes = (data.data.filter(x => x.type == 'note')[0] || { count: 0 }).count; + data.renotes = (data.data.filter(x => x.type == 'renote')[0] || { count: 0 }).count; data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; delete data.data; @@ -99,8 +99,8 @@ module.exports = (params) => new Promise(async (res, rej) => { month: day.getMonth() + 1, // In JavaScript, month is zero-based. day: day.getDate() }, - posts: 0, - reposts: 0, + notes: 0, + renotes: 0, replies: 0 }); } diff --git a/src/server/api/endpoints/aggregation/users/reaction.ts b/src/server/api/endpoints/aggregation/users/reaction.ts index 0c42ba3360..881c7ea693 100644 --- a/src/server/api/endpoints/aggregation/users/reaction.ts +++ b/src/server/api/endpoints/aggregation/users/reaction.ts @@ -3,7 +3,7 @@ */ import $ from 'cafy'; import User from '../../../../../models/user'; -import Reaction from '../../../../../models/post-reaction'; +import Reaction from '../../../../../models/note-reaction'; /** * Aggregate reaction of a user -- cgit v1.2.3-freya