]>
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 } 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 userService: UserService, | |
36 | private blocklistService: BlocklistService, | |
37 | private i18n: I18n | |
38 | ) { } | |
39 | ||
40 | ngOnChanges () { | |
41 | this.buildActions() | |
42 | } | |
43 | ||
44 | openBanUserModal (user: User) { | |
45 | if (user.username === 'root') { | |
46 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot ban root.')) | |
47 | return | |
48 | } | |
49 | ||
50 | this.userBanModal.openModal(user) | |
51 | } | |
52 | ||
53 | onUserBanned () { | |
54 | this.userChanged.emit() | |
55 | } | |
56 | ||
57 | async unbanUser (user: User) { | |
58 | const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username }) | |
59 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
60 | if (res === false) return | |
61 | ||
62 | this.userService.unbanUsers(user) | |
63 | .subscribe( | |
64 | () => { | |
65 | this.notificationsService.success( | |
66 | this.i18n('Success'), | |
67 | this.i18n('User {{username}} unbanned.', { username: user.username }) | |
68 | ) | |
69 | ||
70 | this.userChanged.emit() | |
71 | }, | |
72 | ||
73 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
74 | ) | |
75 | } | |
76 | ||
77 | async removeUser (user: User) { | |
78 | if (user.username === 'root') { | |
79 | this.notificationsService.error(this.i18n('Error'), this.i18n('You cannot delete root.')) | |
80 | return | |
81 | } | |
82 | ||
83 | const message = this.i18n('If you remove this user, you will not be able to create another with the same username!') | |
84 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
85 | if (res === false) return | |
86 | ||
87 | this.userService.removeUser(user).subscribe( | |
88 | () => { | |
89 | this.notificationsService.success( | |
90 | this.i18n('Success'), | |
91 | this.i18n('User {{username}} deleted.', { username: user.username }) | |
92 | ) | |
93 | this.userDeleted.emit() | |
94 | }, | |
95 | ||
96 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
97 | ) | |
98 | } | |
99 | ||
100 | blockAccountByUser (account: Account) { | |
101 | this.blocklistService.blockAccountByUser(account) | |
102 | .subscribe( | |
103 | () => { | |
104 | this.notificationsService.success( | |
105 | this.i18n('Success'), | |
106 | this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost }) | |
107 | ) | |
108 | ||
109 | this.account.mutedByUser = true | |
110 | this.userChanged.emit() | |
111 | }, | |
112 | ||
113 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
114 | ) | |
115 | } | |
116 | ||
117 | unblockAccountByUser (account: Account) { | |
118 | this.blocklistService.unblockAccountByUser(account) | |
119 | .subscribe( | |
120 | () => { | |
121 | this.notificationsService.success( | |
122 | this.i18n('Success'), | |
123 | this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost }) | |
124 | ) | |
125 | ||
126 | this.account.mutedByUser = false | |
127 | this.userChanged.emit() | |
128 | }, | |
129 | ||
130 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
131 | ) | |
132 | } | |
133 | ||
134 | blockServerByUser (host: string) { | |
135 | this.blocklistService.blockServerByUser(host) | |
136 | .subscribe( | |
137 | () => { | |
138 | this.notificationsService.success( | |
139 | this.i18n('Success'), | |
140 | this.i18n('Instance {{host}} muted.', { host }) | |
141 | ) | |
142 | ||
143 | this.account.mutedServerByUser = true | |
144 | this.userChanged.emit() | |
145 | }, | |
146 | ||
147 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
148 | ) | |
149 | } | |
150 | ||
151 | unblockServerByUser (host: string) { | |
152 | this.blocklistService.unblockServerByUser(host) | |
153 | .subscribe( | |
154 | () => { | |
155 | this.notificationsService.success( | |
156 | this.i18n('Success'), | |
157 | this.i18n('Instance {{host}} unmuted.', { host }) | |
158 | ) | |
159 | ||
160 | this.account.mutedServerByUser = false | |
161 | this.userChanged.emit() | |
162 | }, | |
163 | ||
164 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
165 | ) | |
166 | } | |
167 | ||
168 | blockAccountByInstance (account: Account) { | |
169 | this.blocklistService.blockAccountByInstance(account) | |
170 | .subscribe( | |
171 | () => { | |
172 | this.notificationsService.success( | |
173 | this.i18n('Success'), | |
174 | this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }) | |
175 | ) | |
176 | ||
177 | this.account.mutedByInstance = true | |
178 | this.userChanged.emit() | |
179 | }, | |
180 | ||
181 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
182 | ) | |
183 | } | |
184 | ||
185 | unblockAccountByInstance (account: Account) { | |
186 | this.blocklistService.unblockAccountByInstance(account) | |
187 | .subscribe( | |
188 | () => { | |
189 | this.notificationsService.success( | |
190 | this.i18n('Success'), | |
191 | this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost }) | |
192 | ) | |
193 | ||
194 | this.account.mutedByInstance = false | |
195 | this.userChanged.emit() | |
196 | }, | |
197 | ||
198 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
199 | ) | |
200 | } | |
201 | ||
202 | blockServerByInstance (host: string) { | |
203 | this.blocklistService.blockServerByInstance(host) | |
204 | .subscribe( | |
205 | () => { | |
206 | this.notificationsService.success( | |
207 | this.i18n('Success'), | |
208 | this.i18n('Instance {{host}} muted by the instance.', { host }) | |
209 | ) | |
210 | ||
211 | this.account.mutedServerByInstance = true | |
212 | this.userChanged.emit() | |
213 | }, | |
214 | ||
215 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
216 | ) | |
217 | } | |
218 | ||
219 | unblockServerByInstance (host: string) { | |
220 | this.blocklistService.unblockServerByInstance(host) | |
221 | .subscribe( | |
222 | () => { | |
223 | this.notificationsService.success( | |
224 | this.i18n('Success'), | |
225 | this.i18n('Instance {{host}} unmuted by the instance.', { host }) | |
226 | ) | |
227 | ||
228 | this.account.mutedServerByInstance = false | |
229 | this.userChanged.emit() | |
230 | }, | |
231 | ||
232 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
233 | ) | |
234 | } | |
235 | ||
236 | getRouterUserEditLink (user: User) { | |
237 | return [ '/admin', 'users', 'update', user.id ] | |
238 | } | |
239 | ||
240 | private buildActions () { | |
241 | this.userActions = [] | |
242 | ||
243 | if (this.authService.isLoggedIn()) { | |
244 | const authUser = this.authService.getUser() | |
245 | ||
246 | if (this.user && authUser.id === this.user.id) return | |
247 | ||
248 | if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) { | |
249 | this.userActions = this.userActions.concat([ | |
250 | { | |
251 | label: this.i18n('Edit'), | |
252 | linkBuilder: ({ user }) => this.getRouterUserEditLink(user) | |
253 | }, | |
254 | { | |
255 | label: this.i18n('Delete'), | |
256 | handler: ({ user }) => this.removeUser(user) | |
257 | }, | |
258 | { | |
259 | label: this.i18n('Ban'), | |
260 | handler: ({ user }: { user: User }) => this.openBanUserModal(user), | |
261 | isDisplayed: ({ user }: { user: User }) => !user.blocked | |
262 | }, | |
263 | { | |
264 | label: this.i18n('Unban'), | |
265 | handler: ({ user }: { user: User }) => this.unbanUser(user), | |
266 | isDisplayed: ({ user }: { user: User }) => user.blocked | |
267 | } | |
268 | ]) | |
269 | } | |
270 | ||
271 | // Actions on accounts/servers | |
272 | if (this.account) { | |
273 | // User actions | |
274 | this.userActions = this.userActions.concat([ | |
275 | { | |
276 | label: this.i18n('Mute this account'), | |
277 | isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === false, | |
278 | handler: ({ account }: { account: Account }) => this.blockAccountByUser(account) | |
279 | }, | |
280 | { | |
281 | label: this.i18n('Unmute this account'), | |
282 | isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === true, | |
283 | handler: ({ account }: { account: Account }) => this.unblockAccountByUser(account) | |
284 | }, | |
285 | { | |
286 | label: this.i18n('Mute the instance'), | |
287 | isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false, | |
288 | handler: ({ account }: { account: Account }) => this.blockServerByUser(account.host) | |
289 | }, | |
290 | { | |
291 | label: this.i18n('Unmute the instance'), | |
292 | isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true, | |
293 | handler: ({ account }: { account: Account }) => this.unblockServerByUser(account.host) | |
294 | } | |
295 | ]) | |
296 | ||
297 | // Instance actions | |
298 | if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) { | |
299 | this.userActions = this.userActions.concat([ | |
300 | { | |
301 | label: this.i18n('Mute this account by your instance'), | |
302 | isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === false, | |
303 | handler: ({ account }: { account: Account }) => this.blockAccountByInstance(account) | |
304 | }, | |
305 | { | |
306 | label: this.i18n('Unmute this account by your instance'), | |
307 | isDisplayed: ({ account }: { account: Account }) => account.mutedByInstance === true, | |
308 | handler: ({ account }: { account: Account }) => this.unblockAccountByInstance(account) | |
309 | } | |
310 | ]) | |
311 | } | |
312 | ||
313 | // Instance actions | |
314 | if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) { | |
315 | this.userActions = this.userActions.concat([ | |
316 | { | |
317 | label: this.i18n('Mute the instance by your instance'), | |
318 | isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false, | |
319 | handler: ({ account }: { account: Account }) => this.blockServerByInstance(account.host) | |
320 | }, | |
321 | { | |
322 | label: this.i18n('Unmute the instance by your instance'), | |
323 | isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true, | |
324 | handler: ({ account }: { account: Account }) => this.unblockServerByInstance(account.host) | |
325 | } | |
326 | ]) | |
327 | } | |
328 | } | |
329 | } | |
330 | } | |
331 | } |