1 import express from 'express'
2 import { join } from 'path'
3 import { ffprobePromise } from '@server/helpers/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 } 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'
24 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
25 const logger = buildPluginLogger(npmName)
27 const database = buildDatabaseHelpers()
28 const videos = buildVideosHelpers()
30 const config = buildConfigHelpers()
32 const server = buildServerHelpers()
34 const moderation = buildModerationHelpers()
36 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
38 const user = buildUserHelpers()
56 // ---------------------------------------------------------------------------
58 function buildPluginLogger (npmName: string) {
59 return buildLogger(npmName)
62 function buildDatabaseHelpers () {
64 query: sequelizeTypescript.query.bind(sequelizeTypescript)
68 function buildServerHelpers () {
70 getServerActor: () => getServerActor()
74 function buildVideosHelpers () {
76 loadByUrl: (url: string) => {
77 return VideoModel.loadByUrl(url)
80 loadByIdOrUUID: (id: number | string) => {
81 return VideoModel.load(id)
84 removeVideo: (id: number) => {
85 return sequelizeTypescript.transaction(async t => {
86 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
88 await video.destroy({ transaction: t })
92 ffprobe: (path: string) => {
93 return ffprobePromise(path)
96 getFiles: async (id: number | string) => {
97 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
98 if (!video) return undefined
100 const webtorrentVideoFiles = (video.VideoFiles || []).map(f => ({
101 path: f.storage === VideoStorage.FILE_SYSTEM
102 ? VideoPathManager.Instance.getFSVideoFileOutputPath(video, f)
104 url: f.getFileUrl(video),
106 resolution: f.resolution,
111 const hls = video.getHLSPlaylist()
113 const hlsVideoFiles = hls
114 ? (video.getHLSPlaylist().VideoFiles || []).map(f => {
116 path: f.storage === VideoStorage.FILE_SYSTEM
117 ? VideoPathManager.Instance.getFSVideoFileOutputPath(hls, f)
119 url: f.getFileUrl(video),
120 resolution: f.resolution,
127 const thumbnails = video.Thumbnails.map(t => ({
129 url: t.getFileUrl(video),
135 videoFiles: webtorrentVideoFiles
139 videoFiles: hlsVideoFiles
148 function buildModerationHelpers () {
150 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
151 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
153 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
156 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
157 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
158 if (!serverBlock) return
160 await removeServerFromBlocklist(serverBlock)
163 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
164 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
165 if (!accountToBlock) return
167 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
170 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
171 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
172 if (!targetAccount) return
174 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
175 if (!accountBlock) return
177 await removeAccountFromBlocklist(accountBlock)
180 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
181 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
184 await blacklistVideo(video, options.createOptions)
187 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
188 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
191 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
192 if (!videoBlacklist) return
194 await unblacklistVideo(videoBlacklist, video)
199 function buildConfigHelpers () {
206 return ServerConfigManager.Instance.getServerConfig()
211 function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
213 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
215 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
217 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
221 function buildUserHelpers () {
223 getAuthUser: (res: express.Response) => {
224 const user = res.locals.oauth?.token?.User
225 if (!user) return undefined
227 return UserModel.loadByIdFull(user.id)