ActivityPub-federated video streaming platform using P2P directly in your web browser
Add plugin helper to update a video
Chocobozzzd9c2fc1fc0a1
diff --git a/packages/tests/fixtures/peertube-plugin-test-four/main.js b/packages/tests/fixtures/peertube-plugin-test-four/main.jsindex ddee2edb9d..1fe272431b 100644--- a/packages/tests/fixtures/peertube-plugin-test-four/main.js+++ b/packages/tests/fixtures/peertube-plugin-test-four/main.js@@ -167,6 +167,15 @@ async function register ({return res.sendStatus(201)})++ router.post('/update-video/:id', async (req, res) => {+ await peertubeHelpers.videos.updateVideo({+ videoId: req.params.id,+ attributes: req.body+ })++ return res.sendStatus(204)+ })}}diff --git a/packages/tests/src/plugins/plugin-helpers.ts b/packages/tests/src/plugins/plugin-helpers.tsindex 06ce829b29..817dfc21b7 100644--- a/packages/tests/src/plugins/plugin-helpers.ts+++ b/packages/tests/src/plugins/plugin-helpers.ts@@ -1,8 +1,6 @@/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */-import { expect } from 'chai'-import { pathExists } from 'fs-extra/esm'-import { HttpStatusCode } from '@peertube/peertube-models'+import { HttpStatusCode, VideoPrivacy } from '@peertube/peertube-models'import {cleanupTests,ConfigCommand,@@ -18,6 +16,8 @@ import {} from '@peertube/peertube-server-commands'import { MockSmtpServer } from '@tests/shared/mock-servers/mock-email.js'import { checkVideoFilesWereRemoved } from '@tests/shared/videos.js'+import { expect } from 'chai'+import { pathExists } from 'fs-extra/esm'function postCommand (server: PeerTubeServer, command: string, bodyArg?: object) {const body = { command }@@ -377,6 +377,36 @@ describe('Test plugin helpers', function () {expect(body.streams).to.have.lengthOf(2)})+ it('Should update a video', async function () {+ const { uuid } = await servers[0].videos.quickUpload({+ name: 'video to update',+ privacy: VideoPrivacy.PRIVATE,+ nsfw: false+ })++ await makePostBodyRequest({+ url: servers[0].url,+ path: '/plugins/test-four/router/update-video/' + uuid,+ fields: {+ name: 'video1 updated by plugin',+ support: 'support text updated by plugin',+ nsfw: true,+ privacy: VideoPrivacy.PUBLIC+ },+ expectedStatus: HttpStatusCode.NO_CONTENT_204+ })++ await waitJobs(servers)++ for (const server of servers) {+ const video = await server.videos.get({ id: uuid })+ expect(video.name).to.equal('video1 updated by plugin')+ expect(video.support).to.equal('support text updated by plugin')+ expect(video.privacy.id).to.equal(VideoPrivacy.PUBLIC)+ expect(video.nsfw).to.be.true+ }+ })+it('Should remove a video after a view', async function () {this.timeout(40000)diff --git a/server/core/lib/plugins/plugin-helpers-builder.ts b/server/core/lib/plugins/plugin-helpers-builder.tsindex 29a997bc29..b7dfdaab53 100644--- a/server/core/lib/plugins/plugin-helpers-builder.ts+++ b/server/core/lib/plugins/plugin-helpers-builder.ts@@ -22,6 +22,7 @@ import { Server } from 'http'import { join } from 'path'import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist.js'import { JobQueue } from '../job-queue/index.js'+import { LocalVideoUpdater } from '../local-video-updater.js'import { PeerTubeSocket } from '../peertube-socket.js'import { ServerConfigManager } from '../server-config-manager.js'import { blacklistVideo, unblacklistVideo } from '../video-blacklist.js'@@ -32,7 +33,7 @@ function buildPluginHelpers (httpServer: Server, pluginModel: MPlugin, npmName:logger: buildPluginLogger(npmName),database: buildDatabaseHelpers(),- videos: buildVideosHelpers(),+ videos: buildVideosHelpers(npmName),config: buildConfigHelpers(),@@ -76,7 +77,7 @@ function buildServerHelpers (httpServer: Server) {}}-function buildVideosHelpers () {+function buildVideosHelpers (npmName: string) {return {loadByUrl: (url: string) => {return VideoModel.loadByUrl(url)@@ -98,6 +99,14 @@ function buildVideosHelpers () {})},+ updateVideo: async (options: Parameters<PeerTubeHelpers['videos']['updateVideo']>[0]) => {+ const video = await VideoModel.loadFull(options.videoId)+ if (!video) return++ const updater = new LocalVideoUpdater({ video, user: null, tags: [ 'plugins', npmName ] })+ await updater.update(options.attributes)+ },+ffprobe: (path: string) => {return ffprobePromise(path)},diff --git a/server/core/types/plugins/register-server-option.model.ts b/server/core/types/plugins/register-server-option.model.tsindex b41cc1b6a0..4127270ef7 100644--- a/server/core/types/plugins/register-server-option.model.ts+++ b/server/core/types/plugins/register-server-option.model.ts@@ -10,7 +10,9 @@ import {RegisterServerHookOptions,RegisterServerSettingOptions,ServerConfig,- VideoBlacklistCreate+ VideoBlacklistCreate,+ VideoCommentPolicyType,+ VideoPrivacyType} from '@peertube/peertube-models'import { ActorModel } from '@server/models/actor/actor.js'import { Response, Router } from 'express'@@ -39,6 +41,35 @@ export type PeerTubeHelpers = {removeVideo: (videoId: number) => Promise<void>+ // PeerTube >= 8.3+ // Update video metadata (does not handle thumbnail/preview files or the video channel)+ updateVideo: (options: {+ videoId: number | string+ attributes: {+ name?: string+ category?: number+ licence?: number+ language?: string+ description?: string+ support?: string+ privacy?: VideoPrivacyType+ tags?: string[]++ commentsPolicy?: VideoCommentPolicyType++ downloadEnabled?: boolean++ nsfw?: boolean+ nsfwSummary?: string+ nsfwFlags?: number++ waitTranscoding?: boolean++ originallyPublishedAt?: Date | string+ videoPasswords?: string[]+ }+ }) => Promise<void>+ffprobe: (path: string) => Promise<any>getFiles: (id: number | string) => Promise<{diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.mdindex 67cd09b6b6..9ccfae6bcf 100644--- a/support/doc/plugins/guide.md+++ b/support/doc/plugins/guide.md@@ -575,6 +575,17 @@ async function register ({const video = await peertubeHelpers.videos.loadByUrl('...')}+ // Update video metadata (thumbnail/preview files and the video channel are not supported)+ {+ await peertubeHelpers.videos.updateVideo({+ videoId: '...',+ attributes: {+ name: 'New video name',+ support: 'New support text'+ }+ })+ }+// Send an email{await peertubeHelpers.email.createJob({