2019-02-05 02:48:08 +00:00
|
|
|
import $ from 'cafy';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import define from '../../define';
|
|
|
|
import { ApiError } from '../../error';
|
2021-11-07 09:04:32 +00:00
|
|
|
import { Users, Followings, UserProfiles } from '@/models/index';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
|
|
|
import { toPunyNullable } from '@/misc/convert-host';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: false,
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
params: {
|
|
|
|
userId: {
|
2019-02-16 01:58:44 +00:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-02-16 01:58:44 +00:00
|
|
|
username: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.optional.str,
|
2019-02-16 01:58:44 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
host: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.optional.nullable.str,
|
2019-02-16 01:58:44 +00:00
|
|
|
},
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
sinceId: {
|
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
untilId: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-01 18:32:24 +00:00
|
|
|
},
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
limit: {
|
|
|
|
validator: $.optional.num.range(1, 100),
|
2021-12-09 14:58:30 +00:00
|
|
|
default: 10,
|
2019-04-07 12:50:36 +00:00
|
|
|
},
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
2019-02-24 10:42:26 +00:00
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-04-07 12:50:36 +00:00
|
|
|
items: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'Following',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2019-02-24 10:42:26 +00:00
|
|
|
},
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '63e4aba4-4156-4e53-be25-c9559e42d71b',
|
2021-11-07 09:04:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
forbidden: {
|
|
|
|
message: 'Forbidden.',
|
|
|
|
code: 'FORBIDDEN',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'f6cdb0df-c19f-ec5c-7dbb-0ba84a1f92ba',
|
2021-11-07 09:04:32 +00:00
|
|
|
},
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-11-01 18:32:24 +00:00
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-02-22 02:46:58 +00:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-04-07 12:50:36 +00:00
|
|
|
const user = await Users.findOne(ps.userId != null
|
|
|
|
? { id: ps.userId }
|
2019-04-13 07:54:21 +00:00
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) });
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (user == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 09:04:32 +00:00
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
|
|
|
|
|
|
|
if (profile.ffVisibility === 'private') {
|
|
|
|
if (me == null || (me.id !== user.id)) {
|
|
|
|
throw new ApiError(meta.errors.forbidden);
|
|
|
|
}
|
|
|
|
} else if (profile.ffVisibility === 'followers') {
|
|
|
|
if (me == null) {
|
|
|
|
throw new ApiError(meta.errors.forbidden);
|
|
|
|
} else if (me.id !== user.id) {
|
|
|
|
const following = await Followings.findOne({
|
|
|
|
followeeId: user.id,
|
|
|
|
followerId: me.id,
|
|
|
|
});
|
|
|
|
if (following == null) {
|
|
|
|
throw new ApiError(meta.errors.forbidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const query = makePaginationQuery(Followings.createQueryBuilder('following'), ps.sinceId, ps.untilId)
|
2021-03-21 01:41:21 +00:00
|
|
|
.andWhere(`following.followerId = :userId`, { userId: user.id })
|
|
|
|
.innerJoinAndSelect('following.followee', 'followee');
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const followings = await query
|
2019-04-12 16:43:22 +00:00
|
|
|
.take(ps.limit!)
|
2019-04-07 12:50:36 +00:00
|
|
|
.getMany();
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
return await Followings.packMany(followings, me, { populateFollowee: true });
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|