Sharkey/src/server/api/endpoints/users/relation.ts

33 lines
969 B
TypeScript
Raw Normal View History

2018-11-01 20:32:24 +02:00
import $ from 'cafy'; import ID, { transform, ObjectId } from '../../../../misc/cafy-id';
2018-10-31 17:11:21 +02:00
import { ILocalUser, getRelation } from '../../../../models/user';
import getParams from '../../get-params';
export const meta = {
desc: {
'ja-JP': 'ユーザー間のリレーションを取得します。'
},
requireCredential: true,
2018-10-31 17:11:21 +02:00
params: {
2018-11-01 20:32:24 +02:00
userId: {
validator: $.or($.type(ID), $.arr($.type(ID)).unique()),
transform: (v: any): ObjectId | ObjectId[] => Array.isArray(v) ? v.map(x => transform(x)) : transform(v),
2018-10-31 17:11:21 +02:00
desc: {
'ja-JP': 'ユーザーID (配列でも可)'
}
2018-11-01 20:32:24 +02:00
}
2018-10-31 17:11:21 +02:00
}
};
export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
const ids = Array.isArray(ps.userId) ? ps.userId : [ps.userId];
const relations = await Promise.all(ids.map(id => getRelation(me._id, id)));
res(Array.isArray(ps.userId) ? relations : relations[0]);
});