2023-01-12 21:40:33 -07: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";
|
|
|
|
import {
|
|
|
|
UserListJoinings,
|
|
|
|
UserGroupJoinings,
|
|
|
|
Blockings,
|
|
|
|
} from "@/models/index.js";
|
|
|
|
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-07-02 20:10:33 -06:00
|
|
|
const blockingCache = new Cache<User["id"][]>("blocking", 60 * 5);
|
2022-03-21 09:07:43 -06:00
|
|
|
|
|
|
|
// NOTE: フォローしているユーザーのノート、リストのユーザーのノート、グループのユーザーのノート指定はパフォーマンス上の理由で無効になっている
|
2020-01-29 12:37:25 -07:00
|
|
|
|
2021-03-23 00:06:56 -06:00
|
|
|
/**
|
|
|
|
* noteUserFollowers / antennaUserFollowing はどちらか一方が指定されていればよい
|
|
|
|
*/
|
2023-01-12 21:40:33 -07:00
|
|
|
export async function checkHitAntenna(
|
|
|
|
antenna: Antenna,
|
|
|
|
note: Note | Packed<"Note">,
|
|
|
|
noteUser: { id: User["id"]; username: string; host: string | null },
|
|
|
|
noteUserFollowers?: User["id"][],
|
|
|
|
antennaUserFollowing?: User["id"][],
|
|
|
|
): Promise<boolean> {
|
|
|
|
if (note.visibility === "specified") return false;
|
2023-06-04 15:52:44 -06:00
|
|
|
if (note.visibility === "home") return false;
|
2020-01-29 12:37:25 -07:00
|
|
|
|
2022-03-21 09:07:43 -06:00
|
|
|
// アンテナ作成者がノート作成者にブロックされていたらスキップ
|
2023-01-12 21:40:33 -07: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 (note.visibility === "followers") {
|
|
|
|
if (noteUserFollowers && !noteUserFollowers.includes(antenna.userId))
|
|
|
|
return false;
|
|
|
|
if (antennaUserFollowing && !antennaUserFollowing.includes(note.userId))
|
|
|
|
return false;
|
2020-01-29 12:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!antenna.withReplies && note.replyId != null) return false;
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
if (antenna.src === "home") {
|
|
|
|
if (noteUserFollowers && !noteUserFollowers.includes(antenna.userId))
|
|
|
|
return false;
|
|
|
|
if (antennaUserFollowing && !antennaUserFollowing.includes(note.userId))
|
|
|
|
return false;
|
|
|
|
} else if (antenna.src === "list") {
|
|
|
|
const listUsers = (
|
|
|
|
await UserListJoinings.findBy({
|
|
|
|
userListId: antenna.userListId!,
|
|
|
|
})
|
|
|
|
).map((x) => x.userId);
|
2020-01-29 12:37:25 -07:00
|
|
|
|
|
|
|
if (!listUsers.includes(note.userId)) return false;
|
2023-01-12 21:40:33 -07:00
|
|
|
} else if (antenna.src === "group") {
|
|
|
|
const joining = await UserGroupJoinings.findOneByOrFail({
|
|
|
|
id: antenna.userGroupJoiningId!,
|
|
|
|
});
|
2020-02-14 09:03:59 -07:00
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const groupUsers = (
|
|
|
|
await UserGroupJoinings.findBy({
|
|
|
|
userGroupId: joining.userGroupId,
|
|
|
|
})
|
|
|
|
).map((x) => x.userId);
|
2020-02-14 09:03:59 -07:00
|
|
|
|
|
|
|
if (!groupUsers.includes(note.userId)) return false;
|
2023-01-12 21:40:33 -07:00
|
|
|
} else if (antenna.src === "users") {
|
|
|
|
const accts = antenna.users.map((x) => {
|
2021-11-11 10:02:25 -07:00
|
|
|
const { username, host } = Acct.parse(x);
|
2020-01-29 12:37:25 -07:00
|
|
|
return getFullApAccount(username, host).toLowerCase();
|
|
|
|
});
|
2023-01-12 21:40:33 -07:00
|
|
|
if (
|
|
|
|
!accts.includes(
|
|
|
|
getFullApAccount(noteUser.username, noteUser.host).toLowerCase(),
|
|
|
|
)
|
|
|
|
)
|
2023-02-11 18:22:05 -07:00
|
|
|
return false;
|
2023-02-11 18:20:17 -07:00
|
|
|
} else if (antenna.src === "instances") {
|
|
|
|
const instances = antenna.instances
|
2023-02-11 18:22:05 -07:00
|
|
|
.filter((x) => x !== "")
|
|
|
|
.map((host) => {
|
2023-02-11 18:20:17 -07:00
|
|
|
return host.toLowerCase();
|
|
|
|
});
|
|
|
|
if (!instances.includes(noteUser.host?.toLowerCase() ?? "")) return false;
|
2020-01-29 12:37:25 -07:00
|
|
|
}
|
|
|
|
|
2020-05-10 00:20:21 -06:00
|
|
|
const keywords = antenna.keywords
|
|
|
|
// Clean up
|
2023-01-12 21:40:33 -07:00
|
|
|
.map((xs) => xs.filter((x) => x !== ""))
|
|
|
|
.filter((xs) => xs.length > 0);
|
2020-05-10 00:20:21 -06:00
|
|
|
|
|
|
|
if (keywords.length > 0) {
|
2020-01-29 12:37:25 -07:00
|
|
|
if (note.text == null) return false;
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const matched = keywords.some((and) =>
|
|
|
|
and.every((keyword) =>
|
2020-01-29 12:37:25 -07:00
|
|
|
antenna.caseSensitive
|
|
|
|
? note.text!.includes(keyword)
|
2023-01-12 21:40:33 -07:00
|
|
|
: note.text!.toLowerCase().includes(keyword.toLowerCase()),
|
|
|
|
),
|
|
|
|
);
|
2020-03-03 19:45:33 -07:00
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
if (!matched) return false;
|
|
|
|
}
|
|
|
|
|
2020-05-10 00:20:21 -06:00
|
|
|
const excludeKeywords = antenna.excludeKeywords
|
|
|
|
// Clean up
|
2023-01-12 21:40:33 -07:00
|
|
|
.map((xs) => xs.filter((x) => x !== ""))
|
|
|
|
.filter((xs) => xs.length > 0);
|
2020-05-10 00:20:21 -06:00
|
|
|
|
|
|
|
if (excludeKeywords.length > 0) {
|
2020-02-20 08:28:45 -07:00
|
|
|
if (note.text == null) return false;
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const matched = excludeKeywords.some((and) =>
|
|
|
|
and.every((keyword) =>
|
2020-02-20 08:28:45 -07:00
|
|
|
antenna.caseSensitive
|
|
|
|
? note.text!.includes(keyword)
|
2023-01-12 21:40:33 -07:00
|
|
|
: note.text!.toLowerCase().includes(keyword.toLowerCase()),
|
|
|
|
),
|
|
|
|
);
|
2020-03-03 19:45:33 -07:00
|
|
|
|
2020-02-20 08:28:45 -07:00
|
|
|
if (matched) return false;
|
|
|
|
}
|
|
|
|
|
2020-01-29 12:37:25 -07:00
|
|
|
if (antenna.withFile) {
|
2021-03-23 00:06:56 -06:00
|
|
|
if (note.fileIds && note.fileIds.length === 0) return false;
|
2020-01-29 12:37:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: eval expression
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|