Merge pull request #214 from cloudflare/sven/regen-timeline-delete

regenerate the home timeline after status deletion
pull/215/head
Sven Sauleau 2023-02-07 15:03:52 +00:00 zatwierdzone przez GitHub
commit 78d029c452
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 73 dodań i 6 usunięć

Wyświetl plik

@ -815,7 +815,7 @@ describe('Mastodon APIs', () => {
const queue = makeQueue()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const mastodonId = 'abcd'
const res = await statuses_id.handleRequestDelete(db, mastodonId, actor, domain, userKEK, queue)
const res = await statuses_id.handleRequestDelete(db, mastodonId, actor, domain, userKEK, queue, cache)
assert.equal(res.status, 404)
})
@ -826,7 +826,15 @@ describe('Mastodon APIs', () => {
const actor2 = await createPerson(domain, db, userKEK, 'sven2@cloudflare.com')
const note = await createPublicNote(domain, db, 'note from actor2', actor2)
const res = await statuses_id.handleRequestDelete(db, note[mastodonIdSymbol]!, actor, domain, userKEK, queue)
const res = await statuses_id.handleRequestDelete(
db,
note[mastodonIdSymbol]!,
actor,
domain,
userKEK,
queue,
cache
)
assert.equal(res.status, 404)
})
@ -837,7 +845,15 @@ describe('Mastodon APIs', () => {
const note = await createPublicNote(domain, db, 'note from actor', actor)
await addObjectInOutbox(db, actor, note)
const res = await statuses_id.handleRequestDelete(db, note[mastodonIdSymbol]!, actor, domain, userKEK, queue)
const res = await statuses_id.handleRequestDelete(
db,
note[mastodonIdSymbol]!,
actor,
domain,
userKEK,
queue,
cache
)
assert.equal(res.status, 200)
{
@ -850,6 +866,35 @@ describe('Mastodon APIs', () => {
}
})
test('delete status regenerates the timeline', async () => {
const db = await makeDB()
const queue = makeQueue()
const cache = makeCache()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const note = await createPublicNote(domain, db, 'note from actor', actor)
await addObjectInOutbox(db, actor, note)
// Poison the timeline
await cache.put(actor.id.toString() + '/timeline/home', 'funny value')
const res = await statuses_id.handleRequestDelete(
db,
note[mastodonIdSymbol]!,
actor,
domain,
userKEK,
queue,
cache
)
assert.equal(res.status, 200)
// ensure that timeline has been regenerated after the deletion
// and that timeline is empty
const timeline = await cache.get<Array<any>>(actor.id.toString() + '/timeline/home')
assert(timeline)
assert.equal(timeline!.length, 0)
})
test('delete status sends to followers', async () => {
const db = await makeDB()
const queue = makeQueue()
@ -863,7 +908,15 @@ describe('Mastodon APIs', () => {
await addFollowing(db, actor3, actor, 'not needed')
await acceptFollowing(db, actor3, actor)
const res = await statuses_id.handleRequestDelete(db, note[mastodonIdSymbol]!, actor, domain, userKEK, queue)
const res = await statuses_id.handleRequestDelete(
db,
note[mastodonIdSymbol]!,
actor,
domain,
userKEK,
queue,
cache
)
assert.equal(res.status, 200)
assert.equal(queue.messages.length, 2)

Wyświetl plik

@ -1,5 +1,6 @@
// https://docs.joinmastodon.org/methods/statuses/#get
import type { Cache } from 'wildebeest/backend/src/cache'
import { type Note } from 'wildebeest/backend/src/activitypub/objects/note'
import * as activities from 'wildebeest/backend/src/activitypub/activities/delete'
import { cors } from 'wildebeest/backend/src/utils/cors'
@ -13,6 +14,8 @@ import { getObjectByMastodonId } from 'wildebeest/backend/src/activitypub/object
import { urlToHandle } from 'wildebeest/backend/src/utils/handle'
import { deliverFollowers } from 'wildebeest/backend/src/activitypub/deliver'
import type { Queue, DeliverMessageBody } from 'wildebeest/backend/src/types/queue'
import * as timeline from 'wildebeest/backend/src/mastodon/timeline'
import { cacheFromEnv } from 'wildebeest/backend/src/cache'
export const onRequestGet: PagesFunction<Env, any, ContextData> = async ({ params, env, request }) => {
const domain = new URL(request.url).hostname
@ -21,7 +24,15 @@ export const onRequestGet: PagesFunction<Env, any, ContextData> = async ({ param
export const onRequestDelete: PagesFunction<Env, any, ContextData> = async ({ params, env, request, data }) => {
const domain = new URL(request.url).hostname
return handleRequestDelete(env.DATABASE, params.id as UUID, data.connectedActor, domain, env.userKEK, env.QUEUE)
return handleRequestDelete(
env.DATABASE,
params.id as UUID,
data.connectedActor,
domain,
env.userKEK,
env.QUEUE,
cacheFromEnv(env)
)
}
export async function handleRequestGet(db: D1Database, id: UUID, domain: string): Promise<Response> {
@ -68,7 +79,8 @@ export async function handleRequestDelete(
connectedActor: Person,
domain: string,
userKEK: string,
queue: Queue<DeliverMessageBody>
queue: Queue<DeliverMessageBody>,
cache: Cache
): Promise<Response> {
const obj = (await getObjectByMastodonId(db, id)) as Note
if (obj === null) {
@ -89,6 +101,8 @@ export async function handleRequestDelete(
const activity = activities.create(domain, connectedActor, obj)
await deliverFollowers(db, userKEK, connectedActor, activity, queue)
await timeline.pregenerateTimelines(domain, db, cache, connectedActor)
const headers = {
...cors(),
'content-type': 'application/json; charset=utf-8',