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

91 lines
3.0 KiB
TypeScript
Raw Normal View History

import { Antenna } from '@/models/entities/antenna';
import { Note } from '@/models/entities/note';
import { User } from '@/models/entities/user';
import { UserListJoinings, UserGroupJoinings } from '@/models/index';
import { getFullApAccount } from './convert-host';
2021-11-11 18:02:25 +01:00
import * as Acct from 'misskey-js/built/acct';
import { Packed } from './schema';
2021-03-23 07:06:56 +01:00
/**
* noteUserFollowers / antennaUserFollowing
*/
export async function checkHitAntenna(antenna: Antenna, note: (Note | Packed<'Note'>), noteUser: { username: string; host: string | null; }, noteUserFollowers?: User['id'][], antennaUserFollowing?: User['id'][]): Promise<boolean> {
if (note.visibility === 'specified') return false;
if (note.visibility === 'followers') {
2021-03-23 07:06:56 +01:00
if (noteUserFollowers && !noteUserFollowers.includes(antenna.userId)) return false;
if (antennaUserFollowing && !antennaUserFollowing.includes(note.userId)) return false;
}
if (!antenna.withReplies && note.replyId != null) return false;
if (antenna.src === 'home') {
2021-03-23 07:06:56 +01:00
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.find({
userListId: antenna.userListId!
})).map(x => x.userId);
if (!listUsers.includes(note.userId)) return false;
2020-02-14 17:03:59 +01:00
} else if (antenna.src === 'group') {
2021-02-13 07:33:38 +01:00
const joining = await UserGroupJoinings.findOneOrFail(antenna.userGroupJoiningId!);
2020-02-14 17:03:59 +01:00
const groupUsers = (await UserGroupJoinings.find({
userGroupId: joining.userGroupId
})).map(x => x.userId);
if (!groupUsers.includes(note.userId)) return false;
} else if (antenna.src === 'users') {
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();
});
if (!accts.includes(getFullApAccount(noteUser.username, noteUser.host).toLowerCase())) return false;
}
2020-05-10 08:20:21 +02:00
const keywords = antenna.keywords
// Clean up
.map(xs => xs.filter(x => x !== ''))
.filter(xs => xs.length > 0);
if (keywords.length > 0) {
if (note.text == null) return false;
2020-05-10 08:20:21 +02:00
const matched = keywords.some(and =>
and.every(keyword =>
antenna.caseSensitive
? note.text!.includes(keyword)
: note.text!.toLowerCase().includes(keyword.toLowerCase())
));
if (!matched) return false;
}
2020-05-10 08:20:21 +02: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 16:28:45 +01:00
if (note.text == null) return false;
2020-05-10 08:20:21 +02:00
const matched = excludeKeywords.some(and =>
and.every(keyword =>
2020-02-20 16:28:45 +01:00
antenna.caseSensitive
? note.text!.includes(keyword)
: note.text!.toLowerCase().includes(keyword.toLowerCase())
));
2020-02-20 16:28:45 +01:00
if (matched) return false;
}
if (antenna.withFile) {
2021-03-23 07:06:56 +01:00
if (note.fileIds && note.fileIds.length === 0) return false;
}
// TODO: eval expression
return true;
}