From e8afa2c940ec967dda18c0f87cacdb42d8311ca5 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 4 Oct 2018 13:33:59 +0900 Subject: [PATCH] Improve pack function of favorite --- src/models/favorite.ts | 13 +++++++++++++ src/server/api/endpoints/i/favorites.ts | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/models/favorite.ts b/src/models/favorite.ts index b2d2fc93e..2c07959be 100644 --- a/src/models/favorite.ts +++ b/src/models/favorite.ts @@ -41,6 +41,13 @@ export async function deleteFavorite(favorite: string | mongo.ObjectID | IFavori }); } +export const packMany = async ( + favorites: any[], + me: any +) => { + return (await Promise.all(favorites.map(f => pack(f, me)))).filter(x => x != null); +}; + /** * Pack a favorite for API response */ @@ -70,5 +77,11 @@ export const pack = ( // Populate note _favorite.note = await packNote(_favorite.noteId, me); + // (データベースの不具合などで)投稿が見つからなかったら + if (_favorite.note == null) { + console.warn(`favorite: note not found on database: ${_favorite.noteId}`); + return resolve(null); + } + resolve(_favorite); }); diff --git a/src/server/api/endpoints/i/favorites.ts b/src/server/api/endpoints/i/favorites.ts index 32c1a55fb..e7cf8a71a 100644 --- a/src/server/api/endpoints/i/favorites.ts +++ b/src/server/api/endpoints/i/favorites.ts @@ -1,5 +1,5 @@ import $ from 'cafy'; import ID from '../../../../misc/cafy-id'; -import Favorite, { pack } from '../../../../models/favorite'; +import Favorite, { packMany } from '../../../../models/favorite'; import { ILocalUser } from '../../../../models/user'; export const meta = { @@ -55,5 +55,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = .find(query, { limit, sort }); // Serialize - res(await Promise.all(favorites.map(favorite => pack(favorite, user)))); + res(await packMany(favorites, user)); });