2022-12-05 20:14:56 +00:00
|
|
|
// https://docs.joinmastodon.org/methods/statuses/#favourite
|
|
|
|
|
|
|
|
import type { Env } from 'wildebeest/backend/src/types/env'
|
2023-01-17 14:14:58 +00:00
|
|
|
import { cors } from 'wildebeest/backend/src/utils/cors'
|
2022-12-05 20:14:56 +00:00
|
|
|
import { insertLike } from 'wildebeest/backend/src/mastodon/like'
|
|
|
|
import { getSigningKey } from 'wildebeest/backend/src/mastodon/account'
|
|
|
|
import { deliverToActor } from 'wildebeest/backend/src/activitypub/deliver'
|
|
|
|
import type { Person } from 'wildebeest/backend/src/activitypub/actors'
|
|
|
|
import * as actors from 'wildebeest/backend/src/activitypub/actors'
|
|
|
|
import * as like from 'wildebeest/backend/src/activitypub/activities/like'
|
|
|
|
import { getObjectByMastodonId } from 'wildebeest/backend/src/activitypub/objects'
|
|
|
|
import type { Note } from 'wildebeest/backend/src/activitypub/objects/note'
|
|
|
|
import type { ContextData } from 'wildebeest/backend/src/types/context'
|
|
|
|
import { toMastodonStatusFromObject } from 'wildebeest/backend/src/mastodon/status'
|
|
|
|
|
2023-01-10 11:01:08 +00:00
|
|
|
export const onRequest: PagesFunction<Env, any, ContextData> = async ({ env, data, params }) => {
|
2022-12-05 20:14:56 +00:00
|
|
|
return handleRequest(env.DATABASE, params.id as string, data.connectedActor, env.userKEK)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function handleRequest(
|
|
|
|
db: D1Database,
|
|
|
|
id: string,
|
|
|
|
connectedActor: Person,
|
|
|
|
userKEK: string
|
|
|
|
): Promise<Response> {
|
|
|
|
const obj = await getObjectByMastodonId(db, id)
|
|
|
|
if (obj === null) {
|
|
|
|
return new Response('', { status: 404 })
|
|
|
|
}
|
|
|
|
|
|
|
|
const status = await toMastodonStatusFromObject(db, obj as Note)
|
|
|
|
if (status === null) {
|
|
|
|
return new Response('', { status: 404 })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj.originalObjectId && obj.originalActorId) {
|
|
|
|
// Liking an external object delivers the like activity
|
2023-01-13 10:56:24 +00:00
|
|
|
const targetActor = await actors.getAndCache(new URL(obj.originalActorId), db)
|
2022-12-05 20:14:56 +00:00
|
|
|
if (!targetActor) {
|
|
|
|
return new Response(`target Actor ${obj.originalActorId} not found`, { status: 404 })
|
|
|
|
}
|
|
|
|
|
|
|
|
const activity = like.create(connectedActor, new URL(obj.originalObjectId))
|
|
|
|
const signingKey = await getSigningKey(userKEK, db, connectedActor)
|
|
|
|
await deliverToActor(signingKey, connectedActor, targetActor, activity)
|
|
|
|
}
|
|
|
|
|
|
|
|
await insertLike(db, connectedActor, obj)
|
|
|
|
status.favourited = true
|
|
|
|
|
|
|
|
const headers = {
|
2023-01-17 14:14:58 +00:00
|
|
|
...cors(),
|
2022-12-05 20:14:56 +00:00
|
|
|
'content-type': 'application/json; charset=utf-8',
|
|
|
|
}
|
|
|
|
return new Response(JSON.stringify(status), { headers })
|
|
|
|
}
|