aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorkontrollanten <6680299+kontrollanten@users.noreply.github.com>2022-10-10 15:18:31 +0200
committerGitHub <noreply@github.com>2022-10-10 15:18:31 +0200
commitc43ed8e8624383db5a0cf22b210cee202bae323c (patch)
treed2d2c3d06a0b15048bf1e1893b2bc8d07ed2214d
parent213bb3bb58b719679d533811b2e2c3852a3dd067 (diff)
downloadPeerTube-c43ed8e8624383db5a0cf22b210cee202bae323c.tar.gz
PeerTube-c43ed8e8624383db5a0cf22b210cee202bae323c.tar.zst
PeerTube-c43ed8e8624383db5a0cf22b210cee202bae323c.zip
Expose PeerTube socket to plugins (#5239)
* server(pluginHelpers): add socket * test(plugins): add socket cases * fixes after review * Update plugin-helpers.ts * Update plugin-helpers.ts
-rw-r--r--server/lib/plugins/plugin-helpers-builder.ts17
-rw-r--r--server/tests/fixtures/peertube-plugin-test-four/main.js16
-rw-r--r--server/tests/plugins/plugin-helpers.ts27
-rw-r--r--server/types/plugins/register-server-option.model.ts7
4 files changed, 65 insertions, 2 deletions
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts
index 4e799b3d4..35945422c 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'
13import { UserModel } from '@server/models/user/user' 13import { UserModel } from '@server/models/user/user'
14import { VideoModel } from '@server/models/video/video' 14import { VideoModel } from '@server/models/video/video'
15import { VideoBlacklistModel } from '@server/models/video/video-blacklist' 15import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
16import { MPlugin } from '@server/types/models' 16import { MPlugin, MVideo, UserNotificationModelForApi } from '@server/types/models'
17import { PeerTubeHelpers } from '@server/types/plugins' 17import { PeerTubeHelpers } from '@server/types/plugins'
18import { VideoBlacklistCreate, VideoStorage } from '@shared/models' 18import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
19import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' 19import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
20import { ServerConfigManager } from '../server-config-manager' 20import { ServerConfigManager } from '../server-config-manager'
21import { blacklistVideo, unblacklistVideo } from '../video-blacklist' 21import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
22import { VideoPathManager } from '../video-path-manager' 22import { VideoPathManager } from '../video-path-manager'
23import { PeerTubeSocket } from '../peertube-socket'
23 24
24function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { 25function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
25 const logger = buildPluginLogger(npmName) 26 const logger = buildPluginLogger(npmName)
@@ -35,6 +36,8 @@ function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHel
35 36
36 const plugin = buildPluginRelatedHelpers(pluginModel, npmName) 37 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
37 38
39 const socket = buildSocketHelpers()
40
38 const user = buildUserHelpers() 41 const user = buildUserHelpers()
39 42
40 return { 43 return {
@@ -45,6 +48,7 @@ function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHel
45 moderation, 48 moderation,
46 plugin, 49 plugin,
47 server, 50 server,
51 socket,
48 user 52 user
49 } 53 }
50} 54}
@@ -218,6 +222,17 @@ function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
218 } 222 }
219} 223}
220 224
225function buildSocketHelpers () {
226 return {
227 sendNotification: (userId: number, notification: UserNotificationModelForApi) => {
228 PeerTubeSocket.Instance.sendNotification(userId, notification)
229 },
230 sendVideoLiveNewState: (video: MVideo) => {
231 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
232 }
233 }
234}
235
221function buildUserHelpers () { 236function buildUserHelpers () {
222 return { 237 return {
223 loadById: (id: number) => { 238 loadById: (id: number) => {
diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js
index 5194e3e02..3e848c49e 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 ({
128 128
129 return res.json(result) 129 return res.json(result)
130 }) 130 })
131
132 router.post('/send-notification', async (req, res) => {
133 peertubeHelpers.socket.sendNotification(req.body.userId, {
134 type: 1,
135 userId: req.body.userId
136 })
137
138 return res.sendStatus(201)
139 })
140
141 router.post('/send-video-live-new-state/:uuid', async (req, res) => {
142 const video = await peertubeHelpers.videos.loadByIdOrUUID(req.params.uuid)
143 peertubeHelpers.socket.sendVideoLiveNewState(video)
144
145 return res.sendStatus(201)
146 })
131 } 147 }
132 148
133} 149}
diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts
index 955d7ddfd..31c18350a 100644
--- a/server/tests/plugins/plugin-helpers.ts
+++ b/server/tests/plugins/plugin-helpers.ts
@@ -83,6 +83,33 @@ describe('Test plugin helpers', function () {
83 }) 83 })
84 }) 84 })
85 85
86 describe('Socket', function () {
87
88 it('Should sendNotification without any exceptions', async () => {
89 const user = await servers[0].users.create({ username: 'notis_redding', password: 'secret1234?' })
90 await makePostBodyRequest({
91 url: servers[0].url,
92 path: '/plugins/test-four/router/send-notification',
93 fields: {
94 userId: user.id
95 },
96 expectedStatus: HttpStatusCode.CREATED_201
97 })
98 })
99
100 it('Should sendVideoLiveNewState without any exceptions', async () => {
101 const res = await servers[0].videos.quickUpload({ name: 'video server 1' })
102
103 await makePostBodyRequest({
104 url: servers[0].url,
105 path: '/plugins/test-four/router/send-video-live-new-state/' + res.uuid,
106 expectedStatus: HttpStatusCode.CREATED_201
107 })
108
109 await servers[0].videos.remove({ id: res.uuid })
110 })
111 })
112
86 describe('Plugin', function () { 113 describe('Plugin', function () {
87 114
88 it('Should get the base static route', async function () { 115 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.ts
index fb4f12a4c..a8b804b63 100644
--- a/server/types/plugins/register-server-option.model.ts
+++ b/server/types/plugins/register-server-option.model.ts
@@ -16,7 +16,7 @@ import {
16 ThumbnailType, 16 ThumbnailType,
17 VideoBlacklistCreate 17 VideoBlacklistCreate
18} from '@shared/models' 18} from '@shared/models'
19import { MUserDefault, MVideoThumbnail } from '../models' 19import { MUserDefault, MVideo, MVideoThumbnail, UserNotificationModelForApi } from '../models'
20import { 20import {
21 RegisterServerAuthExternalOptions, 21 RegisterServerAuthExternalOptions,
22 RegisterServerAuthExternalResult, 22 RegisterServerAuthExternalResult,
@@ -86,6 +86,11 @@ export type PeerTubeHelpers = {
86 getServerActor: () => Promise<ActorModel> 86 getServerActor: () => Promise<ActorModel>
87 } 87 }
88 88
89 socket: {
90 sendNotification: (userId: number, notification: UserNotificationModelForApi) => void
91 sendVideoLiveNewState: (video: MVideo) => void
92 }
93
89 plugin: { 94 plugin: {
90 // PeerTube >= 3.2 95 // PeerTube >= 3.2
91 getBaseStaticRoute: () => string 96 getBaseStaticRoute: () => string