Improve pack function of favorite
This commit is contained in:
		| @@ -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 |  * Pack a favorite for API response | ||||||
|  */ |  */ | ||||||
| @@ -70,5 +77,11 @@ export const pack = ( | |||||||
| 	// Populate note | 	// Populate note | ||||||
| 	_favorite.note = await packNote(_favorite.noteId, me); | 	_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); | 	resolve(_favorite); | ||||||
| }); | }); | ||||||
|   | |||||||
| @@ -1,5 +1,5 @@ | |||||||
| import $ from 'cafy'; import ID from '../../../../misc/cafy-id'; | 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'; | import { ILocalUser } from '../../../../models/user'; | ||||||
|  |  | ||||||
| export const meta = { | export const meta = { | ||||||
| @@ -55,5 +55,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) = | |||||||
| 		.find(query, { limit, sort }); | 		.find(query, { limit, sort }); | ||||||
|  |  | ||||||
| 	// Serialize | 	// Serialize | ||||||
| 	res(await Promise.all(favorites.map(favorite => pack(favorite, user)))); | 	res(await packMany(favorites, user)); | ||||||
| }); | }); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 syuilo
					syuilo