wildebeest/functions/api/v1/statuses.ts

143 wiersze
5.3 KiB
TypeScript
Czysty Zwykły widok Historia

// https://docs.joinmastodon.org/methods/statuses/#create
2023-02-07 11:49:39 +00:00
import type { Note } from 'wildebeest/backend/src/activitypub/objects/note'
import { cors } from 'wildebeest/backend/src/utils/cors'
2023-01-24 13:03:55 +00:00
import type { APObject } from 'wildebeest/backend/src/activitypub/objects'
2023-01-17 15:21:00 +00:00
import { insertReply } from 'wildebeest/backend/src/mastodon/reply'
import * as timeline from 'wildebeest/backend/src/mastodon/timeline'
2023-01-13 10:56:24 +00:00
import type { Queue, DeliverMessageBody } from 'wildebeest/backend/src/types/queue'
import type { Document } from 'wildebeest/backend/src/activitypub/objects'
import { getObjectByMastodonId } from 'wildebeest/backend/src/activitypub/objects'
import { createStatus, getMentions } from 'wildebeest/backend/src/mastodon/status'
import * as activities from 'wildebeest/backend/src/activitypub/activities/create'
import type { Env } from 'wildebeest/backend/src/types/env'
import type { ContextData } from 'wildebeest/backend/src/types/context'
import { deliverFollowers, deliverToActor } from 'wildebeest/backend/src/activitypub/deliver'
import type { Person } from 'wildebeest/backend/src/activitypub/actors'
import { getSigningKey } from 'wildebeest/backend/src/mastodon/account'
import { readBody } from 'wildebeest/backend/src/utils/body'
2023-01-17 15:21:00 +00:00
import * as errors from 'wildebeest/backend/src/errors'
import type { Visibility } from 'wildebeest/backend/src/types'
import { toMastodonStatusFromObject } from 'wildebeest/backend/src/mastodon/status'
import type { Cache } from 'wildebeest/backend/src/cache'
import { cacheFromEnv } from 'wildebeest/backend/src/cache'
2023-02-01 15:42:18 +00:00
import { enrichStatus } from 'wildebeest/backend/src/mastodon/microformats'
2023-02-07 11:49:39 +00:00
import * as idempotency from 'wildebeest/backend/src/mastodon/idempotency'
2023-02-02 11:22:20 +00:00
import { newMention } from 'wildebeest/backend/src/activitypub/objects/mention'
2023-02-02 14:28:17 +00:00
import { originalObjectIdSymbol } from 'wildebeest/backend/src/activitypub/objects'
type StatusCreate = {
status: string
visibility: Visibility
sensitive: boolean
media_ids?: Array<string>
2023-01-17 15:21:00 +00:00
in_reply_to_id?: string
}
export const onRequest: PagesFunction<Env, any, ContextData> = async ({ request, env, data }) => {
return handleRequest(request, env.DATABASE, data.connectedActor, env.userKEK, env.QUEUE, cacheFromEnv(env))
}
// FIXME: add tests for delivery to followers and mentions to a specific Actor.
export async function handleRequest(
request: Request,
db: D1Database,
connectedActor: Person,
2023-01-13 10:56:24 +00:00
userKEK: string,
queue: Queue<DeliverMessageBody>,
cache: Cache
): Promise<Response> {
if (request.method !== 'POST') {
return new Response('', { status: 400 })
}
2023-02-07 11:49:39 +00:00
const domain = new URL(request.url).hostname
const headers = {
...cors(),
'content-type': 'application/json; charset=utf-8',
}
const idempotencyKey = request.headers.get('Idempotency-Key')
if (idempotencyKey !== null) {
const maybeObject = await idempotency.hasKey(db, idempotencyKey)
if (maybeObject !== null) {
const res = await toMastodonStatusFromObject(db, maybeObject as Note, domain)
return new Response(JSON.stringify(res), { headers })
}
}
const body = await readBody<StatusCreate>(request)
console.log(body)
if (body.status === undefined || body.visibility === undefined) {
return new Response('', { status: 400 })
}
const mediaAttachments: Array<Document> = []
if (body.media_ids && body.media_ids.length > 0) {
2023-01-18 10:44:09 +00:00
if (body.media_ids.length > 4) {
return errors.exceededLimit('up to 4 images are allowed')
}
for (let i = 0, len = body.media_ids.length; i < len; i++) {
const id = body.media_ids[i]
const document = await getObjectByMastodonId(db, id)
if (document === null) {
console.warn('object attachement not found: ' + id)
continue
}
mediaAttachments.push(document)
}
}
2023-01-24 13:03:55 +00:00
let inReplyToObject: APObject | null = null
2023-01-17 15:21:00 +00:00
if (body.in_reply_to_id) {
inReplyToObject = await getObjectByMastodonId(db, body.in_reply_to_id)
if (inReplyToObject === null) {
return errors.statusNotFound(body.in_reply_to_id)
2023-01-17 15:21:00 +00:00
}
}
const extraProperties: any = {}
if (inReplyToObject !== null) {
2023-02-02 14:28:17 +00:00
extraProperties.inReplyTo = inReplyToObject[originalObjectIdSymbol] || inReplyToObject.id.toString()
2023-01-17 15:21:00 +00:00
}
2023-02-01 15:42:18 +00:00
const content = enrichStatus(body.status)
2023-02-01 16:55:32 +00:00
const mentions = await getMentions(body.status, domain)
if (mentions.length > 0) {
2023-02-02 11:22:20 +00:00
extraProperties.tag = mentions.map(newMention)
2023-02-01 16:55:32 +00:00
}
2023-02-01 15:42:18 +00:00
const note = await createStatus(domain, db, connectedActor, content, mediaAttachments, extraProperties)
2023-01-17 15:21:00 +00:00
if (inReplyToObject !== null) {
// after the status has been created, record the reply.
await insertReply(db, connectedActor, note, inReplyToObject)
}
2023-01-13 10:56:24 +00:00
const activity = activities.create(domain, connectedActor, note)
await deliverFollowers(db, userKEK, connectedActor, activity, queue)
{
// If the status is mentioning other persons, we need to delivery it to them.
for (let i = 0, len = mentions.length; i < len; i++) {
2023-02-01 16:55:32 +00:00
const targetActor = mentions[i]
note.cc.push(targetActor.id.toString())
2023-01-13 10:56:24 +00:00
const activity = activities.create(domain, connectedActor, note)
const signingKey = await getSigningKey(userKEK, db, connectedActor)
2023-02-07 16:03:46 +00:00
await deliverToActor(signingKey, connectedActor, targetActor, activity, domain)
}
}
2023-02-07 11:49:39 +00:00
if (idempotencyKey !== null) {
await idempotency.insertKey(db, idempotencyKey, note)
}
await timeline.pregenerateTimelines(domain, db, cache, connectedActor)
const res = await toMastodonStatusFromObject(db, note, domain)
return new Response(JSON.stringify(res), { headers })
}