テストの追加と検出した不備の修正

This commit is contained in:
samunohito 2024-06-29 09:42:34 +09:00
parent f7f9df878b
commit 28fdf1b9a6
15 changed files with 1974 additions and 203 deletions

View File

@ -7,6 +7,7 @@ const base = require('./jest.config.cjs')
module.exports = {
...base,
globalSetup: "<rootDir>/test/jest.setup.unit.cjs",
testMatch: [
"<rootDir>/test/unit/**/*.ts",
"<rootDir>/src/**/*.test.ts",

View File

@ -6,7 +6,7 @@
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
import Redis from 'ioredis';
import { DI } from '@/di-symbols.js';
import type { ChannelFollowingsRepository } from '@/models/_.js';
import type { ChannelFollowingsRepository, ChannelsRepository, MiUser } from '@/models/_.js';
import { MiChannel } from '@/models/_.js';
import { IdService } from '@/core/IdService.js';
import { GlobalEvents, GlobalEventService } from '@/core/GlobalEventService.js';
@ -23,6 +23,8 @@ export class ChannelFollowingService implements OnModuleInit {
private redisClient: Redis.Redis,
@Inject(DI.redisForSub)
private redisForSub: Redis.Redis,
@Inject(DI.channelsRepository)
private channelsRepository: ChannelsRepository,
@Inject(DI.channelFollowingsRepository)
private channelFollowingsRepository: ChannelFollowingsRepository,
private idService: IdService,
@ -45,6 +47,50 @@ export class ChannelFollowingService implements OnModuleInit {
onModuleInit() {
}
/**
* .
* @param params
* @param [opts]
* @param {(boolean|undefined)} [opts.idOnly=false] IDのみを取得するかどうか. ID以外のフィールドに値がセットされなくなりJOINも一切されなくなるので注意.
* @param {(boolean|undefined)} [opts.joinUser=undefined] JOINするかどうか(falseまたは省略時はJOINしない).
* @param {(boolean|undefined)} [opts.joinBannerFile=undefined] JOINするかどうか(falseまたは省略時はJOINしない).
*/
@bindThis
public async list(
params: {
requestUserId: MiUser['id'],
},
opts?: {
idOnly?: boolean;
joinUser?: boolean;
joinBannerFile?: boolean;
},
): Promise<MiChannel[]> {
if (opts?.idOnly) {
const q = this.channelFollowingsRepository.createQueryBuilder('channel_following')
.select('channel_following.followeeId')
.where('channel_following.followerId = :userId', { userId: params.requestUserId });
return q
.getRawMany<{ channel_following_followeeId: string }>()
.then(xs => xs.map(x => ({ id: x.channel_following_followeeId } as MiChannel)));
} else {
const q = this.channelsRepository.createQueryBuilder('channel')
.innerJoin('channel_following', 'channel_following', 'channel_following.followeeId = channel.id')
.where('channel_following.followerId = :userId', { userId: params.requestUserId });
if (opts?.joinUser) {
q.innerJoinAndSelect('channel.user', 'user');
}
if (opts?.joinBannerFile) {
q.leftJoinAndSelect('channel.banner', 'drive_file');
}
return q.getMany();
}
}
@bindThis
public async follow(
requestUser: MiLocalUser,

View File

@ -5,7 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';
import { In } from 'typeorm';
import { Brackets, In } from 'typeorm';
import { DI } from '@/di-symbols.js';
import type { ChannelMutingRepository, ChannelsRepository, MiChannel, MiChannelMuting, MiUser } from '@/models/_.js';
import { IdService } from '@/core/IdService.js';
@ -47,6 +47,7 @@ export class ChannelMutingService {
* .
* @param params
* @param [opts]
* @param {(boolean|undefined)} [opts.idOnly=false] IDのみを取得するかどうか. ID以外のフィールドに値がセットされなくなりJOINも一切されなくなるので注意.
* @param {(boolean|undefined)} [opts.joinUser=undefined] JOINするかどうか(falseまたは省略時はJOINしない).
* @param {(boolean|undefined)} [opts.joinBannerFile=undefined] JOINするかどうか(falseまたは省略時はJOINしない).
*/
@ -56,27 +57,42 @@ export class ChannelMutingService {
requestUserId: MiUser['id'],
},
opts?: {
idOnly?: boolean;
joinUser?: boolean;
joinBannerFile?: boolean;
},
): Promise<MiChannel[]> {
const q = this.channelsRepository.createQueryBuilder('channel')
.innerJoin('channel_muting', 'channel_muting', 'channel_muting.channelId = channel.id')
.where('channel_muting.userId = :userId', { userId: params.requestUserId })
.andWhere(qb => {
qb.where('channel_muting.expiresAt IS NULL')
.orWhere('channel_muting.expiresAt > :now', { now: new Date() });
});
if (opts?.idOnly) {
const q = this.channelMutingRepository.createQueryBuilder('channel_muting')
.select('channel_muting.channelId')
.where('channel_muting.userId = :userId', { userId: params.requestUserId })
.andWhere(new Brackets(qb => {
qb.where('channel_muting.expiresAt IS NULL')
.orWhere('channel_muting.expiresAt > :now', { now: new Date() });
}));
if (opts?.joinUser) {
q.innerJoinAndSelect('channel.user', 'user');
return q
.getRawMany<{ channel_muting_channelId: string }>()
.then(xs => xs.map(x => ({ id: x.channel_muting_channelId } as MiChannel)));
} else {
const q = this.channelsRepository.createQueryBuilder('channel')
.innerJoin('channel_muting', 'channel_muting', 'channel_muting.channelId = channel.id')
.where('channel_muting.userId = :userId', { userId: params.requestUserId })
.andWhere(new Brackets(qb => {
qb.where('channel_muting.expiresAt IS NULL')
.orWhere('channel_muting.expiresAt > :now', { now: new Date() });
}));
if (opts?.joinUser) {
q.innerJoinAndSelect('channel.user', 'user');
}
if (opts?.joinBannerFile) {
q.leftJoinAndSelect('channel.banner', 'drive_file');
}
return q.getMany();
}
if (opts?.joinBannerFile) {
q.leftJoinAndSelect('channel.banner', 'drive_file');
}
return q.getMany();
}
/**

View File

@ -5,6 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import { Brackets } from 'typeorm';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { NotesRepository, AntennasRepository } from '@/models/_.js';
import { QueryService } from '@/core/QueryService.js';
@ -15,6 +16,7 @@ import { IdService } from '@/core/IdService.js';
import { FanoutTimelineService } from '@/core/FanoutTimelineService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { trackPromise } from '@/misc/promise-tracker.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import { ApiError } from '../../error.js';
export const meta = {
@ -74,6 +76,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private noteReadService: NoteReadService,
private fanoutTimelineService: FanoutTimelineService,
private globalEventService: GlobalEventService,
private channelMutingService: ChannelMutingService,
) {
super(meta, paramDef, async (ps, me) => {
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
@ -113,6 +116,21 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.leftJoinAndSelect('reply.user', 'replyUser')
.leftJoinAndSelect('renote.user', 'renoteUser');
// -- ミュートされたチャンネル対策
const mutingChannelIds = await this.channelMutingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id));
if (mutingChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.channelId IS NULL');
qb.orWhere('note.channelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
query.andWhere(new Brackets(qb => {
qb.orWhere('note.renoteChannelId IS NULL');
qb.orWhere('note.renoteChannelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
}
this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateMutedUserQuery(query, me);
this.queryService.generateBlockedUserQuery(query, me);

View File

@ -21,6 +21,7 @@ import { MiLocalUser } from '@/models/User.js';
import { FanoutTimelineEndpointService } from '@/core/FanoutTimelineEndpointService.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import { ApiError } from '../../error.js';
import { ChannelFollowingService } from "@/core/ChannelFollowingService.js";
export const meta = {
tags: ['notes'],
@ -77,16 +78,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
constructor(
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
@Inject(DI.channelFollowingsRepository)
private channelFollowingsRepository: ChannelFollowingsRepository,
private noteEntityService: NoteEntityService,
private roleService: RoleService,
private activeUsersChart: ActiveUsersChart,
private idService: IdService,
private cacheService: CacheService,
private queryService: QueryService,
private userFollowingService: UserFollowingService,
private channelMutingService: ChannelMutingService,
private channelFollowingService: ChannelFollowingService,
private metaService: MetaService,
private fanoutTimelineEndpointService: FanoutTimelineEndpointService,
) {
@ -184,12 +183,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
withReplies: boolean,
}, me: MiLocalUser) {
const followees = await this.userFollowingService.getFollowees(me.id);
const followingChannels = await this.channelFollowingsRepository.find({
where: {
followerId: me.id,
},
});
const mutingChannelIds = await this.channelMutingService.list({ requestUserId: me.id }).then(x => x.map(x => x.id));
const mutingChannelIds = await this.channelMutingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id));
const followingChannelIds = await this.channelFollowingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id).filter(x => !mutingChannelIds.includes(x)));
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
.andWhere(new Brackets(qb => {
@ -208,9 +208,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.leftJoinAndSelect('reply.user', 'replyUser')
.leftJoinAndSelect('renote.user', 'renoteUser');
if (followingChannels.length > 0) {
const followingChannelIds = followingChannels.map(x => x.followeeId);
if (followingChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb.where('note.channelId IN (:...followingChannelIds)', { followingChannelIds });
qb.orWhere('note.channelId IS NULL');
@ -221,23 +219,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
if (mutingChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb
// ミュートしてるチャンネルは含めない
.where(new Brackets(qb2 => {
qb2
.andWhere(new Brackets(qb3 => {
qb3
.andWhere('note.channelId IS NOT NULL')
.andWhere('note.channelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}))
.andWhere(new Brackets(qb3 => {
qb3
.andWhere('note.renoteChannelId IS NOT NULL')
.andWhere('note.renoteChannelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
}))
// チャンネルの投稿ではない
.orWhere('note.channelId IS NULL');
qb.orWhere('note.renoteChannelId IS NULL');
qb.orWhere('note.renoteChannelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
}

View File

@ -18,6 +18,7 @@ import { MetaService } from '@/core/MetaService.js';
import { MiLocalUser } from '@/models/User.js';
import { FanoutTimelineEndpointService } from '@/core/FanoutTimelineEndpointService.js';
import { ApiError } from '../../error.js';
import { ChannelMutingService } from "@/core/ChannelMutingService.js";
export const meta = {
tags: ['notes'],
@ -77,6 +78,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private fanoutTimelineEndpointService: FanoutTimelineEndpointService,
private queryService: QueryService,
private metaService: MetaService,
private channelMutingService: ChannelMutingService,
) {
super(meta, paramDef, async (ps, me) => {
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
@ -123,6 +125,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
: ['localTimeline'],
alwaysIncludeMyNotes: true,
excludePureRenotes: !ps.withRenotes,
excludeMutedChannels: true,
dbFallback: async (untilId, sinceId, limit) => await this.getFromDb({
untilId,
sinceId,
@ -159,9 +162,21 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.leftJoinAndSelect('renote.user', 'renoteUser');
this.queryService.generateVisibilityQuery(query, me);
if (me) this.queryService.generateMutedUserQuery(query, me);
if (me) this.queryService.generateBlockedUserQuery(query, me);
if (me) this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
if (me) {
this.queryService.generateMutedUserQuery(query, me);
this.queryService.generateBlockedUserQuery(query, me);
this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
const mutedChannelIds = await this.channelMutingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id));
if (mutedChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.renoteChannelId IS NULL')
.orWhere('note.renoteChannelId NOT IN (:...mutedChannelIds)', { mutedChannelIds });
}));
}
}
if (ps.withFiles) {
query.andWhere('note.fileIds != \'{}\'');

View File

@ -5,7 +5,7 @@
import { Brackets } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import type { NotesRepository, ChannelFollowingsRepository, ChannelMutingRepository } from '@/models/_.js';
import type { NotesRepository } from '@/models/_.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { QueryService } from '@/core/QueryService.js';
import ActiveUsersChart from '@/core/chart/charts/active-users.js';
@ -18,6 +18,7 @@ import { MiLocalUser } from '@/models/User.js';
import { MetaService } from '@/core/MetaService.js';
import { FanoutTimelineEndpointService } from '@/core/FanoutTimelineEndpointService.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import { ChannelFollowingService } from '@/core/ChannelFollowingService.js';
export const meta = {
tags: ['notes'],
@ -60,9 +61,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
@Inject(DI.channelFollowingsRepository)
private channelFollowingsRepository: ChannelFollowingsRepository,
private noteEntityService: NoteEntityService,
private activeUsersChart: ActiveUsersChart,
private idService: IdService,
@ -70,6 +68,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private fanoutTimelineEndpointService: FanoutTimelineEndpointService,
private userFollowingService: UserFollowingService,
private channelMutingService: ChannelMutingService,
private channelFollowingService: ChannelFollowingService,
private queryService: QueryService,
private metaService: MetaService,
) {
@ -144,12 +143,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private async getFromDb(ps: { untilId: string | null; sinceId: string | null; limit: number; includeMyRenotes: boolean; includeRenotedMyNotes: boolean; includeLocalRenotes: boolean; withFiles: boolean; withRenotes: boolean; }, me: MiLocalUser) {
const followees = await this.userFollowingService.getFollowees(me.id);
const followingChannels = await this.channelFollowingsRepository.find({
where: {
followerId: me.id,
},
});
const mutingChannelIds = await this.channelMutingService.list({ requestUserId: me.id }).then(x => x.map(x => x.id));
const mutingChannelIds = await this.channelMutingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id));
const followingChannelIds = await this.channelFollowingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id).filter(x => !mutingChannelIds.includes(x)));
//#region Construct query
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
@ -159,10 +159,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.leftJoinAndSelect('reply.user', 'replyUser')
.leftJoinAndSelect('renote.user', 'renoteUser');
if (followees.length > 0 && followingChannels.length > 0) {
if (followees.length > 0 && followingChannelIds.length > 0) {
// ユーザー・チャンネルともにフォローあり
const meOrFolloweeIds = [me.id, ...followees.map(f => f.followeeId)];
const followingChannelIds = followingChannels.map(x => x.followeeId);
query.andWhere(new Brackets(qb => {
qb
.where(new Brackets(qb2 => {
@ -179,12 +178,18 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
qb
.andWhere('note.channelId IS NULL')
.andWhere('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds });
if (mutingChannelIds.length > 0) {
qb.andWhere('note.renoteChannelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}
}));
} else if (followingChannels.length > 0) {
} else if (followingChannelIds.length > 0) {
// チャンネルフォローのみ(ユーザーフォローなし)
const followingChannelIds = followingChannels.map(x => x.followeeId);
query.andWhere(new Brackets(qb => {
qb
// renoteChannelIdは見る必要が無い
// ・HTLに流れてくるチャンネルフォローしているチャンネル
// ・HTLにフォロー外のチャンネルが流れるのは、フォローしているユーザがそのチャンネル投稿をリートした場合のみ
// つまり、ユーザフォローしてない前提のこのブロックでは見る必要が無い
.where('note.channelId IN (:...followingChannelIds)', { followingChannelIds })
.orWhere('note.userId = :meId', { meId: me.id });
}));
@ -197,28 +202,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}));
}
if (mutingChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb
// ミュートしてるチャンネルは含めない
.where(new Brackets(qb2 => {
qb2
.andWhere(new Brackets(qb3 => {
qb3
.andWhere('note.channelId IS NOT NULL')
.andWhere('note.channelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}))
.andWhere(new Brackets(qb3 => {
qb3
.andWhere('note.renoteChannelId IS NOT NULL')
.andWhere('note.renoteChannelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
}))
// チャンネルの投稿ではない
.orWhere('note.channelId IS NULL');
}));
}
query.andWhere(new Brackets(qb => {
qb
.where('note.replyId IS NULL') // 返信ではない

View File

@ -17,6 +17,7 @@ import { MiLocalUser } from '@/models/User.js';
import { MetaService } from '@/core/MetaService.js';
import { FanoutTimelineEndpointService } from '@/core/FanoutTimelineEndpointService.js';
import { ApiError } from '../../error.js';
import { ChannelMutingService } from "@/core/ChannelMutingService.js";
export const meta = {
tags: ['notes', 'lists'],
@ -85,6 +86,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private fanoutTimelineEndpointService: FanoutTimelineEndpointService,
private queryService: QueryService,
private metaService: MetaService,
private channelMutingService: ChannelMutingService,
) {
super(meta, paramDef, async (ps, me) => {
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
@ -128,6 +130,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
redisTimelines: ps.withFiles ? [`userListTimelineWithFiles:${list.id}`] : [`userListTimeline:${list.id}`],
alwaysIncludeMyNotes: true,
excludePureRenotes: !ps.withRenotes,
excludeMutedChannels: true,
dbFallback: async (untilId, sinceId, limit) => await this.getFromDb(list, {
untilId,
sinceId,
@ -191,6 +194,17 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateBlockedUserQuery(query, me);
this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
// -- ミュートされたチャンネルのリノート対策
const mutedChannelIds = await this.channelMutingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id));
if (mutedChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.renoteChannelId IS NULL')
.orWhere('note.renoteChannelId NOT IN (:...mutedChannelIds)', { mutedChannelIds });
}));
}
if (ps.includeMyRenotes === false) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.userId != :meId', { meId: me.id });

View File

@ -5,6 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import { Brackets } from 'typeorm';
import { Endpoint } from '@/server/api/endpoint-base.js';
import type { NotesRepository, RolesRepository } from '@/models/_.js';
import { QueryService } from '@/core/QueryService.js';
@ -12,6 +13,7 @@ import { DI } from '@/di-symbols.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { IdService } from '@/core/IdService.js';
import { FanoutTimelineService } from '@/core/FanoutTimelineService.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import { ApiError } from '../../error.js';
export const meta = {
@ -68,6 +70,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private noteEntityService: NoteEntityService,
private queryService: QueryService,
private fanoutTimelineService: FanoutTimelineService,
private channelMutingService: ChannelMutingService,
) {
super(meta, paramDef, async (ps, me) => {
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
@ -101,6 +104,21 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.leftJoinAndSelect('reply.user', 'replyUser')
.leftJoinAndSelect('renote.user', 'renoteUser');
// -- ミュートされたチャンネル対策
const mutingChannelIds = await this.channelMutingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id));
if (mutingChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.channelId IS NULL');
qb.orWhere('note.channelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
query.andWhere(new Brackets(qb => {
qb.orWhere('note.renoteChannelId IS NULL');
qb.orWhere('note.renoteChannelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
}
this.queryService.generateVisibilityQuery(query, me);
this.queryService.generateMutedUserQuery(query, me);
this.queryService.generateBlockedUserQuery(query, me);

View File

@ -17,6 +17,7 @@ import { MiLocalUser } from '@/models/User.js';
import { FanoutTimelineEndpointService } from '@/core/FanoutTimelineEndpointService.js';
import { FanoutTimelineName } from '@/core/FanoutTimelineService.js';
import { ApiError } from '@/server/api/error.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
export const meta = {
tags: ['users', 'notes'],
@ -69,13 +70,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
constructor(
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
private noteEntityService: NoteEntityService,
private queryService: QueryService,
private cacheService: CacheService,
private idService: IdService,
private fanoutTimelineEndpointService: FanoutTimelineEndpointService,
private metaService: MetaService,
private channelMutingService: ChannelMutingService,
) {
super(meta, paramDef, async (ps, me) => {
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
@ -127,6 +128,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
excludeReplies: ps.withChannelNotes && !ps.withReplies, // userTimelineWithChannel may include replies
excludeNoFiles: ps.withChannelNotes && ps.withFiles, // userTimelineWithChannel may include notes without files
excludePureRenotes: !ps.withRenotes,
excludeMutedChannels: true,
noteFilter: note => {
if (note.channel?.isSensitive && !isSelf) return false;
if (note.visibility === 'specified' && (!me || (me.id !== note.userId && !note.visibleUserIds.some(v => v === me.id)))) return false;
@ -158,6 +160,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
withFiles: boolean,
withRenotes: boolean,
}, me: MiLocalUser | null) {
const mutingChannelIds = me
? await this.channelMutingService
.list({ requestUserId: me.id }, { idOnly: true })
.then(x => x.map(x => x.id))
: [];
const isSelf = me && (me.id === ps.userId);
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
@ -170,14 +177,30 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
.leftJoinAndSelect('renote.user', 'renoteUser');
if (ps.withChannelNotes) {
if (!isSelf) query.andWhere(new Brackets(qb => {
qb.orWhere('note.channelId IS NULL');
qb.orWhere('channel.isSensitive = false');
query.andWhere(new Brackets(qb => {
if (mutingChannelIds.length > 0) {
qb.andWhere('note.channelId NOT IN (:...mutingChannelIds)', { mutingChannelIds: mutingChannelIds });
}
if (!isSelf) {
qb.andWhere(new Brackets(qb2 => {
qb2.orWhere('note.channelId IS NULL');
qb2.orWhere('channel.isSensitive = false');
}));
}
}));
} else {
query.andWhere('note.channelId IS NULL');
}
// -- ミュートされたチャンネルのリノート対策
if (mutingChannelIds.length > 0) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.renoteChannelId IS NULL');
qb.orWhere('note.renoteChannelId NOT IN (:...mutingChannelIds)', { mutingChannelIds });
}));
}
this.queryService.generateVisibilityQuery(query, me);
if (me) {
this.queryService.generateMutedUserQuery(query, me, { id: ps.userId });

View File

@ -69,6 +69,9 @@ describe('アンテナ', () => {
let userMutingAlice: User;
let userMutedByAlice: User;
let testChannel: misskey.entities.Channel;
let testMutedChannel: misskey.entities.Channel;
beforeAll(async () => {
root = await signup({ username: 'root' });
alice = await signup({ username: 'alice' });
@ -120,6 +123,10 @@ describe('アンテナ', () => {
userMutedByAlice = await signup({ username: 'userMutedByAlice' });
await post(userMutedByAlice, { text: 'test' });
await api('mute/create', { userId: userMutedByAlice.id }, alice);
testChannel = (await api('channels/create', { name: 'test' }, root)).body;
testMutedChannel = (await api('channels/create', { name: 'test-muted' }, root)).body;
await api('channels/mute/create', { channelId: testMutedChannel.id }, alice);
}, 1000 * 60 * 10);
beforeEach(async () => {
@ -570,6 +577,20 @@ describe('アンテナ', () => {
{ note: (): Promise<Note> => post(bob, { text: `${keyword}` }), included: true },
],
},
{
label: 'チャンネルノートも含む',
parameters: () => ({ src: 'all' }),
posts: [
{ note: (): Promise<Note> => post(bob, { text: `test ${keyword}`, channelId: testChannel.id }), included: true },
],
},
{
label: 'ミュートしてるチャンネルは含まない',
parameters: () => ({ src: 'all' }),
posts: [
{ note: (): Promise<Note> => post(bob, { text: `test ${keyword}`, channelId: testMutedChannel.id }) },
],
},
])('が取得できること($label', async ({ parameters, posts }) => {
const antenna = await successfulApiCall({
endpoint: 'antennas/create',

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,9 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
module.exports = async () => {
// DBはUTCっぽいので、テスト側も合わせておく
process.env.TZ = 'UTC';
};

View File

@ -0,0 +1,235 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
/* eslint-disable */
import { afterEach, beforeEach, describe, expect } from '@jest/globals';
import { Test, TestingModule } from '@nestjs/testing';
import { GlobalModule } from '@/GlobalModule.js';
import { CoreModule } from '@/core/CoreModule.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { IdService } from '@/core/IdService.js';
import {
type ChannelFollowingsRepository,
ChannelsRepository,
DriveFilesRepository,
MiChannel,
MiChannelFollowing,
MiDriveFile,
MiUser,
UserProfilesRepository,
UsersRepository,
} from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { ChannelFollowingService } from "@/core/ChannelFollowingService.js";
import { MiLocalUser } from "@/models/User.js";
describe('ChannelFollowingService', () => {
let app: TestingModule;
let service: ChannelFollowingService;
let channelsRepository: ChannelsRepository;
let channelFollowingsRepository: ChannelFollowingsRepository;
let usersRepository: UsersRepository;
let userProfilesRepository: UserProfilesRepository;
let driveFilesRepository: DriveFilesRepository;
let idService: IdService;
let alice: MiLocalUser;
let bob: MiLocalUser;
let channel1: MiChannel;
let channel2: MiChannel;
let channel3: MiChannel;
let driveFile1: MiDriveFile;
let driveFile2: MiDriveFile;
async function createUser(data: Partial<MiUser> = {}) {
const user = await usersRepository
.insert({
id: idService.gen(),
username: 'username',
usernameLower: 'username',
...data,
})
.then(x => usersRepository.findOneByOrFail(x.identifiers[0]));
await userProfilesRepository.insert({
userId: user.id,
});
return user;
}
async function createChannel(data: Partial<MiChannel> = {}) {
return await channelsRepository
.insert({
id: idService.gen(),
...data,
})
.then(x => channelsRepository.findOneByOrFail(x.identifiers[0]));
}
async function createChannelFollowing(data: Partial<MiChannelFollowing> = {}) {
return await channelFollowingsRepository
.insert({
id: idService.gen(),
...data,
})
.then(x => channelFollowingsRepository.findOneByOrFail(x.identifiers[0]));
}
async function fetchChannelFollowing() {
return await channelFollowingsRepository.findBy({});
}
async function createDriveFile(data: Partial<MiDriveFile> = {}) {
return await driveFilesRepository
.insert({
id: idService.gen(),
md5: 'md5',
name: 'name',
size: 0,
type: 'type',
storedInternal: false,
url: 'url',
...data,
})
.then(x => driveFilesRepository.findOneByOrFail(x.identifiers[0]));
}
beforeAll(async () => {
app = await Test.createTestingModule({
imports: [
GlobalModule,
CoreModule,
],
providers: [
GlobalEventService,
IdService,
ChannelFollowingService,
],
}).compile();
app.enableShutdownHooks();
service = app.get<ChannelFollowingService>(ChannelFollowingService);
idService = app.get<IdService>(IdService);
channelsRepository = app.get<ChannelsRepository>(DI.channelsRepository);
channelFollowingsRepository = app.get<ChannelFollowingsRepository>(DI.channelFollowingsRepository);
usersRepository = app.get<UsersRepository>(DI.usersRepository);
userProfilesRepository = app.get<UserProfilesRepository>(DI.userProfilesRepository);
driveFilesRepository = app.get<DriveFilesRepository>(DI.driveFilesRepository);
});
afterAll(async () => {
await app.close();
});
beforeEach(async () => {
alice = { ...await createUser({ username: 'alice' }), host: null, uri: null };
bob = { ...await createUser({ username: 'bob' }), host: null, uri: null };
driveFile1 = await createDriveFile();
driveFile2 = await createDriveFile();
channel1 = await createChannel({ name: 'channel1', userId: alice.id, bannerId: driveFile1.id });
channel2 = await createChannel({ name: 'channel2', userId: alice.id, bannerId: driveFile2.id });
channel3 = await createChannel({ name: 'channel3', userId: alice.id, bannerId: driveFile2.id });
});
afterEach(async () => {
await channelFollowingsRepository.delete({});
await channelsRepository.delete({});
await userProfilesRepository.delete({});
await usersRepository.delete({});
});
describe('list', () => {
test('default', async () => {
await createChannelFollowing({ followerId: alice.id, followeeId: channel1.id });
await createChannelFollowing({ followerId: alice.id, followeeId: channel2.id });
await createChannelFollowing({ followerId: bob.id, followeeId: channel3.id });
const followings = await service.list({ requestUserId: alice.id });
expect(followings).toHaveLength(2);
expect(followings[0].id).toBe(channel1.id);
expect(followings[0].userId).toBe(alice.id);
expect(followings[0].user).toBeFalsy();
expect(followings[0].bannerId).toBe(driveFile1.id);
expect(followings[0].banner).toBeFalsy();
expect(followings[1].id).toBe(channel2.id);
expect(followings[1].userId).toBe(alice.id);
expect(followings[1].user).toBeFalsy();
expect(followings[1].bannerId).toBe(driveFile2.id);
expect(followings[1].banner).toBeFalsy();
});
test('idOnly', async () => {
await createChannelFollowing({ followerId: alice.id, followeeId: channel1.id });
await createChannelFollowing({ followerId: alice.id, followeeId: channel2.id });
await createChannelFollowing({ followerId: bob.id, followeeId: channel3.id });
const followings = await service.list({ requestUserId: alice.id }, { idOnly: true });
expect(followings).toHaveLength(2);
expect(followings[0].id).toBe(channel1.id);
expect(followings[1].id).toBe(channel2.id);
});
test('joinUser', async () => {
await createChannelFollowing({ followerId: alice.id, followeeId: channel1.id });
await createChannelFollowing({ followerId: alice.id, followeeId: channel2.id });
await createChannelFollowing({ followerId: bob.id, followeeId: channel3.id });
const followings = await service.list({ requestUserId: alice.id }, { joinUser: true });
expect(followings).toHaveLength(2);
expect(followings[0].id).toBe(channel1.id);
expect(followings[0].user).toEqual(alice);
expect(followings[0].banner).toBeFalsy();
expect(followings[1].id).toBe(channel2.id);
expect(followings[1].user).toEqual(alice);
expect(followings[1].banner).toBeFalsy();
});
test('joinBannerFile', async () => {
await createChannelFollowing({ followerId: alice.id, followeeId: channel1.id });
await createChannelFollowing({ followerId: alice.id, followeeId: channel2.id });
await createChannelFollowing({ followerId: bob.id, followeeId: channel3.id });
const followings = await service.list({ requestUserId: alice.id }, { joinBannerFile: true });
expect(followings).toHaveLength(2);
expect(followings[0].id).toBe(channel1.id);
expect(followings[0].user).toBeFalsy();
expect(followings[0].banner).toEqual(driveFile1);
expect(followings[1].id).toBe(channel2.id);
expect(followings[1].user).toBeFalsy();
expect(followings[1].banner).toEqual(driveFile2);
});
});
describe('follow', () => {
test('default', async () => {
await service.follow(alice, channel1);
const followings = await fetchChannelFollowing();
expect(followings).toHaveLength(1);
expect(followings[0].followeeId).toBe(channel1.id);
expect(followings[0].followerId).toBe(alice.id);
});
});
describe('unfollow', () => {
test('default', async () => {
await createChannelFollowing({ followerId: alice.id, followeeId: channel1.id });
await service.unfollow(alice, channel1);
const followings = await fetchChannelFollowing();
expect(followings).toHaveLength(0);
});
});
});

View File

@ -0,0 +1,334 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
/* eslint-disable */
import { afterEach, beforeEach, describe, expect } from '@jest/globals';
import { Test, TestingModule } from '@nestjs/testing';
import { GlobalModule } from '@/GlobalModule.js';
import { CoreModule } from '@/core/CoreModule.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { IdService } from '@/core/IdService.js';
import { ChannelMutingService } from '@/core/ChannelMutingService.js';
import {
ChannelMutingRepository,
ChannelsRepository, DriveFilesRepository,
MiChannel,
MiChannelMuting, MiDriveFile,
MiUser,
UserProfilesRepository,
UsersRepository,
} from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { sleep } from "../utils.js";
describe('ChannelMutingService', () => {
let app: TestingModule;
let service: ChannelMutingService;
let channelsRepository: ChannelsRepository;
let channelMutingRepository: ChannelMutingRepository;
let usersRepository: UsersRepository;
let userProfilesRepository: UserProfilesRepository;
let driveFilesRepository: DriveFilesRepository;
let idService: IdService;
let alice: MiUser;
let bob: MiUser;
let channel1: MiChannel;
let channel2: MiChannel;
let channel3: MiChannel;
let driveFile1: MiDriveFile;
let driveFile2: MiDriveFile;
async function createUser(data: Partial<MiUser> = {}) {
const user = await usersRepository
.insert({
id: idService.gen(),
username: 'username',
usernameLower: 'username',
...data,
})
.then(x => usersRepository.findOneByOrFail(x.identifiers[0]));
await userProfilesRepository.insert({
userId: user.id,
});
return user;
}
async function createChannel(data: Partial<MiChannel> = {}) {
return await channelsRepository
.insert({
id: idService.gen(),
...data,
})
.then(x => channelsRepository.findOneByOrFail(x.identifiers[0]));
}
async function createChannelMuting(data: Partial<MiChannelMuting> = {}) {
return await channelMutingRepository
.insert({
id: idService.gen(),
...data,
})
.then(x => channelMutingRepository.findOneByOrFail(x.identifiers[0]));
}
async function fetchChannelMuting() {
return await channelMutingRepository.findBy({});
}
async function createDriveFile(data: Partial<MiDriveFile> = {}) {
return await driveFilesRepository
.insert({
id: idService.gen(),
md5: 'md5',
name: 'name',
size: 0,
type: 'type',
storedInternal: false,
url: 'url',
...data,
})
.then(x => driveFilesRepository.findOneByOrFail(x.identifiers[0]));
}
beforeAll(async () => {
app = await Test.createTestingModule({
imports: [
GlobalModule,
CoreModule,
],
providers: [
GlobalEventService,
IdService,
ChannelMutingService,
],
}).compile();
app.enableShutdownHooks();
service = app.get<ChannelMutingService>(ChannelMutingService);
idService = app.get<IdService>(IdService);
channelsRepository = app.get<ChannelsRepository>(DI.channelsRepository);
channelMutingRepository = app.get<ChannelMutingRepository>(DI.channelMutingRepository);
usersRepository = app.get<UsersRepository>(DI.usersRepository);
userProfilesRepository = app.get<UserProfilesRepository>(DI.userProfilesRepository);
driveFilesRepository = app.get<DriveFilesRepository>(DI.driveFilesRepository);
});
afterAll(async () => {
await app.close();
});
beforeEach(async () => {
alice = await createUser({ username: 'alice' });
bob = await createUser({ username: 'bob' });
driveFile1 = await createDriveFile();
driveFile2 = await createDriveFile();
channel1 = await createChannel({ name: 'channel1', userId: alice.id, bannerId: driveFile1.id });
channel2 = await createChannel({ name: 'channel2', userId: alice.id, bannerId: driveFile2.id });
channel3 = await createChannel({ name: 'channel3', userId: alice.id, bannerId: driveFile2.id });
});
afterEach(async () => {
await channelMutingRepository.delete({});
await channelsRepository.delete({});
await userProfilesRepository.delete({});
await usersRepository.delete({});
});
describe('list', () => {
test('default', async () => {
await createChannelMuting({ userId: alice.id, channelId: channel1.id });
await createChannelMuting({ userId: alice.id, channelId: channel2.id });
await createChannelMuting({ userId: bob.id, channelId: channel3.id });
const mutings = await service.list({ requestUserId: alice.id });
expect(mutings).toHaveLength(2);
expect(mutings[0].id).toBe(channel1.id);
expect(mutings[0].userId).toBe(alice.id);
expect(mutings[0].user).toBeFalsy();
expect(mutings[0].bannerId).toBe(driveFile1.id);
expect(mutings[0].banner).toBeFalsy();
expect(mutings[1].id).toBe(channel2.id);
expect(mutings[1].userId).toBe(alice.id);
expect(mutings[1].user).toBeFalsy();
expect(mutings[1].bannerId).toBe(driveFile2.id);
expect(mutings[1].banner).toBeFalsy();
});
test('withoutExpires', async () => {
const now = new Date();
const past = new Date(now);
const future = new Date(now);
past.setMinutes(past.getMinutes() - 1);
future.setMinutes(future.getMinutes() + 1);
await createChannelMuting({ userId: alice.id, channelId: channel1.id, expiresAt: past });
await createChannelMuting({ userId: alice.id, channelId: channel2.id, expiresAt: null });
await createChannelMuting({ userId: alice.id, channelId: channel3.id, expiresAt: future });
const mutings = await service.list({ requestUserId: alice.id });
expect(mutings).toHaveLength(2);
expect(mutings[0].id).toBe(channel2.id);
expect(mutings[1].id).toBe(channel3.id);
});
test('idOnly', async () => {
await createChannelMuting({ userId: alice.id, channelId: channel1.id });
await createChannelMuting({ userId: alice.id, channelId: channel2.id });
await createChannelMuting({ userId: bob.id, channelId: channel3.id });
const mutings = await service.list({ requestUserId: alice.id }, { idOnly: true });
expect(mutings).toHaveLength(2);
expect(mutings[0].id).toBe(channel1.id);
expect(mutings[1].id).toBe(channel2.id);
});
test('withoutExpires-idOnly', async () => {
const now = new Date();
const past = new Date(now);
const future = new Date(now);
past.setMinutes(past.getMinutes() - 1);
future.setMinutes(future.getMinutes() + 1);
await createChannelMuting({ userId: alice.id, channelId: channel1.id, expiresAt: past });
await createChannelMuting({ userId: alice.id, channelId: channel2.id, expiresAt: null });
await createChannelMuting({ userId: alice.id, channelId: channel3.id, expiresAt: future });
const mutings = await service.list({ requestUserId: alice.id }, { idOnly: true });
expect(mutings).toHaveLength(2);
expect(mutings[0].id).toBe(channel2.id);
expect(mutings[1].id).toBe(channel3.id);
});
test('joinUser', async () => {
await createChannelMuting({ userId: alice.id, channelId: channel1.id });
await createChannelMuting({ userId: alice.id, channelId: channel2.id });
await createChannelMuting({ userId: bob.id, channelId: channel3.id });
const mutings = await service.list({ requestUserId: alice.id }, { joinUser: true });
expect(mutings).toHaveLength(2);
expect(mutings[0].id).toBe(channel1.id);
expect(mutings[0].user).toEqual(alice);
expect(mutings[0].banner).toBeFalsy();
expect(mutings[1].id).toBe(channel2.id);
expect(mutings[1].user).toEqual(alice);
expect(mutings[1].banner).toBeFalsy();
});
test('joinBannerFile', async () => {
await createChannelMuting({ userId: alice.id, channelId: channel1.id });
await createChannelMuting({ userId: alice.id, channelId: channel2.id });
await createChannelMuting({ userId: bob.id, channelId: channel3.id });
const mutings = await service.list({ requestUserId: alice.id }, { joinBannerFile: true });
expect(mutings).toHaveLength(2);
expect(mutings[0].id).toBe(channel1.id);
expect(mutings[0].user).toBeFalsy();
expect(mutings[0].banner).toEqual(driveFile1);
expect(mutings[1].id).toBe(channel2.id);
expect(mutings[1].user).toBeFalsy();
expect(mutings[1].banner).toEqual(driveFile2);
});
});
describe('findExpiredMutings', () => {
test('default', async () => {
const now = new Date();
const future = new Date(now);
const past = new Date(now);
future.setMinutes(now.getMinutes() + 1);
past.setMinutes(now.getMinutes() - 1);
await createChannelMuting({ userId: alice.id, channelId: channel1.id, expiresAt: past });
await createChannelMuting({ userId: alice.id, channelId: channel2.id, expiresAt: future });
await createChannelMuting({ userId: bob.id, channelId: channel3.id, expiresAt: past });
const mutings = await service.findExpiredMutings();
expect(mutings).toHaveLength(2);
expect(mutings[0].channelId).toBe(channel1.id);
expect(mutings[1].channelId).toBe(channel3.id);
});
});
describe('isMuted', () => {
test('isMuted: true', async () => {
// キャッシュを読むのでServiceの機能を使って登録し、キャッシュを作成する
await service.mute({ requestUserId: alice.id, targetChannelId: channel1.id });
await service.mute({ requestUserId: alice.id, targetChannelId: channel2.id });
await sleep(500);
const result = await service.isMuted({ requestUserId: alice.id, targetChannelId: channel1.id });
expect(result).toBe(true);
});
test('isMuted: false', async () => {
await service.mute({ requestUserId: alice.id, targetChannelId: channel2.id });
await sleep(500);
const result = await service.isMuted({ requestUserId: alice.id, targetChannelId: channel1.id });
expect(result).toBe(false);
});
});
describe('mute', () => {
test('default', async () => {
await service.mute({ requestUserId: alice.id, targetChannelId: channel1.id });
const muting = await fetchChannelMuting();
expect(muting).toHaveLength(1);
expect(muting[0].channelId).toBe(channel1.id);
});
});
describe('unmute', () => {
test('default', async () => {
await createChannelMuting({ userId: alice.id, channelId: channel1.id });
let muting = await fetchChannelMuting();
expect(muting).toHaveLength(1);
expect(muting[0].channelId).toBe(channel1.id);
await service.unmute({ requestUserId: alice.id, targetChannelId: channel1.id });
muting = await fetchChannelMuting();
expect(muting).toHaveLength(0);
});
});
describe('eraseExpiredMutings', () => {
test('default', async () => {
const now = new Date();
const future = new Date(now);
const past = new Date(now);
future.setMinutes(now.getMinutes() + 1);
past.setMinutes(now.getMinutes() - 1);
await createChannelMuting({ userId: alice.id, channelId: channel1.id, expiresAt: past });
await createChannelMuting({ userId: alice.id, channelId: channel2.id, expiresAt: future });
await createChannelMuting({ userId: bob.id, channelId: channel3.id, expiresAt: past });
await service.eraseExpiredMutings();
const mutings = await fetchChannelMuting();
expect(mutings).toHaveLength(1);
expect(mutings[0].channelId).toBe(channel2.id);
});
});
});