From 8c8ff7d49aeed251c6af5018d5fa1cbcedc3117b Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 25 Oct 2018 07:04:15 +0900 Subject: [PATCH] Implement featured note API --- src/models/note.ts | 13 ++++-- src/server/api/endpoints/notes/featured.ts | 49 ++++++++++++++++++++++ src/services/note/create.ts | 3 +- src/services/note/reaction/create.ts | 8 ++-- 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/server/api/endpoints/notes/featured.ts diff --git a/src/models/note.ts b/src/models/note.ts index 6edf6ec31..c147b63f0 100644 --- a/src/models/note.ts +++ b/src/models/note.ts @@ -22,11 +22,11 @@ Note.createIndex('userId'); Note.createIndex('mentions'); Note.createIndex('visibleUserIds'); Note.createIndex('tagsLower'); +Note.createIndex('_user.host'); Note.createIndex('_files._id'); Note.createIndex('_files.contentType'); -Note.createIndex({ - createdAt: -1 -}); +Note.createIndex({ createdAt: -1 }); +Note.createIndex({ score: -1 }, { sparse: true }); export default Note; export function isValidText(text: string): boolean { @@ -85,8 +85,14 @@ export type INote = { heading: number; speed: number; }; + uri: string; + /** + * 人気の投稿度合いを表すスコア + */ + score: number; + // 非正規化 _reply?: { userId: mongo.ObjectID; @@ -298,6 +304,7 @@ export const pack = async ( delete _note.prev; delete _note.next; delete _note.tagsLower; + delete _note.score; delete _note._user; delete _note._reply; delete _note._renote; diff --git a/src/server/api/endpoints/notes/featured.ts b/src/server/api/endpoints/notes/featured.ts new file mode 100644 index 000000000..363170ead --- /dev/null +++ b/src/server/api/endpoints/notes/featured.ts @@ -0,0 +1,49 @@ +import $ from 'cafy'; +import Note from '../../../../models/note'; +import { packMany } from '../../../../models/note'; +import { ILocalUser } from '../../../../models/user'; +import getParams from '../../get-params'; + +export const meta = { + desc: { + 'ja-JP': 'Featuredな投稿を取得します。', + 'en-US': 'Get featured notes.' + }, + + requireCredential: false, + + params: { + limit: $.num.optional.range(1, 30).note({ + default: 10, + desc: { + 'ja-JP': '最大数' + } + }) + } +}; + +export default async (params: any, user: ILocalUser) => { + const [ps, psErr] = getParams(meta, params); + if (psErr) throw psErr; + + const day = 1000 * 60 * 60 * 24; + + const notes = await Note + .find({ + createdAt: { + $gt: new Date(Date.now() - day) + }, + deletedAt: null, + visibility: { $in: ['public', 'home'] } + }, { + limit: ps.limit, + sort: { + score: -1 + }, + hint: { + score: -1 + } + }); + + return await packMany(notes, user); +}; diff --git a/src/services/note/create.ts b/src/services/note/create.ts index 96ffe9687..55e1d569a 100644 --- a/src/services/note/create.ts +++ b/src/services/note/create.ts @@ -314,7 +314,8 @@ async function renderActivity(data: Option, note: INote) { function incRenoteCount(renote: INote) { Note.update({ _id: renote._id }, { $inc: { - renoteCount: 1 + renoteCount: 1, + score: 1 } }); } diff --git a/src/services/note/reaction/create.ts b/src/services/note/reaction/create.ts index edf648109..4a09cf535 100644 --- a/src/services/note/reaction/create.ts +++ b/src/services/note/reaction/create.ts @@ -36,12 +36,12 @@ export default async (user: IUser, note: INote, reaction: string) => new Promise res(); - const inc: {[key: string]: number} = {}; - inc[`reactionCounts.${reaction}`] = 1; - // Increment reactions count await Note.update({ _id: note._id }, { - $inc: inc + $inc: { + [`reactionCounts.${reaction}`]: 1, + score: 1 + } }); perUserReactionsChart.update(user, note);