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