2020-01-30 04:37:25 +09:00
|
|
|
import { Antenna } from '../models/entities/antenna';
|
|
|
|
import { Note } from '../models/entities/note';
|
|
|
|
import { User } from '../models/entities/user';
|
2020-02-15 01:03:59 +09:00
|
|
|
import { UserListJoinings, UserGroupJoinings } from '../models';
|
2020-01-30 04:37:25 +09:00
|
|
|
import parseAcct from './acct/parse';
|
|
|
|
import { getFullApAccount } from './convert-host';
|
2020-02-15 01:03:59 +09:00
|
|
|
import { ensure } from '../prelude/ensure';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
export async function checkHitAntenna(antenna: Antenna, note: Note, noteUser: User, followers: User['id'][]): Promise<boolean> {
|
|
|
|
if (note.visibility === 'specified') return false;
|
|
|
|
|
|
|
|
if (note.visibility === 'followers') {
|
|
|
|
if (!followers.includes(antenna.userId)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!antenna.withReplies && note.replyId != null) return false;
|
|
|
|
|
|
|
|
if (antenna.src === 'home') {
|
|
|
|
if (!followers.includes(antenna.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-15 01:03:59 +09:00
|
|
|
} else if (antenna.src === 'group') {
|
|
|
|
const joining = await UserGroupJoinings.findOne(antenna.userGroupJoiningId!).then(ensure);
|
|
|
|
|
|
|
|
const groupUsers = (await UserGroupJoinings.find({
|
|
|
|
userGroupId: joining.userGroupId
|
|
|
|
})).map(x => x.userId);
|
|
|
|
|
|
|
|
if (!groupUsers.includes(note.userId)) return false;
|
2020-01-30 04:37:25 +09:00
|
|
|
} else if (antenna.src === 'users') {
|
|
|
|
const accts = antenna.users.map(x => {
|
|
|
|
const { username, host } = parseAcct(x);
|
|
|
|
return getFullApAccount(username, host).toLowerCase();
|
|
|
|
});
|
|
|
|
if (!accts.includes(getFullApAccount(noteUser.username, noteUser.host).toLowerCase())) return false;
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:20:21 +09:00
|
|
|
const keywords = antenna.keywords
|
|
|
|
// Clean up
|
|
|
|
.map(xs => xs.filter(x => x !== ''))
|
|
|
|
.filter(xs => xs.length > 0);
|
|
|
|
|
|
|
|
if (keywords.length > 0) {
|
2020-01-30 04:37:25 +09:00
|
|
|
if (note.text == null) return false;
|
|
|
|
|
2020-05-10 15:20:21 +09:00
|
|
|
const matched = keywords.some(and =>
|
|
|
|
and.every(keyword =>
|
2020-01-30 04:37:25 +09:00
|
|
|
antenna.caseSensitive
|
|
|
|
? note.text!.includes(keyword)
|
|
|
|
: note.text!.toLowerCase().includes(keyword.toLowerCase())
|
|
|
|
));
|
2020-03-04 11:45:33 +09:00
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
if (!matched) return false;
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:20:21 +09:00
|
|
|
const excludeKeywords = antenna.excludeKeywords
|
|
|
|
// Clean up
|
|
|
|
.map(xs => xs.filter(x => x !== ''))
|
|
|
|
.filter(xs => xs.length > 0);
|
|
|
|
|
|
|
|
if (excludeKeywords.length > 0) {
|
2020-02-21 00:28:45 +09:00
|
|
|
if (note.text == null) return false;
|
|
|
|
|
2020-05-10 15:20:21 +09:00
|
|
|
const matched = excludeKeywords.some(and =>
|
|
|
|
and.every(keyword =>
|
2020-02-21 00:28:45 +09:00
|
|
|
antenna.caseSensitive
|
|
|
|
? note.text!.includes(keyword)
|
|
|
|
: note.text!.toLowerCase().includes(keyword.toLowerCase())
|
|
|
|
));
|
2020-03-04 11:45:33 +09:00
|
|
|
|
2020-02-21 00:28:45 +09:00
|
|
|
if (matched) return false;
|
|
|
|
}
|
|
|
|
|
2020-01-30 04:37:25 +09:00
|
|
|
if (antenna.withFile) {
|
|
|
|
if (note.fileIds.length === 0) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: eval expression
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|