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

This commit is contained in:
syuilo 2018-10-04 00:39:11 +09:00
parent ce336438a1
commit fcb1ed7340
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)) {

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../misc/cafy-id';
import Note, { pack } from '../../../models/note';
import Note, { packMany } from '../../../models/note';
import getParams from '../get-params';
export const meta = {
@ -116,5 +116,5 @@ export default (params: any) => new Promise(async (res, rej) => {
});
// Serialize
res(await Promise.all(notes.map(note => pack(note))));
res(await packMany(notes));
});

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note, { pack, INote } from '../../../../models/note';
import Note, { packMany, INote } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
/**
@ -52,5 +52,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
}
// Serialize
res(await Promise.all(conversation.map(note => pack(note, user))));
res(await packMany(conversation, user));
});

View file

@ -1,7 +1,7 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
import { countIf } from '../../../../prelude/array';
@ -113,5 +113,5 @@ export default async (params: any, user: ILocalUser) => {
});
// Serialize
return await Promise.all(timeline.map(note => pack(note, user)));
return await packMany(timeline, user);
};

View file

@ -2,7 +2,7 @@ import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import Mute from '../../../../models/mute';
import { getFriends } from '../../common/get-friends';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
import { countIf } from '../../../../prelude/array';
@ -240,5 +240,5 @@ export default async (params: any, user: ILocalUser) => {
});
// Serialize
return await Promise.all(timeline.map(note => pack(note, user)));
return await packMany(timeline, user);
};

View file

@ -1,7 +1,7 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
import { countIf } from '../../../../prelude/array';
@ -141,5 +141,5 @@ export default async (params: any, user: ILocalUser) => {
});
// Serialize
return await Promise.all(timeline.map(note => pack(note, user)));
return await packMany(timeline, user);
};

View file

@ -1,7 +1,7 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import { getFriendIds } from '../../common/get-friends';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
import read from '../../../../services/note/read';
@ -89,5 +89,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
mentions.forEach(note => read(user._id, note._id));
// Serialize
res(await Promise.all(mentions.map(mention => pack(mention, user))));
res(await packMany(mentions, user));
});

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note, { pack } from '../../../../models/note';
import Note, { packMany } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
/**
@ -30,5 +30,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
const ids = (note._replyIds || []).slice(offset, offset + limit);
// Serialize
res(await Promise.all(ids.map(id => pack(id, user))));
res(await packMany(ids, user));
});

View file

@ -1,5 +1,5 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note, { pack } from '../../../../models/note';
import Note, { packMany } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
/**
@ -62,6 +62,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
});
// Serialize
res(await Promise.all(renotes.map(async note =>
await pack(note, user))));
res(await packMany(renotes, user));
});

View file

@ -2,7 +2,7 @@ import $ from 'cafy';
import * as mongo from 'mongodb';
import Note from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import es from '../../../../db/elasticsearch';
export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
@ -60,6 +60,6 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
}
});
res(await Promise.all(notes.map(note => pack(note, me))));
res(await packMany(notes, me));
});
});

View file

@ -3,7 +3,7 @@ import Note from '../../../../models/note';
import User, { ILocalUser } from '../../../../models/user';
import Mute from '../../../../models/mute';
import { getFriendIds } from '../../common/get-friends';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import getParams from '../../get-params';
import { erase } from '../../../../prelude/array';
@ -363,5 +363,5 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
});
// Serialize
res(await Promise.all(notes.map(note => pack(note, me))));
res(await packMany(notes, me));
});

View file

@ -2,7 +2,7 @@ import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import Mute from '../../../../models/mute';
import { getFriends } from '../../common/get-friends';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
import { countIf } from '../../../../prelude/array';
@ -237,5 +237,5 @@ export default async (params: any, user: ILocalUser) => {
});
// Serialize
return await Promise.all(timeline.map(note => pack(note, user)));
return await packMany(timeline, user);
};

View file

@ -1,7 +1,7 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import Mute from '../../../../models/mute';
import { pack } from '../../../../models/note';
import { packMany } from '../../../../models/note';
import UserList from '../../../../models/user-list';
import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
@ -242,5 +242,5 @@ export default async (params: any, user: ILocalUser) => {
});
// Serialize
return await Promise.all(timeline.map(note => pack(note, user)));
return await packMany(timeline, user);
};

View file

@ -1,6 +1,6 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import getHostLower from '../../common/get-host-lower';
import Note, { pack } from '../../../../models/note';
import Note, { packMany } from '../../../../models/note';
import User, { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
import { countIf } from '../../../../prelude/array';
@ -181,5 +181,5 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
});
// Serialize
res(await Promise.all(notes.map(note => pack(note, me))));
res(await packMany(notes, me));
});