mirror of
https://iceshrimp.dev/crimekillz/trashposs
synced 2024-11-22 00:43:49 +01:00
[backend] Parse incorrectly formatted mentions in from-html
This commit is contained in:
parent
c02747116d
commit
72f048a24d
@ -6,7 +6,7 @@ import { getSubjectHostFromUriAndUsernameCached } from "@/remote/resolve-user.js
|
||||
const urlRegex = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+/;
|
||||
const urlRegexFull = /^https?:\/\/[\w\/:%#@$&?!()\[\]~.,=+\-]+$/;
|
||||
|
||||
export async function fromHtml(html: string, hashtagNames?: string[], basicMentionResolve: boolean = false): Promise<string> {
|
||||
export async function fromHtml(html: string, hashtagNames?: string[]): Promise<string> {
|
||||
// some AP servers like Pixelfed use br tags as well as newlines
|
||||
html = html.replace(/<br\s?\/?>\r?\n/gi, "\n");
|
||||
|
||||
@ -73,9 +73,7 @@ export async function fromHtml(html: string, hashtagNames?: string[], basicMenti
|
||||
|
||||
if (part.length === 2 && href) {
|
||||
//#region ホスト名部分が省略されているので復元する
|
||||
const acct = basicMentionResolve
|
||||
? `${txt}@${new URL(href.value).hostname}`
|
||||
: `${txt}@${await getSubjectHostFromUriAndUsernameCached(href.value, txt)}`;
|
||||
const acct = `${txt}@${await getSubjectHostFromUriAndUsernameCached(href.value, txt)}`;
|
||||
text += acct;
|
||||
//#endregion
|
||||
} else if (part.length === 3) {
|
||||
|
@ -190,9 +190,16 @@ export async function getSubjectHostFromUri(uri: string): Promise<string | null>
|
||||
}
|
||||
|
||||
export async function getSubjectHostFromUriAndUsernameCached(uri: string, username: string): Promise<string | null> {
|
||||
const hostname = new URL(uri).hostname;
|
||||
const url = new URL(uri);
|
||||
const hostname = url.hostname;
|
||||
username = username.substring(1); // remove leading @ from username
|
||||
|
||||
// This resolves invalid mentions with the URL format https://host.tld/@user@otherhost.tld
|
||||
const match = url.pathname.match(/^\/@(?<user>[a-zA-Z0-9_]+|$)@(?<host>[a-zA-Z0-9-.]+\.[a-zA-Z0-9-]+)$/)
|
||||
if (match && match.groups?.host) {
|
||||
return match.groups.host;
|
||||
}
|
||||
|
||||
if (hostname === config.hostname) {
|
||||
// user is local, return local account domain
|
||||
return config.domain;
|
||||
|
@ -109,11 +109,7 @@ describe("fromHtml", () => {
|
||||
|
||||
it("mention", async () => {
|
||||
assert.deepStrictEqual(
|
||||
await fromHtml(
|
||||
'<p>a <a href="https://joiniceshrimp.org/@user" class="u-url mention">@user</a> d</p>',
|
||||
undefined,
|
||||
false
|
||||
),
|
||||
await fromHtml('<p>a <a href="https://joiniceshrimp.org/@user" class="u-url mention">@user</a> d</p>'),
|
||||
"a @user@joiniceshrimp.org d",
|
||||
);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user