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