]>
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' |
e724fa93 | 7 | import { AuthService, ConfirmService } 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 | |
65b21c96 | 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, | |
35 | private userService: UserService, | |
af5767ff | 36 | private blocklistService: BlocklistService, |
e724fa93 C |
37 | private i18n: I18n |
38 | ) { } | |
39 | ||
af5767ff | 40 | ngOnChanges () { |
79bd2632 | 41 | this.buildActions() |
e724fa93 C |
42 | } |
43 | ||
e724fa93 C |
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 | ||
791645e6 | 62 | this.userService.unbanUsers(user) |
e724fa93 C |
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 | ) | |
79bd2632 | 93 | this.userDeleted.emit() |
e724fa93 C |
94 | }, |
95 | ||
96 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
97 | ) | |
98 | } | |
99 | ||
af5767ff C |
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 | ||
65b21c96 | 109 | this.account.mutedByUser = true |
af5767ff C |
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 | ||
65b21c96 | 126 | this.account.mutedByUser = false |
af5767ff C |
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 | ||
65b21c96 | 143 | this.account.mutedServerByUser = true |
af5767ff C |
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 | ||
65b21c96 C |
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 | |
af5767ff C |
229 | this.userChanged.emit() |
230 | }, | |
231 | ||
232 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
233 | ) | |
234 | } | |
235 | ||
e724fa93 C |
236 | getRouterUserEditLink (user: User) { |
237 | return [ '/admin', 'users', 'update', user.id ] | |
238 | } | |
79bd2632 C |
239 | |
240 | private buildActions () { | |
241 | this.userActions = [] | |
242 | ||
243 | if (this.authService.isLoggedIn()) { | |
244 | const authUser = this.authService.getUser() | |
245 | ||
af5767ff C |
246 | if (this.user && authUser.id === this.user.id) return |
247 | ||
248 | if (this.user && authUser.hasRight(UserRight.MANAGE_USERS)) { | |
79bd2632 C |
249 | this.userActions = this.userActions.concat([ |
250 | { | |
251 | label: this.i18n('Edit'), | |
af5767ff | 252 | linkBuilder: ({ user }) => this.getRouterUserEditLink(user) |
79bd2632 C |
253 | }, |
254 | { | |
255 | label: this.i18n('Delete'), | |
af5767ff | 256 | handler: ({ user }) => this.removeUser(user) |
79bd2632 C |
257 | }, |
258 | { | |
259 | label: this.i18n('Ban'), | |
65b21c96 C |
260 | handler: ({ user }: { user: User }) => this.openBanUserModal(user), |
261 | isDisplayed: ({ user }: { user: User }) => !user.blocked | |
79bd2632 C |
262 | }, |
263 | { | |
264 | label: this.i18n('Unban'), | |
65b21c96 C |
265 | handler: ({ user }: { user: User }) => this.unbanUser(user), |
266 | isDisplayed: ({ user }: { user: User }) => user.blocked | |
af5767ff C |
267 | } |
268 | ]) | |
269 | } | |
270 | ||
65b21c96 | 271 | // Actions on accounts/servers |
af5767ff | 272 | if (this.account) { |
65b21c96 | 273 | // User actions |
af5767ff C |
274 | this.userActions = this.userActions.concat([ |
275 | { | |
276 | label: this.i18n('Mute this account'), | |
65b21c96 C |
277 | isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === false, |
278 | handler: ({ account }: { account: Account }) => this.blockAccountByUser(account) | |
af5767ff C |
279 | }, |
280 | { | |
281 | label: this.i18n('Unmute this account'), | |
65b21c96 C |
282 | isDisplayed: ({ account }: { account: Account }) => account.mutedByUser === true, |
283 | handler: ({ account }: { account: Account }) => this.unblockAccountByUser(account) | |
af5767ff C |
284 | }, |
285 | { | |
286 | label: this.i18n('Mute the instance'), | |
65b21c96 C |
287 | isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === false, |
288 | handler: ({ account }: { account: Account }) => this.blockServerByUser(account.host) | |
af5767ff C |
289 | }, |
290 | { | |
291 | label: this.i18n('Unmute the instance'), | |
65b21c96 C |
292 | isDisplayed: ({ account }: { account: Account }) => !account.userId && account.mutedServerByInstance === true, |
293 | handler: ({ account }: { account: Account }) => this.unblockServerByUser(account.host) | |
79bd2632 C |
294 | } |
295 | ]) | |
65b21c96 C |
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 | } | |
79bd2632 C |
328 | } |
329 | } | |
330 | } | |
e724fa93 | 331 | } |