1 import express from 'express'
2 import { join } from 'path'
3 import { ffprobePromise } from '@server/helpers/ffmpeg/ffprobe-utils'
4 import { buildLogger } from '@server/helpers/logger'
5 import { CONFIG } from '@server/initializers/config'
6 import { WEBSERVER } from '@server/initializers/constants'
7 import { sequelizeTypescript } from '@server/initializers/database'
8 import { AccountModel } from '@server/models/account/account'
9 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
10 import { getServerActor } from '@server/models/application/application'
11 import { ServerModel } from '@server/models/server/server'
12 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
13 import { UserModel } from '@server/models/user/user'
14 import { VideoModel } from '@server/models/video/video'
15 import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
16 import { MPlugin, MVideo, UserNotificationModelForApi } from '@server/types/models'
17 import { PeerTubeHelpers } from '@server/types/plugins'
18 import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
19 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
20 import { ServerConfigManager } from '../server-config-manager'
21 import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
22 import { VideoPathManager } from '../video-path-manager'
23 import { PeerTubeSocket } from '../peertube-socket'
25 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
26 const logger = buildPluginLogger(npmName)
28 const database = buildDatabaseHelpers()
29 const videos = buildVideosHelpers()
31 const config = buildConfigHelpers()
33 const server = buildServerHelpers()
35 const moderation = buildModerationHelpers()
37 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
39 const socket = buildSocketHelpers()
41 const user = buildUserHelpers()
60 // ---------------------------------------------------------------------------
62 function buildPluginLogger (npmName: string) {
63 return buildLogger(npmName)
66 function buildDatabaseHelpers () {
68 query: sequelizeTypescript.query.bind(sequelizeTypescript)
72 function buildServerHelpers () {
74 getServerActor: () => getServerActor()
78 function buildVideosHelpers () {
80 loadByUrl: (url: string) => {
81 return VideoModel.loadByUrl(url)
84 loadByIdOrUUID: (id: number | string) => {
85 return VideoModel.load(id)
88 removeVideo: (id: number) => {
89 return sequelizeTypescript.transaction(async t => {
90 const video = await VideoModel.loadFull(id, t)
92 await video.destroy({ transaction: t })
96 ffprobe: (path: string) => {
97 return ffprobePromise(path)
100 getFiles: async (id: number | string) => {
101 const video = await VideoModel.loadFull(id)
102 if (!video) return undefined
104 const webtorrentVideoFiles = (video.VideoFiles || []).map(f => ({
105 path: f.storage === VideoStorage.FILE_SYSTEM
106 ? VideoPathManager.Instance.getFSVideoFileOutputPath(video, f)
108 url: f.getFileUrl(video),
110 resolution: f.resolution,
115 const hls = video.getHLSPlaylist()
117 const hlsVideoFiles = hls
118 ? (video.getHLSPlaylist().VideoFiles || []).map(f => {
120 path: f.storage === VideoStorage.FILE_SYSTEM
121 ? VideoPathManager.Instance.getFSVideoFileOutputPath(hls, f)
123 url: f.getFileUrl(video),
124 resolution: f.resolution,
131 const thumbnails = video.Thumbnails.map(t => ({
133 url: t.getFileUrl(video),
139 videoFiles: webtorrentVideoFiles
143 videoFiles: hlsVideoFiles
152 function buildModerationHelpers () {
154 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
155 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
157 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
160 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
161 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
162 if (!serverBlock) return
164 await removeServerFromBlocklist(serverBlock)
167 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
168 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
169 if (!accountToBlock) return
171 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
174 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
175 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
176 if (!targetAccount) return
178 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
179 if (!accountBlock) return
181 await removeAccountFromBlocklist(accountBlock)
184 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
185 const video = await VideoModel.loadFull(options.videoIdOrUUID)
188 await blacklistVideo(video, options.createOptions)
191 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
192 const video = await VideoModel.loadFull(options.videoIdOrUUID)
195 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
196 if (!videoBlacklist) return
198 await unblacklistVideo(videoBlacklist, video)
203 function buildConfigHelpers () {
210 return ServerConfigManager.Instance.getServerConfig()
215 function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
217 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
219 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
221 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
225 function buildSocketHelpers () {
227 sendNotification: (userId: number, notification: UserNotificationModelForApi) => {
228 PeerTubeSocket.Instance.sendNotification(userId, notification)
230 sendVideoLiveNewState: (video: MVideo) => {
231 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
236 function buildUserHelpers () {
238 loadById: (id: number) => {
239 return UserModel.loadByIdFull(id)
242 getAuthUser: (res: express.Response) => {
243 const user = res.locals.oauth?.token?.User
244 if (!user) return undefined
246 return UserModel.loadByIdFull(user.id)