From 96d00a997b9847f6796c02dbff01bca2c99385c6 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 10 Mar 2023 15:08:56 +0100 Subject: Add test on AP hooks --- .../+videos/+video-edit/shared/video-edit.component.ts | 4 ++-- server/lib/activitypub/send/send-update.ts | 2 +- server/models/video/video.ts | 2 +- server/tests/fixtures/peertube-plugin-test/main.js | 12 ++++++++++++ server/tests/plugins/filter-hooks.ts | 17 +++++++++++++++++ shared/models/plugins/server/server-hook.model.ts | 2 +- 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/client/src/app/+videos/+video-edit/shared/video-edit.component.ts b/client/src/app/+videos/+video-edit/shared/video-edit.component.ts index e3bd5fe71..2bc0e6bb4 100644 --- a/client/src/app/+videos/+video-edit/shared/video-edit.component.ts +++ b/client/src/app/+videos/+video-edit/shared/video-edit.component.ts @@ -240,11 +240,11 @@ export class VideoEditComponent implements OnInit, OnDestroy { this.schedulerInterval = setInterval(() => this.minScheduledDate = new Date(), 1000 * 60) // Update every minute }) - const updateForm = (values: any) => { + const updateFormForPlugins = (values: any) => { this.form.patchValue(values) this.cd.detectChanges() } - this.hooks.runAction('action:video-edit.init', 'video-edit', { type: this.type, updateForm }) + this.hooks.runAction('action:video-edit.init', 'video-edit', { type: this.type, updateForm: updateFormForPlugins }) this.form.valueChanges.subscribe(() => { this.hooks.runAction('action:video-edit.form.updated', 'video-edit', { type: this.type, formValues: this.form.value }) diff --git a/server/lib/activitypub/send/send-update.ts b/server/lib/activitypub/send/send-update.ts index 5a66294e6..379e2d9d8 100644 --- a/server/lib/activitypub/send/send-update.ts +++ b/server/lib/activitypub/send/send-update.ts @@ -59,7 +59,7 @@ async function sendUpdateActor (accountOrChannel: MChannelDefault | MAccountDefa logger.info('Creating job to update actor %s.', byActor.url) const url = getUpdateActivityPubUrl(byActor.url, byActor.updatedAt.toISOString()) - const accountOrChannelObject = (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug? + const accountOrChannelObject = await (accountOrChannel as any).toActivityPubObject() // FIXME: typescript bug? const audience = getAudience(byActor) const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience) diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 7fe2ec293..aa9c62e36 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1717,7 +1717,7 @@ export class VideoModel extends Model>> { toActivityPubObject (this: MVideoAP): Promise { return Hooks.wrapObject( videoModelToActivityPubObject(this), - 'filter:activity-pub.video.jsonld.build.result', + 'filter:activity-pub.video.json-ld.build.result', { video: this } ) } diff --git a/server/tests/fixtures/peertube-plugin-test/main.js b/server/tests/fixtures/peertube-plugin-test/main.js index 8b918b634..89ca52625 100644 --- a/server/tests/fixtures/peertube-plugin-test/main.js +++ b/server/tests/fixtures/peertube-plugin-test/main.js @@ -207,6 +207,18 @@ async function register ({ registerHook, registerSetting, settingsManager, stora // --------------------------------------------------------------------------- + registerHook({ + target: 'filter:activity-pub.activity.context.build.result', + handler: context => context.concat([ 'https://example.com/new-context' ]) + }) + + registerHook({ + target: 'filter:activity-pub.video.json-ld.build.result', + handler: (jsonld, { video }) => ({ ...jsonld, videoName: video.name }) + }) + + // --------------------------------------------------------------------------- + registerHook({ target: 'filter:api.video-threads.list.params', handler: obj => addToCount(obj) diff --git a/server/tests/plugins/filter-hooks.ts b/server/tests/plugins/filter-hooks.ts index 6bd38cf65..3577fc022 100644 --- a/server/tests/plugins/filter-hooks.ts +++ b/server/tests/plugins/filter-hooks.ts @@ -14,6 +14,7 @@ import { cleanupTests, createMultipleServers, doubleFollow, + makeActivityPubGetRequest, makeGetRequest, makeRawRequest, PeerTubeServer, @@ -846,6 +847,22 @@ describe('Test plugin filter hooks', function () { }) }) + describe('Activity Pub', function () { + + it('Should run filter:activity-pub.activity.context.build.result', async function () { + const { body } = await makeActivityPubGetRequest(servers[0].url, '/w/' + videoUUID) + expect(body.type).to.equal('Video') + + expect(body['@context'].some(c => c === 'https://example.com/new-context')).to.be.true + }) + + it('Should run filter:activity-pub.video.json-ld.build.result', async function () { + const { body } = await makeActivityPubGetRequest(servers[0].url, '/w/' + videoUUID) + expect(body.name).to.equal('default video 0') + expect(body.videoName).to.equal('default video 0') + }) + }) + after(async function () { await cleanupTests(servers) }) diff --git a/shared/models/plugins/server/server-hook.model.ts b/shared/models/plugins/server/server-hook.model.ts index 7881beaa2..ca83672d0 100644 --- a/shared/models/plugins/server/server-hook.model.ts +++ b/shared/models/plugins/server/server-hook.model.ts @@ -119,7 +119,7 @@ export const serverFilterHookObject = { // Filter the result of video JSON LD builder // You may also need to use filter:activity-pub.activity.context.build.result to also update JSON LD context - 'filter:activity-pub.video.jsonld.build.result': true + 'filter:activity-pub.video.json-ld.build.result': true } export type ServerFilterHookName = keyof typeof serverFilterHookObject -- cgit v1.2.3