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