implement createStatus to reduce code duplication

pull/151/head
Dario Piotrowicz 2023-01-23 13:26:17 +00:00
rodzic e6e59c7b5e
commit 67109a2601
8 zmienionych plików z 61 dodań i 55 usunięć

Wyświetl plik

@ -1,4 +1,4 @@
import { Actor } from 'wildebeest/backend/src/activitypub/actors'
import type { Actor } from 'wildebeest/backend/src/activitypub/actors'
import { toMastodonStatusFromRow } from './status'
import type { Object } from 'wildebeest/backend/src/activitypub/objects'
import type { MastodonStatus } from 'wildebeest/backend/src/types/status'

Wyświetl plik

@ -2,13 +2,16 @@ import type { Handle } from '../utils/parse'
import type { MediaAttachment } from 'wildebeest/backend/src/types/media'
import type { UUID } from 'wildebeest/backend/src/types'
import { getObjectByMastodonId } from 'wildebeest/backend/src/activitypub/objects'
import type { Note } from 'wildebeest/backend/src/activitypub/objects/note'
import { createPublicNote, type Note } from 'wildebeest/backend/src/activitypub/objects/note'
import { loadExternalMastodonAccount } from 'wildebeest/backend/src/mastodon/account'
import * as actors from 'wildebeest/backend/src/activitypub/actors'
import * as media from 'wildebeest/backend/src/media/'
import type { MastodonStatus } from 'wildebeest/backend/src/types'
import { parseHandle } from '../utils/parse'
import { urlToHandle } from '../utils/handle'
import type { Person } from 'wildebeest/backend/src/activitypub/actors'
import { addObjectInOutbox } from '../activitypub/actors/outbox'
import type { Object as ActivityPubObject } from 'wildebeest/backend/src/activitypub/objects'
export function getMentions(input: string): Array<Handle> {
const mentions: Array<Handle> = []
@ -171,3 +174,27 @@ export async function getMastodonStatusById(db: D1Database, id: UUID, domain: st
}
return toMastodonStatusFromObject(db, obj as Note, domain)
}
/**
* Creates a status object in the given actor's outbox.
*
* @param domain the domain to use
* @param db D1Database
* @param actor Author of the reply
* @param content content of the reply
* @param attachments optional attachments for the status
* @param extraProperties optional extra properties for the status
* @returns the created Note for the status
*/
export async function createStatus(
domain: string,
db: D1Database,
actor: Person,
content: string,
attachments?: ActivityPubObject[],
extraProperties?: any
) {
const note = await createPublicNote(domain, db, content, actor, attachments, extraProperties)
await addObjectInOutbox(db, actor, note)
return note
}

Wyświetl plik

