This commit is contained in:
かっこかり 2024-11-12 03:08:24 +09:00 committed by GitHub
commit 3d55e8a160
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 69 additions and 18 deletions

View file

@ -3,19 +3,29 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { Injectable } from '@nestjs/common';
import { Injectable, Inject } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
import type { UserProfilesRepository } from '@/models/_.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { RoleService } from '@/core/RoleService.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { getJsonSchema } from '@/core/chart/core.js';
import PerUserFollowingChart from '@/core/chart/charts/per-user-following.js';
import { schema } from '@/core/chart/charts/entities/per-user-following.js';
import { ApiError } from '@/server/api/error.js';
export const meta = {
tags: ['charts', 'users', 'following'],
res: getJsonSchema(schema),
allowGet: true,
cacheSec: 60 * 60,
errors: {
ffIsMarkedAsPrivate: {
message: 'This user\'s followings and/or followers is marked as private.',
code: 'FF_IS_MARKED_AS_PRIVATE',
id: '52e90f27-3dfd-441e-a1f2-ca6ac7068040',
},
},
} as const;
export const paramDef = {
@ -32,10 +42,48 @@ export const paramDef = {
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
constructor(
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
private roleService: RoleService,
private userEntityService: UserEntityService,
private perUserFollowingChart: PerUserFollowingChart,
) {
super(meta, paramDef, async (ps, me) => {
return await this.perUserFollowingChart.getChart(ps.span, ps.limit, ps.offset ? new Date(ps.offset) : null, ps.userId);
const done = async () => {
return await this.perUserFollowingChart.getChart(ps.span, ps.limit, ps.offset ? new Date(ps.offset) : null, ps.userId);
};
if (me != null && me.id === ps.userId) {
return await done();
}
const profile = await this.userProfilesRepository.findOneByOrFail({ userId: ps.userId });
if (profile.followingVisibility === 'public' && profile.followersVisibility === 'public') {
return await done();
}
const iAmModerator = await this.roleService.isModerator(me);
if (iAmModerator) {
return await done();
}
if (
me != null && (
(profile.followingVisibility === 'followers' && profile.followersVisibility === 'followers') ||
(profile.followingVisibility === 'followers' && profile.followersVisibility === 'public') ||
(profile.followingVisibility === 'public' && profile.followersVisibility === 'followers')
)
) {
const relations = await this.userEntityService.getRelation(me.id, ps.userId);
if (relations.following) {
return await done();
}
}
throw new ApiError(meta.errors.ffIsMarkedAsPrivate);
});
}
}