]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/plugin-helpers-builder.ts
cbd849742e95ef1181beaad2f6dd7122c0f5dc84
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
1 import { PeerTubeHelpers } from '@server/types/plugins'
2 import { sequelizeTypescript } from '@server/initializers/database'
3 import { buildLogger } from '@server/helpers/logger'
4 import { VideoModel } from '@server/models/video/video'
5 import { WEBSERVER } from '@server/initializers/constants'
6 import { ServerModel } from '@server/models/server/server'
7 import { getServerActor } from '@server/models/application/application'
8 import { addServerInBlocklist, removeServerFromBlocklist, addAccountInBlocklist, removeAccountFromBlocklist } from '../blocklist'
9 import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
10 import { AccountModel } from '@server/models/account/account'
11 import { VideoBlacklistCreate } from '@shared/models'
12 import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
13 import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
14 import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
15 import { getServerConfig } from '../config'
16 import { MPlugin } from '@server/types/models'
17
18 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
19 const logger = buildPluginLogger(npmName)
20
21 const database = buildDatabaseHelpers()
22 const videos = buildVideosHelpers()
23
24 const config = buildConfigHelpers()
25
26 const server = buildServerHelpers()
27
28 const moderation = buildModerationHelpers()
29
30 const plugin = buildPluginRelatedHelpers(pluginModel)
31
32 return {
33 logger,
34 database,
35 videos,
36 config,
37 moderation,
38 plugin,
39 server
40 }
41 }
42
43 export {
44 buildPluginHelpers
45 }
46
47 // ---------------------------------------------------------------------------
48
49 function buildPluginLogger (npmName: string) {
50 return buildLogger(npmName)
51 }
52
53 function buildDatabaseHelpers () {
54 return {
55 query: sequelizeTypescript.query.bind(sequelizeTypescript)
56 }
57 }
58
59 function buildServerHelpers () {
60 return {
61 getServerActor: () => getServerActor()
62 }
63 }
64
65 function buildVideosHelpers () {
66 return {
67 loadByUrl: (url: string) => {
68 return VideoModel.loadByUrl(url)
69 },
70
71 loadByIdOrUUID: (id: number | string) => {
72 return VideoModel.load(id)
73 },
74
75 removeVideo: (id: number) => {
76 return sequelizeTypescript.transaction(async t => {
77 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
78
79 await video.destroy({ transaction: t })
80 })
81 }
82 }
83 }
84
85 function buildModerationHelpers () {
86 return {
87 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
88 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
89
90 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
91 },
92
93 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
94 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
95 if (!serverBlock) return
96
97 await removeServerFromBlocklist(serverBlock)
98 },
99
100 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
101 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
102 if (!accountToBlock) return
103
104 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
105 },
106
107 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
108 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
109 if (!targetAccount) return
110
111 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
112 if (!accountBlock) return
113
114 await removeAccountFromBlocklist(accountBlock)
115 },
116
117 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
118 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
119 if (!video) return
120
121 await blacklistVideo(video, options.createOptions)
122 },
123
124 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
125 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
126 if (!video) return
127
128 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
129 if (!videoBlacklist) return
130
131 await unblacklistVideo(videoBlacklist, video)
132 }
133 }
134 }
135
136 function buildConfigHelpers () {
137 return {
138 getWebserverUrl () {
139 return WEBSERVER.URL
140 },
141
142 getServerConfig () {
143 return getServerConfig()
144 }
145 }
146 }
147
148 function buildPluginRelatedHelpers (plugin: MPlugin) {
149 return {
150 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`
151 }
152 }