tooot/src/utils/helpers/urlMatcher.ts

82 lines
2.6 KiB
TypeScript
Raw Normal View History

import { getAccountStorage } from '@utils/storage/actions'
2023-03-13 23:16:41 +01:00
import * as Linking from 'expo-linking'
2022-11-12 17:52:50 +01:00
// Would mess with the /@username format
const BLACK_LIST = ['matters.news', 'medium.com']
2023-01-03 23:57:23 +01:00
export const urlMatcher = (
2022-11-12 17:52:50 +01:00
url: string
2023-01-03 23:57:23 +01:00
):
| {
domain: string
2023-02-06 14:00:40 +01:00
account?: Partial<Pick<Mastodon.Account, 'acct' | '_remote'>>
2023-01-03 23:57:23 +01:00
status?: Partial<Pick<Mastodon.Status, 'id' | '_remote'>>
}
| undefined => {
2023-03-13 23:16:41 +01:00
const parsed = Linking.parse(url)
if (!parsed.hostname?.length || !parsed.path?.length) return undefined
2022-11-12 17:52:50 +01:00
2023-01-03 23:57:23 +01:00
const domain = parsed.hostname
if (BLACK_LIST.includes(domain)) {
return
}
2023-01-03 23:57:23 +01:00
const _remote = parsed.hostname !== getAccountStorage.string('auth.domain')
2022-11-12 17:52:50 +01:00
2023-01-03 23:57:23 +01:00
let statusId: string | undefined
let accountAcct: string | undefined
2022-11-12 17:52:50 +01:00
2023-03-13 23:16:41 +01:00
const segments = parsed.path.split('/')
const last = segments.at(-1)
2023-01-03 23:57:23 +01:00
const length = segments.length // there is a starting slash
2022-12-12 22:56:31 +01:00
2023-02-06 14:00:40 +01:00
const testAndAssignStatusId = (id: string) => {
if (!!parseInt(id)) {
statusId = id
}
}
2023-01-03 23:57:23 +01:00
switch (last?.startsWith('@')) {
case true:
2023-03-13 23:16:41 +01:00
if (length === 1 || (length === 2 && segments.at(-2) === 'web')) {
2023-01-03 23:57:23 +01:00
// https://social.xmflsct.com/@tooot <- Mastodon v4.0 and above
// https://social.xmflsct.com/web/@tooot <- Mastodon v3.5 and below ! cannot be searched on the same instance
accountAcct = `${last}@${domain}`
}
break
case false:
const nextToLast = segments[length - 2]
if (nextToLast) {
if (nextToLast === 'statuses') {
2023-03-13 23:16:41 +01:00
if (length === 3 && segments.at(-3) === 'web') {
2023-01-03 23:57:23 +01:00
// https://social.xmflsct.com/web/statuses/105590085754428765 <- old
2023-02-06 14:00:40 +01:00
testAndAssignStatusId(last)
2023-01-03 23:57:23 +01:00
} else if (
2023-03-13 23:16:41 +01:00
length === 4 &&
segments.at(-2) === 'statuses' &&
segments.at(-4) === 'users'
2023-01-03 23:57:23 +01:00
) {
// https://social.xmflsct.com/users/tooot/statuses/105590085754428765 <- default Mastodon
2023-02-06 14:00:40 +01:00
testAndAssignStatusId(last)
2023-01-03 23:57:23 +01:00
// accountAcct = `@${segments[length - 3]}@${domain}`
}
} else if (
nextToLast.startsWith('@') &&
2023-03-13 23:16:41 +01:00
(length === 2 || (length === 3 && segments.at(-3) === 'web'))
2023-01-03 23:57:23 +01:00
) {
// https://social.xmflsct.com/web/@tooot/105590085754428765 <- pretty Mastodon v3.5 and below
// https://social.xmflsct.com/@tooot/105590085754428765 <- pretty Mastodon v4.0 and above
2023-02-06 14:00:40 +01:00
testAndAssignStatusId(last)
2023-01-03 23:57:23 +01:00
// accountAcct = `${nextToLast}@${domain}`
}
}
break
2022-11-12 17:52:50 +01:00
}
2023-01-03 23:57:23 +01:00
return {
domain,
2023-02-25 23:42:04 +01:00
...(accountAcct && { account: { acct: accountAcct, _remote: domain } }),
2023-01-03 23:57:23 +01:00
...(statusId && { status: { id: statusId, _remote } })
}
2022-11-12 17:52:50 +01:00
}