2022-12-05 20:14:56 +00:00
|
|
|
// https://docs.joinmastodon.org/methods/notifications/#get
|
|
|
|
|
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 type { Env } from 'wildebeest/backend/src/types/env'
|
|
|
|
import type { Person } from 'wildebeest/backend/src/activitypub/actors'
|
|
|
|
import type { ContextData } from 'wildebeest/backend/src/types/context'
|
|
|
|
|
|
|
|
export const onRequest: PagesFunction<Env, any, ContextData> = async ({ request, env, data }) => {
|
|
|
|
return handleRequest(request, env.KV_CACHE, data.connectedActor)
|
|
|
|
}
|
|
|
|
|
|
|
|
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',
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function handleRequest(request: Request, cache: KVNamespace, connectedActor: Person): Promise<Response> {
|
|
|
|
const url = new URL(request.url)
|
|
|
|
if (url.searchParams.has('max_id')) {
|
|
|
|
// We just return the pregenerated notifications, without any filter for
|
|
|
|
// pagination. Return an empty array to avoid duplicating notifications
|
|
|
|
// in the app.
|
|
|
|
return new Response(JSON.stringify([]), { headers })
|
|
|
|
}
|
|
|
|
|
|
|
|
const notifications = await cache.get(connectedActor.id + '/notifications')
|
|
|
|
if (notifications === null) {
|
|
|
|
return new Response(JSON.stringify([]), { headers })
|
|
|
|
}
|
|
|
|
return new Response(notifications, { headers })
|
|
|
|
}
|