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