diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2019-02-22 11:46:58 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-02-22 11:46:58 +0900 |
| commit | 2756f553c68082342a784ef716c62da6cea6f3ca (patch) | |
| tree | 1e0364ca9ddc1fd88e311f0687746f44e007effd /src/server/api/endpoints/users/notes.ts | |
| parent | Update CHANGELOG.md (diff) | |
| download | sharkey-2756f553c68082342a784ef716c62da6cea6f3ca.tar.gz sharkey-2756f553c68082342a784ef716c62da6cea6f3ca.tar.bz2 sharkey-2756f553c68082342a784ef716c62da6cea6f3ca.zip | |
Improve error handling of API (#4345)
* wip
* wip
* wip
* Update attached_notes.ts
* wip
* Refactor
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* Update call.ts
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* wip
* :v:
* Fix
Diffstat (limited to 'src/server/api/endpoints/users/notes.ts')
| -rw-r--r-- | src/server/api/endpoints/users/notes.ts | 58 |
1 files changed, 19 insertions, 39 deletions
diff --git a/src/server/api/endpoints/users/notes.ts b/src/server/api/endpoints/users/notes.ts index 5c776e48e2..98d67e865a 100644 --- a/src/server/api/endpoints/users/notes.ts +++ b/src/server/api/endpoints/users/notes.ts @@ -1,11 +1,10 @@ import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id'; -import getHostLower from '../../common/get-host-lower'; import Note, { packMany } from '../../../../models/note'; import User from '../../../../models/user'; import define from '../../define'; -import { countIf } from '../../../../prelude/array'; import Following from '../../../../models/following'; +import { ApiError } from '../../error'; export const meta = { desc: { @@ -14,7 +13,7 @@ export const meta = { params: { userId: { - validator: $.optional.type(ID), + validator: $.type(ID), transform: transform, desc: { 'ja-JP': '対象のユーザーのID', @@ -22,17 +21,6 @@ export const meta = { } }, - username: { - validator: $.optional.str, - desc: { - 'ja-JP': 'ユーザー名' - } - }, - - host: { - validator: $.optional.nullable.str, - }, - includeReplies: { validator: $.optional.bool, default: true, @@ -134,32 +122,27 @@ export const meta = { 'ja-JP': 'true にすると、NSFW指定されたファイルを除外します(fileTypeが指定されている場合のみ有効)' } }, - } -}; - -export default define(meta, (ps, me) => new Promise(async (res, rej) => { - if (ps.userId === undefined && ps.username === undefined) { - return rej('userId or username is required'); - } + }, - // Check if only one of sinceId, untilId, sinceDate, untilDate specified - if (countIf(x => x != null, [ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate]) > 1) { - throw 'only one of sinceId, untilId, sinceDate, untilDate can be specified'; + errors: { + noSuchUser: { + message: 'No such user.', + code: 'NO_SUCH_USER', + id: '27e494ba-2ac2-48e8-893b-10d4d8c2387b' + } } +}; - const q = ps.userId != null - ? { _id: ps.userId } - : { usernameLower: ps.username.toLowerCase(), host: getHostLower(ps.host) } ; - +export default define(meta, async (ps, me) => { // Lookup user - const user = await User.findOne(q, { + const user = await User.findOne({ _id: ps.userId }, { fields: { _id: true } }); if (user === null) { - return rej('user not found'); + throw new ApiError(meta.errors.noSuchUser); } const isFollowing = me == null ? false : ((await Following.findOne({ @@ -259,13 +242,10 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => { } //#endregion - // Issue query - const notes = await Note - .find(query, { - limit: ps.limit, - sort: sort - }); + const notes = await Note.find(query, { + limit: ps.limit, + sort: sort + }); - // Serialize - res(await packMany(notes, me)); -})); + return await packMany(notes, me); +}); |