]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/plugins/plugin-helpers-builder.ts
Tests that show the bug.
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / plugin-helpers-builder.ts
CommitLineData
67ed6552 1import { PeerTubeHelpers } from '@server/types/plugins'
1b05d82d
C
2import { sequelizeTypescript } from '@server/initializers/database'
3import { buildLogger } from '@server/helpers/logger'
ab3ead3a 4import { VideoModel } from '@server/models/video/video'
5a7eecdd 5import { WEBSERVER } from '@server/initializers/constants'
80fdaf06
C
6import { ServerModel } from '@server/models/server/server'
7import { getServerActor } from '@server/models/application/application'
8import { addServerInBlocklist, removeServerFromBlocklist, addAccountInBlocklist, removeAccountFromBlocklist } from '../blocklist'
9import { ServerBlocklistModel } from '@server/models/server/server-blocklist'
10import { AccountModel } from '@server/models/account/account'
11import { VideoBlacklistCreate } from '@shared/models'
12import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
13import { VideoBlacklistModel } from '@server/models/video/video-blacklist'
14import { AccountBlocklistModel } from '@server/models/account/account-blocklist'
bc0d801b 15
1b05d82d
C
16function buildPluginHelpers (npmName: string): PeerTubeHelpers {
17 const logger = buildPluginLogger(npmName)
18
19 const database = buildDatabaseHelpers()
ab3ead3a 20 const videos = buildVideosHelpers()
bc0d801b 21
5a7eecdd
C
22 const config = buildConfigHelpers()
23
80fdaf06
C
24 const server = buildServerHelpers()
25
26 const moderation = buildModerationHelpers()
27
bc0d801b 28 return {
1b05d82d 29 logger,
ab3ead3a 30 database,
5a7eecdd 31 videos,
80fdaf06
C
32 config,
33 moderation,
34 server
bc0d801b
C
35 }
36}
37
38export {
39 buildPluginHelpers
40}
41
42// ---------------------------------------------------------------------------
43
1b05d82d 44function buildPluginLogger (npmName: string) {
bc0d801b
C
45 return buildLogger(npmName)
46}
1b05d82d
C
47
48function buildDatabaseHelpers () {
49 return {
50 query: sequelizeTypescript.query.bind(sequelizeTypescript)
51 }
52}
ab3ead3a 53
80fdaf06
C
54function buildServerHelpers () {
55 return {
56 getServerActor: () => getServerActor()
57 }
58}
59
ab3ead3a
C
60function buildVideosHelpers () {
61 return {
80fdaf06
C
62 loadByUrl: (url: string) => {
63 return VideoModel.loadByUrl(url)
64 },
65
6559da28
C
66 loadByIdOrUUID: (id: number | string) => {
67 return VideoModel.load(id)
68 },
69
ab3ead3a
C
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}
5a7eecdd 79
80fdaf06
C
80function 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
5a7eecdd
C
131function buildConfigHelpers () {
132 return {
133 getWebserverUrl () {
134 return WEBSERVER.URL
135 }
136 }
137}