From cf43dd6ec530ba4a3f589ae917e89533b352f6a3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 27 Jul 2020 13:34:20 +0900 Subject: ワードミュート (#6594) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip * wip * wip * wip * wip * wip * wip * wip * wip --- src/misc/check-word-mute.ts | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/misc/check-word-mute.ts (limited to 'src/misc') diff --git a/src/misc/check-word-mute.ts b/src/misc/check-word-mute.ts new file mode 100644 index 0000000000..5af267d75d --- /dev/null +++ b/src/misc/check-word-mute.ts @@ -0,0 +1,39 @@ +const RE2 = require('re2'); +import { Note } from '../models/entities/note'; +import { User } from '../models/entities/user'; + +type NoteLike = { + userId: Note['userId']; + text: Note['text']; +}; + +type UserLike = { + id: User['id']; +}; + +export async function checkWordMute(note: NoteLike, me: UserLike | null | undefined, mutedWords: string[][]): Promise { + // 自分自身 + if (me && (note.userId === me.id)) return false; + + const words = mutedWords + // Clean up + .map(xs => xs.filter(x => x !== '')) + .filter(xs => xs.length > 0); + + if (words.length > 0) { + if (note.text == null) return false; + + const matched = words.some(and => + and.every(keyword => { + const regexp = keyword.match(/^\/(.+)\/(.*)$/); + if (regexp) { + return new RE2(regexp[1], regexp[2]).test(note.text!); + } + return note.text!.includes(keyword); + })); + + if (matched) return true; + } + + return false; +} -- cgit v1.2.3-freya