43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
import { UserGroupJoinings, UserGroups, Users } from "../models/index.js";
|
|
import { In } from "typeorm";
|
|
const GROUP_MENTION = /(^|[^\w@])@@([a-zA-Z0-9_]{1,64})\b/g;
|
|
const WRAPPED_ASSET = /[:;][^:;\s]{1,100}[:;]/g;
|
|
export function extractGroupMentionNames(texts) {
|
|
const names = new Set();
|
|
for (const text of texts){
|
|
if (!text) continue;
|
|
const sanitized = text.replace(WRAPPED_ASSET, (match)=>" ".repeat(match.length));
|
|
for (const match of sanitized.matchAll(GROUP_MENTION)){
|
|
names.add(match[2].toLowerCase());
|
|
}
|
|
}
|
|
return [
|
|
...names
|
|
];
|
|
}
|
|
export async function extractGroupMentionedUsers(texts) {
|
|
const names = extractGroupMentionNames(texts);
|
|
if (names.length === 0) return [];
|
|
const groups = await UserGroups.find({
|
|
where: names.map((username)=>({
|
|
username
|
|
}))
|
|
});
|
|
const mentionedUserIds = new Set();
|
|
for (const group of groups){
|
|
mentionedUserIds.add(group.userId);
|
|
const joinings = await UserGroupJoinings.findBy({
|
|
userGroupId: group.id
|
|
});
|
|
for (const joining of joinings){
|
|
mentionedUserIds.add(joining.userId);
|
|
}
|
|
}
|
|
if (mentionedUserIds.size === 0) return [];
|
|
return await Users.findBy({
|
|
id: In([
|
|
...mentionedUserIds
|
|
])
|
|
});
|
|
}
|