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