1 import express from 'express'
2 import { join } from 'path'
3 import { buildLogger } from '@server/helpers/logger'
4 import { CONFIG } from '@server/initializers/config'
5 import { WEBSERVER } from '@server/initializers/constants'
6 import { sequelizeTypescript } from '@server/initializers/database'
7 import { AccountModel } from '@server/models/account/account'
8 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
9 import { getServerActor } from '@server/models/application/application'
10 import { ServerModel } from '@server/models/server/server'
11 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
12 import { UserModel } from '@server/models/user/user'
13 import { VideoModel } from '@server/models/video/video'
14 import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
15 import { MPlugin } from '@server/types/models'
16 import { PeerTubeHelpers } from '@server/types/plugins'
17 import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
18 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
19 import { ServerConfigManager } from '../server-config-manager'
20 import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
21 import { VideoPathManager } from '../video-path-manager'
23 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
24 const logger = buildPluginLogger(npmName)
26 const database = buildDatabaseHelpers()
27 const videos = buildVideosHelpers()
29 const config = buildConfigHelpers()
31 const server = buildServerHelpers()
33 const moderation = buildModerationHelpers()
35 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
37 const user = buildUserHelpers()
55 // ---------------------------------------------------------------------------
57 function buildPluginLogger (npmName: string) {
58 return buildLogger(npmName)
61 function buildDatabaseHelpers () {
63 query: sequelizeTypescript.query.bind(sequelizeTypescript)
67 function buildServerHelpers () {
69 getServerActor: () => getServerActor()
73 function buildVideosHelpers () {
75 loadByUrl: (url: string) => {
76 return VideoModel.loadByUrl(url)
79 loadByIdOrUUID: (id: number | string) => {
80 return VideoModel.load(id)
83 removeVideo: (id: number) => {
84 return sequelizeTypescript.transaction(async t => {
85 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
87 await video.destroy({ transaction: t })
91 getFiles: async (id: number | string) => {
92 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
93 if (!video) return undefined
95 const webtorrentVideoFiles = (video.VideoFiles || []).map(f => ({
96 path: f.storage === VideoStorage.FILE_SYSTEM
97 ? VideoPathManager.Instance.getFSVideoFileOutputPath(video, f)
99 url: f.getFileUrl(video),
101 resolution: f.resolution,
106 const hls = video.getHLSPlaylist()
108 const hlsVideoFiles = hls
109 ? (video.getHLSPlaylist().VideoFiles || []).map(f => {
111 path: f.storage === VideoStorage.FILE_SYSTEM
112 ? VideoPathManager.Instance.getFSVideoFileOutputPath(hls, f)
114 url: f.getFileUrl(video),
115 resolution: f.resolution,
122 const thumbnails = video.Thumbnails.map(t => ({
124 url: t.getFileUrl(video),
130 videoFiles: webtorrentVideoFiles
134 videoFiles: hlsVideoFiles
143 function buildModerationHelpers () {
145 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
146 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
148 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
151 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
152 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
153 if (!serverBlock) return
155 await removeServerFromBlocklist(serverBlock)
158 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
159 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
160 if (!accountToBlock) return
162 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
165 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
166 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
167 if (!targetAccount) return
169 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
170 if (!accountBlock) return
172 await removeAccountFromBlocklist(accountBlock)
175 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
176 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
179 await blacklistVideo(video, options.createOptions)
182 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
183 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
186 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
187 if (!videoBlacklist) return
189 await unblacklistVideo(videoBlacklist, video)
194 function buildConfigHelpers () {
201 return ServerConfigManager.Instance.getServerConfig()
206 function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
208 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
210 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
212 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
216 function buildUserHelpers () {
218 getAuthUser: (res: express.Response) => {
219 const user = res.locals.oauth?.token?.User
220 if (!user) return undefined
222 return UserModel.loadByIdFull(user.id)