]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/plugins/plugin-helpers-builder.ts
Add data directory for plugins and some helpers
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
CommitLineData
302eba0d
C
1import * as express from 'express'
2import { join } from 'path'
1b05d82d 3import { buildLogger } from '@server/helpers/logger'
302eba0d 4import { CONFIG } from '@server/initializers/config'
5a7eecdd 5import { WEBSERVER } from '@server/initializers/constants'
302eba0d
C
6import { sequelizeTypescript } from '@server/initializers/database'
7import { AccountModel } from '@server/models/account/account'
8import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
80fdaf06 9import { getServerActor } from '@server/models/application/application'
302eba0d 10import { ServerModel } from '@server/models/server/server'
80fdaf06 11import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
302eba0d 12import { VideoModel } from '@server/models/video/video'
80fdaf06 13import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
22820226 14import { MPlugin } from '@server/types/models'
302eba0d
C
15import { PeerTubeHelpers } from '@server/types/plugins'
16import { VideoBlacklistCreate } from '@shared/models'
17import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
18import { getServerConfig } from '../config'
19import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
bc0d801b 20
22820226 21function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
1b05d82d
C
22 const logger = buildPluginLogger(npmName)
23
24 const database = buildDatabaseHelpers()
ab3ead3a 25 const videos = buildVideosHelpers()
bc0d801b 26
5a7eecdd
C
27 const config = buildConfigHelpers()
28
80fdaf06
C
29 const server = buildServerHelpers()
30
31 const moderation = buildModerationHelpers()
32
302eba0d
C
33 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
34
35 const user = buildUserHelpers()
22820226 36
bc0d801b 37 return {
1b05d82d 38 logger,
ab3ead3a 39 database,
5a7eecdd 40 videos,
80fdaf06
C
41 config,
42 moderation,
22820226 43 plugin,
302eba0d
C
44 server,
45 user
bc0d801b
C
46 }
47}
48
49export {
50 buildPluginHelpers
51}
52
53// ---------------------------------------------------------------------------
54
1b05d82d 55function buildPluginLogger (npmName: string) {
bc0d801b
C
56 return buildLogger(npmName)
57}
1b05d82d
C
58
59function buildDatabaseHelpers () {
60 return {
61 query: sequelizeTypescript.query.bind(sequelizeTypescript)
62 }
63}
ab3ead3a 64
80fdaf06
C
65function buildServerHelpers () {
66 return {
67 getServerActor: () => getServerActor()
68 }
69}
70
ab3ead3a
C
71function buildVideosHelpers () {
72 return {
80fdaf06
C
73 loadByUrl: (url: string) => {
74 return VideoModel.loadByUrl(url)
75 },
76
6559da28
C
77 loadByIdOrUUID: (id: number | string) => {
78 return VideoModel.load(id)
79 },
80
ab3ead3a
C
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}
5a7eecdd 90
80fdaf06
C
91function 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
5a7eecdd
C
142function buildConfigHelpers () {
143 return {
144 getWebserverUrl () {
145 return WEBSERVER.URL
22820226
C
146 },
147
148 getServerConfig () {
149 return getServerConfig()
5a7eecdd
C
150 }
151 }
152}
22820226 153
302eba0d
C
154function 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
164function buildUserHelpers () {
22820226 165 return {
302eba0d 166 getAuthUser: (res: express.Response) => res.locals.oauth?.token?.User
22820226
C
167 }
168}