]>
Commit | Line | Data |
---|---|---|
302eba0d C |
1 | import * as express from 'express' |
2 | import { join } from 'path' | |
1b05d82d | 3 | import { buildLogger } from '@server/helpers/logger' |
302eba0d | 4 | import { CONFIG } from '@server/initializers/config' |
5a7eecdd | 5 | import { WEBSERVER } from '@server/initializers/constants' |
302eba0d C |
6 | import { sequelizeTypescript } from '@server/initializers/database' |
7 | import { AccountModel } from '@server/models/account/account' | |
8 | import { AccountBlocklistModel } from '@server/models/account/account-blocklist' | |
80fdaf06 | 9 | import { getServerActor } from '@server/models/application/application' |
302eba0d | 10 | import { ServerModel } from '@server/models/server/server' |
80fdaf06 | 11 | import { ServerBlocklistModel } from '@server/models/server/server-blocklist' |
302eba0d | 12 | import { VideoModel } from '@server/models/video/video' |
80fdaf06 | 13 | import { VideoBlacklistModel } from '@server/models/video/video-blacklist' |
22820226 | 14 | import { MPlugin } from '@server/types/models' |
302eba0d C |
15 | import { PeerTubeHelpers } from '@server/types/plugins' |
16 | import { VideoBlacklistCreate } from '@shared/models' | |
17 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' | |
18 | import { getServerConfig } from '../config' | |
19 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' | |
b31d7262 | 20 | import { UserModel } from '@server/models/account/user' |
bc0d801b | 21 | |
22820226 | 22 | function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { |
1b05d82d C |
23 | const logger = buildPluginLogger(npmName) |
24 | ||
25 | const database = buildDatabaseHelpers() | |
ab3ead3a | 26 | const videos = buildVideosHelpers() |
bc0d801b | 27 | |
5a7eecdd C |
28 | const config = buildConfigHelpers() |
29 | ||
80fdaf06 C |
30 | const server = buildServerHelpers() |
31 | ||
32 | const moderation = buildModerationHelpers() | |
33 | ||
302eba0d C |
34 | const plugin = buildPluginRelatedHelpers(pluginModel, npmName) |
35 | ||
36 | const user = buildUserHelpers() | |
22820226 | 37 | |
bc0d801b | 38 | return { |
1b05d82d | 39 | logger, |
ab3ead3a | 40 | database, |
5a7eecdd | 41 | videos, |
80fdaf06 C |
42 | config, |
43 | moderation, | |
22820226 | 44 | plugin, |
302eba0d C |
45 | server, |
46 | user | |
bc0d801b C |
47 | } |
48 | } | |
49 | ||
50 | export { | |
51 | buildPluginHelpers | |
52 | } | |
53 | ||
54 | // --------------------------------------------------------------------------- | |
55 | ||
1b05d82d | 56 | function buildPluginLogger (npmName: string) { |
bc0d801b C |
57 | return buildLogger(npmName) |
58 | } | |
1b05d82d C |
59 | |
60 | function buildDatabaseHelpers () { | |
61 | return { | |
62 | query: sequelizeTypescript.query.bind(sequelizeTypescript) | |
63 | } | |
64 | } | |
ab3ead3a | 65 | |
80fdaf06 C |
66 | function buildServerHelpers () { |
67 | return { | |
68 | getServerActor: () => getServerActor() | |
69 | } | |
70 | } | |
71 | ||
ab3ead3a C |
72 | function buildVideosHelpers () { |
73 | return { | |
80fdaf06 C |
74 | loadByUrl: (url: string) => { |
75 | return VideoModel.loadByUrl(url) | |
76 | }, | |
77 | ||
6559da28 C |
78 | loadByIdOrUUID: (id: number | string) => { |
79 | return VideoModel.load(id) | |
80 | }, | |
81 | ||
ab3ead3a C |
82 | removeVideo: (id: number) => { |
83 | return sequelizeTypescript.transaction(async t => { | |
84 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t) | |
85 | ||
86 | await video.destroy({ transaction: t }) | |
87 | }) | |
88 | } | |
89 | } | |
90 | } | |
5a7eecdd | 91 | |
80fdaf06 C |
92 | function buildModerationHelpers () { |
93 | return { | |
94 | blockServer: async (options: { byAccountId: number, hostToBlock: string }) => { | |
95 | const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock) | |
96 | ||
97 | await addServerInBlocklist(options.byAccountId, serverToBlock.id) | |
98 | }, | |
99 | ||
100 | unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => { | |
101 | const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock) | |
102 | if (!serverBlock) return | |
103 | ||
104 | await removeServerFromBlocklist(serverBlock) | |
105 | }, | |
106 | ||
107 | blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => { | |
108 | const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock) | |
109 | if (!accountToBlock) return | |
110 | ||
111 | await addAccountInBlocklist(options.byAccountId, accountToBlock.id) | |
112 | }, | |
113 | ||
114 | unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => { | |
115 | const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock) | |
116 | if (!targetAccount) return | |
117 | ||
118 | const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id) | |
119 | if (!accountBlock) return | |
120 | ||
121 | await removeAccountFromBlocklist(accountBlock) | |
122 | }, | |
123 | ||
124 | blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => { | |
125 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID) | |
126 | if (!video) return | |
127 | ||
128 | await blacklistVideo(video, options.createOptions) | |
129 | }, | |
130 | ||
131 | unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => { | |
132 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID) | |
133 | if (!video) return | |
134 | ||
135 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id) | |
136 | if (!videoBlacklist) return | |
137 | ||
138 | await unblacklistVideo(videoBlacklist, video) | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
5a7eecdd C |
143 | function buildConfigHelpers () { |
144 | return { | |
145 | getWebserverUrl () { | |
146 | return WEBSERVER.URL | |
22820226 C |
147 | }, |
148 | ||
149 | getServerConfig () { | |
150 | return getServerConfig() | |
5a7eecdd C |
151 | } |
152 | } | |
153 | } | |
22820226 | 154 | |
302eba0d C |
155 | function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) { |
156 | return { | |
157 | getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`, | |
158 | ||
159 | getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`, | |
160 | ||
161 | getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName) | |
162 | } | |
163 | } | |
164 | ||
165 | function buildUserHelpers () { | |
22820226 | 166 | return { |
b31d7262 C |
167 | getAuthUser: (res: express.Response) => { |
168 | const user = res.locals.oauth?.token?.User | |
169 | if (!user) return undefined | |
170 | ||
171 | return UserModel.loadByIdFull(user.id) | |
172 | } | |
22820226 C |
173 | } |
174 | } |