1 import express from 'express'
2 import { Server } from 'http'
3 import { join } from 'path'
4 import { ffprobePromise } from '@server/helpers/ffmpeg/ffprobe-utils'
5 import { buildLogger } from '@server/helpers/logger'
6 import { CONFIG } from '@server/initializers/config'
7 import { WEBSERVER } from '@server/initializers/constants'
8 import { sequelizeTypescript } from '@server/initializers/database'
9 import { AccountModel } from '@server/models/account/account'
10 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
11 import { getServerActor } from '@server/models/application/application'
12 import { ServerModel } from '@server/models/server/server'
13 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
14 import { UserModel } from '@server/models/user/user'
15 import { VideoModel } from '@server/models/video/video'
16 import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
17 import { MPlugin, MVideo, UserNotificationModelForApi } from '@server/types/models'
18 import { PeerTubeHelpers } from '@server/types/plugins'
19 import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
20 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
21 import { PeerTubeSocket } from '../peertube-socket'
22 import { ServerConfigManager } from '../server-config-manager'
23 import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
24 import { VideoPathManager } from '../video-path-manager'
26 function buildPluginHelpers (httpServer: Server, pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
27 const logger = buildPluginLogger(npmName)
29 const database = buildDatabaseHelpers()
30 const videos = buildVideosHelpers()
32 const config = buildConfigHelpers()
34 const server = buildServerHelpers(httpServer)
36 const moderation = buildModerationHelpers()
38 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
40 const socket = buildSocketHelpers()
42 const user = buildUserHelpers()
61 // ---------------------------------------------------------------------------
63 function buildPluginLogger (npmName: string) {
64 return buildLogger(npmName)
67 function buildDatabaseHelpers () {
69 query: sequelizeTypescript.query.bind(sequelizeTypescript)
73 function buildServerHelpers (httpServer: Server) {
75 getHTTPServer: () => httpServer,
77 getServerActor: () => getServerActor()
81 function buildVideosHelpers () {
83 loadByUrl: (url: string) => {
84 return VideoModel.loadByUrl(url)
87 loadByIdOrUUID: (id: number | string) => {
88 return VideoModel.load(id)
91 removeVideo: (id: number) => {
92 return sequelizeTypescript.transaction(async t => {
93 const video = await VideoModel.loadFull(id, t)
95 await video.destroy({ transaction: t })
99 ffprobe: (path: string) => {
100 return ffprobePromise(path)
103 getFiles: async (id: number | string) => {
104 const video = await VideoModel.loadFull(id)
105 if (!video) return undefined
107 const webtorrentVideoFiles = (video.VideoFiles || []).map(f => ({
108 path: f.storage === VideoStorage.FILE_SYSTEM
109 ? VideoPathManager.Instance.getFSVideoFileOutputPath(video, f)
111 url: f.getFileUrl(video),
113 resolution: f.resolution,
118 const hls = video.getHLSPlaylist()
120 const hlsVideoFiles = hls
121 ? (video.getHLSPlaylist().VideoFiles || []).map(f => {
123 path: f.storage === VideoStorage.FILE_SYSTEM
124 ? VideoPathManager.Instance.getFSVideoFileOutputPath(hls, f)
126 url: f.getFileUrl(video),
127 resolution: f.resolution,
134 const thumbnails = video.Thumbnails.map(t => ({
136 url: t.getFileUrl(video),
142 videoFiles: webtorrentVideoFiles
146 videoFiles: hlsVideoFiles
155 function buildModerationHelpers () {
157 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
158 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
160 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
163 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
164 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
165 if (!serverBlock) return
167 await removeServerFromBlocklist(serverBlock)
170 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
171 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
172 if (!accountToBlock) return
174 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
177 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
178 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
179 if (!targetAccount) return
181 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
182 if (!accountBlock) return
184 await removeAccountFromBlocklist(accountBlock)
187 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
188 const video = await VideoModel.loadFull(options.videoIdOrUUID)
191 await blacklistVideo(video, options.createOptions)
194 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
195 const video = await VideoModel.loadFull(options.videoIdOrUUID)
198 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
199 if (!videoBlacklist) return
201 await unblacklistVideo(videoBlacklist, video)
206 function buildConfigHelpers () {
213 return ServerConfigManager.Instance.getServerConfig()
218 function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
220 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
222 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
224 getBaseWebSocketRoute: () => `/plugins/${plugin.name}/${plugin.version}/ws/`,
226 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
230 function buildSocketHelpers () {
232 sendNotification: (userId: number, notification: UserNotificationModelForApi) => {
233 PeerTubeSocket.Instance.sendNotification(userId, notification)
235 sendVideoLiveNewState: (video: MVideo) => {
236 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
241 function buildUserHelpers () {
243 loadById: (id: number) => {
244 return UserModel.loadByIdFull(id)
247 getAuthUser: (res: express.Response) => {
248 const user = res.locals.oauth?.token?.User
249 if (!user) return undefined
251 return UserModel.loadByIdFull(user.id)