diff options
Diffstat (limited to 'server/lib/plugins/plugin-helpers-builder.ts')
-rw-r--r-- | server/lib/plugins/plugin-helpers-builder.ts | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts new file mode 100644 index 000000000..39773f693 --- /dev/null +++ b/server/lib/plugins/plugin-helpers-builder.ts | |||
@@ -0,0 +1,133 @@ | |||
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 | 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 | } | ||
75 | |||
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 | |||
127 | function buildConfigHelpers () { | ||
128 | return { | ||
129 | getWebserverUrl () { | ||
130 | return WEBSERVER.URL | ||
131 | } | ||
132 | } | ||
133 | } | ||