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