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'
22 function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
23 const logger = buildPluginLogger(npmName)
25 const database = buildDatabaseHelpers()
26 const videos = buildVideosHelpers()
28 const config = buildConfigHelpers()
30 const server = buildServerHelpers()
32 const moderation = buildModerationHelpers()
34 const plugin = buildPluginRelatedHelpers(pluginModel, npmName)
36 const user = buildUserHelpers()
54 // ---------------------------------------------------------------------------
56 function buildPluginLogger (npmName: string) {
57 return buildLogger(npmName)
60 function buildDatabaseHelpers () {
62 query: sequelizeTypescript.query.bind(sequelizeTypescript)
66 function buildServerHelpers () {
68 getServerActor: () => getServerActor()
72 function buildVideosHelpers () {
74 loadByUrl: (url: string) => {
75 return VideoModel.loadByUrl(url)
78 loadByIdOrUUID: (id: number | string) => {
79 return VideoModel.load(id)
82 removeVideo: (id: number) => {
83 return sequelizeTypescript.transaction(async t => {
84 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
86 await video.destroy({ transaction: t })
92 function buildModerationHelpers () {
94 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
95 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
97 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
100 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
101 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
102 if (!serverBlock) return
104 await removeServerFromBlocklist(serverBlock)
107 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
108 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
109 if (!accountToBlock) return
111 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
114 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
115 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
116 if (!targetAccount) return
118 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
119 if (!accountBlock) return
121 await removeAccountFromBlocklist(accountBlock)
124 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
125 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
128 await blacklistVideo(video, options.createOptions)
131 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
132 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
135 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
136 if (!videoBlacklist) return
138 await unblacklistVideo(videoBlacklist, video)
143 function buildConfigHelpers () {
150 return getServerConfig()
155 function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
157 getBaseStaticRoute: () => `/plugins/${plugin.name}/${plugin.version}/static/`,
159 getBaseRouterRoute: () => `/plugins/${plugin.name}/${plugin.version}/router/`,
161 getDataDirectoryPath: () => join(CONFIG.STORAGE.PLUGINS_DIR, 'data', npmName)
165 function buildUserHelpers () {
167 getAuthUser: (res: express.Response) => {
168 const user = res.locals.oauth?.token?.User
169 if (!user) return undefined
171 return UserModel.loadByIdFull(user.id)