hippofish/packages/backend/src/misc/check-hit-antenna.ts

106 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import type { Antenna } from "@/models/entities/antenna.js";
import type { Note } from "@/models/entities/note.js";
import type { User } from "@/models/entities/user.js";
2023-10-17 00:34:19 +02:00
import { Blockings, UserProfiles } from "@/models/index.js";
2023-01-13 05:40:33 +01:00
import { getFullApAccount } from "./convert-host.js";
import * as Acct from "@/misc/acct.js";
import type { Packed } from "./schema.js";
import { Cache } from "./cache.js";
2023-10-17 00:34:19 +02:00
import { getWordHardMute } from "./check-word-mute.js";
2023-01-13 05:40:33 +01:00
2023-07-03 04:10:33 +02:00
const blockingCache = new Cache<User["id"][]>("blocking", 60 * 5);
2023-01-13 05:40:33 +01:00
export async function checkHitAntenna(
antenna: Antenna,
note: Note | Packed<"Note">,
noteUser: { id: User["id"]; username: string; host: string | null },
): Promise<boolean> {
if (note.visibility === "specified") return false;
if (note.visibility === "home") return false;
2023-10-17 00:34:19 +02:00
if (!antenna.withReplies && note.replyId != null) return false;
if (antenna.withFile) {
if (note.fileIds && note.fileIds.length === 0) return false;
}
// アンテナ作成者がノート作成者にブロックされていたらスキップ
2023-01-13 05:40:33 +01:00
const blockings = await blockingCache.fetch(noteUser.id, () =>
Blockings.findBy({ blockerId: noteUser.id }).then((res) =>
res.map((x) => x.blockeeId),
),
);
if (blockings.some((blocking) => blocking === antenna.userId)) return false;
if (antenna.src === "users") {
2023-01-13 05:40:33 +01:00
const accts = antenna.users.map((x) => {
2021-11-11 18:02:25 +01:00
const { username, host } = Acct.parse(x);
return getFullApAccount(username, host).toLowerCase();
});
2023-01-13 05:40:33 +01:00
if (
!accts.includes(
getFullApAccount(noteUser.username, noteUser.host).toLowerCase(),
)
)
2023-02-12 02:22:05 +01:00
return false;
} else if (antenna.src === "instances") {
const instances = antenna.instances
2023-02-12 02:22:05 +01:00
.filter((x) => x !== "")
.map((host) => {
return host.toLowerCase();
});
if (!instances.includes(noteUser.host?.toLowerCase() ?? "")) return false;
}
2020-05-10 08:20:21 +02:00
const keywords = antenna.keywords
// Clean up
2023-01-13 05:40:33 +01:00
.map((xs) => xs.filter((x) => x !== ""))
.filter((xs) => xs.length > 0);
2020-05-10 08:20:21 +02:00
if (keywords.length > 0) {
if (note.text == null) return false;
2023-01-13 05:40:33 +01:00
const matched = keywords.some((and) =>
and.every((keyword) =>
antenna.caseSensitive
? note.text!.includes(keyword)
2023-01-13 05:40:33 +01:00
: note.text!.toLowerCase().includes(keyword.toLowerCase()),
),
);
if (!matched) return false;
}
2020-05-10 08:20:21 +02:00
const excludeKeywords = antenna.excludeKeywords
// Clean up
2023-01-13 05:40:33 +01:00
.map((xs) => xs.filter((x) => x !== ""))
.filter((xs) => xs.length > 0);
2020-05-10 08:20:21 +02:00
if (excludeKeywords.length > 0) {
2020-02-20 16:28:45 +01:00
if (note.text == null) return false;
2023-01-13 05:40:33 +01:00
const matched = excludeKeywords.some((and) =>
and.every((keyword) =>
2020-02-20 16:28:45 +01:00
antenna.caseSensitive
? note.text!.includes(keyword)
2023-01-13 05:40:33 +01:00
: note.text!.toLowerCase().includes(keyword.toLowerCase()),
),
);
2020-02-20 16:28:45 +01:00
if (matched) return false;
}
2023-10-17 00:34:19 +02:00
if (
await getWordHardMute(
note,
antenna.userId,
(
await UserProfiles.findOneBy({ userId: antenna.userId })
)?.mutedWords,
)
)
return false;
// TODO: eval expression
return true;
}