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