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