]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/plugin-helpers-builder.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
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 import { UserModel } from '@server/models/account/user'
21
22 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
23 const logger = buildPluginLogger(npmName)
24
25 const database = buildDatabaseHelpers()
26 const videos = buildVideosHelpers()
27
28 const config = buildConfigHelpers()
29
30 const server = buildServerHelpers()
31
32 const moderation = buildModerationHelpers()
33
34 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
35
36 const user = buildUserHelpers()
37
38 return {
39 logger,
40 database,
41 videos,
42 config,
43 moderation,
44 plugin,
45 server,
46 user
47 }
48 }
49
50 export {
51 buildPluginHelpers
52 }
53
54 // ---------------------------------------------------------------------------
55
56 function buildPluginLogger (npmName: string) {
57 return buildLogger(npmName)
58 }
59
60 function buildDatabaseHelpers () {
61 return {
62 query: sequelizeTypescript.query.bind(sequelizeTypescript)
63 }
64 }
65
66 function buildServerHelpers () {
67 return {
68 getServerActor: () => getServerActor()
69 }
70 }
71
72 function buildVideosHelpers () {
73 return {
74 loadByUrl: (url: string) => {
75 return VideoModel.loadByUrl(url)
76 },
77
78 loadByIdOrUUID: (id: number | string) => {
79 return VideoModel.load(id)
80 },
81
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 }
91
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
143 function buildConfigHelpers () {
144 return {
145 getWebserverUrl () {
146 return WEBSERVER.URL
147 },
148
149 getServerConfig () {
150 return getServerConfig()
151 }
152 }
153 }
154
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 () {
166 return {
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 }
173 }
174 }