ドキュメントが見つからなくてもエラーにせずnullを返すように

This commit is contained in:
syuilo
2018-10-04 00:39:11 +09:00
parent 7e50646ede
commit baad11288a
15 changed files with 50 additions and 34 deletions

View File

@@ -226,6 +226,17 @@ export const hideNote = async (packedNote: any, meId: mongo.ObjectID) => {
}
};
export const packMany = async (
notes: (string | mongo.ObjectID | INote)[],
me?: string | mongo.ObjectID | IUser,
options?: {
detail?: boolean;
skipHide?: boolean;
}
) => {
return (await Promise.all(notes.map(n => pack(n, me, options)))).filter(x => x != null);
};
/**
* Pack a note for API response
*
@@ -271,7 +282,11 @@ export const pack = async (
_note = deepcopy(note);
}
if (!_note) throw `invalid note arg ${note}`;
// 投稿がデータベース上に見つからなかったとき
if (_note == null) {
console.warn(`note not found on database: ${note}`);
return null;
}
const id = _note._id;

View File

@@ -3,7 +3,7 @@ const deepcopy = require('deepcopy');
const sequential = require('promise-sequential');
import rap from '@prezzemolo/rap';
import db from '../db/mongodb';
import Note, { pack as packNote, deleteNote } from './note';
import Note, { packMany as packNoteMany, deleteNote } from './note';
import Following, { deleteFollowing } from './following';
import Mute, { deleteMute } from './mute';
import { getFriendIds } from '../server/api/common/get-friends';
@@ -361,9 +361,11 @@ export const pack = (
_user = deepcopy(user);
}
// TODO: ここでエラーにするのではなくダミーのユーザーデータを返す
// SEE: https://github.com/syuilo/misskey/issues/1432
if (!_user) return reject('invalid user arg.');
// ユーザーデータベース上に見つからなかったとき
if (_user == null) {
console.warn(`user not found on database: ${user}`);
return null;
}
// Me
const meId: mongo.ObjectID = me
@@ -468,9 +470,9 @@ export const pack = (
if (opts.detail) {
if (_user.pinnedNoteIds) {
// Populate pinned notes
_user.pinnedNotes = Promise.all(_user.pinnedNoteIds.map((id: mongo.ObjectId) => packNote(id, meId, {
_user.pinnedNotes = packNoteMany(_user.pinnedNoteIds, meId, {
detail: true
})));
});
}
if (meId && !meId.equals(_user.id)) {