Skip to content

Commit

Permalink
Serviceでチェックするように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
samunohito committed Feb 9, 2024
1 parent 6244231 commit 1d3962f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 35 deletions.
8 changes: 8 additions & 0 deletions packages/backend/src/core/NoteCreateService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ type Option = {
export class NoteCreateService implements OnApplicationShutdown {
#shutdownController = new AbortController();

public static ContainsProhibitedWordsError = class extends Error {};

constructor(
@Inject(DI.config)
private config: Config,
Expand Down Expand Up @@ -261,6 +263,12 @@ export class NoteCreateService implements OnApplicationShutdown {
}
}

if (!user.host) {
if (this.utilityService.isKeyWordIncluded(data.cw ?? data.text ?? '', meta.prohibitedWords)) {
throw new NoteCreateService.ContainsProhibitedWordsError();
}
}

const inSilencedInstance = this.utilityService.isSilencedHost(meta.silencedHosts, user.host);

if (data.visibility === 'public' && inSilencedInstance && user.host !== null) {
Expand Down
69 changes: 34 additions & 35 deletions packages/backend/src/server/api/endpoints/notes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-

private noteEntityService: NoteEntityService,
private noteCreateService: NoteCreateService,
private metaService: MetaService,
private utilityService: UtilityService,
) {
super(meta, paramDef, async (ps, me) => {
if (ps.text) {
// Check prohibited words
const miMeta = await this.metaService.fetch();
if (this.utilityService.isKeyWordIncluded(ps.text, miMeta.prohibitedWords)) {
throw new ApiError(meta.errors.containsProhibitedWords);
}
}

let visibleUsers: MiUser[] = [];
if (ps.visibleUserIds) {
visibleUsers = await this.usersRepository.findBy({
Expand Down Expand Up @@ -358,31 +348,40 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
}

// 投稿を作成
const note = await this.noteCreateService.create(me, {
createdAt: new Date(),
files: files,
poll: ps.poll ? {
choices: ps.poll.choices,
multiple: ps.poll.multiple ?? false,
expiresAt: ps.poll.expiresAt ? new Date(ps.poll.expiresAt) : null,
} : undefined,
text: ps.text ?? undefined,
reply,
renote,
cw: ps.cw,
localOnly: ps.localOnly,
reactionAcceptance: ps.reactionAcceptance,
visibility: ps.visibility,
visibleUsers,
channel,
apMentions: ps.noExtractMentions ? [] : undefined,
apHashtags: ps.noExtractHashtags ? [] : undefined,
apEmojis: ps.noExtractEmojis ? [] : undefined,
});

return {
createdNote: await this.noteEntityService.pack(note, me),
};
try {
const note = await this.noteCreateService.create(me, {
createdAt: new Date(),
files: files,
poll: ps.poll ? {
choices: ps.poll.choices,
multiple: ps.poll.multiple ?? false,
expiresAt: ps.poll.expiresAt ? new Date(ps.poll.expiresAt) : null,
} : undefined,
text: ps.text ?? undefined,
reply,
renote,
cw: ps.cw,
localOnly: ps.localOnly,
reactionAcceptance: ps.reactionAcceptance,
visibility: ps.visibility,
visibleUsers,
channel,
apMentions: ps.noExtractMentions ? [] : undefined,
apHashtags: ps.noExtractHashtags ? [] : undefined,
apEmojis: ps.noExtractEmojis ? [] : undefined,
});

return {
createdNote: await this.noteEntityService.pack(note, me),
};
} catch (e) {
// TODO: 他のErrorもここでキャッチしてエラーメッセージを当てるようにしたい
if (e instanceof NoteCreateService.ContainsProhibitedWordsError) {
throw new ApiError(meta.errors.containsProhibitedWords);
}

throw e;
}
});
}
}
20 changes: 20 additions & 0 deletions packages/backend/test/e2e/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ describe('Note', () => {

let alice: misskey.entities.SignupResponse;
let bob: misskey.entities.SignupResponse;
let tom: misskey.entities.SignupResponse;

beforeAll(async () => {
const connection = await initTestDb(true);
Notes = connection.getRepository(MiNote);
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
tom = await signup({ username: 'tom', host: 'example.com' });
}, 1000 * 60 * 2);

test('投稿できる', async () => {
Expand Down Expand Up @@ -660,6 +662,24 @@ describe('Note', () => {
assert.strictEqual(note2.status, 400);
assert.strictEqual(note2.body.error.code, 'CONTAINS_PROHIBITED_WORDS');
});

test('禁止ワードを含んでいてもリモートノートはエラーにならない', async () => {
const prohibited = await api('admin/update-meta', {
prohibitedWords: [
'test',
],
}, alice);

assert.strictEqual(prohibited.status, 204);

await new Promise(x => setTimeout(x, 2));

const note1 = await api('/notes/create', {
text: 'hogetesthuge',
}, tom);

assert.strictEqual(note1.status, 200);
});
});

describe('notes/delete', () => {
Expand Down

0 comments on commit 1d3962f

Please sign in to comment.