mirror of
https://iceshrimp.dev/iceshrimp/iceshrimp
synced 2024-12-27 13:08:05 +09:00
d2b5b67b44
将来ESMに移行しやすいように Related: #7658 なんかmochaが起動しなくなってるけど理由不明 すぐ直したい
40 lines
907 B
TypeScript
40 lines
907 B
TypeScript
const RE2 = require('re2');
|
|
import { Note } from '@/models/entities/note.js';
|
|
import { User } from '@/models/entities/user.js';
|
|
|
|
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<boolean> {
|
|
// 自分自身
|
|
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;
|
|
}
|