From bd1f3a2f0196214248e8a259f8aacfe3e8259c19 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 29 May 2018 00:36:52 +0900 Subject: [PATCH] #1579 --- src/models/note.ts | 1 + src/server/api/endpoints/notes/replies.ts | 26 +++-------------------- src/services/note/create.ts | 18 ++++++++++++++++ 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/models/note.ts b/src/models/note.ts index 5d7bab383..ad8c1565f 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -77,6 +77,7 @@ export type INote = { host: string; inbox?: string; }; + _replyIds?: mongo.ObjectID[]; }; /** diff --git a/src/server/api/endpoints/notes/replies.ts b/src/server/api/endpoints/notes/replies.ts index 11d221d8f..7261d3dbf 100644 --- a/src/server/api/endpoints/notes/replies.ts +++ b/src/server/api/endpoints/notes/replies.ts @@ -1,15 +1,8 @@ -/** - * Module dependencies - */ import $ from 'cafy'; import ID from '../../../../cafy-id'; import Note, { pack } from '../../../../models/note'; /** - * Show a replies of a note - * - * @param {any} params - * @param {any} user - * @return {Promise} + * GEt replies of a note */ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'noteId' parameter @@ -24,10 +17,6 @@ module.exports = (params, user) => new Promise(async (res, rej) => { const [offset = 0, offsetErr] = $.num.optional().min(0).get(params.offset); if (offsetErr) return rej('invalid offset param'); - // Get 'sort' parameter - const [sort = 'desc', sortError] = $.str.optional().or('desc asc').get(params.sort); - if (sortError) return rej('invalid sort param'); - // Lookup note const note = await Note.findOne({ _id: noteId @@ -37,17 +26,8 @@ module.exports = (params, user) => new Promise(async (res, rej) => { return rej('note not found'); } - // Issue query - const replies = await Note - .find({ replyId: note._id }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); + const ids = note._replyIds.slice(offset, offset + limit); // Serialize - res(await Promise.all(replies.map(async note => - await pack(note, user)))); + res(await Promise.all(ids.map(id => pack(id, user)))); }); diff --git a/src/services/note/create.ts b/src/services/note/create.ts index b9ff1f679..37d21feca 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -172,6 +172,24 @@ export default async (user: IUser, data: { } }); + if (data.reply) { + Note.update({ _id: data.reply._id }, { + $push: { + _replyIds: note._id + } + }); + } + + const isQuote = data.renote && (data.text || data.poll || data.media); + + if (isQuote) { + Note.update({ _id: data.renote._id }, { + $push: { + _quoteIds: note._id + } + }); + } + // Serialize const noteObj = await pack(note);