jormungandr-bite/src/server/api/endpoints/users/show.ts

89 lines
1.9 KiB
TypeScript
Raw Normal View History

2018-11-01 12:32:24 -06:00
import $ from 'cafy'; import ID, { transform, transformMany } from '../../../../misc/cafy-id';
2018-11-01 22:47:44 -06:00
import User, { pack, isRemoteUser } from '../../../../models/user';
2018-04-01 13:15:27 -06:00
import resolveRemoteUser from '../../../../remote/resolve-user';
2018-11-01 22:47:44 -06:00
import define from '../../define';
2018-03-27 01:51:12 -06:00
2018-03-31 04:55:00 -06:00
const cursorOption = { fields: { data: false } };
2016-12-28 15:49:51 -07:00
2018-11-01 12:32:24 -06:00
export const meta = {
desc: {
'ja-JP': '指定したユーザーの情報を取得します。'
},
requireCredential: false,
2018-03-27 01:51:12 -06:00
2018-11-01 12:32:24 -06:00
params: {
userId: {
validator: $.type(ID).optional,
transform: transform,
desc: {
2018-11-03 07:49:36 -06:00
'ja-JP': '対象のユーザーのID',
'en-US': 'Target user ID'
2018-11-01 12:32:24 -06:00
}
},
2016-12-28 15:49:51 -07:00
2018-11-01 12:32:24 -06:00
userIds: {
validator: $.arr($.type(ID)).optional.unique(),
transform: transformMany,
desc: {
'ja-JP': 'ユーザーID (配列)'
}
},
2018-04-25 07:37:08 -06:00
2018-11-01 12:32:24 -06:00
username: {
validator: $.str.optional
},
2016-12-28 15:49:51 -07:00
2018-11-01 12:32:24 -06:00
host: {
validator: $.str.optional.nullable
}
}
};
2018-11-01 22:47:44 -06:00
export default define(meta, (ps, me) => new Promise(async (res, rej) => {
2018-11-01 12:32:24 -06:00
let user;
2016-12-28 15:49:51 -07:00
2018-11-01 12:32:24 -06:00
if (ps.userIds) {
2018-04-25 07:37:08 -06:00
const users = await User.find({
_id: {
2018-11-01 12:32:24 -06:00
$in: ps.userIds
2018-04-25 07:37:08 -06:00
}
});
2017-02-21 21:08:33 -07:00
2018-04-25 07:37:08 -06:00
res(await Promise.all(users.map(u => pack(u, me, {
detail: true
}))));
2018-03-27 01:51:12 -06:00
} else {
2018-04-25 07:37:08 -06:00
// Lookup user
2018-11-01 12:32:24 -06:00
if (typeof ps.host === 'string') {
2018-04-25 07:37:08 -06:00
try {
2018-11-01 12:32:24 -06:00
user = await resolveRemoteUser(ps.username, ps.host, cursorOption);
2018-04-25 07:37:08 -06:00
} catch (e) {
console.warn(`failed to resolve remote user: ${e}`);
return rej('failed to resolve remote user');
}
} else {
2018-11-01 12:32:24 -06:00
const q: any = ps.userId != null
? { _id: ps.userId }
: { usernameLower: ps.username.toLowerCase(), host: null };
2016-12-28 15:49:51 -07:00
2018-04-25 07:37:08 -06:00
user = await User.findOne(q, cursorOption);
2018-03-27 01:51:12 -06:00
2018-04-25 07:37:08 -06:00
if (user === null) {
return rej('user not found');
}
2018-03-27 01:51:12 -06:00
}
2016-12-28 15:49:51 -07:00
2018-04-25 07:37:08 -06:00
// Send response
res(await pack(user, me, {
detail: true
}));
if (isRemoteUser(user)) {
if (user.lastFetchedAt == null || Date.now() - user.lastFetchedAt.getTime() > 1000 * 60 * 60 * 24) {
2018-11-01 12:32:24 -06:00
resolveRemoteUser(ps.username, ps.host, { }, true);
}
}
2018-04-25 07:37:08 -06:00
}
2018-11-01 22:47:44 -06:00
}));