From 357528d1399f4f7046c4cd8ed1a9884ffbba905f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Sun, 17 Feb 2019 21:40:53 +0900 Subject: [PATCH] Use object instead of if chain (#4212) --- src/prelude/symbol.ts | 1 + src/server/api/endpoints/admin/drive/files.ts | 36 ++---- src/server/api/endpoints/admin/show-users.ts | 46 ++------ src/server/api/endpoints/users.ts | 104 +++++++----------- 4 files changed, 65 insertions(+), 122 deletions(-) create mode 100644 src/prelude/symbol.ts diff --git a/src/prelude/symbol.ts b/src/prelude/symbol.ts new file mode 100644 index 000000000..51e12f745 --- /dev/null +++ b/src/prelude/symbol.ts @@ -0,0 +1 @@ +export const fallback = Symbol('fallback'); diff --git a/src/server/api/endpoints/admin/drive/files.ts b/src/server/api/endpoints/admin/drive/files.ts index 12b2bac37..3a837e418 100644 --- a/src/server/api/endpoints/admin/drive/files.ts +++ b/src/server/api/endpoints/admin/drive/files.ts @@ -1,6 +1,7 @@ import $ from 'cafy'; import File, { packMany } from '../../../../../models/drive-file'; import define from '../../../define'; +import { fallback } from '../../../../../prelude/symbol'; export const meta = { requireCredential: false, @@ -37,32 +38,15 @@ export const meta = { } }; -export default define(meta, (ps, me) => new Promise(async (res, rej) => { - let _sort; - if (ps.sort) { - if (ps.sort == '+createdAt') { - _sort = { - uploadDate: -1 - }; - } else if (ps.sort == '-createdAt') { - _sort = { - uploadDate: 1 - }; - } else if (ps.sort == '+size') { - _sort = { - length: -1 - }; - } else if (ps.sort == '-size') { - _sort = { - length: 1 - }; - } - } else { - _sort = { - _id: -1 - }; - } +const sort: any = { // < https://github.com/Microsoft/TypeScript/issues/1863 + '+createdAt': { uploadDate: -1 }, + '-createdAt': { uploadDate: 1 }, + '+size': { length: -1 }, + '-size': { length: 1 }, + [fallback]: { _id: -1 } +}; +export default define(meta, (ps, me) => new Promise(async (res, rej) => { const q = { 'metadata.deletedAt': { $exists: false }, } as any; @@ -73,7 +57,7 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => { const files = await File .find(q, { limit: ps.limit, - sort: _sort, + sort: sort[ps.sort] || sort[fallback], skip: ps.offset }); diff --git a/src/server/api/endpoints/admin/show-users.ts b/src/server/api/endpoints/admin/show-users.ts index cc21b4e67..3646f96d7 100644 --- a/src/server/api/endpoints/admin/show-users.ts +++ b/src/server/api/endpoints/admin/show-users.ts @@ -1,6 +1,7 @@ import $ from 'cafy'; import User, { pack } from '../../../../models/user'; import define from '../../define'; +import { fallback } from '../../../../prelude/symbol'; export const meta = { requireCredential: true, @@ -52,40 +53,17 @@ export const meta = { } }; -export default define(meta, (ps, me) => new Promise(async (res, rej) => { - let _sort; - if (ps.sort) { - if (ps.sort == '+follower') { - _sort = { - followersCount: -1 - }; - } else if (ps.sort == '-follower') { - _sort = { - followersCount: 1 - }; - } else if (ps.sort == '+createdAt') { - _sort = { - createdAt: -1 - }; - } else if (ps.sort == '+updatedAt') { - _sort = { - updatedAt: -1 - }; - } else if (ps.sort == '-createdAt') { - _sort = { - createdAt: 1 - }; - } else if (ps.sort == '-updatedAt') { - _sort = { - updatedAt: 1 - }; - } - } else { - _sort = { - _id: -1 - }; - } +const sort: any = { // < https://github.com/Microsoft/TypeScript/issues/1863 + '+follower': { followersCount: -1 }, + '-follower': { followersCount: 1 }, + '+createdAt': { createdAt: -1 }, + '-createdAt': { createdAt: 1 }, + '+updatedAt': { updatedAt: -1 }, + '-updatedAt': { updatedAt: 1 }, + [fallback]: { _id: -1 } +}; +export default define(meta, (ps, me) => new Promise(async (res, rej) => { const q = { $and: [] } as any; @@ -117,7 +95,7 @@ export default define(meta, (ps, me) => new Promise(async (res, rej) => { const users = await User .find(q, { limit: ps.limit, - sort: _sort, + sort: sort[ps.sort] || sort[fallback], skip: ps.offset }); diff --git a/src/server/api/endpoints/users.ts b/src/server/api/endpoints/users.ts index fa35235a1..c17f8c1b5 100644 --- a/src/server/api/endpoints/users.ts +++ b/src/server/api/endpoints/users.ts @@ -1,6 +1,9 @@ import $ from 'cafy'; import User, { pack } from '../../../models/user'; import define from '../define'; +import { fallback } from '../../../prelude/symbol'; + +const nonnull = { $ne: null }; export const meta = { requireCredential: false, @@ -50,71 +53,48 @@ export const meta = { } }; +const state: any = { // < https://github.com/Microsoft/TypeScript/issues/1863 + 'admin': { isAdmin: true }, + 'moderator': { isModerator: true }, + 'adminOrModerator': { + $or: [ + { isAdmin: true }, + { isModerator: true } + ] + }, + 'verified': { isVerified: true }, + 'alive': { + updatedAt: { $gt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5) } + }, + [fallback]: {} +}; + +const origin: any = { // < https://github.com/Microsoft/TypeScript/issues/1863 + 'local': { host: null }, + 'remote': { host: nonnull }, + [fallback]: {} +}; + +const sort: any = { // < https://github.com/Microsoft/TypeScript/issues/1863 + '+follower': { followersCount: -1 }, + '-follower': { followersCount: 1 }, + '+createdAt': { createdAt: -1 }, + '-createdAt': { createdAt: 1 }, + '+updatedAt': { updatedAt: -1 }, + '-updatedAt': { updatedAt: 1 }, + [fallback]: { _id: -1 } +}; + export default define(meta, (ps, me) => new Promise(async (res, rej) => { - let _sort; - if (ps.sort) { - if (ps.sort == '+follower') { - _sort = { - followersCount: -1 - }; - } else if (ps.sort == '-follower') { - _sort = { - followersCount: 1 - }; - } else if (ps.sort == '+createdAt') { - _sort = { - createdAt: -1 - }; - } else if (ps.sort == '+updatedAt') { - _sort = { - updatedAt: -1 - }; - } else if (ps.sort == '-createdAt') { - _sort = { - createdAt: 1 - }; - } else if (ps.sort == '-updatedAt') { - _sort = { - updatedAt: 1 - }; - } - } else { - _sort = { - _id: -1 - }; - } - - const q = { - $and: [] - } as any; - - // state - q.$and.push( - ps.state == 'admin' ? { isAdmin: true } : - ps.state == 'moderator' ? { isModerator: true } : - ps.state == 'adminOrModerator' ? { - $or: [{ - isAdmin: true - }, { - isModerator: true - }] - } : - ps.state == 'verified' ? { isVerified: true } : - ps.state == 'alive' ? { updatedAt: { $gt: new Date(Date.now() - (1000 * 60 * 60 * 24 * 5)) } } : - {} - ); - - // origin - q.$and.push( - ps.origin == 'local' ? { host: null } : - ps.origin == 'remote' ? { host: { $ne: null } } : - {} - ); - const users = await User - .find(q, { + .find({ + $and: [ + state[ps.state] || state[fallback], + origin[ps.origin] || origin[fallback] + ] + }, { limit: ps.limit, - sort: _sort, + sort: sort[ps.sort] || sort[fallback], skip: ps.offset });