]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { PeerTubeHelpers } from '@server/types/plugins' |
1b05d82d C |
2 | import { sequelizeTypescript } from '@server/initializers/database' |
3 | import { buildLogger } from '@server/helpers/logger' | |
ab3ead3a | 4 | import { VideoModel } from '@server/models/video/video' |
5a7eecdd | 5 | import { WEBSERVER } from '@server/initializers/constants' |
80fdaf06 C |
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' | |
bc0d801b | 15 | |
1b05d82d C |
16 | function 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 | ||
38 | export { | |
39 | buildPluginHelpers | |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
1b05d82d | 44 | function buildPluginLogger (npmName: string) { |
bc0d801b C |
45 | return buildLogger(npmName) |
46 | } | |
1b05d82d C |
47 | |
48 | function buildDatabaseHelpers () { | |
49 | return { | |
50 | query: sequelizeTypescript.query.bind(sequelizeTypescript) | |
51 | } | |
52 | } | |
ab3ead3a | 53 | |
80fdaf06 C |
54 | function buildServerHelpers () { |
55 | return { | |
56 | getServerActor: () => getServerActor() | |
57 | } | |
58 | } | |
59 | ||
ab3ead3a C |
60 | function buildVideosHelpers () { |
61 | return { | |
80fdaf06 C |
62 | loadByUrl: (url: string) => { |
63 | return VideoModel.loadByUrl(url) | |
64 | }, | |
65 | ||
ab3ead3a C |
66 | removeVideo: (id: number) => { |
67 | return sequelizeTypescript.transaction(async t => { | |
68 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t) | |
69 | ||
70 | await video.destroy({ transaction: t }) | |
71 | }) | |
72 | } | |
73 | } | |
74 | } | |
5a7eecdd | 75 | |
80fdaf06 C |
76 | function buildModerationHelpers () { |
77 | return { | |
78 | blockServer: async (options: { byAccountId: number, hostToBlock: string }) => { | |
79 | const serverToBlock = await ServerModel.loadOrCreateByHost(options.hostToBlock) | |
80 | ||
81 | await addServerInBlocklist(options.byAccountId, serverToBlock.id) | |
82 | }, | |
83 | ||
84 | unblockServer: async (options: { byAccountId: number, hostToUnblock: string }) => { | |
85 | const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(options.byAccountId, options.hostToUnblock) | |
86 | if (!serverBlock) return | |
87 | ||
88 | await removeServerFromBlocklist(serverBlock) | |
89 | }, | |
90 | ||
91 | blockAccount: async (options: { byAccountId: number, handleToBlock: string }) => { | |
92 | const accountToBlock = await AccountModel.loadByNameWithHost(options.handleToBlock) | |
93 | if (!accountToBlock) return | |
94 | ||
95 | await addAccountInBlocklist(options.byAccountId, accountToBlock.id) | |
96 | }, | |
97 | ||
98 | unblockAccount: async (options: { byAccountId: number, handleToUnblock: string }) => { | |
99 | const targetAccount = await AccountModel.loadByNameWithHost(options.handleToUnblock) | |
100 | if (!targetAccount) return | |
101 | ||
102 | const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(options.byAccountId, targetAccount.id) | |
103 | if (!accountBlock) return | |
104 | ||
105 | await removeAccountFromBlocklist(accountBlock) | |
106 | }, | |
107 | ||
108 | blacklistVideo: async (options: { videoIdOrUUID: number | string, createOptions: VideoBlacklistCreate }) => { | |
109 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID) | |
110 | if (!video) return | |
111 | ||
112 | await blacklistVideo(video, options.createOptions) | |
113 | }, | |
114 | ||
115 | unblacklistVideo: async (options: { videoIdOrUUID: number | string }) => { | |
116 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(options.videoIdOrUUID) | |
117 | if (!video) return | |
118 | ||
119 | const videoBlacklist = await VideoBlacklistModel.loadByVideoId(video.id) | |
120 | if (!videoBlacklist) return | |
121 | ||
122 | await unblacklistVideo(videoBlacklist, video) | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
5a7eecdd C |
127 | function buildConfigHelpers () { |
128 | return { | |
129 | getWebserverUrl () { | |
130 | return WEBSERVER.URL | |
131 | } | |
132 | } | |
133 | } |