backend: move mentionedUsers query to mention handler #134
1 changed files with 31 additions and 30 deletions
|
@ -14,23 +14,8 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const mentionedUsers = await UserProfiles.createQueryBuilder('user_profile')
|
|
||||||
.leftJoin('user_profile.user', 'user')
|
|
||||||
.select('user.username')
|
|
||||||
.addSelect('user.host')
|
|
||||||
// links should preferably use user friendly urls, only fall back to AP ids
|
|
||||||
.addSelect('COALESCE(user_profile.url, user.uri)', 'url')
|
|
||||||
.where('userId IN (:...ids)', { ids: mentions ?? extractMentions(nodes) })
|
|
||||||
.getMany();
|
|
||||||
|
|
||||||
const doc = new JSDOM('').window.document;
|
const doc = new JSDOM('').window.document;
|
||||||
|
|
||||||
function appendChildren(children: mfm.MfmNode[], targetElement: any): void {
|
|
||||||
if (children) {
|
|
||||||
for (const child of children.map(x => (handlers as any)[x.type](x))) targetElement.appendChild(child);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const handlers: { [K in mfm.MfmNode['type']]: (node: mfm.NodeType<K>) => any } = {
|
const handlers: { [K in mfm.MfmNode['type']]: (node: mfm.NodeType<K>) => any } = {
|
||||||
bold(node) {
|
bold(node) {
|
||||||
const el = doc.createElement('b');
|
const el = doc.createElement('b');
|
||||||
|
@ -117,8 +102,18 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
|
||||||
return a;
|
return a;
|
||||||
},
|
},
|
||||||
|
|
||||||
mention(node) {
|
async mention(node): Promise<HTMLElement | Text> {
|
||||||
const { username, host, acct } = node.props;
|
const { username, host, acct } = node.props;
|
||||||
|
const ids = mentions ?? extractMentions(nodes);
|
||||||
|
if (ids.length > 0) {
|
||||||
|
const mentionedUsers = await UserProfiles.createQueryBuilder('user_profile')
|
||||||
|
.leftJoin('user_profile.user', 'user')
|
||||||
|
.select('user.username')
|
||||||
|
.addSelect('user.host')
|
||||||
|
// links should preferably use user friendly urls, only fall back to AP ids
|
||||||
|
.addSelect('COALESCE(user_profile.url, user.uri)', 'url')
|
||||||
|
.where('"userId" IN (:...ids)', { ids })
|
||||||
|
.getMany();
|
||||||
const userInfo = mentionedUsers.find(user => user.user?.username === username && user.userHost === host);
|
const userInfo = mentionedUsers.find(user => user.user?.username === username && user.userHost === host);
|
||||||
if (userInfo != null) {
|
if (userInfo != null) {
|
||||||
// Mastodon microformat: span.h-card > a.u-url.mention
|
// Mastodon microformat: span.h-card > a.u-url.mention
|
||||||
|
@ -131,10 +126,10 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
|
||||||
card.className = 'h-card';
|
card.className = 'h-card';
|
||||||
card.appendChild(a);
|
card.appendChild(a);
|
||||||
return card;
|
return card;
|
||||||
} else {
|
}
|
||||||
|
}
|
||||||
// this user does not actually exist
|
// this user does not actually exist
|
||||||
return doc.createTextNode(acct);
|
return doc.createTextNode(acct);
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
quote(node) {
|
quote(node) {
|
||||||
|
@ -169,6 +164,12 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function appendChildren(children: mfm.MfmNode[], targetElement: any): void {
|
||||||
|
if (children.length > 0) {
|
||||||
|
for (const child of children.map(x => (handlers as any)[x.type](x))) targetElement.appendChild(child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
appendChildren(nodes, doc.body);
|
appendChildren(nodes, doc.body);
|
||||||
|
|
||||||
return `<p>${doc.body.innerHTML}</p>`;
|
return `<p>${doc.body.innerHTML}</p>`;
|
||||||
|
|
Loading…
Reference in a new issue