]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/plugins/plugin-helpers-builder.ts
dac6b3185409327eba9cf864c307c9ae5fa36e0b
[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
16 function buildPluginHelpers (npmName: string): PeerTubeHelpers {
17 const logger = buildPluginLogger(npmName)
18
19 const database = buildDatabaseHelpers()
20 const videos = buildVideosHelpers()
21
22 const config = buildConfigHelpers()
23
24 const server = buildServerHelpers()
25
26 const moderation = buildModerationHelpers()
27
28 return {
29 logger,
30 database,
31 videos,
32 config,
33 moderation,
34 server
35 }
36 }
37
38 export {
39 buildPluginHelpers
40 }
41
42 // ---------------------------------------------------------------------------
43
44 function buildPluginLogger (npmName: string) {
45 return buildLogger(npmName)
46 }
47
48 function buildDatabaseHelpers () {
49 return {
50 query: sequelizeTypescript.query.bind(sequelizeTypescript)
51 }
52 }
53
54 function buildServerHelpers () {
55 return {
56 getServerActor: () => getServerActor()
57 }
58 }
59
60 function buildVideosHelpers () {
61 return {
62 loadByUrl: (url: string) => {
63 return VideoModel.loadByUrl(url)
64 },
65
66 loadByIdOrUUID: (id: number | string) => {
67 return VideoModel.load(id)
68 },
69
70 removeVideo: (id: number) => {
71 return sequelizeTypescript.transaction(async t => {
72 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t)
73
74 await video.destroy({ transaction: t })
75 })
76 }
77 }
78 }
79
80 function buildModerationHelpers () {
81 return {
82 blockServer: async (options: { byAccountId: number, hostToBlock: string }) => {
83 const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock)
84
85 await addServerInBlocklist(options.byAccountId, serverToBlock.id)
86 },
87
88 unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => {
89 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock)
90 if (!serverBlock) return
91
92 await removeServerFromBlocklist(serverBlock)
93 },
94
95 blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => {
96 const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock)
97 if (!accountToBlock) return
98
99 await addAccountInBlocklist(options.byAccountId, accountToBlock.id)
100 },
101
102 unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => {
103 const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock)
104 if (!targetAccount) return
105
106 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id)
107 if (!accountBlock) return
108
109 await removeAccountFromBlocklist(accountBlock)
110 },
111
112 blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => {
113 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
114 if (!video) return
115
116 await blacklistVideo(video, options.createOptions)
117 },
118
119 unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => {
120 const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID)
121 if (!video) return
122
123 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id)
124 if (!videoBlacklist) return
125
126 await unblacklistVideo(videoBlacklist, video)
127 }
128 }
129 }
130
131 function buildConfigHelpers () {
132 return {
133 getWebserverUrl () {
134 return WEBSERVER.URL
135 }
136 }
137 }