2022-12-05 20:14:56 +00:00
|
|
|
import { createPerson, getPersonByEmail, type Person } from 'wildebeest/backend/src/activitypub/actors'
|
|
|
|
import * as statusesAPI from 'wildebeest/functions/api/v1/statuses'
|
2023-01-11 21:42:46 +00:00
|
|
|
import * as reblogAPI from 'wildebeest/functions/api/v1/statuses/[id]/reblog'
|
2022-12-05 20:14:56 +00:00
|
|
|
import { statuses } from 'wildebeest/frontend/src/dummyData'
|
2023-01-11 21:42:46 +00:00
|
|
|
import type { Account, MastodonStatus } from 'wildebeest/frontend/src/types'
|
2022-12-05 20:14:56 +00:00
|
|
|
|
|
|
|
const kek = 'test-kek'
|
|
|
|
/**
|
|
|
|
* Run helper commands to initialize the database with actors, statuses, etc.
|
|
|
|
*/
|
|
|
|
export async function init(domain: string, db: D1Database) {
|
2023-01-11 21:42:46 +00:00
|
|
|
const loadedStatuses: MastodonStatus[] = []
|
|
|
|
for (const status of statuses) {
|
|
|
|
const actor = await getOrCreatePerson(domain, db, status.account)
|
|
|
|
loadedStatuses.push(await createStatus(db, actor, status.content))
|
2022-12-05 20:14:56 +00:00
|
|
|
}
|
2023-01-11 21:42:46 +00:00
|
|
|
|
|
|
|
const reblogger = await getOrCreatePerson(domain, db, loadedStatuses[1].account)
|
|
|
|
await reblogStatus(db, reblogger, loadedStatuses[2])
|
2022-12-05 20:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a status object in the given actors outbox.
|
|
|
|
*/
|
|
|
|
async function createStatus(db: D1Database, actor: Person, status: string, visibility = 'public') {
|
|
|
|
const body = {
|
|
|
|
status,
|
|
|
|
visibility,
|
|
|
|
}
|
2023-01-11 17:48:06 +00:00
|
|
|
const headers = {
|
|
|
|
'content-type': 'application/json',
|
|
|
|
}
|
2022-12-05 20:14:56 +00:00
|
|
|
const req = new Request('https://example.com', {
|
|
|
|
method: 'POST',
|
2023-01-11 17:48:06 +00:00
|
|
|
headers,
|
2022-12-05 20:14:56 +00:00
|
|
|
body: JSON.stringify(body),
|
|
|
|
})
|
2023-01-11 21:42:46 +00:00
|
|
|
return (await (await statusesAPI.handleRequest(req, db, actor, kek)).json()) as MastodonStatus
|
2022-12-05 20:14:56 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 21:42:46 +00:00
|
|
|
async function getOrCreatePerson(
|
|
|
|
domain: string,
|
|
|
|
db: D1Database,
|
|
|
|
{ username, avatar, display_name }: Account
|
|
|
|
): Promise<Person> {
|
2022-12-05 20:14:56 +00:00
|
|
|
const person = await getPersonByEmail(db, username)
|
|
|
|
if (person) return person
|
2023-01-11 21:42:46 +00:00
|
|
|
const newPerson = await createPerson(domain, db, kek, username, {
|
|
|
|
icon: { url: avatar },
|
|
|
|
name: display_name,
|
|
|
|
})
|
2022-12-05 20:14:56 +00:00
|
|
|
if (!newPerson) {
|
|
|
|
throw new Error('Could not create Actor ' + username)
|
|
|
|
}
|
|
|
|
return newPerson
|
|
|
|
}
|
2023-01-11 21:42:46 +00:00
|
|
|
|
|
|
|
async function reblogStatus(db: D1Database, actor: Person, status: MastodonStatus) {
|
|
|
|
await reblogAPI.handleRequest(db, status.id, actor, kek)
|
|
|
|
}
|