From 13e43a4f7451ffc2b6ce40a2b855ebd08fbbddbe Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 22 Feb 2019 14:53:03 +0900 Subject: [PATCH] Refactor --- src/server/api/endpoints/notes/favorites/create.ts | 11 ++++------- src/server/api/endpoints/notes/favorites/delete.ts | 11 ++++------- src/server/api/endpoints/notes/polls/vote.ts | 10 ++++------ 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/server/api/endpoints/notes/favorites/create.ts b/src/server/api/endpoints/notes/favorites/create.ts index bdb4aff72d..26e3e34d55 100644 --- a/src/server/api/endpoints/notes/favorites/create.ts +++ b/src/server/api/endpoints/notes/favorites/create.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import Favorite from '../../../../../models/favorite'; -import Note from '../../../../../models/note'; import define from '../../../define'; import { ApiError } from '../../../error'; +import { getNote } from '../../../common/getters'; export const meta = { stability: 'stable', @@ -45,14 +45,11 @@ export const meta = { export default define(meta, async (ps, user) => { // Get favoritee - const note = await Note.findOne({ - _id: ps.noteId + const note = await getNote(ps.noteId).catch(e => { + if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote); + throw e; }); - if (note === null) { - throw new ApiError(meta.errors.noSuchNote); - } - // if already favorited const exist = await Favorite.findOne({ noteId: note._id, diff --git a/src/server/api/endpoints/notes/favorites/delete.ts b/src/server/api/endpoints/notes/favorites/delete.ts index 700eafdaf7..d79a3d88cb 100644 --- a/src/server/api/endpoints/notes/favorites/delete.ts +++ b/src/server/api/endpoints/notes/favorites/delete.ts @@ -1,9 +1,9 @@ import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id'; import Favorite from '../../../../../models/favorite'; -import Note from '../../../../../models/note'; import define from '../../../define'; import { ApiError } from '../../../error'; +import { getNote } from '../../../common/getters'; export const meta = { stability: 'stable', @@ -45,14 +45,11 @@ export const meta = { export default define(meta, async (ps, user) => { // Get favoritee - const note = await Note.findOne({ - _id: ps.noteId + const note = await getNote(ps.noteId).catch(e => { + if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote); + throw e; }); - if (note === null) { - throw new ApiError(meta.errors.noSuchNote); - } - // if already favorited const exist = await Favorite.findOne({ noteId: note._id, diff --git a/src/server/api/endpoints/notes/polls/vote.ts b/src/server/api/endpoints/notes/polls/vote.ts index fe0fffec61..00840579dd 100644 --- a/src/server/api/endpoints/notes/polls/vote.ts +++ b/src/server/api/endpoints/notes/polls/vote.ts @@ -10,6 +10,7 @@ import define from '../../../define'; import createNote from '../../../../../services/note/create'; import User from '../../../../../models/user'; import { ApiError } from '../../../error'; +import { getNote } from '../../../common/getters'; export const meta = { desc: { @@ -65,14 +66,11 @@ export const meta = { export default define(meta, async (ps, user) => { // Get votee - const note = await Note.findOne({ - _id: ps.noteId + const note = await getNote(ps.noteId).catch(e => { + if (e.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') throw new ApiError(meta.errors.noSuchNote); + throw e; }); - if (note === null) { - throw new ApiError(meta.errors.noSuchNote); - } - if (note.poll == null) { throw new ApiError(meta.errors.noPoll); }