backend: move mentionedUsers query to mention handler
Some checks failed
ci/woodpecker/push/lint-client Pipeline was successful
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/pr/lint-foundkey-js Pipeline was successful
ci/woodpecker/pr/lint-backend Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/lint-client Pipeline failed
ci/woodpecker/pr/test Pipeline failed

The mentionedUsers query was being run on every post, regardless of
whether a mention was in it or not.

This resulted in an SQL query ending in `WHERE userId IN ()` which is
not allowed in PostgreSQL.

Reference: #132
Changelog: Fixed
This commit is contained in:
Norm 2022-09-08 16:17:35 -04:00
parent 736c21a80a
commit 19e29c3465
Signed by: norm
GPG key ID: 7123E30E441E80DE

View file

@ -14,23 +14,8 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
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;
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 } = {
bold(node) {
const el = doc.createElement('b');
@ -117,8 +102,17 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
return a;
},
mention(node) {
async mention(node): Promise<HTMLElement | Text> {
const { username, host, acct } = node.props;
const ids = mentions ?? extractMentions(nodes);
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);
if (userInfo != null) {
// Mastodon microformat: span.h-card > a.u-url.mention
@ -131,10 +125,9 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
card.className = 'h-card';
card.appendChild(a);
return card;
} else {
// this user does not actually exist
return doc.createTextNode(acct);
}
// this user does not actually exist
return doc.createTextNode(acct);
},
quote(node) {
@ -169,6 +162,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);
return `<p>${doc.body.innerHTML}</p>`;