]>
Commit | Line | Data |
---|---|---|
791645e6 | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
a95a4cc8 | 2 | import { AuthService, Notifier } from '@app/core' |
f77eb73b | 3 | import { SortMeta } from 'primeng/api' |
fc2ec87a | 4 | import { ConfirmService, ServerService } from '../../../core' |
e724fa93 | 5 | import { RestPagination, RestTable, UserService } from '../../../shared' |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
ba430d75 | 7 | import { ServerConfig, User } from '../../../../../../shared' |
791645e6 C |
8 | import { UserBanModalComponent } from '@app/shared/moderation' |
9 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' | |
d6af8146 | 10 | import { Actor } from '@app/shared/actor/actor.model' |
7da18e44 C |
11 | |
12 | @Component({ | |
13 | selector: 'my-user-list', | |
ec8d8440 C |
14 | templateUrl: './user-list.component.html', |
15 | styleUrls: [ './user-list.component.scss' ] | |
7da18e44 | 16 | }) |
ab998f7b | 17 | export class UserListComponent extends RestTable implements OnInit { |
f36da21e | 18 | @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent |
791645e6 | 19 | |
d592e0a9 C |
20 | users: User[] = [] |
21 | totalRecords = 0 | |
ab998f7b | 22 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 23 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
141b177d | 24 | |
791645e6 | 25 | selectedUsers: User[] = [] |
9b82d49d | 26 | bulkUserActions: DropdownAction<User[]>[][] = [] |
791645e6 | 27 | |
ba430d75 C |
28 | private serverConfig: ServerConfig |
29 | ||
df98563e | 30 | constructor ( |
f8b2c1b4 | 31 | private notifier: Notifier, |
5769e1db | 32 | private confirmService: ConfirmService, |
fc2ec87a | 33 | private serverService: ServerService, |
b1d40cff | 34 | private userService: UserService, |
a95a4cc8 | 35 | private auth: AuthService, |
b1d40cff | 36 | private i18n: I18n |
28798b5d | 37 | ) { |
d592e0a9 | 38 | super() |
7da18e44 C |
39 | } |
40 | ||
a95a4cc8 C |
41 | get authUser () { |
42 | return this.auth.getUser() | |
43 | } | |
44 | ||
fc2ec87a | 45 | get requiresEmailVerification () { |
ba430d75 | 46 | return this.serverConfig.signup.requiresEmailVerification |
fc2ec87a JM |
47 | } |
48 | ||
ab998f7b | 49 | ngOnInit () { |
ba430d75 C |
50 | this.serverConfig = this.serverService.getTmpConfig() |
51 | this.serverService.getConfig() | |
52 | .subscribe(config => this.serverConfig = config) | |
53 | ||
24b9417c | 54 | this.initialize() |
ab998f7b | 55 | |
791645e6 | 56 | this.bulkUserActions = [ |
9b82d49d RK |
57 | [ |
58 | { | |
59 | label: this.i18n('Delete'), | |
60 | description: this.i18n('Videos will be deleted, comments will be tombstoned.'), | |
61 | handler: users => this.removeUsers(users), | |
62 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
63 | }, | |
64 | { | |
65 | label: this.i18n('Ban'), | |
f0ad4710 | 66 | description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'), |
9b82d49d RK |
67 | handler: users => this.openBanUserModal(users), |
68 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) | |
69 | }, | |
70 | { | |
71 | label: this.i18n('Unban'), | |
72 | handler: users => this.unbanUsers(users), | |
73 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) | |
a95a4cc8 | 74 | } |
9b82d49d RK |
75 | ], |
76 | [ | |
77 | { | |
78 | label: this.i18n('Set Email as Verified'), | |
79 | handler: users => this.setEmailsAsVerified(users), | |
80 | isDisplayed: users => { | |
81 | return this.requiresEmailVerification && | |
82 | users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false) | |
83 | } | |
84 | } | |
85 | ] | |
791645e6 | 86 | ] |
141b177d C |
87 | } |
88 | ||
8e11a1b3 C |
89 | getIdentifier () { |
90 | return 'UserListComponent' | |
91 | } | |
92 | ||
791645e6 C |
93 | openBanUserModal (users: User[]) { |
94 | for (const user of users) { | |
95 | if (user.username === 'root') { | |
f8b2c1b4 | 96 | this.notifier.error(this.i18n('You cannot ban root.')) |
791645e6 C |
97 | return |
98 | } | |
99 | } | |
100 | ||
101 | this.userBanModal.openModal(users) | |
102 | } | |
103 | ||
2fbe7f19 | 104 | onUserChanged () { |
791645e6 C |
105 | this.loadData() |
106 | } | |
107 | ||
d6af8146 RK |
108 | switchToDefaultAvatar ($event: Event) { |
109 | ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL() | |
110 | } | |
111 | ||
791645e6 C |
112 | async unbanUsers (users: User[]) { |
113 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
114 | ||
115 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
116 | if (res === false) return | |
117 | ||
118 | this.userService.unbanUsers(users) | |
119 | .subscribe( | |
120 | () => { | |
121 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
122 | ||
f8b2c1b4 | 123 | this.notifier.success(message) |
791645e6 C |
124 | this.loadData() |
125 | }, | |
126 | ||
f8b2c1b4 | 127 | err => this.notifier.error(err.message) |
791645e6 C |
128 | ) |
129 | } | |
130 | ||
131 | async removeUsers (users: User[]) { | |
132 | for (const user of users) { | |
133 | if (user.username === 'root') { | |
f8b2c1b4 | 134 | this.notifier.error(this.i18n('You cannot delete root.')) |
791645e6 C |
135 | return |
136 | } | |
137 | } | |
138 | ||
139 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
140 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
141 | if (res === false) return | |
142 | ||
143 | this.userService.removeUser(users).subscribe( | |
144 | () => { | |
f8b2c1b4 | 145 | this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length })) |
791645e6 C |
146 | this.loadData() |
147 | }, | |
148 | ||
f8b2c1b4 | 149 | err => this.notifier.error(err.message) |
791645e6 C |
150 | ) |
151 | } | |
152 | ||
fc2ec87a JM |
153 | async setEmailsAsVerified (users: User[]) { |
154 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
155 | () => { | |
f8b2c1b4 | 156 | this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length })) |
fc2ec87a JM |
157 | this.loadData() |
158 | }, | |
159 | ||
f8b2c1b4 | 160 | err => this.notifier.error(err.message) |
fc2ec87a JM |
161 | ) |
162 | } | |
163 | ||
791645e6 C |
164 | isInSelectionMode () { |
165 | return this.selectedUsers.length !== 0 | |
166 | } | |
dffd5d12 B |
167 | |
168 | protected loadData () { | |
169 | this.selectedUsers = [] | |
170 | ||
171 | this.userService.getUsers(this.pagination, this.sort, this.search) | |
f8b2c1b4 C |
172 | .subscribe( |
173 | resultList => { | |
174 | this.users = resultList.data | |
175 | this.totalRecords = resultList.total | |
176 | }, | |
177 | ||
178 | err => this.notifier.error(err.message) | |
179 | ) | |
dffd5d12 | 180 | } |
7da18e44 | 181 | } |