2022-02-27 02:07:39 +00:00
|
|
|
import define from '../../define.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
|
|
|
import { Users, Followings, UserProfiles } from '@/models/index.js';
|
|
|
|
import { makePaginationQuery } from '../../common/make-pagination-query.js';
|
|
|
|
import { toPunyNullable } from '@/misc/convert-host.js';
|
2022-03-26 06:34:00 +00:00
|
|
|
import { IsNull } from 'typeorm';
|
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
|
|
|
|
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: '27fa5435-88ab-43de-9360-387de88727cd',
|
2021-11-07 09:04:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
forbidden: {
|
|
|
|
message: 'Forbidden.',
|
|
|
|
code: 'FORBIDDEN',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '3c6a84db-d619-26af-ca14-06232a21df8a',
|
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-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
sinceId: { type: 'string', format: 'misskey:id' },
|
|
|
|
untilId: { type: 'string', format: 'misskey:id' },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
},
|
2022-04-02 06:04:36 +00:00
|
|
|
anyOf: [
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
|
|
|
},
|
|
|
|
required: ['userId'],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
username: { type: 'string' },
|
|
|
|
host: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
description: 'The local host is represented with `null`.',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ['username', 'host'],
|
|
|
|
},
|
|
|
|
],
|
2022-02-19 05:05:32 +00:00
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 05:05:32 +00:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2022-03-26 06:34:00 +00:00
|
|
|
const user = await Users.findOneBy(ps.userId != null
|
2019-04-07 12:50:36 +00:00
|
|
|
? { id: ps.userId }
|
2022-03-26 06:34:00 +00:00
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: toPunyNullable(ps.host) ?? IsNull() });
|
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
|
|
|
}
|
|
|
|
|
2022-03-26 06:34:00 +00:00
|
|
|
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
|
2021-11-07 09:04:32 +00:00
|
|
|
|
|
|
|
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) {
|
2022-03-26 06:34:00 +00:00
|
|
|
const following = await Followings.findOneBy({
|
2021-11-07 09:04:32 +00:00
|
|
|
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.followeeId = :userId`, { userId: user.id })
|
|
|
|
.innerJoinAndSelect('following.follower', 'follower');
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const followings = await query
|
2022-02-19 05:05:32 +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, { populateFollower: true });
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|