diff options
author | Chocobozzz <me@florianbigard.com> | 2020-05-07 14:58:24 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2020-05-07 15:07:01 +0200 |
commit | 80fdaf064562aff968f4c9cea1cf220bc12a70da (patch) | |
tree | a0ec49f433b828660f741b394dfd87056cd2c6ef /server/lib/plugins/plugin-helpers.ts | |
parent | 3cc665f48fd233d09f778d7e887488dde6f03ef6 (diff) | |
download | PeerTube-80fdaf064562aff968f4c9cea1cf220bc12a70da.tar.gz PeerTube-80fdaf064562aff968f4c9cea1cf220bc12a70da.tar.zst PeerTube-80fdaf064562aff968f4c9cea1cf220bc12a70da.zip |
Add moderation helpers to plugins
Diffstat (limited to 'server/lib/plugins/plugin-helpers.ts')
-rw-r--r-- | server/lib/plugins/plugin-helpers.ts | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/server/lib/plugins/plugin-helpers.ts b/server/lib/plugins/plugin-helpers.ts index 608207e05..de82b4918 100644 --- a/server/lib/plugins/plugin-helpers.ts +++ b/server/lib/plugins/plugin-helpers.ts | |||
@@ -3,6 +3,15 @@ import { sequelizeTypescript } from '@server/initializers/database' | |||
3 | import { buildLogger } from '@server/helpers/logger' | 3 | import { buildLogger } from '@server/helpers/logger' |
4 | import { VideoModel } from '@server/models/video/video' | 4 | import { VideoModel } from '@server/models/video/video' |
5 | import { WEBSERVER } from '@server/initializers/constants' | 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' | ||
6 | 15 | ||
7 | function buildPluginHelpers (npmName: string): PeerTubeHelpers { | 16 | function buildPluginHelpers (npmName: string): PeerTubeHelpers { |
8 | const logger = buildPluginLogger(npmName) | 17 | const logger = buildPluginLogger(npmName) |
@@ -12,11 +21,17 @@ function buildPluginHelpers (npmName: string): PeerTubeHelpers { | |||
12 | 21 | ||
13 | const config = buildConfigHelpers() | 22 | const config = buildConfigHelpers() |
14 | 23 | ||
24 | const server = buildServerHelpers() | ||
25 | |||
26 | const moderation = buildModerationHelpers() | ||
27 | |||
15 | return { | 28 | return { |
16 | logger, | 29 | logger, |
17 | database, | 30 | database, |
18 | videos, | 31 | videos, |
19 | config | 32 | config, |
33 | moderation, | ||
34 | server | ||
20 | } | 35 | } |
21 | } | 36 | } |
22 | 37 | ||
@@ -36,8 +51,18 @@ function buildDatabaseHelpers () { | |||
36 | } | 51 | } |
37 | } | 52 | } |
38 | 53 | ||
54 | function buildServerHelpers () { | ||
55 | return { | ||
56 | getServerActor: () => getServerActor() | ||
57 | } | ||
58 | } | ||
59 | |||
39 | function buildVideosHelpers () { | 60 | function buildVideosHelpers () { |
40 | return { | 61 | return { |
62 | loadByUrl: (url: string) => { | ||
63 | return VideoModel.loadByUrl(url) | ||
64 | }, | ||
65 | |||
41 | removeVideo: (id: number) => { | 66 | removeVideo: (id: number) => { |
42 | return sequelizeTypescript.transaction(async t => { | 67 | return sequelizeTypescript.transaction(async t => { |
43 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t) | 68 | const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(id, t) |
@@ -48,6 +73,57 @@ function buildVideosHelpers () { | |||
48 | } | 73 | } |
49 | } | 74 | } |
50 | 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 | |||
51 | function buildConfigHelpers () { | 127 | function buildConfigHelpers () { |
52 | return { | 128 | return { |
53 | getWebserverUrl () { | 129 | getWebserverUrl () { |