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