aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins/plugin-helpers-builder.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib/plugins/plugin-helpers-builder.ts')
-rw-r--r--server/lib/plugins/plugin-helpers-builder.ts262
1 files changed, 0 insertions, 262 deletions
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts
deleted file mode 100644
index b4e3eece4..000000000
--- a/server/lib/plugins/plugin-helpers-builder.ts
+++ /dev/null
@@ -1,262 +0,0 @@
1import express from 'express'
2import { Server } from 'http'
3import { join } from 'path'
4import { buildLogger } from '@server/helpers/logger'
5import { CONFIG } from '@server/initializers/config'
6import { WEBSERVER } from '@server/initializers/constants'
7import { sequelizeTypescript } from '@server/initializers/database'
8import { AccountModel } from '@server/models/account/account'
9import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
10import { getServerActor } from '@server/models/application/application'
11import { ServerModel } from '@server/models/server/server'
12import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
13import { UserModel } from '@server/models/user/user'
14import { VideoModel } from '@server/models/video/video'
15import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
16import { MPlugin, MVideo, UserNotificationModelForApi } from '@server/types/models'
17import { PeerTubeHelpers } from '@server/types/plugins'
18import { ffprobePromise } from '@shared/ffmpeg'
19import { VideoBlacklistCreate, VideoStorage } from '@shared/models'
20import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
21import { PeerTubeSocket } from '../peertube-socket'
22import { ServerConfigManager } from '../server-config-manager'
23import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
24import { VideoPathManager } from '../video-path-manager'
25
26function 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
57export {
58 buildPluginHelpers
59}
60
61// ---------------------------------------------------------------------------
62
63function buildPluginLogger (npmName: string) {
64 return buildLogger(npmName)
65}
66
67function buildDatabaseHelpers () {
68 return {
69 query: sequelizeTypescript.query.bind(sequelizeTypescript)
70 }
71}
72
73function buildServerHelpers (httpServer: Server) {
74 return {
75 getHTTPServer: () => httpServer,
76
77 getServerActor: () => getServerActor()
78 }
79}
80
81function 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 webVideoFiles = (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.getOriginFileUrl(video),
137 path: t.getPath()
138 }))
139
140 return {
141 webtorrent: { // TODO: remove in v7
142 videoFiles: webVideoFiles
143 },
144
145 webVideo: {
146 videoFiles: webVideoFiles
147 },
148
149 hls: {
150 videoFiles: hlsVideoFiles
151 },
152
153 thumbnails
154 }
155 }
156 }
157}
158
159function buildModerationHelpers () {
160 return {
161 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
162 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
163
164 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
165 },
166
167 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
168 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
169 if (!serverBlock) return
170
171 await removeServerFromBlocklist(serverBlock)
172 },
173
174 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
175 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
176 if (!accountToBlock) return
177
178 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
179 },
180
181 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
182 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
183 if (!targetAccount) return
184
185 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
186 if (!accountBlock) return
187
188 await removeAccountFromBlocklist(accountBlock)
189 },
190
191 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
192 const video = await VideoModel.loadFull(options.videoIdOrUUID)
193 if (!video) return
194
195 await blacklistVideo(video, options.createOptions)
196 },
197
198 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
199 const video = await VideoModel.loadFull(options.videoIdOrUUID)
200 if (!video) return
201
202 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
203 if (!videoBlacklist) return
204
205 await unblacklistVideo(videoBlacklist, video)
206 }
207 }
208}
209
210function buildConfigHelpers () {
211 return {
212 getWebserverUrl () {
213 return WEBSERVER.URL
214 },
215
216 getServerListeningConfig () {
217 return { hostname: CONFIG.LISTEN.HOSTNAME, port: CONFIG.LISTEN.PORT }
218 },
219
220 getServerConfig () {
221 return ServerConfigManager.Instance.getServerConfig()
222 }
223 }
224}
225
226function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
227 return {
228 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
229
230 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
231
232 getBaseWebSocketRoute: () => `/plugins/${plugin.name}/${plugin.version}/ws/`,
233
234 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
235 }
236}
237
238function buildSocketHelpers () {
239 return {
240 sendNotification: (userId: number, notification: UserNotificationModelForApi) => {
241 PeerTubeSocket.Instance.sendNotification(userId, notification)
242 },
243 sendVideoLiveNewState: (video: MVideo) => {
244 PeerTubeSocket.Instance.sendVideoLiveNewState(video)
245 }
246 }
247}
248
249function buildUserHelpers () {
250 return {
251 loadById: (id: number) => {
252 return UserModel.loadByIdFull(id)
253 },
254
255 getAuthUser: (res: express.Response) => {
256 const user = res.locals.oauth?.token?.User || res.locals.videoFileToken?.user
257 if (!user) return undefined
258
259 return UserModel.loadByIdFull(user.id)
260 }
261 }
262}