ActivityPub-federated video streaming platform using P2P directly in your web browser
Expose PeerTube socket to plugins (#5239)
kontrollantenc43ed8e86243
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.tsindex 4e799b3d40..35945422c2 100644--- a/server/lib/plugins/plugin-helpers-builder.ts+++ b/server/lib/plugins/plugin-helpers-builder.ts@@ -13,13 +13,14 @@ import { ServerBlocklistModel } from '@server/models/server/server-blocklist'import { UserModel } from '@server/models/user/user'import { VideoModel } from '@server/models/video/video'import { VideoBlacklistModel } from '@server/models/video/video-blacklist'-import { MPlugin } from '@server/types/models'+import { MPlugin, MVideo, UserNotificationModelForApi } from '@server/types/models'import { PeerTubeHelpers } from '@server/types/plugins'import { VideoBlacklistCreate, VideoStorage } from '@shared/models'import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'import { ServerConfigManager } from '../server-config-manager'import { blacklistVideo, unblacklistVideo } from '../video-blacklist'import { VideoPathManager } from '../video-path-manager'+import { PeerTubeSocket } from '../peertube-socket'function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {const logger = buildPluginLogger(npmName)@@ -35,6 +36,8 @@ function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelconst plugin = buildPluginRelatedHelpers(pluginModel, npmName)+ const socket = buildSocketHelpers()+const user = buildUserHelpers()return {@@ -45,6 +48,7 @@ function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelmoderation,plugin,server,+ socket,user}}@@ -218,6 +222,17 @@ function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {}}+function buildSocketHelpers () {+ return {+ sendNotification: (userId: number, notification: UserNotificationModelForApi) => {+ PeerTubeSocket.Instance.sendNotification(userId, notification)+ },+ sendVideoLiveNewState: (video: MVideo) => {+ PeerTubeSocket.Instance.sendVideoLiveNewState(video)+ }+ }+}+function buildUserHelpers () {return {loadById: (id: number) => {diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.jsindex 5194e3e02b..3e848c49e0 100644--- a/server/tests/fixtures/peertube-plugin-test-four/main.js+++ b/server/tests/fixtures/peertube-plugin-test-four/main.js@@ -128,6 +128,22 @@ async function register ({return res.json(result)})++ router.post('/send-notification', async (req, res) => {+ peertubeHelpers.socket.sendNotification(req.body.userId, {+ type: 1,+ userId: req.body.userId+ })++ return res.sendStatus(201)+ })++ router.post('/send-video-live-new-state/:uuid', async (req, res) => {+ const video = await peertubeHelpers.videos.loadByIdOrUUID(req.params.uuid)+ peertubeHelpers.socket.sendVideoLiveNewState(video)++ return res.sendStatus(201)+ })}}diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.tsindex 955d7ddfd1..31c18350ab 100644--- a/server/tests/plugins/plugin-helpers.ts+++ b/server/tests/plugins/plugin-helpers.ts@@ -83,6 +83,33 @@ describe('Test plugin helpers', function () {})})+ describe('Socket', function () {++ it('Should sendNotification without any exceptions', async () => {+ const user = await servers[0].users.create({ username: 'notis_redding', password: 'secret1234?' })+ await makePostBodyRequest({+ url: servers[0].url,+ path: '/plugins/test-four/router/send-notification',+ fields: {+ userId: user.id+ },+ expectedStatus: HttpStatusCode.CREATED_201+ })+ })++ it('Should sendVideoLiveNewState without any exceptions', async () => {+ const res = await servers[0].videos.quickUpload({ name: 'video server 1' })++ await makePostBodyRequest({+ url: servers[0].url,+ path: '/plugins/test-four/router/send-video-live-new-state/' + res.uuid,+ expectedStatus: HttpStatusCode.CREATED_201+ })++ await servers[0].videos.remove({ id: res.uuid })+ })+ })+describe('Plugin', function () {it('Should get the base static route', async function () {diff --git a/server/types/plugins/register-server-option.model.ts b/server/types/plugins/register-server-option.model.tsindex fb4f12a4c4..a8b804b636 100644--- a/server/types/plugins/register-server-option.model.ts+++ b/server/types/plugins/register-server-option.model.ts@@ -16,7 +16,7 @@ import {ThumbnailType,VideoBlacklistCreate} from '@shared/models'-import { MUserDefault, MVideoThumbnail } from '../models'+import { MUserDefault, MVideo, MVideoThumbnail, UserNotificationModelForApi } from '../models'import {RegisterServerAuthExternalOptions,RegisterServerAuthExternalResult,@@ -86,6 +86,11 @@ export type PeerTubeHelpers = {getServerActor: () => Promise<ActorModel>}+ socket: {+ sendNotification: (userId: number, notification: UserNotificationModelForApi) => void+ sendVideoLiveNewState: (video: MVideo) => void+ }+plugin: {// PeerTube >= 3.2getBaseStaticRoute: () => string