summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/users.ts
diff options
context:
space:
mode:
authorAcid Chicken (硫酸鶏) <root@acid-chicken.com>2019-02-17 21:40:53 +0900
committersyuilo <Syuilotan@yahoo.co.jp>2019-02-17 21:40:53 +0900
commit357528d1399f4f7046c4cd8ed1a9884ffbba905f (patch)
tree10fa35890bd78c47c224684e3e0fd2dccda893f0 /src/server/api/endpoints/users.ts
parentFix #4292 (#4294) (diff)
downloadsharkey-357528d1399f4f7046c4cd8ed1a9884ffbba905f.tar.gz
sharkey-357528d1399f4f7046c4cd8ed1a9884ffbba905f.tar.bz2
sharkey-357528d1399f4f7046c4cd8ed1a9884ffbba905f.zip
Use object instead of if chain (#4212)
Diffstat (limited to 'src/server/api/endpoints/users.ts')
-rw-r--r--src/server/api/endpoints/users.ts100
1 files changed, 40 insertions, 60 deletions
diff --git a/src/server/api/endpoints/users.ts b/src/server/api/endpoints/users.ts
index fa35235a1d..c17f8c1b5b 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 = {
}
};
-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;
+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]: {}
+};
- // 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)) } } :
- {}
- );
+const origin: any = { // < https://github.com/Microsoft/TypeScript/issues/1863
+ 'local': { host: null },
+ 'remote': { host: nonnull },
+ [fallback]: {}
+};
- // origin
- q.$and.push(
- ps.origin == 'local' ? { host: null } :
- ps.origin == 'remote' ? { host: { $ne: null } } :
- {}
- );
+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 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
});