]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
302eba0d | 2 | import { join } from 'path' |
c729caf6 | 3 | import { ffprobePromise } from '@server/helpers/ffmpeg/ffprobe-utils' |
1b05d82d | 4 | import { buildLogger } from '@server/helpers/logger' |
302eba0d | 5 | import { CONFIG } from '@server/initializers/config' |
5a7eecdd | 6 | import { WEBSERVER } from '@server/initializers/constants' |
302eba0d C |
7 | import { sequelizeTypescript } from '@server/initializers/database' |
8 | import { AccountModel } from '@server/models/account/account' | |
9 | import { AccountBlocklistModel } from '@server/models/account/account-blocklist' | |
80fdaf06 | 10 | import { getServerActor } from '@server/models/application/application' |
302eba0d | 11 | import { ServerModel } from '@server/models/server/server' |
80fdaf06 | 12 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' |
2e9c7877 | 13 | import { UserModel } from '@server/models/user/user' |
302eba0d | 14 | import { VideoModel } from '@server/models/video/video' |
80fdaf06 | 15 | import { VideoBlacklistModel } from '@server/models/video/video-blacklist' |
22820226 | 16 | import { MPlugin } from '@server/types/models' |
302eba0d | 17 | import { PeerTubeHelpers } from '@server/types/plugins' |
2e9c7877 | 18 | import { VideoBlacklistCreate, VideoStorage } from '@shared/models' |
302eba0d | 19 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' |
2539932e | 20 | import { ServerConfigManager } from '../server-config-manager' |
302eba0d | 21 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' |
2e9c7877 | 22 | import { VideoPathManager } from '../video-path-manager' |
bc0d801b | 23 | |
22820226 | 24 | function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { |
1b05d82d C |
25 | const logger = buildPluginLogger(npmName) |
26 | ||
27 | const database = buildDatabaseHelpers() | |
ab3ead3a | 28 | const videos = buildVideosHelpers() |
bc0d801b | 29 | |
5a7eecdd C |
30 | const config = buildConfigHelpers() |
31 | ||
80fdaf06 C |
32 | const server = buildServerHelpers() |
33 | ||
34 | const moderation = buildModerationHelpers() | |
35 | ||
302eba0d C |
36 | const plugin = buildPluginRelatedHelpers(pluginModel, npmName) |
37 | ||
38 | const user = buildUserHelpers() | |
22820226 | 39 | |
bc0d801b | 40 | return { |
1b05d82d | 41 | logger, |
ab3ead3a | 42 | database, |
5a7eecdd | 43 | videos, |
80fdaf06 C |
44 | config, |
45 | moderation, | |
22820226 | 46 | plugin, |
302eba0d C |
47 | server, |
48 | user | |
bc0d801b C |
49 | } |
50 | } | |
51 | ||
52 | export { | |
53 | buildPluginHelpers | |
54 | } | |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | ||
1b05d82d | 58 | function buildPluginLogger (npmName: string) { |
bc0d801b C |
59 | return buildLogger(npmName) |
60 | } | |
1b05d82d C |
61 | |
62 | function buildDatabaseHelpers () { | |
63 | return { | |
64 | query: sequelizeTypescript.query.bind(sequelizeTypescript) | |
65 | } | |
66 | } | |
ab3ead3a | 67 | |
80fdaf06 C |
68 | function buildServerHelpers () { |
69 | return { | |
70 | getServerActor: () => getServerActor() | |
71 | } | |
72 | } | |
73 | ||
ab3ead3a C |
74 | function buildVideosHelpers () { |
75 | return { | |
80fdaf06 C |
76 | loadByUrl: (url: string) => { |
77 | return VideoModel.loadByUrl(url) | |
78 | }, | |
79 | ||
6559da28 C |
80 | loadByIdOrUUID: (id: number | string) => { |
81 | return VideoModel.load(id) | |
82 | }, | |
83 | ||
ab3ead3a C |
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 | }) | |
2e9c7877 | 90 | }, |
754c52b9 C |
91 | |
92 | ffprobe: (path: string) => { | |
93 | return ffprobePromise(path) | |
94 | }, | |
2e9c7877 C |
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 | } | |
ab3ead3a C |
144 | } |
145 | } | |
146 | } | |
5a7eecdd | 147 | |
80fdaf06 C |
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 | ||
5a7eecdd C |
199 | function buildConfigHelpers () { |
200 | return { | |
201 | getWebserverUrl () { | |
202 | return WEBSERVER.URL | |
22820226 C |
203 | }, |
204 | ||
205 | getServerConfig () { | |
2539932e | 206 | return ServerConfigManager.Instance.getServerConfig() |
5a7eecdd C |
207 | } |
208 | } | |
209 | } | |
22820226 | 210 | |
302eba0d C |
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 () { | |
22820226 | 222 | return { |
b31d7262 C |
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 | } | |
22820226 C |
229 | } |
230 | } |