]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-moderation/user-moderation-dropdown.component.ts
reword intellectual property
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-moderation / user-moderation-dropdown.component.ts
CommitLineData
ba430d75 1import { Component, EventEmitter, Input, OnChanges, OnInit, Output, ViewChild } from '@angular/core'
67ed6552
C
2import { AuthService, ConfirmService, Notifier, ServerService, UserService } from '@app/core'
3import { Account, DropdownAction } from '@app/shared/shared-main'
e724fa93 4import { I18n } from '@ngx-translate/i18n-polyfill'
67ed6552
C
5import { BulkRemoveCommentsOfBody, ServerConfig, User, UserRight } from '@shared/models'
6import { BlocklistService } from './blocklist.service'
7import { BulkService } from './bulk.service'
8import { UserBanModalComponent } from './user-ban-modal.component'
e724fa93
C
9
10@Component({
11 selector: 'my-user-moderation-dropdown',
f421fa06 12 templateUrl: './user-moderation-dropdown.component.html'
e724fa93 13})
ba430d75 14export class UserModerationDropdownComponent implements OnInit, OnChanges {
2f5d2ec5 15 @ViewChild('userBanModal') userBanModal: UserBanModalComponent
e724fa93
C
16
17 @Input() user: User
af5767ff 18 @Input() account: Account
8ca56654 19 @Input() prependActions: DropdownAction<{ user: User, account: Account }>[]
af5767ff 20
79bd2632 21 @Input() buttonSize: 'normal' | 'small' = 'normal'
30814423 22 @Input() placement = 'right-top right-bottom auto'
edf1a4e5 23 @Input() label: string
5ff52366 24 @Input() container: 'body' | undefined = undefined
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
ba430d75
C
31 private serverConfig: ServerConfig
32
e724fa93
C
33 constructor (
34 private authService: AuthService,
f8b2c1b4 35 private notifier: Notifier,
e724fa93 36 private confirmService: ConfirmService,
fc2ec87a 37 private serverService: ServerService,
e724fa93 38 private userService: UserService,
af5767ff 39 private blocklistService: BlocklistService,
923ff87d 40 private bulkService: BulkService,
e724fa93
C
41 private i18n: I18n
42 ) { }
43
fc2ec87a 44 get requiresEmailVerification () {
ba430d75
C
45 return this.serverConfig.signup.requiresEmailVerification
46 }
47
48 ngOnInit (): void {
49 this.serverConfig = this.serverService.getTmpConfig()
50 this.serverService.getConfig()
51 .subscribe(config => this.serverConfig = config)
fc2ec87a
JM
52 }
53
af5767ff 54 ngOnChanges () {
79bd2632 55 this.buildActions()
e724fa93
C
56 }
57
e724fa93
C
58 openBanUserModal (user: User) {
59 if (user.username === 'root') {
f8b2c1b4 60 this.notifier.error(this.i18n('You cannot ban root.'))
e724fa93
C
61 return
62 }
63
64 this.userBanModal.openModal(user)
65 }
66
67 onUserBanned () {
68 this.userChanged.emit()
69 }
70
71 async unbanUser (user: User) {
72 const message = this.i18n('Do you really want to unban {{username}}?', { username: user.username })
73 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
74 if (res === false) return
75
791645e6 76 this.userService.unbanUsers(user)
e724fa93
C
77 .subscribe(
78 () => {
f8b2c1b4 79 this.notifier.success(this.i18n('User {{username}} unbanned.', { username: user.username }))
e724fa93
C
80
81 this.userChanged.emit()
82 },
83
f8b2c1b4 84 err => this.notifier.error(err.message)
e724fa93
C
85 )
86 }
87
88 async removeUser (user: User) {
89 if (user.username === 'root') {
f8b2c1b4 90 this.notifier.error(this.i18n('You cannot delete root.'))
e724fa93
C
91 return
92 }
93
94 const message = this.i18n('If you remove this user, you will not be able to create another with the same username!')
95 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
96 if (res === false) return
97
98 this.userService.removeUser(user).subscribe(
99 () => {
f8b2c1b4 100 this.notifier.success(this.i18n('User {{username}} deleted.', { username: user.username }))
79bd2632 101 this.userDeleted.emit()
e724fa93
C
102 },
103
f8b2c1b4 104 err => this.notifier.error(err.message)
e724fa93
C
105 )
106 }
107
fc2ec87a
JM
108 setEmailAsVerified (user: User) {
109 this.userService.updateUser(user.id, { emailVerified: true }).subscribe(
110 () => {
f8b2c1b4 111 this.notifier.success(this.i18n('User {{username}} email set as verified', { username: user.username }))
a99e2d94
C
112
113 this.userChanged.emit()
fc2ec87a
JM
114 },
115
f8b2c1b4 116 err => this.notifier.error(err.message)
fc2ec87a
JM
117 )
118 }
119
af5767ff
C
120 blockAccountByUser (account: Account) {
121 this.blocklistService.blockAccountByUser(account)
122 .subscribe(
123 () => {
f8b2c1b4 124 this.notifier.success(this.i18n('Account {{nameWithHost}} muted.', { nameWithHost: account.nameWithHost }))
af5767ff 125
65b21c96 126 this.account.mutedByUser = true
af5767ff
C
127 this.userChanged.emit()
128 },
129
f8b2c1b4 130 err => this.notifier.error(err.message)
af5767ff
C
131 )
132 }
133
134 unblockAccountByUser (account: Account) {
135 this.blocklistService.unblockAccountByUser(account)
136 .subscribe(
137 () => {
f8b2c1b4 138 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted.', { nameWithHost: account.nameWithHost }))
af5767ff 139
65b21c96 140 this.account.mutedByUser = false
af5767ff
C
141 this.userChanged.emit()
142 },
143
f8b2c1b4 144 err => this.notifier.error(err.message)
af5767ff
C
145 )
146 }
147
148 blockServerByUser (host: string) {
149 this.blocklistService.blockServerByUser(host)
150 .subscribe(
151 () => {
f8b2c1b4 152 this.notifier.success(this.i18n('Instance {{host}} muted.', { host }))
af5767ff 153
65b21c96 154 this.account.mutedServerByUser = true
af5767ff
C
155 this.userChanged.emit()
156 },
157
f8b2c1b4 158 err => this.notifier.error(err.message)
af5767ff
C
159 )
160 }
161
162 unblockServerByUser (host: string) {
163 this.blocklistService.unblockServerByUser(host)
164 .subscribe(
165 () => {
f8b2c1b4 166 this.notifier.success(this.i18n('Instance {{host}} unmuted.', { host }))
af5767ff 167
65b21c96
C
168 this.account.mutedServerByUser = false
169 this.userChanged.emit()
170 },
171
f8b2c1b4 172 err => this.notifier.error(err.message)
65b21c96
C
173 )
174 }
175
176 blockAccountByInstance (account: Account) {
177 this.blocklistService.blockAccountByInstance(account)
178 .subscribe(
179 () => {
f8b2c1b4 180 this.notifier.success(this.i18n('Account {{nameWithHost}} muted by the instance.', { nameWithHost: account.nameWithHost }))
65b21c96
C
181
182 this.account.mutedByInstance = true
183 this.userChanged.emit()
184 },
185
f8b2c1b4 186 err => this.notifier.error(err.message)
65b21c96
C
187 )
188 }
189
190 unblockAccountByInstance (account: Account) {
191 this.blocklistService.unblockAccountByInstance(account)
192 .subscribe(
193 () => {
f8b2c1b4 194 this.notifier.success(this.i18n('Account {{nameWithHost}} unmuted by the instance.', { nameWithHost: account.nameWithHost }))
65b21c96
C
195
196 this.account.mutedByInstance = false
197 this.userChanged.emit()
198 },
199
f8b2c1b4 200 err => this.notifier.error(err.message)
65b21c96
C
201 )
202 }
203
204 blockServerByInstance (host: string) {
205 this.blocklistService.blockServerByInstance(host)
206 .subscribe(
207 () => {
f8b2c1b4 208 this.notifier.success(this.i18n('Instance {{host}} muted by the instance.', { host }))
65b21c96
C
209
210 this.account.mutedServerByInstance = true
211 this.userChanged.emit()
212 },
213
f8b2c1b4 214 err => this.notifier.error(err.message)
65b21c96
C
215 )
216 }
217
218 unblockServerByInstance (host: string) {
219 this.blocklistService.unblockServerByInstance(host)
220 .subscribe(
221 () => {
f8b2c1b4 222 this.notifier.success(this.i18n('Instance {{host}} unmuted by the instance.', { host }))
65b21c96
C
223
224 this.account.mutedServerByInstance = false
af5767ff
C
225 this.userChanged.emit()
226 },
227
f8b2c1b4 228 err => this.notifier.error(err.message)
af5767ff
C
229 )
230 }
231
923ff87d
C
232 async bulkRemoveCommentsOf (body: BulkRemoveCommentsOfBody) {
233 const message = this.i18n('Are you sure you want to remove all the comments of this account?')
234 const res = await this.confirmService.confirm(message, this.i18n('Delete account comments'))
235 if (res === false) return
236
237 this.bulkService.removeCommentsOf(body)
238 .subscribe(
239 () => {
240 this.notifier.success(this.i18n('Will remove comments of this account (may take several minutes).'))
241 },
242
243 err => this.notifier.error(err.message)
244 )
245 }
246
e724fa93
C
247 getRouterUserEditLink (user: User) {
248 return [ '/admin', 'users', 'update', user.id ]
249 }
79bd2632
C
250
251 private buildActions () {
252 this.userActions = []
253
8ca56654
C
254 if (this.prependActions) {
255 this.userActions = [
256 this.prependActions
257 ]
258 }
259
79bd2632
C
260 if (this.authService.isLoggedIn()) {
261 const authUser = this.authService.getUser()
262
af5767ff
C
263 if (this.user && authUser.id === this.user.id) return
264
a95a4cc8 265 if (this.user && authUser.hasRight(UserRight.MANAGE_USERS) && authUser.canManage(this.user)) {
f97c91f7 266 this.userActions.push([
79bd2632 267 {
f0ad4710 268 label: this.i18n('Edit user'),
9b82d49d 269 description: this.i18n('Change quota, role, and more.'),
af5767ff 270 linkBuilder: ({ user }) => this.getRouterUserEditLink(user)
79bd2632
C
271 },
272 {
f0ad4710 273 label: this.i18n('Delete user'),
9b82d49d 274 description: this.i18n('Videos will be deleted, comments will be tombstoned.'),
af5767ff 275 handler: ({ user }) => this.removeUser(user)
79bd2632
C
276 },
277 {
9b82d49d 278 label: this.i18n('Ban'),
f0ad4710 279 description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'),
4e74e803
C
280 handler: ({ user }) => this.openBanUserModal(user),
281 isDisplayed: ({ user }) => !user.blocked
79bd2632
C
282 },
283 {
edf1a4e5 284 label: this.i18n('Unban user'),
9b82d49d 285 description: this.i18n('Allow the user to login and create videos/comments again'),
4e74e803
C
286 handler: ({ user }) => this.unbanUser(user),
287 isDisplayed: ({ user }) => user.blocked
fc2ec87a
JM
288 },
289 {
290 label: this.i18n('Set Email as Verified'),
4e74e803
C
291 handler: ({ user }) => this.setEmailAsVerified(user),
292 isDisplayed: ({ user }) => this.requiresEmailVerification && !user.blocked && user.emailVerified === false
af5767ff
C
293 }
294 ])
295 }
296
65b21c96 297 // Actions on accounts/servers
af5767ff 298 if (this.account) {
65b21c96 299 // User actions
f97c91f7 300 this.userActions.push([
af5767ff
C
301 {
302 label: this.i18n('Mute this account'),
9b82d49d 303 description: this.i18n('Hide any content from that user for you.'),
4e74e803
C
304 isDisplayed: ({ account }) => account.mutedByUser === false,
305 handler: ({ account }) => this.blockAccountByUser(account)
af5767ff
C
306 },
307 {
308 label: this.i18n('Unmute this account'),
9b82d49d 309 description: this.i18n('Show back content from that user for you.'),
4e74e803
C
310 isDisplayed: ({ account }) => account.mutedByUser === true,
311 handler: ({ account }) => this.unblockAccountByUser(account)
af5767ff
C
312 },
313 {
314 label: this.i18n('Mute the instance'),
9b82d49d 315 description: this.i18n('Hide any content from that instance for you.'),
4e74e803
C
316 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
317 handler: ({ account }) => this.blockServerByUser(account.host)
af5767ff
C
318 },
319 {
320 label: this.i18n('Unmute the instance'),
9b82d49d 321 description: this.i18n('Show back content from that instance for you.'),
4e74e803
C
322 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
323 handler: ({ account }) => this.unblockServerByUser(account.host)
923ff87d
C
324 },
325 {
326 label: this.i18n('Remove comments from your videos'),
327 description: this.i18n('Remove comments of this account from your videos.'),
328 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'my-videos' })
79bd2632
C
329 }
330 ])
65b21c96 331
f97c91f7
C
332 let instanceActions: DropdownAction<{ user: User, account: Account }>[] = []
333
923ff87d 334 // Instance actions on account blocklists
65b21c96 335 if (authUser.hasRight(UserRight.MANAGE_ACCOUNTS_BLOCKLIST)) {
f97c91f7 336 instanceActions = instanceActions.concat([
65b21c96
C
337 {
338 label: this.i18n('Mute this account by your instance'),
9b82d49d 339 description: this.i18n('Hide any content from that user for you, your instance and its users.'),
4e74e803
C
340 isDisplayed: ({ account }) => account.mutedByInstance === false,
341 handler: ({ account }) => this.blockAccountByInstance(account)
65b21c96
C
342 },
343 {
344 label: this.i18n('Unmute this account by your instance'),
9b82d49d 345 description: this.i18n('Show back content from that user for you, your instance and its users.'),
4e74e803
C
346 isDisplayed: ({ account }) => account.mutedByInstance === true,
347 handler: ({ account }) => this.unblockAccountByInstance(account)
65b21c96
C
348 }
349 ])
350 }
351
923ff87d 352 // Instance actions on server blocklists
65b21c96 353 if (authUser.hasRight(UserRight.MANAGE_SERVERS_BLOCKLIST)) {
f97c91f7 354 instanceActions = instanceActions.concat([
65b21c96
C
355 {
356 label: this.i18n('Mute the instance by your instance'),
9b82d49d 357 description: this.i18n('Hide any content from that instance for you, your instance and its users.'),
4e74e803
C
358 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === false,
359 handler: ({ account }) => this.blockServerByInstance(account.host)
65b21c96
C
360 },
361 {
362 label: this.i18n('Unmute the instance by your instance'),
9b82d49d 363 description: this.i18n('Show back content from that instance for you, your instance and its users.'),
4e74e803
C
364 isDisplayed: ({ account }) => !account.userId && account.mutedServerByInstance === true,
365 handler: ({ account }) => this.unblockServerByInstance(account.host)
65b21c96
C
366 }
367 ])
368 }
f97c91f7 369
923ff87d
C
370 if (authUser.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT)) {
371 instanceActions = instanceActions.concat([
372 {
373 label: this.i18n('Remove comments from your instance'),
374 description: this.i18n('Remove comments of this account from your instance.'),
375 handler: ({ account }) => this.bulkRemoveCommentsOf({ accountName: account.nameWithHost, scope: 'instance' })
376 }
377 ])
378 }
379
f97c91f7
C
380 if (instanceActions.length !== 0) {
381 this.userActions.push(instanceActions)
382 }
79bd2632
C
383 }
384 }
385 }
e724fa93 386}