kopia lustrzana https://github.com/cloudflare/wildebeest
commit
996a610074
|
@ -0,0 +1,20 @@
|
||||||
|
import { setActorAlias } from 'wildebeest/backend/src/activitypub/actors'
|
||||||
|
import type { Actor } from 'wildebeest/backend/src/activitypub/actors'
|
||||||
|
import { parseHandle } from 'wildebeest/backend/src/utils/parse'
|
||||||
|
import { queryAcct } from 'wildebeest/backend/src/webfinger'
|
||||||
|
import { type Database } from 'wildebeest/backend/src/database'
|
||||||
|
|
||||||
|
export async function addAlias(db: Database, alias: string, connectedActor: Actor) {
|
||||||
|
const handle = parseHandle(alias)
|
||||||
|
const acct = `${handle.localPart}@${handle.domain}`
|
||||||
|
if (handle.domain === null) {
|
||||||
|
throw new Error("account migration within an instance isn't supported")
|
||||||
|
}
|
||||||
|
|
||||||
|
const actor = await queryAcct(handle.domain, db, acct)
|
||||||
|
if (actor === null) {
|
||||||
|
throw new Error('actor not found')
|
||||||
|
}
|
||||||
|
|
||||||
|
await setActorAlias(db, connectedActor.id, actor.id)
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
import { makeDB } from '../utils'
|
import { makeDB } from '../utils'
|
||||||
import { strict as assert } from 'node:assert/strict'
|
import { strict as assert } from 'node:assert/strict'
|
||||||
import { createPerson, getActorById } from 'wildebeest/backend/src/activitypub/actors'
|
import { createPerson, getActorById } from 'wildebeest/backend/src/activitypub/actors'
|
||||||
import * as account_alias from 'wildebeest/functions/api/wb/settings/account/alias'
|
import * as alias from 'wildebeest/backend/src/accounts/alias'
|
||||||
|
|
||||||
const domain = 'cloudflare.com'
|
const domain = 'cloudflare.com'
|
||||||
const userKEK = 'test_kek22'
|
const userKEK = 'test_kek22'
|
||||||
|
@ -39,14 +39,7 @@ describe('Wildebeest', () => {
|
||||||
throw new Error('unexpected request to ' + input)
|
throw new Error('unexpected request to ' + input)
|
||||||
}
|
}
|
||||||
|
|
||||||
const alias = 'test@example.com'
|
await alias.addAlias(db, 'test@example.com', actor)
|
||||||
|
|
||||||
const req = new Request('https://example.com', {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({ alias }),
|
|
||||||
})
|
|
||||||
const res = await account_alias.handleRequestPost(db, req, actor)
|
|
||||||
assert.equal(res.status, 201)
|
|
||||||
|
|
||||||
// Ensure the actor has the alias set
|
// Ensure the actor has the alias set
|
||||||
const newActor = await getActorById(db, actor.id)
|
const newActor = await getActorById(db, actor.id)
|
||||||
|
|
|
@ -23,7 +23,7 @@ import type { Person } from 'wildebeest/backend/src/activitypub/actors'
|
||||||
const qwikHandler = createQwikCity({ render, qwikCityPlan })
|
const qwikHandler = createQwikCity({ render, qwikCityPlan })
|
||||||
|
|
||||||
type QwikContextData = {
|
type QwikContextData = {
|
||||||
connectedActor: Person | null,
|
connectedActor: Person | null
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
|
|
|
@ -1,7 +1,35 @@
|
||||||
import { component$, useStore, useSignal, $ } from '@builder.io/qwik'
|
import { component$, useStore, useSignal, $ } from '@builder.io/qwik'
|
||||||
|
import { getErrorHtml } from '~/utils/getErrorHtml/getErrorHtml'
|
||||||
|
import { getDatabase } from 'wildebeest/backend/src/database'
|
||||||
|
import { action$, Form, zod$, z } from '@builder.io/qwik-city'
|
||||||
|
import { addAlias } from 'wildebeest/backend/src/accounts/alias'
|
||||||
|
|
||||||
|
const zodSchema = zod$({
|
||||||
|
alias: z.string().min(1),
|
||||||
|
})
|
||||||
|
|
||||||
|
export const action = action$(async (data, { env, platform, html }) => {
|
||||||
|
const db = await getDatabase(platform)
|
||||||
|
// eslint-disable-next-line
|
||||||
|
const connectedActor = (env as any).data.connectedActor
|
||||||
|
if (connectedActor === null) {
|
||||||
|
throw html(500, getErrorHtml('user not present in context'))
|
||||||
|
}
|
||||||
|
|
||||||
|
let success = false
|
||||||
|
try {
|
||||||
|
await addAlias(db, data.alias, connectedActor)
|
||||||
|
success = true
|
||||||
|
} catch (e: unknown) {
|
||||||
|
success = false
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
success,
|
||||||
|
}
|
||||||
|
}, zodSchema)
|
||||||
|
|
||||||
export default component$(() => {
|
export default component$(() => {
|
||||||
const ref = useSignal<Element>()
|
|
||||||
const state = useStore({ alias: '' })
|
const state = useStore({ alias: '' })
|
||||||
const toast = useSignal<'success' | 'failure' | null>(null)
|
const toast = useSignal<'success' | 'failure' | null>(null)
|
||||||
|
|
||||||
|
@ -9,17 +37,10 @@ export default component$(() => {
|
||||||
state.alias = (event.target as HTMLInputElement).value
|
state.alias = (event.target as HTMLInputElement).value
|
||||||
})
|
})
|
||||||
|
|
||||||
const handleSubmit = $(async () => {
|
const saveAction = action()
|
||||||
const res = await fetch('/api/wb/settings/account/alias', { method: 'POST', body: JSON.stringify(state) })
|
|
||||||
if (res.status == 200) {
|
|
||||||
toast.value = 'success'
|
|
||||||
} else {
|
|
||||||
toast.value = 'failure'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form ref={ref} class="login-form" preventdefault:submit onSubmit$={handleSubmit}>
|
<Form class="login-form" preventdefault:submit action={saveAction}>
|
||||||
<div class="max-w-4xl py-14 px-8">
|
<div class="max-w-4xl py-14 px-8">
|
||||||
<h2 class="text-2xl font-bold mb-6">Account Aliases</h2>
|
<h2 class="text-2xl font-bold mb-6">Account Aliases</h2>
|
||||||
|
|
||||||
|
@ -92,6 +113,6 @@ export default component$(() => {
|
||||||
</tbody>
|
</tbody>
|
||||||
</table> */}
|
</table> */}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</Form>
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,35 +0,0 @@
|
||||||
import type { Env } from 'wildebeest/backend/src/types/env'
|
|
||||||
import { setActorAlias } from 'wildebeest/backend/src/activitypub/actors'
|
|
||||||
import type { ContextData } from 'wildebeest/backend/src/types/context'
|
|
||||||
import type { Actor } from 'wildebeest/backend/src/activitypub/actors'
|
|
||||||
import { parseHandle } from 'wildebeest/backend/src/utils/parse'
|
|
||||||
import { queryAcct } from 'wildebeest/backend/src/webfinger'
|
|
||||||
import * as errors from 'wildebeest/backend/src/errors'
|
|
||||||
import { type Database, getDatabase } from 'wildebeest/backend/src/database'
|
|
||||||
|
|
||||||
export const onRequestPost: PagesFunction<Env, any, ContextData> = async ({ env, request, data }) => {
|
|
||||||
return handleRequestPost(await getDatabase(env), request, data.connectedActor)
|
|
||||||
}
|
|
||||||
|
|
||||||
type AddAliasRequest = {
|
|
||||||
alias: string
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function handleRequestPost(db: Database, request: Request, connectedActor: Actor): Promise<Response> {
|
|
||||||
const body = await request.json<AddAliasRequest>()
|
|
||||||
|
|
||||||
const handle = parseHandle(body.alias)
|
|
||||||
const acct = `${handle.localPart}@${handle.domain}`
|
|
||||||
if (handle.domain === null) {
|
|
||||||
console.warn("account migration within an instance isn't supported")
|
|
||||||
return new Response('', { status: 400 })
|
|
||||||
}
|
|
||||||
const actor = await queryAcct(handle.domain, db, acct)
|
|
||||||
if (actor === null) {
|
|
||||||
return errors.resourceNotFound('actor', acct)
|
|
||||||
}
|
|
||||||
|
|
||||||
await setActorAlias(db, connectedActor.id, actor.id)
|
|
||||||
|
|
||||||
return new Response('', { status: 201 })
|
|
||||||
}
|
|
Ładowanie…
Reference in New Issue