Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(backend): スレッドミュートにおいて、RN/引用/リアクションの通知もミュートするように #15271

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

### Server
- Enhance: 成り済まし対策として、ActivityPub照会された時にリモートのリダイレクトを拒否できるように (config.disallowExternalApRedirect)
- Enhance: スレッドミュートにおいて、リノート、引用、リアクションの通知もミュートするように
- なお、以下のケースでは引き続き通知がミュートされません。(ミュートを行っているユーザーをAとします)
- ミュート対象ノートを、当該スレッドの外にあるAへの返信/メンション付きノートにおいて引用する
- Fix: `following/invalidate`でフォロワーを解除しようとしているユーザーの情報を返すように
- Fix: オブジェクトストレージの設定でPrefixを設定していなかった場合nullまたは空文字になる問題を修正
- Fix: pgroongaでの検索時にはじめのキーワードのみが検索に使用される問題を修正
Expand Down
24 changes: 18 additions & 6 deletions packages/backend/src/core/NoteCreateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ export class NoteCreateService implements OnApplicationShutdown {
await this.createMentionedEvents(mentionedUsers, note, nm);

// If has in reply to note
let isOnThreadMutedTree = false;
if (data.reply) {
// 通知
if (data.reply.userHost === null) {
Expand All @@ -630,6 +631,7 @@ export class NoteCreateService implements OnApplicationShutdown {
threadId: data.reply.threadId ?? data.reply.id,
},
});
isOnThreadMutedTree = isThreadMuted;

if (!isThreadMuted) {
nm.push(data.reply.userId, 'reply');
Expand All @@ -645,13 +647,23 @@ export class NoteCreateService implements OnApplicationShutdown {

// Notify
if (data.renote.userHost === null) {
nm.push(data.renote.userId, type);
}
const isThreadMuted = await this.noteThreadMutingsRepository.exists({
where: {
userId: data.renote.userId,
threadId: data.renote.threadId ?? data.renote.id,
},
});

// If the quoted note is not thread muted but the quoting note is on thread muted tree, need to mute it.
if (!isThreadMuted && !isOnThreadMutedTree) {
nm.push(data.renote.userId, type);

// Publish event
if ((user.id !== data.renote.userId) && data.renote.userHost === null) {
this.globalEventService.publishMainStream(data.renote.userId, 'renote', noteObj);
this.webhookService.enqueueUserWebhook(data.renote.userId, 'renote', { note: noteObj });
// Publish event
if (user.id !== data.renote.userId) {
this.globalEventService.publishMainStream(data.renote.userId, 'renote', noteObj);
this.webhookService.enqueueUserWebhook(data.renote.userId, 'renote', { note: noteObj });
}
}
}
}

Expand Down
22 changes: 17 additions & 5 deletions packages/backend/src/core/ReactionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import { Inject, Injectable } from '@nestjs/common';
import { DI } from '@/di-symbols.js';
import type { EmojisRepository, NoteReactionsRepository, UsersRepository, NotesRepository, MiMeta } from '@/models/_.js';
import type { EmojisRepository, NoteReactionsRepository, UsersRepository, NotesRepository, MiMeta, NoteThreadMutingsRepository } from '@/models/_.js';
import { IdentifiableError } from '@/misc/identifiable-error.js';
import type { MiRemoteUser, MiUser } from '@/models/User.js';
import type { MiNote } from '@/models/Note.js';
Expand Down Expand Up @@ -82,6 +82,9 @@ export class ReactionService {
@Inject(DI.noteReactionsRepository)
private noteReactionsRepository: NoteReactionsRepository,

@Inject(DI.noteThreadMutingsRepository)
private noteThreadMutingsRepository: NoteThreadMutingsRepository,

@Inject(DI.emojisRepository)
private emojisRepository: EmojisRepository,

Expand Down Expand Up @@ -256,10 +259,19 @@ export class ReactionService {

// リアクションされたユーザーがローカルユーザーなら通知を作成
if (note.userHost === null) {
this.notificationService.createNotification(note.userId, 'reaction', {
noteId: note.id,
reaction: reaction,
}, user.id);
const isThreadMuted = await this.noteThreadMutingsRepository.exists({
where: {
userId: note.userId,
threadId: note.threadId ?? note.id,
},
});

if (!isThreadMuted) {
this.notificationService.createNotification(note.userId, 'reaction', {
noteId: note.id,
reaction: reaction,
}, user.id);
}
}

//#region 配信
Expand Down
75 changes: 64 additions & 11 deletions packages/backend/test/e2e/thread-mute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
process.env.NODE_ENV = 'test';

import * as assert from 'assert';
import { api, connectStream, post, signup } from '../utils.js';
import { setTimeout } from 'node:timers/promises';
import { api, connectStream, post, signup, react } from '../utils.js';
import type * as misskey from 'misskey-js';

function waitForPushToNotification() {
return setTimeout(500);
}

describe('Note thread mute', () => {
let alice: misskey.entities.SignupResponse;
let bob: misskey.entities.SignupResponse;
Expand All @@ -29,6 +34,8 @@ describe('Note thread mute', () => {
const carolReply = await post(carol, { replyId: bobNote.id, text: '@bob @alice child note' });
const carolReplyWithoutMention = await post(carol, { replyId: aliceReply.id, text: 'child note' });

await waitForPushToNotification();

const res = await api('notes/mentions', {}, alice);

assert.strictEqual(res.status, 200);
Expand All @@ -48,13 +55,15 @@ describe('Note thread mute', () => {

const carolReply = await post(carol, { replyId: bobNote.id, text: '@bob @alice child note' });

await waitForPushToNotification();

const res = await api('i', {}, alice);

assert.strictEqual(res.status, 200);
assert.strictEqual(res.body.hasUnreadMentions, false);
});

test('ミュートしているスレッドからメンションされても、ストリームに unreadMention イベントが流れてこない', () => new Promise<void>(async done => {
test('ミュートしているスレッドからメンションされても、ストリームに unreadMention イベントが流れてこない', async () => {
// 状態リセット
await api('i/read-all-unread-notes', {}, alice);

Expand All @@ -66,36 +75,80 @@ describe('Note thread mute', () => {

const ws = await connectStream(alice, 'main', async ({ type, body }) => {
if (type === 'unreadMention') {
if (body === bobNote.id) return;
fired = true;
if (body === bobNote.id) fired = true;
}
});

const carolReply = await post(carol, { replyId: bobNote.id, text: '@bob @alice child note' });

setTimeout(() => {
assert.strictEqual(fired, false);
ws.close();
done();
}, 5000);
}));
await setTimeout(5000);

assert.strictEqual(fired, false);
ws.close();
});

test('i/notifications にミュートしているスレッドの通知が含まれない', async () => {
test('i/notifications にミュートしているスレッドの通知(メンション, リプライ, リノート, 引用リノート, リアクション)が含まれない', async () => {
const bobNote = await post(bob, { text: '@alice @carol root note' });
const aliceReply = await post(alice, { replyId: bobNote.id, text: '@bob @carol child note' });

await api('notes/thread-muting/create', { noteId: bobNote.id }, alice);

const carolReply = await post(carol, { replyId: bobNote.id, text: '@bob @alice child note' });
const carolReplyWithoutMention = await post(carol, { replyId: aliceReply.id, text: 'child note' });
const carolRenote = await post(carol, { renoteId: aliceReply.id });
const carolQuote = await post(carol, { renoteId: aliceReply.id, text: 'quote note' });
await react(carol, aliceReply, 'like'); // react method returns nothing.

await waitForPushToNotification();

const res = await api('i/notifications', {}, alice);

assert.strictEqual(res.status, 200);
assert.strictEqual(Array.isArray(res.body), true);
assert.strictEqual(res.body.some(notification => 'note' in notification && notification.note.id === carolReply.id), false);
assert.strictEqual(res.body.some(notification => 'note' in notification && notification.note.id === carolReplyWithoutMention.id), false);
assert.strictEqual(res.body.some(notification => 'note' in notification && notification.note.id === carolRenote.id), false);
assert.strictEqual(res.body.some(notification => 'note' in notification && notification.note.id === carolQuote.id), false);
assert.strictEqual(res.body.some(notification => 'userId' in notification && notification.userId === carol.id), false);

// NOTE: bobの投稿はスレッドミュート前に行われたため通知に含まれていてもよい
});

test('ミュートしているスレッドへのリプライで、ミュートしていないスレッドのノートが引用されても i/notifications に通知が含まれない', async () => {
const bobNote = await post(bob, { text: '@alice @carol root note' });
const aliceReply = await post(alice, { replyId: bobNote.id, text: '@bob @carol child note' });
const aliceNote = await post(alice, { text: 'another root note' });

await api('notes/thread-muting/create', { noteId: bobNote.id }, alice);

const carolReplyWithQuotingAnother = await post(carol, { replyId: aliceReply.id, renoteId: aliceNote.id, text: 'child note with quoting another note' });

await waitForPushToNotification();

const res = await api('i/notifications', {}, alice);

assert.strictEqual(res.status, 200);
assert.strictEqual(Array.isArray(res.body), true);
assert.strictEqual(res.body.some(notification => 'note' in notification && notification.note.id === carolReplyWithQuotingAnother.id), false);
});

test('ミュートしていないスレッドでのメンション付きノートまたはリプライで、ミュートしているスレッドのノートが引用された場合は i/notifications に通知が含まれる', async () => {
const bobNote = await post(bob, { text: '@alice @carol root note' });
const aliceReply = await post(alice, { replyId: bobNote.id, text: '@bob @carol child note' });
const aliceNote = await post(alice, { text: 'another root note' });

await api('notes/thread-muting/create', { noteId: bobNote.id }, alice);

const carolMentionWithQuotingMuted = await post(carol, { renoteId: aliceReply.id, text: '@alice another root note with quoting muted note' });
const carolReplyWithQuotingMuted = await post(carol, { replyId: aliceNote.id, renoteId: aliceReply.id, text: 'another child note with quoting muted note' });

await waitForPushToNotification();

const res = await api('i/notifications', {}, alice);

assert.strictEqual(res.status, 200);
assert.strictEqual(Array.isArray(res.body), true);
assert.strictEqual(res.body.some(notification => 'note' in notification && notification.note.id === carolMentionWithQuotingMuted.id), true);
assert.strictEqual(res.body.some(notification => 'note' in notification && notification.note.id === carolReplyWithQuotingMuted.id), true);
});
});
Loading