]>
Commit | Line | Data |
---|---|---|
1 | import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core' | |
2 | import { AuthService, ConfirmService, Notifier, ServerService } from '@app/core' | |
3 | import { Account, DropdownAction } from '@app/shared/shared-main' | |
4 | import { BulkRemoveCommentsOfBody, User, UserRight } from '@shared/models' | |
5 | import { UserAdminService } from '../shared-users' | |
6 | import { BlocklistService } from './blocklist.service' | |
7 | import { BulkService } from './bulk.service' | |
8 | import { UserBanModalComponent } from './user-ban-modal.component' | |
9 | ||
10 | @Component({ | |
11 | selector: 'my-user-moderation-dropdown', | |
12 | templateUrl: './user-moderation-dropdown.component.html' | |
13 | }) | |
14 | export class UserModerationDropdownComponent implements OnInit, OnChanges { | |
15 | @ViewChild('userBanModal') userBanModal: UserBanModalComponent | |
16 | ||
17 | @Input() user: User | |
18 | @Input() account: Account | |
19 | @Input() prependActions: DropdownAction<{ user: User, account: Account }>[] | |
20 | ||
21 | @Input() buttonSize: 'normal' | 'small' = 'normal' | |
22 | @Input() buttonStyled = true | |
23 | @Input() placement = 'right-top right-bottom auto' | |
24 | @Input() label: string | |
25 | @Input() container: 'body' | undefined = undefined | |
26 | ||
27 | @Output() userChanged = new EventEmitter() | |
28 | @Output() userDeleted = new EventEmitter() | |
29 | ||
30 | userActions: DropdownAction<{ user: User, account: Account }>[][] = [] | |
31 | ||
32 | requiresEmailVerification = false | |
33 | ||
34 | constructor ( | |
35 | private authService: AuthService, | |
36 | private notifier: Notifier, | |
37 | private confirmService: ConfirmService, | |
38 | private serverService: ServerService, | |
39 | private userAdminService: UserAdminService, | |
40 | private blocklistService: BlocklistService, | |
41 | private bulkService: BulkService | |
42 | ) { } | |
43 | ||
44 | ngOnInit () { | |
45 | this.serverService.getConfig() | |
46 | .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification) | |
47 | } | |
48 | ||
49 | ngOnChanges () { | |
50 | this.buildActions() | |
51 | } | |
52 | ||
53 | openBanUserModal (user: User) { | |
54 | if (user.username === 'root') { | |
55 | this.notifier.error($localize`You cannot ban root.`) | |
56 | return | |
57 | } | |
58 | ||
59 | this.userBanModal.openModal(user) | |
60 | } | |
61 | ||
62 | onUserBanned () { | |
63 | this.userChanged.emit() | |
64 | } | |
65 | ||
66 | async unbanUser (user: User) { | |
67 | const res = await this.confirmService.confirm($localize`Do you really want to unban ${user.username}?`, $localize`Unban`) | |
68 | if (res === false) return | |
69 | ||
70 | this.userAdminService.unbanUsers(user) | |
71 | .subscribe({ | |
72 | next: () => { | |
73 | this.notifier.success($localize`User ${user.username} unbanned.`) | |
74 | this.userChanged.emit() | |
75 | }, | |
76 | ||
77 | error: err => this.notifier.error(err.message) | |
78 | }) | |
79 | } | |
80 | ||
81 | async removeUser (user: User) { | |
82 | if (user.username === 'root') { | |
83 | this.notifier.error($localize`You cannot delete root.`) | |
84 | return | |
85 | } | |
86 | ||
87 | const message = $localize`If you remove user ${user.username}, you won't be able to create another with the same username!` | |
88 | const res = await this.confirmService.confirm(message, $localize`Delete ${user.username}`) | |
89 | if (res === false) return | |
90 | ||
91 | this.userAdminService.removeUser(user) | |
92 | .subscribe({ | |
93 | next: () => { | |
94 | this.notifier.success($localize`User ${user.username} deleted.`) | |
95 | this.userDeleted.emit() | |
96 | }, | |
97 | ||
98 | error: err => this.notifier.error(err.message) | |
99 | }) | |
100 | } | |
101 | ||
102 | setEmailAsVerified (user: User) { | |
103 | this.userAdminService.updateUser(user.id, { emailVerified: true }) | |
104 | .subscribe({ | |
105 | next: () => { | |
106 | this.notifier.success($localize`User ${user.username} email set as verified`) | |
107 | this.userChanged.emit() | |
108 | }, | |
109 | ||
110 | error: err => this.notifier.error(err.message) | |
111 | }) | |
112 | } | |
113 | ||
114 | blockAccountByUser (account: Account) { | |
115 | this.blocklistService.blockAccountByUser(account) | |
116 | .subscribe({ | |
117 | next: () => { | |
118 | this.notifier.success($localize`Account ${account.nameWithHost} muted.`) | |
119 | ||
120 | this.account.mutedByUser = true | |
121 | this.userChanged.emit() | |
122 | }, | |
123 | ||
124 | error: err => this.notifier.error(err.message) | |
125 | }) | |
126 | } | |
127 | ||
128 | unblockAccountByUser (account: Account) { | |
129 | this.blocklistService.unblockAccountByUser(account) | |
130 | .subscribe({ | |
131 | next: () => { | |
132 | this.notifier.success($localize`Account ${account.nameWithHost} unmuted.`) | |
133 | ||
134 | this.account.mutedByUser = false | |
135 | this.userChanged.emit() | |
136 | }, | |
137 | ||
138 | error: err => this.notifier.error(err.message) | |
139 | }) | |
140 | } | |
141 | ||
142 | blockServerByUser (host: string) { | |
143 | this.blocklistService.blockServerByUser(host) | |
144 | .subscribe({ | |
145 | next: () => { | |
146 | this.notifier.success($localize`Instance ${host} muted.`) | |
147 | ||
148 | this.account.mutedServerByUser = true | |
149 | this.userChanged.emit() | |
150 | }, | |
151 | ||
152 | error: err => this.notifier.error(err.message) | |
153 | }) | |
154 | } | |
155 | ||
156 | unblockServerByUser (host: string) { | |
157 | this.blocklistService.unblockServerByUser(host) | |
158 | .subscribe({ | |
159 | next: () => { | |
160 | this.notifier.success($localize`Instance ${host} unmuted.`) | |
161 | ||
162 | this.account.mutedServerByUser = false | |
163 | this.userChanged.emit() | |
164 | }, | |
165 | ||
166 | error: err => this.notifier.error(err.message) | |
167 | }) | |
168 | } | |
169 | ||
170 | blockAccountByInstance (account: Account) { | |
171 | this.blocklistService.blockAccountByInstance(account) | |
172 | .subscribe({ | |
173 | next: () => { | |
174 | this.notifier.success($localize`Account ${account.nameWithHost} muted by the instance.`) | |
175 | ||
176 | this.account.mutedByInstance = true | |
177 | this.userChanged.emit() | |
178 | }, | |
179 | ||
180 | error: err => this.notifier.error(err.message) | |
181 | }) | |
182 | } | |
183 | ||
184 | unblockAccountByInstance (account: Account) { | |
185 | this.blocklistService.unblockAccountByInstance(account) | |
186 | .subscribe({ | |
187 | next: () => { | |
188 | this.notifier.success($localize`Account ${account.nameWithHost} unmuted by the instance.`) | |
189 | ||
190 | this.account.mutedByInstance = false | |
191 | this.userChanged.emit() | |
192 | }, | |
193 | ||
194 | error: err => this.notifier.error(err.message) | |
195 | }) | |
196 | } | |
197 | ||
198 | blockServerByInstance (host: string) { | |
199 | this.blocklistService.blockServerByInstance(host) | |
200 | .subscribe({ | |
201 | next: () => { | |
202 | this.notifier.success($localize`Instance ${host} muted by the instance.`) | |
203 | ||
204 | this.account.mutedServerByInstance = true | |
205 | this.userChanged.emit() | |
206 | }, | |
207 | ||
208 | error: err => this.notifier.error(err.message) | |
209 | }) | |
210 | } | |
211 | ||
212 | unblockServerByInstance (host: string) { | |
213 | this.blocklistService.unblockServerByInstance(host) | |
214 | .subscribe({ | |
215 | next: () => { | |
216 | this.notifier.success($localize`Instance ${host} unmuted by the instance.`) | |
217 | ||
218 | this.account.mutedServerByInstance = false | |
219 | this.userChanged.emit() | |
220 | }, | |
221 | ||
222 | error: err => this.notifier.error(err.message) | |
223 | }) | |
224 | } | |
225 | ||
226 | async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) { | |
227 | const message = $localize`Are you sure you want to remove all the comments of this account?` | |
228 | const res = await this.confirmService.confirm(message, $localize`Delete account comments`) | |
229 | if (res === false) return | |
230 | ||
231 | this.bulkService.removeCommentsOf(body) | |
232 | .subscribe({ | |
233 | next: () => { | |
234 | this.notifier.success($localize`Will remove comments of this account (may take several minutes).`) | |
235 | }, | |
236 | ||
237 | error: err => this.notifier.error(err.message) | |
238 | }) | |
239 | } | |
240 | ||
241 | getRouterUserEditLink (user: User) { | |
242 | return [ '/admin', 'users', 'update', user.id ] | |
243 | } | |
244 | ||
245 | private isMyUser (user: User) { | |
246 | return user && this.authService.getUser().id === user.id | |
247 | } | |
248 | ||
249 | private isMyAccount (account: Account) { | |
250 | return account && this.authService.getUser().account.id === account.id | |
251 | } | |
252 | ||
253 | private buildActions () { | |
254 | this.userActions = [] | |
255 | ||
256 | if (this.prependActions && this.prependActions.length !== 0) { | |
257 | this.userActions = [ | |
258 | this.prependActions | |
259 | ] | |
260 | } | |
261 | ||
262 | const myAccountModerationActions = this.buildMyAccountModerationActions() | |
263 | const instanceModerationActions = this.buildInstanceModerationActions() | |
264 | ||
265 | if (myAccountModerationActions.length !== 0) this.userActions.push(myAccountModerationActions) | |
266 | if (instanceModerationActions.length !== 0) this.userActions.push(instanceModerationActions) | |
267 | } | |
268 | ||
269 | private buildMyAccountModerationActions () { | |
270 | if (!this.account || !this.authService.isLoggedIn()) return [] | |
271 | ||
272 | const myAccountActions: DropdownAction<{ user: User, account: Account }>[] = [ | |
273 | { | |
274 | label: $localize`My account moderation`, | |
275 | class: [ 'red' ], | |
276 | isHeader: true | |
277 | }, | |
278 | { | |
279 | label: $localize`Mute this account`, | |
280 | description: $localize`Hide any content from that user from you.`, | |
281 | isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === false, | |
282 | handler: ({ account }) => this.blockAccountByUser(account) | |
283 | }, | |
284 | { | |
285 | label: $localize`Unmute this account`, | |
286 | description: $localize`Show back content from that user for you.`, | |
287 | isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByUser === true, | |
288 | handler: ({ account }) => this.unblockAccountByUser(account) | |
289 | }, | |
290 | { | |
291 | label: $localize`Mute the instance`, | |
292 | description: $localize`Hide any content from that instance for you.`, | |
293 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === false, | |
294 | handler: ({ account }) => this.blockServerByUser(account.host) | |
295 | }, | |
296 | { | |
297 | label: $localize`Unmute the instance`, | |
298 | description: $localize`Show back content from that instance for you.`, | |
299 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByUser === true, | |
300 | handler: ({ account }) => this.unblockServerByUser(account.host) | |
301 | }, | |
302 | { | |
303 | label: $localize`Remove comments from your videos`, | |
304 | description: $localize`Remove comments made by this account on your videos.`, | |
305 | isDisplayed: ({ account }) => !this.isMyAccount(account), | |
306 | handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' }) | |
307 | } | |
308 | ] | |
309 | ||
310 | return myAccountActions | |
311 | } | |
312 | ||
313 | private buildInstanceModerationActions () { | |
314 | if (!this.authService.isLoggedIn()) return [] | |
315 | ||
316 | const authUser = this.authService.getUser() | |
317 | ||
318 | let instanceActions: DropdownAction<{ user: User, account: Account }>[] = [] | |
319 | ||
320 | if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) { | |
321 | instanceActions = instanceActions.concat([ | |
322 | { | |
323 | label: $localize`Edit user`, | |
324 | description: $localize`Change quota, role, and more.`, | |
325 | linkBuilder: ({ user }) => this.getRouterUserEditLink(user) | |
326 | }, | |
327 | { | |
328 | label: $localize`Delete user`, | |
329 | description: $localize`Videos will be deleted, comments will be tombstoned.`, | |
330 | isDisplayed: ({ user }) => !this.isMyUser(user), | |
331 | handler: ({ user }) => this.removeUser(user) | |
332 | }, | |
333 | { | |
334 | label: $localize`Ban`, | |
335 | description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`, | |
336 | handler: ({ user }) => this.openBanUserModal(user), | |
337 | isDisplayed: ({ user }) => !this.isMyUser(user) && !user.blocked | |
338 | }, | |
339 | { | |
340 | label: $localize`Unban user`, | |
341 | description: $localize`Allow the user to login and create videos/comments again`, | |
342 | handler: ({ user }) => this.unbanUser(user), | |
343 | isDisplayed: ({ user }) => !this.isMyUser(user) && user.blocked | |
344 | }, | |
345 | { | |
346 | label: $localize`Set Email as Verified`, | |
347 | handler: ({ user }) => this.setEmailAsVerified(user), | |
348 | isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false | |
349 | } | |
350 | ]) | |
351 | } | |
352 | ||
353 | // Instance actions on account blocklists | |
354 | if (this.account && authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) { | |
355 | instanceActions = instanceActions.concat([ | |
356 | { | |
357 | label: $localize`Mute this account`, | |
358 | description: $localize`Hide any content from that user from you, your instance and its users.`, | |
359 | isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === false, | |
360 | handler: ({ account }) => this.blockAccountByInstance(account) | |
361 | }, | |
362 | { | |
363 | label: $localize`Unmute this account`, | |
364 | description: $localize`Show this user's content to the users of this instance again.`, | |
365 | isDisplayed: ({ account }) => !this.isMyAccount(account) && account.mutedByInstance === true, | |
366 | handler: ({ account }) => this.unblockAccountByInstance(account) | |
367 | } | |
368 | ]) | |
369 | } | |
370 | ||
371 | // Instance actions on server blocklists | |
372 | if (this.account && authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) { | |
373 | instanceActions = instanceActions.concat([ | |
374 | { | |
375 | label: $localize`Mute the instance`, | |
376 | description: $localize`Hide any content from that instance from you, your instance and its users.`, | |
377 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false, | |
378 | handler: ({ account }) => this.blockServerByInstance(account.host) | |
379 | }, | |
380 | { | |
381 | label: $localize`Unmute the instance by your instance`, | |
382 | description: $localize`Show back content from that instance for you, your instance and its users.`, | |
383 | isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true, | |
384 | handler: ({ account }) => this.unblockServerByInstance(account.host) | |
385 | } | |
386 | ]) | |
387 | } | |
388 | ||
389 | if (this.account && authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) { | |
390 | instanceActions = instanceActions.concat([ | |
391 | { | |
392 | label: $localize`Remove comments from your instance`, | |
393 | description: $localize`Remove comments made by this account from your instance.`, | |
394 | isDisplayed: ({ account }) => !this.isMyAccount(account), | |
395 | handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' }) | |
396 | } | |
397 | ]) | |
398 | } | |
399 | ||
400 | if (instanceActions.length === 0) return [] | |
401 | ||
402 | return [ { label: $localize`Instance moderation`, isHeader: true }, ...instanceActions ] | |
403 | } | |
404 | } |