]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/plugin-helpers-builder.ts
bea0f89592c96d3ec7a8d5975ded5c0847849a4e
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
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'
22
23 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
24 const logger = buildPluginLogger(npmName)
25
26 const database = buildDatabaseHelpers()
27 const videos = buildVideosHelpers()
28
29 const config = buildConfigHelpers()
30
31 const server = buildServerHelpers()
32
33 const moderation = buildModerationHelpers()
34
35 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
36
37 const user = buildUserHelpers()
38
39 return {
40 logger,
41 database,
42 videos,
43 config,
44 moderation,
45 plugin,
46 server,
47 user
48 }
49 }
50
51 export {
52 buildPluginHelpers
53 }
54
55 // ---------------------------------------------------------------------------
56
57 function buildPluginLogger (npmName: string) {
58 return buildLogger(npmName)
59 }
60
61 function buildDatabaseHelpers () {
62 return {
63 query: sequelizeTypescript.query.bind(sequelizeTypescript)
64 }
65 }
66
67 function buildServerHelpers () {
68 return {
69 getServerActor: () => getServerActor()
70 }
71 }
72
73 function buildVideosHelpers () {
74 return {
75 loadByUrl: (url: string) => {
76 return VideoModel.loadByUrl(url)
77 },
78
79 loadByIdOrUUID: (id: number | string) => {
80 return VideoModel.load(id)
81 },
82
83 removeVideo: (id: number) => {
84 return sequelizeTypescript.transaction(async t => {
85 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
86
87 await video.destroy({ transaction: t })
88 })
89 },
90
91 getFiles: async (id: number | string) => {
92 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id)
93 if (!video) return undefined
94
95 const webtorrentVideoFiles = (video.VideoFiles || []).map(f => ({
96 path: f.storage === VideoStorage.FILE_SYSTEM
97 ? VideoPathManager.Instance.getFSVideoFileOutputPath(video, f)
98 : null,
99 url: f.getFileUrl(video),
100
101 resolution: f.resolution,
102 size: f.size,
103 fps: f.fps
104 }))
105
106 const hls = video.getHLSPlaylist()
107
108 const hlsVideoFiles = hls
109 ? (video.getHLSPlaylist().VideoFiles || []).map(f => {
110 return {
111 path: f.storage === VideoStorage.FILE_SYSTEM
112 ? VideoPathManager.Instance.getFSVideoFileOutputPath(hls, f)
113 : null,
114 url: f.getFileUrl(video),
115 resolution: f.resolution,
116 size: f.size,
117 fps: f.fps
118 }
119 })
120 : []
121
122 const thumbnails = video.Thumbnails.map(t => ({
123 type: t.type,
124 url: t.getFileUrl(video),
125 path: t.getPath()
126 }))
127
128 return {
129 webtorrent: {
130 videoFiles: webtorrentVideoFiles
131 },
132
133 hls: {
134 videoFiles: hlsVideoFiles
135 },
136
137 thumbnails
138 }
139 }
140 }
141 }
142
143 function buildModerationHelpers () {
144 return {
145 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
146 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
147
148 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
149 },
150
151 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
152 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
153 if (!serverBlock) return
154
155 await removeServerFromBlocklist(serverBlock)
156 },
157
158 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
159 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
160 if (!accountToBlock) return
161
162 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
163 },
164
165 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
166 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
167 if (!targetAccount) return
168
169 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
170 if (!accountBlock) return
171
172 await removeAccountFromBlocklist(accountBlock)
173 },
174
175 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
176 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
177 if (!video) return
178
179 await blacklistVideo(video, options.createOptions)
180 },
181
182 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
183 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
184 if (!video) return
185
186 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
187 if (!videoBlacklist) return
188
189 await unblacklistVideo(videoBlacklist, video)
190 }
191 }
192 }
193
194 function buildConfigHelpers () {
195 return {
196 getWebserverUrl () {
197 return WEBSERVER.URL
198 },
199
200 getServerConfig () {
201 return ServerConfigManager.Instance.getServerConfig()
202 }
203 }
204 }
205
206 function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
207 return {
208 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
209
210 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
211
212 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
213 }
214 }
215
216 function buildUserHelpers () {
217 return {
218 getAuthUser: (res: express.Response) => {
219 const user = res.locals.oauth?.token?.User
220 if (!user) return undefined
221
222 return UserModel.loadByIdFull(user.id)
223 }
224 }
225 }