Merge pull request #304 from cloudflare/sven/move-following

migration: move following actors
pull/306/head
Sven Sauleau 2023-02-16 16:54:04 +00:00 zatwierdzone przez GitHub
commit c106e8d163
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 40 dodań i 1 usunięć

Wyświetl plik

@ -17,7 +17,7 @@ import {
import { type APObject, updateObject } from 'wildebeest/backend/src/activitypub/objects'
import { parseHandle } from 'wildebeest/backend/src/utils/parse'
import type { Note } from 'wildebeest/backend/src/activitypub/objects/note'
import { addFollowing, acceptFollowing, moveFollowers } from 'wildebeest/backend/src/mastodon/follow'
import { addFollowing, acceptFollowing, moveFollowers, moveFollowing } from 'wildebeest/backend/src/mastodon/follow'
import { deliverToActor } from 'wildebeest/backend/src/activitypub/deliver'
import { getSigningKey } from 'wildebeest/backend/src/mastodon/account'
import { insertLike } from 'wildebeest/backend/src/mastodon/like'
@ -403,6 +403,23 @@ export async function handle(
}
}
// move following
{
const collection = await getMetadata(fromActor.following)
collection.items = await loadItems(collection)
// TODO: eventually move to queue and move workers
while (collection.items.length > 0) {
const batch = collection.items.splice(0, 20)
await Promise.all(
batch.map(async (items) => {
await moveFollowing(db, localActor, items)
console.log(`moved ${items.length} following`)
})
)
}
}
break
}

Wyświetl plik

@ -29,6 +29,28 @@ export async function moveFollowers(db: D1Database, actor: Actor, followers: Arr
await db.batch(batch)
}
export async function moveFollowing(db: D1Database, actor: Actor, followingActors: Array<string>): Promise<void> {
const batch = []
const stmt = db.prepare(`
INSERT OR IGNORE
INTO actor_following (id, actor_id, target_actor_id, target_actor_acct, state)
VALUES (?1, ?2, ?3, ?4, 'accepted');
`)
const actorId = actor.id.toString()
for (let i = 0; i < followingActors.length; i++) {
const following = new URL(followingActors[i])
const followingActor = await actors.getAndCache(following, db)
const actorAcc = urlToHandle(followingActor.id)
const id = crypto.randomUUID()
batch.push(stmt.bind(id, actorId, followingActor.id.toString(), actorAcc))
}
await db.batch(batch)
}
// Add a pending following
export async function addFollowing(db: D1Database, actor: Actor, target: Actor, targetAcct: string): Promise<string> {
const id = crypto.randomUUID()