@ -2,7 +2,7 @@ import { makeDB, isUrlValid } from './utils'
import { MessageType } from 'wildebeest/backend/src/types/queue'
import type { JWK } from 'wildebeest/backend/src/webpush/jwk'
import { createPerson } from 'wildebeest/backend/src/activitypub/actors'
import { createPublicNote, createPrivateNote } from 'wildebeest/backend/src/activitypub/objects/note'
import { createPrivateNote } from 'wildebeest/backend/src/activitypub/objects/note'
import { addObjectInOutbox } from 'wildebeest/backend/src/activitypub/actors/outbox'
import { strict as assert } from 'node:assert/strict'
import { cacheObject } from 'wildebeest/backend/src/activitypub/objects/'
@ -10,6 +10,7 @@ import * as ap_users from 'wildebeest/functions/ap/users/[id]'
import * as ap_outbox from 'wildebeest/functions/ap/users/[id]/outbox'
import * as ap_inbox from 'wildebeest/functions/ap/users/[id]/inbox'
import * as ap_outbox_page from 'wildebeest/functions/ap/users/[id]/outbox/page'
import { createStatus } from '../src/mastodon/status'
const userKEK = 'test_kek5'
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
@ -55,8 +56,8 @@ describe('ActivityPub', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
await addObjectInOutbox(db, actor, await createPublicNote(domain, db, 'my first status', actor))
await addObjectInOutbox(db, actor, await createPublicNote(domain, db, 'my second status', actor))
await createStatus(domain, db, actor, 'my first status')
await createStatus(domain, db, actor, 'my second status')
const res = await ap_outbox.handleRequest(domain, db, 'sven', userKEK)
assert.equal(res.status, 200)
@ -70,9 +71,9 @@ describe('ActivityPub', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
await addObjectInOutbox(db, actor, await createPublicNote(domain, db, 'my first status', actor))
await createStatus(domain, db, actor, 'my first status')
await sleep(10)
await addObjectInOutbox(db, actor, await createPublicNote(domain, db, 'my second status', actor))
await createStatus(domain, db, actor, 'my second status')
const res = await ap_outbox_page.handleRequest(domain, db, 'sven')
assert.equal(res.status, 200)

Wyświetl plik

@ -2,7 +2,6 @@ import { strict as assert } from 'node:assert/strict'
import { createReply } from 'wildebeest/backend/test/shared.utils'
import { createImage } from 'wildebeest/backend/src/activitypub/objects/image'
import { MessageType } from 'wildebeest/backend/src/types/queue'
import { addObjectInOutbox } from 'wildebeest/backend/src/activitypub/actors/outbox'
import { createPublicNote } from 'wildebeest/backend/src/activitypub/objects/note'
import * as accounts_following from 'wildebeest/functions/api/v1/accounts/[id]/following'
import * as accounts_featured_tags from 'wildebeest/functions/api/v1/accounts/[id]/featured_tags'
@ -21,6 +20,7 @@ import { addFollowing, acceptFollowing } from 'wildebeest/backend/src/mastodon/f
import { insertLike } from 'wildebeest/backend/src/mastodon/like'
import { insertReblog } from 'wildebeest/backend/src/mastodon/reblog'
import * as filters from 'wildebeest/functions/api/v1/filters'
import { createStatus } from 'wildebeest/backend/src/mastodon/status'
const userKEK = 'test_kek2'
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
@ -320,8 +320,7 @@ describe('Mastodon APIs', () => {
await addFollowing(db, actor3, actor, 'sven@' + domain)
await acceptFollowing(db, actor3, actor)
const firstNote = await createPublicNote(domain, db, 'my first status', actor)
await addObjectInOutbox(db, actor, firstNote)
await createStatus(domain, db, actor, 'my first status')
const res = await accounts_get.handleRequest(domain, 'sven', db)
assert.equal(res.status, 200)
@ -340,13 +339,11 @@ describe('Mastodon APIs', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const firstNote = await createPublicNote(domain, db, 'my first status', actor)
await addObjectInOutbox(db, actor, firstNote)
const firstNote = await createStatus(domain, db, actor, 'my first status')
await insertLike(db, actor, firstNote)
await sleep(10)
const secondNode = await createPublicNote(domain, db, 'my second status', actor)
await addObjectInOutbox(db, actor, secondNode)
await insertReblog(db, actor, secondNode)
const secondNote = await createStatus(domain, db, actor, 'my second status')
await insertReblog(db, actor, secondNote)
const req = new Request('https://' + domain)
const res = await accounts_statuses.handleRequest(req, db, 'sven@' + domain)
@ -373,8 +370,8 @@ describe('Mastodon APIs', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const note = await createPublicNote(domain, db, 'a post', actor)
await addObjectInOutbox(db, actor, note)
const note = await createStatus(domain, db, actor, 'a post')
await sleep(10)
await createReply(domain, db, actor, note, 'a reply')
@ -395,8 +392,7 @@ describe('Mastodon APIs', () => {
const properties = { url: 'https://example.com/image.jpg' }
const mediaAttachments = [await createImage(domain, db, actor, properties)]
const note = await createPublicNote(domain, db, 'status from actor', actor, mediaAttachments)
await addObjectInOutbox(db, actor, note)
await createStatus(domain, db, actor, 'status from actor', mediaAttachments)
const req = new Request('https://' + domain)
const res = await accounts_statuses.handleRequest(req, db, 'sven@' + domain)

Wyświetl plik

@ -1,7 +1,6 @@
import { strict as assert } from 'node:assert/strict'
import { createReply } from 'wildebeest/backend/test/shared.utils'
import { getMentions } from 'wildebeest/backend/src/mastodon/status'
import { addObjectInOutbox } from 'wildebeest/backend/src/activitypub/actors/outbox'
import { createStatus, getMentions } from 'wildebeest/backend/src/mastodon/status'
import { createPublicNote, type Note } from 'wildebeest/backend/src/activitypub/objects/note'
import { createImage } from 'wildebeest/backend/src/activitypub/objects/image'
import * as statuses from 'wildebeest/functions/api/v1/statuses'
@ -421,8 +420,7 @@ describe('Mastodon APIs', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const note = await createPublicNote(domain, db, 'a post', actor)
await addObjectInOutbox(db, actor, note)
const note = await createStatus(domain, db, actor, 'a post')
await sleep(10)
await createReply(domain, db, actor, note, 'a reply')

Wyświetl plik

@ -11,6 +11,7 @@ import * as timelines_public from 'wildebeest/functions/api/v1/timelines/public'
import * as timelines from 'wildebeest/backend/src/mastodon/timeline'
import { insertLike } from 'wildebeest/backend/src/mastodon/like'
import { insertReblog } from 'wildebeest/backend/src/mastodon/reblog'
import { createStatus } from 'wildebeest/backend/src/mastodon/status'
const userKEK = 'test_kek6'
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
@ -29,12 +30,11 @@ describe('Mastodon APIs', () => {
await acceptFollowing(db, actor, actor2)
// Actor 2 is posting
const firstNoteFromActor2 = await createPublicNote(domain, db, 'first status from actor2', actor2)
await addObjectInOutbox(db, actor2, firstNoteFromActor2)
const firstNoteFromActor2 = await createStatus(domain, db, actor2, 'first status from actor2')
await sleep(10)
await addObjectInOutbox(db, actor2, await createPublicNote(domain, db, 'second status from actor2', actor2))
await createStatus(domain, db, actor2, 'second status from actor2')
await sleep(10)
await addObjectInOutbox(db, actor3, await createPublicNote(domain, db, 'first status from actor3', actor3))
await createStatus(domain, db, actor3, 'first status from actor3')
await sleep(10)
await insertLike(db, actor, firstNoteFromActor2)
@ -92,7 +92,7 @@ describe('Mastodon APIs', () => {
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
// Actor is posting
await addObjectInOutbox(db, actor, await createPublicNote(domain, db, 'status from myself', actor))
await createStatus(domain, db, actor, 'status from myself')
// Actor should only see posts from actor2 in the timeline
const connectedActor = actor
@ -130,10 +130,9 @@ describe('Mastodon APIs', () => {
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const actor2 = await createPerson(domain, db, userKEK, 'sven2@cloudflare.com')
const statusFromActor = await createPublicNote(domain, db, 'status from actor', actor)
await addObjectInOutbox(db, actor, statusFromActor)
const statusFromActor = await createStatus(domain, db, actor, 'status from actor')
await sleep(10)
await addObjectInOutbox(db, actor2, await createPublicNote(domain, db, 'status from actor2', actor2))
await createStatus(domain, db, actor2, 'status from actor2')
await insertLike(db, actor, statusFromActor)
await insertReblog(db, actor, statusFromActor)
@ -172,8 +171,7 @@ describe('Mastodon APIs', () => {
const properties = { url: 'https://example.com/image.jpg' }
const mediaAttachments = [await createImage(domain, db, actor, properties)]
const note = await createPublicNote(domain, db, 'status from actor', actor, mediaAttachments)
await addObjectInOutbox(db, actor, note)
await createStatus(domain, db, actor, 'status from actor', mediaAttachments)
const res = await timelines_public.handleRequest(domain, db)
assert.equal(res.status, 200)
@ -209,8 +207,8 @@ describe('Mastodon APIs', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const note = await createPublicNote(domain, db, 'a post', actor)
await addObjectInOutbox(db, actor, note)
const note = await createStatus(domain, db, actor, 'a post')
await sleep(10)
await createReply(domain, db, actor, note, 'a reply')
@ -236,8 +234,7 @@ describe('Mastodon APIs', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const note = await createPublicNote(domain, db, 'a post', actor)
await addObjectInOutbox(db, actor, note)
const note = await createStatus(domain, db, actor, 'a post')
await insertReblog(db, actor, note)
const connectedActor: any = actor
@ -251,8 +248,7 @@ describe('Mastodon APIs', () => {
const db = await makeDB()
const actor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')
const note = await createPublicNote(domain, db, 'a post', actor)
await addObjectInOutbox(db, actor, note)
const note = await createStatus(domain, db, actor, 'a post')
await insertLike(db, actor, note)
const connectedActor: any = actor

Wyświetl plik

@ -1,10 +1,10 @@
import { createPerson, getPersonByEmail, type Person } from 'wildebeest/backend/src/activitypub/actors'
import { replies, statuses } from 'wildebeest/frontend/src/dummyData'
import type { Account, MastodonStatus } from 'wildebeest/frontend/src/types'
import { createPublicNote, Note } from 'wildebeest/backend/src/activitypub/objects/note'
import { addObjectInOutbox } from 'wildebeest/backend/src/activitypub/actors/outbox'
import { Note } from 'wildebeest/backend/src/activitypub/objects/note'
import { createReblog } from 'wildebeest/backend/src/mastodon/reblog'
import { createReply as createReplyInBackend } from 'wildebeest/backend/test/shared.utils'
import { createStatus } from 'wildebeest/backend/src/mastodon/status'
/**
* Run helper commands to initialize the database with actors, statuses, etc.
*/
@ -24,15 +24,6 @@ export async function init(domain: string, db: D1Database) {
}
}
/**
* Create a status object in the given actor's outbox.
*/
async function createStatus(domain: string, db: D1Database, actor: Person, content: string) {
const note = await createPublicNote(domain, db, content, actor)
await addObjectInOutbox(db, actor, note)
return note
}
/**
* Creates a reply for a note (representing a status)
*/

Wyświetl plik

@ -5,16 +5,14 @@ import type { Object } from 'wildebeest/backend/src/activitypub/objects'
import { insertReply } from 'wildebeest/backend/src/mastodon/reply'
import * as timeline from 'wildebeest/backend/src/mastodon/timeline'
import type { Queue, DeliverMessageBody } from 'wildebeest/backend/src/types/queue'
import { createPublicNote } from 'wildebeest/backend/src/activitypub/objects/note'
import type { Document } from 'wildebeest/backend/src/activitypub/objects'
import { getObjectByMastodonId } from 'wildebeest/backend/src/activitypub/objects'
import { getMentions } from 'wildebeest/backend/src/mastodon/status'
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 { queryAcct } from 'wildebeest/backend/src/webfinger'
import { deliverFollowers, deliverToActor } from 'wildebeest/backend/src/activitypub/deliver'
import { addObjectInOutbox } from 'wildebeest/backend/src/activitypub/actors/outbox'
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'
@ -89,8 +87,7 @@ export async function handleRequest(
}
const domain = new URL(request.url).hostname
const note = await createPublicNote(domain, db, body.status, connectedActor, mediaAttachments, extraProperties)
await addObjectInOutbox(db, connectedActor, note)
const note = await createStatus(domain, db, connectedActor, body.status, mediaAttachments, extraProperties)
if (inReplyToObject !== null) {
// after the status has been created, record the reply.