2022-06-14 11:01:23 +02:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { UsersRepository, FollowingsRepository, UserProfilesRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { QueryService } from '@/core/QueryService.js';
|
|
|
|
import { FollowingEntityService } from '@/core/entities/FollowingEntityService.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { ApiError } from '../../error.js';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Show everyone that follows this user.',
|
|
|
|
|
2019-02-24 11:42:26 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-04-07 14:50:36 +02:00
|
|
|
items: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'Following',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-24 11:42:26 +01:00
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '27fa5435-88ab-43de-9360-387de88727cd',
|
2021-11-07 10:04:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
forbidden: {
|
|
|
|
message: 'Forbidden.',
|
|
|
|
code: 'FORBIDDEN',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '3c6a84db-d619-26af-ca14-06232a21df8a',
|
2021-11-07 10:04:32 +01:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-11-01 19:32:24 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01: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-03 06:57:26 +02: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 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.followingsRepository)
|
|
|
|
private followingsRepository: FollowingsRepository,
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private utilityService: UtilityService,
|
|
|
|
private followingEntityService: FollowingEntityService,
|
|
|
|
private queryService: QueryService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const user = await this.usersRepository.findOneBy(ps.userId != null
|
|
|
|
? { id: ps.userId }
|
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: this.utilityService.toPunyNullable(ps.host) ?? IsNull() });
|
|
|
|
|
|
|
|
if (user == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2021-11-07 10:04:32 +01:00
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: user.id });
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-09-17 20:27:08 +02: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) {
|
|
|
|
const following = await this.followingsRepository.findOneBy({
|
|
|
|
followeeId: user.id,
|
|
|
|
followerId: me.id,
|
|
|
|
});
|
|
|
|
if (following == null) {
|
|
|
|
throw new ApiError(meta.errors.forbidden);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = this.queryService.makePaginationQuery(this.followingsRepository.createQueryBuilder('following'), ps.sinceId, ps.untilId)
|
|
|
|
.andWhere('following.followeeId = :userId', { userId: user.id })
|
|
|
|
.innerJoinAndSelect('following.follower', 'follower');
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const followings = await query
|
|
|
|
.take(ps.limit)
|
|
|
|
.getMany();
|
|
|
|
|
|
|
|
return await this.followingEntityService.packMany(followings, me, { populateFollower: true });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|