2019-02-04 20:48:08 -06:00
|
|
|
import $ from 'cafy';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { ID } from '../../../../misc/cafy-id';
|
2018-11-01 23:47:44 -05:00
|
|
|
import define from '../../define';
|
2019-02-21 20:46:58 -06:00
|
|
|
import { ApiError } from '../../error';
|
2019-04-07 07:50:36 -05:00
|
|
|
import { Users, Followings } from '../../../../models';
|
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
2019-04-13 02:54:21 -05:00
|
|
|
import { toPunyNullable } from '../../../../misc/convert-host';
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
'ja-JP': '指定したユーザーのフォロワー一覧を取得します。',
|
|
|
|
'en-US': 'Get followers of a user.'
|
|
|
|
},
|
|
|
|
|
2019-02-22 20:20:58 -06:00
|
|
|
tags: ['users'],
|
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
requireCredential: false,
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-11-01 13:32:24 -05:00
|
|
|
params: {
|
|
|
|
userId: {
|
2019-02-15 19:58:44 -06:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-03 08:49:36 -05:00
|
|
|
desc: {
|
|
|
|
'ja-JP': '対象のユーザーのID',
|
|
|
|
'en-US': 'Target user ID'
|
|
|
|
}
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-02-15 19:58:44 -06:00
|
|
|
username: {
|
|
|
|
validator: $.optional.str
|
|
|
|
},
|
|
|
|
|
|
|
|
host: {
|
|
|
|
validator: $.optional.nullable.str
|
|
|
|
},
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
sinceId: {
|
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
untilId: {
|
2019-02-13 01:33:07 -06:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 13:32:24 -05:00
|
|
|
},
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
limit: {
|
|
|
|
validator: $.optional.num.range(1, 100),
|
|
|
|
default: 10
|
|
|
|
},
|
2019-02-21 20:46:58 -06:00
|
|
|
},
|
|
|
|
|
2019-02-24 04:42:26 -06:00
|
|
|
res: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-07 07:50:36 -05:00
|
|
|
items: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
ref: 'Following',
|
|
|
|
}
|
2019-02-24 04:42:26 -06:00
|
|
|
},
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
|
|
|
id: '27fa5435-88ab-43de-9360-387de88727cd'
|
|
|
|
}
|
2018-11-01 13:32:24 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-21 20:46:58 -06:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-04-07 07:50:36 -05:00
|
|
|
const user = await Users.findOne(ps.userId != null
|
|
|
|
? { id: ps.userId }
|
2019-04-13 02:54:21 -05:00
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) });
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
if (user == null) {
|
2019-02-21 20:46:58 -06:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const query = makePaginationQuery(Followings.createQueryBuilder('following'), ps.sinceId, ps.untilId)
|
|
|
|
.andWhere(`following.followeeId = :userId`, { userId: user.id });
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
const followings = await query
|
2019-04-12 11:43:22 -05:00
|
|
|
.take(ps.limit!)
|
2019-04-07 07:50:36 -05:00
|
|
|
.getMany();
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
return await Followings.packMany(followings, me, { populateFollower: true });
|
2019-02-21 20:46:58 -06:00
|
|
|
});
|