]>
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 | ||
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 |
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 | ||
5a7eecdd C |
131 | function buildConfigHelpers () { |
132 | return { | |
133 | getWebserverUrl () { | |
134 | return WEBSERVER.URL | |
135 | } | |
136 | } | |
137 | } |