2019-01-11 01:09:44 +02:00
|
|
|
import $ from 'cafy';
|
|
|
|
import User, { pack } from '../../../../models/user';
|
|
|
|
import define from '../../define';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
requireCredential: true,
|
|
|
|
requireModerator: true,
|
|
|
|
|
|
|
|
params: {
|
|
|
|
limit: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2019-01-11 01:09:44 +02:00
|
|
|
default: 10
|
|
|
|
},
|
|
|
|
|
|
|
|
offset: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.num.min(0),
|
2019-01-11 01:09:44 +02:00
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
sort: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.str.or([
|
2019-01-11 01:09:44 +02:00
|
|
|
'+follower',
|
|
|
|
'-follower',
|
|
|
|
'+createdAt',
|
|
|
|
'-createdAt',
|
|
|
|
'+updatedAt',
|
|
|
|
'-updatedAt',
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
|
|
|
|
state: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.str.or([
|
2019-01-11 01:09:44 +02:00
|
|
|
'all',
|
|
|
|
'admin',
|
|
|
|
'moderator',
|
|
|
|
'adminOrModerator',
|
|
|
|
'verified',
|
2019-02-10 12:43:46 +02:00
|
|
|
'silenced',
|
2019-01-11 01:09:44 +02:00
|
|
|
'suspended',
|
|
|
|
]),
|
|
|
|
default: 'all'
|
|
|
|
},
|
|
|
|
|
|
|
|
origin: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.str.or([
|
2019-01-11 01:09:44 +02:00
|
|
|
'combined',
|
|
|
|
'local',
|
|
|
|
'remote',
|
|
|
|
]),
|
|
|
|
default: 'local'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 } :
|
2019-02-10 12:43:46 +02:00
|
|
|
ps.state == 'silenced' ? { isSilenced: true } :
|
2019-01-11 01:09:44 +02:00
|
|
|
ps.state == 'suspended' ? { isSuspended: true } :
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
// origin
|
|
|
|
q.$and.push(
|
|
|
|
ps.origin == 'local' ? { host: null } :
|
|
|
|
ps.origin == 'remote' ? { host: { $ne: null } } :
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
|
|
|
|
const users = await User
|
|
|
|
.find(q, {
|
|
|
|
limit: ps.limit,
|
|
|
|
sort: _sort,
|
|
|
|
skip: ps.offset
|
|
|
|
});
|
|
|
|
|
|
|
|
res(await Promise.all(users.map(user => pack(user, me, { detail: true }))));
|
|
|
|
}));
|