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