forked from mirrors/firefish
[mastodon-client] fix mentions of remote local users without host
This commit is contained in:
parent
006ff07a0c
commit
870618a196
|
@ -1267,7 +1267,7 @@ export default class Misskey implements MegalodonInterface {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async addMentionsToStatus(status: Entity.Status, cache: AccountCache) : Promise<Entity.Status> {
|
public async addMentionsToStatus(status: Entity.Status, cache: AccountCache) : Promise<Entity.Status> {
|
||||||
if (status.mentions.length > 0)
|
if (status.mentions.length > 0)
|
||||||
return status;
|
return status;
|
||||||
|
|
||||||
|
@ -1277,29 +1277,39 @@ export default class Misskey implements MegalodonInterface {
|
||||||
if (status.quote != null)
|
if (status.quote != null)
|
||||||
status.quote = await this.addMentionsToStatus(status.quote, cache);
|
status.quote = await this.addMentionsToStatus(status.quote, cache);
|
||||||
|
|
||||||
status.mentions = (await this.getMentions(status.plain_content!, cache)).filter(p => p != null);
|
const idx = status.account.acct.indexOf('@');
|
||||||
for (const m of status.mentions.filter((value, index, array) => array.indexOf(value) === index)) {
|
const origin = idx < 0 ? null : status.account.acct.substring(idx + 1);
|
||||||
if (m.acct == m.username)
|
|
||||||
status.content = status.content.replace(new RegExp(`(?<=^|\\s|>)@${m.acct}@${this.baseUrlToHost(this.baseUrl)}(?=[^a-zA-Z0-9]|$)`, 'g'), `@${m.acct}`);
|
|
||||||
status.content = status.content.replace(new RegExp(`(?<=^|\\s|>)@${m.acct}(?=[^a-zA-Z0-9]|$)`, 'g'), `<a href="${m.url}" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@${m.acct}</a>`);
|
|
||||||
}
|
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async getMentions(text: string, cache: AccountCache): Promise<Entity.Mention[]> {
|
status.mentions = (await this.getMentions(status.plain_content!, origin, cache)).filter(p => p != null);
|
||||||
|
for (const m of status.mentions.filter((value, index, array) => array.indexOf(value) === index)) {
|
||||||
|
const regexFull = new RegExp(`(?<=^|\\s|>)@${m.acct}(?=[^a-zA-Z0-9]|$)`, 'gi');
|
||||||
|
const regexLocalUser = new RegExp(`(?<=^|\\s|>)@${m.acct}@${this.baseUrlToHost(this.baseUrl)}(?=[^a-zA-Z0-9]|$)`, 'gi');
|
||||||
|
const regexRemoteUser = new RegExp(`(?<=^|\\s|>)@${m.username}(?=[^a-zA-Z0-9@]|$)`, 'gi');
|
||||||
|
|
||||||
|
if (m.acct == m.username)
|
||||||
|
status.content = status.content.replace(regexLocalUser, `@${m.acct}`);
|
||||||
|
else if (!status.content.match(regexFull))
|
||||||
|
status.content = status.content.replace(regexRemoteUser, `@${m.acct}`);
|
||||||
|
|
||||||
|
status.content = status.content.replace(regexFull, `<a href="${m.url}" class="u-url mention" rel="nofollow noopener noreferrer" target="_blank">@${m.acct}</a>`);
|
||||||
|
}
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getMentions(text: string, origin: string | null, cache: AccountCache): Promise<Entity.Mention[]> {
|
||||||
const mentions :Entity.Mention[] = [];
|
const mentions :Entity.Mention[] = [];
|
||||||
|
|
||||||
if (text == undefined)
|
if (text == undefined)
|
||||||
return mentions;
|
return mentions;
|
||||||
|
|
||||||
const mentionMatch = text.matchAll(/(?<=^|\s)@(?<user>[a-zA-Z0-9_]+)(?:@(?<host>[a-zA-Z0-9-.]+\.[a-zA-Z0-9-]+)|)(?=[^a-zA-Z0-9]|$)/g,);
|
const mentionMatch = text.matchAll(/(?<=^|\s)@(?<user>[a-zA-Z0-9_]+)(?:@(?<host>[a-zA-Z0-9-.]+\.[a-zA-Z0-9-]+)|)(?=[^a-zA-Z0-9]|$)/g,);
|
||||||
|
|
||||||
for (const m of mentionMatch) {
|
for (const m of mentionMatch) {
|
||||||
try {
|
try {
|
||||||
if (m.groups == null)
|
if (m.groups == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const account = await this.getAccountByNameCached(m.groups.user, m.groups.host, cache);
|
const account = await this.getAccountByNameCached(m.groups.user, m.groups.host ?? origin, cache);
|
||||||
|
|
||||||
if (account == null)
|
if (account == null)
|
||||||
continue;
|
continue;
|
||||||
|
@ -1312,10 +1322,10 @@ export default class Misskey implements MegalodonInterface {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch {}
|
catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mentions;
|
return mentions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAccountByNameCached(user: string, host: string | null, cache: AccountCache): Promise<Entity.Account | undefined | null> {
|
public async getAccountByNameCached(user: string, host: string | null, cache: AccountCache): Promise<Entity.Account | undefined | null> {
|
||||||
const acctToFind = host == null ? user : `${user}@${host}`;
|
const acctToFind = host == null ? user : `${user}@${host}`;
|
||||||
|
|
Loading…
Reference in a new issue