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'
18 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
19 const logger = buildPluginLogger(npmName)
21 const database = buildDatabaseHelpers()
22 const videos = buildVideosHelpers()
24 const config = buildConfigHelpers()
26 const server = buildServerHelpers()
28 const moderation = buildModerationHelpers()
30 const plugin = buildPluginRelatedHelpers(pluginModel)
47 // ---------------------------------------------------------------------------
49 function buildPluginLogger (npmName: string) {
50 return buildLogger(npmName)
53 function buildDatabaseHelpers () {
55 query: sequelizeTypescript.query.bind(sequelizeTypescript)
59 function buildServerHelpers () {
61 getServerActor: () => getServerActor()
65 function buildVideosHelpers () {
67 loadByUrl: (url: string) => {
68 return VideoModel.loadByUrl(url)
71 loadByIdOrUUID: (id: number | string) => {
72 return VideoModel.load(id)
75 removeVideo: (id: number) => {
76 return sequelizeTypescript.transaction(async t => {
77 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
79 await video.destroy({ transaction: t })
85 function buildModerationHelpers () {
87 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
88 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
90 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
93 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
94 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
95 if (!serverBlock) return
97 await removeServerFromBlocklist(serverBlock)
100 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
101 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
102 if (!accountToBlock) return
104 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
107 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
108 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
109 if (!targetAccount) return
111 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
112 if (!accountBlock) return
114 await removeAccountFromBlocklist(accountBlock)
117 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
118 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
121 await blacklistVideo(video, options.createOptions)
124 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
125 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
128 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
129 if (!videoBlacklist) return
131 await unblacklistVideo(videoBlacklist, video)
136 function buildConfigHelpers () {
143 return getServerConfig()
148 function buildPluginRelatedHelpers (plugin: MPlugin) {
150 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`