]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/plugins/plugin-helpers-builder.ts
Expose PeerTube socket to plugins (#5239)
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
index b76c0a8a4894ba46bc0c3a6a00a9c1e6baa8e651..35945422c24e1db9900754390e14977339562588 100644 (file)
@@ -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): PeerTubeHel
 
   const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
 
+  const socket = buildSocketHelpers()
+
   const user = buildUserHelpers()
 
   return {
@@ -45,6 +48,7 @@ function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHel
     moderation,
     plugin,
     server,
+    socket,
     user
   }
 }
@@ -218,8 +222,23 @@ 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) => {
+      return UserModel.loadByIdFull(id)
+    },
+
     getAuthUser: (res: express.Response) => {
       const user = res.locals.oauth?.token?.User
       if (!user) return undefined