2019-02-05 11:48:08 +09:00
|
|
|
import $ from 'cafy';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import define from '../../define';
|
|
|
|
import { ApiError } from '../../error';
|
2021-11-07 18:04:32 +09:00
|
|
|
import { Users, Followings, UserProfiles } from '@/models/index';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query';
|
|
|
|
import { toPunyNullable } from '@/misc/convert-host';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['users'],
|
|
|
|
|
2020-02-15 21:33:32 +09:00
|
|
|
requireCredential: false as const,
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-11-02 03:32:24 +09:00
|
|
|
params: {
|
|
|
|
userId: {
|
2019-02-16 10:58:44 +09:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-02-16 10:58:44 +09:00
|
|
|
username: {
|
2021-12-09 23:58:30 +09:00
|
|
|
validator: $.optional.str,
|
2019-02-16 10:58:44 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
host: {
|
2021-12-09 23:58:30 +09:00
|
|
|
validator: $.optional.nullable.str,
|
2019-02-16 10:58:44 +09:00
|
|
|
},
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
sinceId: {
|
|
|
|
validator: $.optional.type(ID),
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
untilId: {
|
2019-02-13 16:33:07 +09:00
|
|
|
validator: $.optional.type(ID),
|
2018-11-02 03:32:24 +09:00
|
|
|
},
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
limit: {
|
|
|
|
validator: $.optional.num.range(1, 100),
|
2021-12-09 23:58:30 +09:00
|
|
|
default: 10,
|
2019-04-07 21:50:36 +09:00
|
|
|
},
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
2019-02-24 19:42:26 +09:00
|
|
|
res: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-07 21:50:36 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'Following',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-24 19:42:26 +09:00
|
|
|
},
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '27fa5435-88ab-43de-9360-387de88727cd',
|
2021-11-07 18:04:32 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
forbidden: {
|
|
|
|
message: 'Forbidden.',
|
|
|
|
code: 'FORBIDDEN',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '3c6a84db-d619-26af-ca14-06232a21df8a',
|
2021-11-07 18:04:32 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2018-11-02 03:32:24 +09:00
|
|
|
};
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-02-22 11:46:58 +09:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-04-07 21:50:36 +09:00
|
|
|
const user = await Users.findOne(ps.userId != null
|
|
|
|
? { id: ps.userId }
|
2019-04-13 16:54:21 +09:00
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) });
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (user == null) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
2021-11-07 18:04:32 +09: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 21:50:36 +09:00
|
|
|
const query = makePaginationQuery(Followings.createQueryBuilder('following'), ps.sinceId, ps.untilId)
|
2021-03-21 10:41:21 +09:00
|
|
|
.andWhere(`following.followeeId = :userId`, { userId: user.id })
|
|
|
|
.innerJoinAndSelect('following.follower', 'follower');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const followings = await query
|
2019-04-13 01:43:22 +09:00
|
|
|
.take(ps.limit!)
|
2019-04-07 21:50:36 +09:00
|
|
|
.getMany();
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
return await Followings.packMany(followings, me, { populateFollower: true });
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|