kopia lustrzana https://github.com/cloudflare/wildebeest
60 wiersze
2.2 KiB
TypeScript
60 wiersze
2.2 KiB
TypeScript
// https://docs.joinmastodon.org/methods/accounts/#get
|
|
|
|
import { actorURL, getActorById } from 'wildebeest/backend/src/activitypub/actors'
|
|
import { parseHandle } from 'wildebeest/backend/src/utils/parse'
|
|
import type { Handle } from 'wildebeest/backend/src/utils/parse'
|
|
import { queryAcct } from 'wildebeest/backend/src/webfinger/index'
|
|
import { loadExternalMastodonAccount, loadLocalMastodonAccount } from 'wildebeest/backend/src/mastodon/account'
|
|
import { MastodonAccount } from '../types'
|
|
|
|
export async function getAccount(domain: string, accountId: string, db: D1Database): Promise<MastodonAccount | null> {
|
|
const handle = parseHandle(accountId)
|
|
|
|
if (handle.domain === null || (handle.domain !== null && handle.domain === domain)) {
|
|
// Retrieve the statuses from a local user
|
|
return getLocalAccount(domain, db, handle)
|
|
} else if (handle.domain !== null) {
|
|
// Retrieve the statuses of a remote actor
|
|
const acct = `${handle.localPart}@${handle.domain}`
|
|
return getRemoteAccount(handle, acct)
|
|
} else {
|
|
return null
|
|
}
|
|
}
|
|
|
|
async function getRemoteAccount(handle: Handle, acct: string): Promise<MastodonAccount | null> {
|
|
// TODO: using webfinger isn't the optimal implementation. We could cache
|
|
// the object in D1 and directly query the remote API, indicated by the actor's
|
|
// url field. For now, let's keep it simple.
|
|
const actor = await queryAcct(handle.domain!, acct)
|
|
if (actor === null) {
|
|
return null
|
|
}
|
|
|
|
return await loadExternalMastodonAccount(acct, actor, true)
|
|
}
|
|
|
|
async function getLocalAccount(domain: string, db: D1Database, handle: Handle): Promise<MastodonAccount | null> {
|
|
const actorId = actorURL(adjustLocalHostDomain(domain), handle.localPart)
|
|
|
|
const actor = await getActorById(db, actorId)
|
|
if (actor === null) {
|
|
return null
|
|
}
|
|
|
|
return await loadLocalMastodonAccount(db, actor)
|
|
}
|
|
|
|
/**
|
|
* checks if a domain is a localhost one ('localhost' or '127.x.x.x') and
|
|
* in that case replaces it with '0.0.0.0' (which is what we use for our local data)
|
|
*
|
|
* Note: only needed for local development
|
|
*
|
|
* @param domain the potentially localhost domain
|
|
* @returns the adjusted domain if it was a localhost one, the original domain otherwise
|
|
*/
|
|
function adjustLocalHostDomain(domain: string) {
|
|
return domain.replace(/^localhost$|^127(\.(?:\d){1,3}){3}$/, '0.0.0.0')
|
|
}
|