feat: チャンネルミュートの実装 (#14105)

* add channel_muting table and entities

* add channel_muting services

* タイムライン取得処理への組み込み

* misskey-jsの型とインターフェース生成

* Channelスキーマにミュート情報を追加

* フロントエンドの実装

* 条件が逆だったのを修正

* 期限切れミュートを掃除する機能を実装

* TLの抽出条件調節

* 名前の変更と変更不要の差分をロールバック

* 修正漏れ

* isChannelRelatedの条件に誤りがあった

* [wip] テスト追加

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

* fix test

* fix CHANGELOG.md

* 通常はFTTにしておく

* 実装忘れ対応

* fix merge

* fix merge

* add channel tl test

* fix CHANGELOG.md

* remove unused import

* fix lint

* fix test

* fix favorite -> favorited

* exclude -> include

* fix CHANGELOG.md

* fix CHANGELOG.md

* maintenance

* fix CHANGELOG.md

* fix

* fix ci

* regenerate

* fix

* Revert "fix"

This reverts commit 699d50c6ec798777d8e9667cb5d45a26b06bfc93.

* fixed

---------

Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
This commit is contained in:
おさむのひと
2025-11-07 08:39:21 +09:00
committed by GitHub
parent e74ab35de3
commit 729abbef62
53 changed files with 3564 additions and 151 deletions

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 () => {
@@ -605,6 +612,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,10 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/
module.exports = async () => {
// DBはUTCっぽいので、テスト側も合わせておく
process.env.TZ = 'UTC';
process.env.NODE_ENV = 'test';
};

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.deleteAll();
await channelsRepository.deleteAll();
await userProfilesRepository.deleteAll();
await usersRepository.deleteAll();
});
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,336 @@
/*
* 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 { setTimeout } from 'node:timers/promises';
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.deleteAll();
await channelsRepository.deleteAll();
await userProfilesRepository.deleteAll();
await usersRepository.deleteAll();
});
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 setTimeout(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 setTimeout(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);
});
});
});

View File

@@ -61,6 +61,7 @@ describe('NoteCreateService', () => {
replyUserHost: null,
renoteUserId: null,
renoteUserHost: null,
renoteChannelId: null,
};
const poll: IPoll = {

View File

@@ -44,6 +44,7 @@ const base: MiNote = {
replyUserHost: null,
renoteUserId: null,
renoteUserHost: null,
renoteChannelId: null,
};
describe('misc:is-renote', () => {