]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/plugins/plugin-helpers-builder.ts
feat(plugins): add p2p-media-loader options filter (#5318)
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
CommitLineData
41fb13c3 1import express from 'express'
302eba0d 2import { join } from 'path'
c729caf6 3import { ffprobePromise } from '@server/helpers/ffmpeg/ffprobe-utils'
1b05d82d 4import { buildLogger } from '@server/helpers/logger'
302eba0d 5import { CONFIG } from '@server/initializers/config'
5a7eecdd 6import { WEBSERVER } from '@server/initializers/constants'
302eba0d
C
7import { sequelizeTypescript } from '@server/initializers/database'
8import { AccountModel } from '@server/models/account/account'
9import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
80fdaf06 10import { getServerActor } from '@server/models/application/application'
302eba0d 11import { ServerModel } from '@server/models/server/server'
80fdaf06 12import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
2e9c7877 13import { UserModel } from '@server/models/user/user'
302eba0d 14import { VideoModel } from '@server/models/video/video'
80fdaf06 15import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
c43ed8e8 16import { MPlugin, MVideo, UserNotificationModelForApi } from '@server/types/models'
302eba0d 17import { PeerTubeHelpers } from '@server/types/plugins'
2e9c7877 18import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
302eba0d 19import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
2539932e 20import { ServerConfigManager } from '../server-config-manager'
302eba0d 21import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
2e9c7877 22import { VideoPathManager } from '../video-path-manager'
c43ed8e8 23import { PeerTubeSocket } from '../peertube-socket'
bc0d801b 24
22820226 25function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
1b05d82d
C
26 const logger = buildPluginLogger(npmName)
27
28 const database = buildDatabaseHelpers()
ab3ead3a 29 const videos = buildVideosHelpers()
bc0d801b 30
5a7eecdd
C
31 const config = buildConfigHelpers()
32
80fdaf06
C
33 const server = buildServerHelpers()
34
35 const moderation = buildModerationHelpers()
36
302eba0d
C
37 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
38
c43ed8e8 39 const socket = buildSocketHelpers()
40
302eba0d 41 const user = buildUserHelpers()
22820226 42
bc0d801b 43 return {
1b05d82d 44 logger,
ab3ead3a 45 database,
5a7eecdd 46 videos,
80fdaf06
C
47 config,
48 moderation,
22820226 49 plugin,
302eba0d 50 server,
c43ed8e8 51 socket,
302eba0d 52 user
bc0d801b
C
53 }
54}
55
56export {
57 buildPluginHelpers
58}
59
60// ---------------------------------------------------------------------------
61
1b05d82d 62function buildPluginLogger (npmName: string) {
bc0d801b
C
63 return buildLogger(npmName)
64}
1b05d82d
C
65
66function buildDatabaseHelpers () {
67 return {
68 query: sequelizeTypescript.query.bind(sequelizeTypescript)
69 }
70}
ab3ead3a 71
80fdaf06
C
72function buildServerHelpers () {
73 return {
74 getServerActor: () => getServerActor()
75 }
76}
77
ab3ead3a
C
78function buildVideosHelpers () {
79 return {
80fdaf06
C
80 loadByUrl: (url: string) => {
81 return VideoModel.loadByUrl(url)
82 },
83
6559da28
C
84 loadByIdOrUUID: (id: number | string) => {
85 return VideoModel.load(id)
86 },
87
ab3ead3a
C
88 removeVideo: (id: number) => {
89 return sequelizeTypescript.transaction(async t => {
4fae2b1f 90 const video = await VideoModel.loadFull(id, t)
ab3ead3a
C
91
92 await video.destroy({ transaction: t })
93 })
2e9c7877 94 },
754c52b9
C
95
96 ffprobe: (path: string) => {
97 return ffprobePromise(path)
98 },
2e9c7877
C
99
100 getFiles: async (id: number | string) => {
4fae2b1f 101 const video = await VideoModel.loadFull(id)
2e9c7877
C
102 if (!video) return undefined
103
104 const webtorrentVideoFiles = (video.VideoFiles || []).map(f => ({
105 path: f.storage === VideoStorage.FILE_SYSTEM
106 ? VideoPathManager.Instance.getFSVideoFileOutputPath(video, f)
107 : null,
108 url: f.getFileUrl(video),
109
110 resolution: f.resolution,
111 size: f.size,
112 fps: f.fps
113 }))
114
115 const hls = video.getHLSPlaylist()
116
117 const hlsVideoFiles = hls
118 ? (video.getHLSPlaylist().VideoFiles || []).map(f => {
119 return {
120 path: f.storage === VideoStorage.FILE_SYSTEM
121 ? VideoPathManager.Instance.getFSVideoFileOutputPath(hls, f)
122 : null,
123 url: f.getFileUrl(video),
124 resolution: f.resolution,
125 size: f.size,
126 fps: f.fps
127 }
128 })
129 : []
130
131 const thumbnails = video.Thumbnails.map(t => ({
132 type: t.type,
133 url: t.getFileUrl(video),
134 path: t.getPath()
135 }))
136
137 return {
138 webtorrent: {
139 videoFiles: webtorrentVideoFiles
140 },
141
142 hls: {
143 videoFiles: hlsVideoFiles
144 },
145
146 thumbnails
147 }
ab3ead3a
C
148 }
149 }
150}
5a7eecdd 151
80fdaf06
C
152function buildModerationHelpers () {
153 return {
154 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
155 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
156
157 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
158 },
159
160 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
161 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
162 if (!serverBlock) return
163
164 await removeServerFromBlocklist(serverBlock)
165 },
166
167 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
168 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
169 if (!accountToBlock) return
170
171 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
172 },
173
174 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
175 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
176 if (!targetAccount) return
177
178 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
179 if (!accountBlock) return
180
181 await removeAccountFromBlocklist(accountBlock)
182 },
183
184 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
4fae2b1f 185 const video = await VideoModel.loadFull(options.videoIdOrUUID)
80fdaf06
C
186 if (!video) return
187
188 await blacklistVideo(video, options.createOptions)
189 },
190
191 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
4fae2b1f 192 const video = await VideoModel.loadFull(options.videoIdOrUUID)
80fdaf06
C
193 if (!video) return
194
195 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
196 if (!videoBlacklist) return
197
198 await unblacklistVideo(videoBlacklist, video)
199 }
200 }
201}
202
5a7eecdd
C
203function buildConfigHelpers () {
204 return {
205 getWebserverUrl () {
206 return WEBSERVER.URL
22820226
C
207 },
208
209 getServerConfig () {
2539932e 210 return ServerConfigManager.Instance.getServerConfig()
5a7eecdd
C
211 }
212 }
213}
22820226 214
302eba0d
C
215function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
216 return {
217 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
218
219 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
220
221 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
222 }
223}
224
c43ed8e8 225function buildSocketHelpers () {
226 return {
227 sendNotification: (userId: number, notification: UserNotificationModelForApi) => {
228 PeerTubeSocket.Instance.sendNotification(userId, notification)
229 },
230 sendVideoLiveNewState: (video: MVideo) => {
231 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
232 }
233 }
234}
235
302eba0d 236function buildUserHelpers () {
22820226 237 return {
22df69fd
C
238 loadById: (id: number) => {
239 return UserModel.loadByIdFull(id)
240 },
241
b31d7262
C
242 getAuthUser: (res: express.Response) => {
243 const user = res.locals.oauth?.token?.User
244 if (!user) return undefined
245
246 return UserModel.loadByIdFull(user.id)
247 }
22820226
C
248 }
249}