fix(X (Formerly Twitter) Node): Change how tweet id is retrieved from quote URL (#9635)

Co-authored-by: Jonathan Bennetts <jonathan.bennetts@gmail.com>
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Ria Scholz
2024-06-06 09:02:38 +02:00
committed by GitHub
parent be0dee2a15
commit 9853ecc5bc
2 changed files with 73 additions and 6 deletions

View File

@@ -83,12 +83,19 @@ export function returnId(tweetId: INodeParameterResourceLocator) {
if (tweetId.mode === 'id') {
return tweetId.value as string;
} else if (tweetId.mode === 'url') {
const value = tweetId.value as string;
const tweetIdMatch = value.includes('lists')
? value.match(/^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/list(s)?\/(\d+)$/)
: value.match(/^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)$/);
return tweetIdMatch?.[3] as string;
try {
const url = new URL(tweetId.value as string);
if (!/(twitter|x).com$/.test(url.hostname)) {
throw new ApplicationError('Invalid domain');
}
const parts = url.pathname.split('/');
if (parts.length !== 4 || parts[2] !== 'status' || !/^\d+$/.test(parts[3])) {
throw new ApplicationError('Invalid path');
}
return parts[3];
} catch (error) {
throw new ApplicationError('Not a valid tweet url', { level: 'warning', cause: error });
}
} else {
throw new ApplicationError(`The mode ${tweetId.mode} is not valid!`, { level: 'warning' });
}