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