]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/plugin-helpers-builder.ts
7b1def6e30c9e171f8703f4751047374f8fbbfed
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
1 import express from 'express'
2 import { Server } from 'http'
3 import { join } from 'path'
4 import { ffprobePromise } from '@server/helpers/ffmpeg/ffprobe-utils'
5 import { buildLogger } from '@server/helpers/logger'
6 import { CONFIG } from '@server/initializers/config'
7 import { WEBSERVER } from '@server/initializers/constants'
8 import { sequelizeTypescript } from '@server/initializers/database'
9 import { AccountModel } from '@server/models/account/account'
10 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
11 import { getServerActor } from '@server/models/application/application'
12 import { ServerModel } from '@server/models/server/server'
13 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
14 import { UserModel } from '@server/models/user/user'
15 import { VideoModel } from '@server/models/video/video'
16 import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
17 import { MPlugin, MVideo, UserNotificationModelForApi } from '@server/types/models'
18 import { PeerTubeHelpers } from '@server/types/plugins'
19 import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
20 import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
21 import { PeerTubeSocket } from '../peertube-socket'
22 import { ServerConfigManager } from '../server-config-manager'
23 import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
24 import { VideoPathManager } from '../video-path-manager'
25
26 function buildPluginHelpers (httpServer: Server, pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
27 const logger = buildPluginLogger(npmName)
28
29 const database = buildDatabaseHelpers()
30 const videos = buildVideosHelpers()
31
32 const config = buildConfigHelpers()
33
34 const server = buildServerHelpers(httpServer)
35
36 const moderation = buildModerationHelpers()
37
38 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
39
40 const socket = buildSocketHelpers()
41
42 const user = buildUserHelpers()
43
44 return {
45 logger,
46 database,
47 videos,
48 config,
49 moderation,
50 plugin,
51 server,
52 socket,
53 user
54 }
55 }
56
57 export {
58 buildPluginHelpers
59 }
60
61 // ---------------------------------------------------------------------------
62
63 function buildPluginLogger (npmName: string) {
64 return buildLogger(npmName)
65 }
66
67 function buildDatabaseHelpers () {
68 return {
69 query: sequelizeTypescript.query.bind(sequelizeTypescript)
70 }
71 }
72
73 function buildServerHelpers (httpServer: Server) {
74 return {
75 getHTTPServer: () => httpServer,
76
77 getServerActor: () => getServerActor()
78 }
79 }
80
81 function buildVideosHelpers () {
82 return {
83 loadByUrl: (url: string) => {
84 return VideoModel.loadByUrl(url)
85 },
86
87 loadByIdOrUUID: (id: number | string) => {
88 return VideoModel.load(id)
89 },
90
91 removeVideo: (id: number) => {
92 return sequelizeTypescript.transaction(async t => {
93 const video = await VideoModel.loadFull(id, t)
94
95 await video.destroy({ transaction: t })
96 })
97 },
98
99 ffprobe: (path: string) => {
100 return ffprobePromise(path)
101 },
102
103 getFiles: async (id: number | string) => {
104 const video = await VideoModel.loadFull(id)
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,
136 url: t.getFileUrl(video),
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 }
151 }
152 }
153 }
154
155 function 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 }) => {
188 const video = await VideoModel.loadFull(options.videoIdOrUUID)
189 if (!video) return
190
191 await blacklistVideo(video, options.createOptions)
192 },
193
194 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
195 const video = await VideoModel.loadFull(options.videoIdOrUUID)
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
206 function buildConfigHelpers () {
207 return {
208 getWebserverUrl () {
209 return WEBSERVER.URL
210 },
211
212 getServerConfig () {
213 return ServerConfigManager.Instance.getServerConfig()
214 }
215 }
216 }
217
218 function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
219 return {
220 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
221
222 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
223
224 getBaseWebSocketRoute: () => `/plugins/${plugin.name}/${plugin.version}/ws/`,
225
226 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
227 }
228 }
229
230 function buildSocketHelpers () {
231 return {
232 sendNotification: (userId: number, notification: UserNotificationModelForApi) => {
233 PeerTubeSocket.Instance.sendNotification(userId, notification)
234 },
235 sendVideoLiveNewState: (video: MVideo) => {
236 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
237 }
238 }
239 }
240
241 function buildUserHelpers () {
242 return {
243 loadById: (id: number) => {
244 return UserModel.loadByIdFull(id)
245 },
246
247 getAuthUser: (res: express.Response) => {
248 const user = res.locals.oauth?.token?.User
249 if (!user) return undefined
250
251 return UserModel.loadByIdFull(user.id)
252 }
253 }
254 }