diff options
Diffstat (limited to 'client/src/app/shared/users')
-rw-r--r-- | client/src/app/shared/users/user.model.ts | 46 | ||||
-rw-r--r-- | client/src/app/shared/users/user.service.ts | 82 |
2 files changed, 113 insertions, 15 deletions
diff --git a/client/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts index 7707d7dda..3348fe75f 100644 --- a/client/src/app/shared/users/user.model.ts +++ b/client/src/app/shared/users/user.model.ts | |||
@@ -1,10 +1,32 @@ | |||
1 | import { hasUserRight, User as UserServerModel, UserNotificationSetting, UserRight, UserRole, VideoChannel } from '../../../../../shared' | 1 | import { |
2 | hasUserRight, | ||
3 | User as UserServerModel, | ||
4 | UserNotificationSetting, | ||
5 | UserRight, | ||
6 | UserRole | ||
7 | } from '../../../../../shared/models/users' | ||
8 | import { VideoChannel } from '../../../../../shared/models/videos' | ||
2 | import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' | 9 | import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' |
3 | import { Account } from '@app/shared/account/account.model' | 10 | import { Account } from '@app/shared/account/account.model' |
4 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' | 11 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' |
5 | import { UserAdminFlag } from '@shared/models/users/user-flag.model' | 12 | import { UserAdminFlag } from '@shared/models/users/user-flag.model' |
6 | 13 | ||
7 | export class User implements UserServerModel { | 14 | export class User implements UserServerModel { |
15 | static KEYS = { | ||
16 | ID: 'id', | ||
17 | ROLE: 'role', | ||
18 | EMAIL: 'email', | ||
19 | VIDEOS_HISTORY_ENABLED: 'videos-history-enabled', | ||
20 | USERNAME: 'username', | ||
21 | NSFW_POLICY: 'nsfw_policy', | ||
22 | WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', | ||
23 | AUTO_PLAY_VIDEO: 'auto_play_video', | ||
24 | SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video', | ||
25 | AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist', | ||
26 | THEME: 'last_active_theme', | ||
27 | VIDEO_LANGUAGES: 'video_languages' | ||
28 | } | ||
29 | |||
8 | id: number | 30 | id: number |
9 | username: string | 31 | username: string |
10 | email: string | 32 | email: string |
@@ -29,6 +51,11 @@ export class User implements UserServerModel { | |||
29 | videoQuotaDaily: number | 51 | videoQuotaDaily: number |
30 | videoQuotaUsed?: number | 52 | videoQuotaUsed?: number |
31 | videoQuotaUsedDaily?: number | 53 | videoQuotaUsedDaily?: number |
54 | videosCount?: number | ||
55 | videoAbusesCount?: number | ||
56 | videoAbusesAcceptedCount?: number | ||
57 | videoAbusesCreatedCount?: number | ||
58 | videoCommentsCount?: number | ||
32 | 59 | ||
33 | theme: string | 60 | theme: string |
34 | 61 | ||
@@ -42,6 +69,10 @@ export class User implements UserServerModel { | |||
42 | noInstanceConfigWarningModal: boolean | 69 | noInstanceConfigWarningModal: boolean |
43 | noWelcomeModal: boolean | 70 | noWelcomeModal: boolean |
44 | 71 | ||
72 | pluginAuth: string | null | ||
73 | |||
74 | lastLoginDate: Date | null | ||
75 | |||
45 | createdAt: Date | 76 | createdAt: Date |
46 | 77 | ||
47 | constructor (hash: Partial<UserServerModel>) { | 78 | constructor (hash: Partial<UserServerModel>) { |
@@ -57,11 +88,19 @@ export class User implements UserServerModel { | |||
57 | this.videoQuotaDaily = hash.videoQuotaDaily | 88 | this.videoQuotaDaily = hash.videoQuotaDaily |
58 | this.videoQuotaUsed = hash.videoQuotaUsed | 89 | this.videoQuotaUsed = hash.videoQuotaUsed |
59 | this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily | 90 | this.videoQuotaUsedDaily = hash.videoQuotaUsedDaily |
91 | this.videosCount = hash.videosCount | ||
92 | this.videoAbusesCount = hash.videoAbusesCount | ||
93 | this.videoAbusesAcceptedCount = hash.videoAbusesAcceptedCount | ||
94 | this.videoAbusesCreatedCount = hash.videoAbusesCreatedCount | ||
95 | this.videoCommentsCount = hash.videoCommentsCount | ||
60 | 96 | ||
61 | this.nsfwPolicy = hash.nsfwPolicy | 97 | this.nsfwPolicy = hash.nsfwPolicy |
62 | this.webTorrentEnabled = hash.webTorrentEnabled | 98 | this.webTorrentEnabled = hash.webTorrentEnabled |
63 | this.videosHistoryEnabled = hash.videosHistoryEnabled | ||
64 | this.autoPlayVideo = hash.autoPlayVideo | 99 | this.autoPlayVideo = hash.autoPlayVideo |
100 | this.autoPlayNextVideo = hash.autoPlayNextVideo | ||
101 | this.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylist | ||
102 | this.videosHistoryEnabled = hash.videosHistoryEnabled | ||
103 | this.videoLanguages = hash.videoLanguages | ||
65 | 104 | ||
66 | this.theme = hash.theme | 105 | this.theme = hash.theme |
67 | 106 | ||
@@ -77,6 +116,9 @@ export class User implements UserServerModel { | |||
77 | 116 | ||
78 | this.createdAt = hash.createdAt | 117 | this.createdAt = hash.createdAt |
79 | 118 | ||
119 | this.pluginAuth = hash.pluginAuth | ||
120 | this.lastLoginDate = hash.lastLoginDate | ||
121 | |||
80 | if (hash.account !== undefined) { | 122 | if (hash.account !== undefined) { |
81 | this.account = new Account(hash.account) | 123 | this.account = new Account(hash.account) |
82 | } | 124 | } |
diff --git a/client/src/app/shared/users/user.service.ts b/client/src/app/shared/users/user.service.ts index e24d91df3..abb4088b5 100644 --- a/client/src/app/shared/users/user.service.ts +++ b/client/src/app/shared/users/user.service.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import { from, Observable, of } from 'rxjs' | 1 | import { from, Observable } from 'rxjs' |
2 | import { catchError, concatMap, map, share, shareReplay, tap, toArray } from 'rxjs/operators' | 2 | import { catchError, concatMap, map, shareReplay, toArray } from 'rxjs/operators' |
3 | import { HttpClient, HttpParams } from '@angular/common/http' | 3 | import { HttpClient, HttpParams } from '@angular/common/http' |
4 | import { Injectable } from '@angular/core' | 4 | import { Injectable } from '@angular/core' |
5 | import { ResultList, User, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' | 5 | import { ResultList, User as UserServerModel, UserCreate, UserRole, UserUpdate, UserUpdateMe, UserVideoQuota } from '../../../../../shared' |
6 | import { environment } from '../../../environments/environment' | 6 | import { environment } from '../../../environments/environment' |
7 | import { RestExtractor, RestPagination, RestService } from '../rest' | 7 | import { RestExtractor, RestPagination, RestService } from '../rest' |
8 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' | 8 | import { Avatar } from '../../../../../shared/models/avatars/avatar.model' |
@@ -10,6 +10,10 @@ import { SortMeta } from 'primeng/api' | |||
10 | import { BytesPipe } from 'ngx-pipes' | 10 | import { BytesPipe } from 'ngx-pipes' |
11 | import { I18n } from '@ngx-translate/i18n-polyfill' | 11 | import { I18n } from '@ngx-translate/i18n-polyfill' |
12 | import { UserRegister } from '@shared/models/users/user-register.model' | 12 | import { UserRegister } from '@shared/models/users/user-register.model' |
13 | import { User } from './user.model' | ||
14 | import { NSFWPolicyType } from '@shared/models/videos/nsfw-policy.type' | ||
15 | import { has } from 'lodash-es' | ||
16 | import { LocalStorageService, SessionStorageService } from '../misc/storage.service' | ||
13 | 17 | ||
14 | @Injectable() | 18 | @Injectable() |
15 | export class UserService { | 19 | export class UserService { |
@@ -17,12 +21,14 @@ export class UserService { | |||
17 | 21 | ||
18 | private bytesPipe = new BytesPipe() | 22 | private bytesPipe = new BytesPipe() |
19 | 23 | ||
20 | private userCache: { [ id: number ]: Observable<User> } = {} | 24 | private userCache: { [ id: number ]: Observable<UserServerModel> } = {} |
21 | 25 | ||
22 | constructor ( | 26 | constructor ( |
23 | private authHttp: HttpClient, | 27 | private authHttp: HttpClient, |
24 | private restExtractor: RestExtractor, | 28 | private restExtractor: RestExtractor, |
25 | private restService: RestService, | 29 | private restService: RestService, |
30 | private localStorageService: LocalStorageService, | ||
31 | private sessionStorageService: SessionStorageService, | ||
26 | private i18n: I18n | 32 | private i18n: I18n |
27 | ) { } | 33 | ) { } |
28 | 34 | ||
@@ -64,6 +70,30 @@ export class UserService { | |||
64 | ) | 70 | ) |
65 | } | 71 | } |
66 | 72 | ||
73 | updateMyAnonymousProfile (profile: UserUpdateMe) { | ||
74 | const supportedKeys = { | ||
75 | // local storage keys | ||
76 | nsfwPolicy: (val: NSFWPolicyType) => this.localStorageService.setItem(User.KEYS.NSFW_POLICY, val), | ||
77 | webTorrentEnabled: (val: boolean) => this.localStorageService.setItem(User.KEYS.WEBTORRENT_ENABLED, String(val)), | ||
78 | autoPlayVideo: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO, String(val)), | ||
79 | autoPlayNextVideoPlaylist: (val: boolean) => this.localStorageService.setItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST, String(val)), | ||
80 | theme: (val: string) => this.localStorageService.setItem(User.KEYS.THEME, val), | ||
81 | videoLanguages: (val: string[]) => this.localStorageService.setItem(User.KEYS.VIDEO_LANGUAGES, JSON.stringify(val)), | ||
82 | |||
83 | // session storage keys | ||
84 | autoPlayNextVideo: (val: boolean) => | ||
85 | this.sessionStorageService.setItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, String(val)) | ||
86 | } | ||
87 | |||
88 | for (const key of Object.keys(profile)) { | ||
89 | try { | ||
90 | if (has(supportedKeys, key)) supportedKeys[key](profile[key]) | ||
91 | } catch (err) { | ||
92 | console.error(`Cannot set item ${key} in localStorage. Likely due to a value impossible to stringify.`, err) | ||
93 | } | ||
94 | } | ||
95 | } | ||
96 | |||
67 | deleteMe () { | 97 | deleteMe () { |
68 | const url = UserService.BASE_USERS_URL + 'me' | 98 | const url = UserService.BASE_USERS_URL + 'me' |
69 | 99 | ||
@@ -187,7 +217,7 @@ export class UserService { | |||
187 | ) | 217 | ) |
188 | } | 218 | } |
189 | 219 | ||
190 | updateUsers (users: User[], userUpdate: UserUpdate) { | 220 | updateUsers (users: UserServerModel[], userUpdate: UserUpdate) { |
191 | return from(users) | 221 | return from(users) |
192 | .pipe( | 222 | .pipe( |
193 | concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)), | 223 | concatMap(u => this.authHttp.put(UserService.BASE_USERS_URL + u.id, userUpdate)), |
@@ -204,18 +234,44 @@ export class UserService { | |||
204 | return this.userCache[userId] | 234 | return this.userCache[userId] |
205 | } | 235 | } |
206 | 236 | ||
207 | getUser (userId: number) { | 237 | getUser (userId: number, withStats = false) { |
208 | return this.authHttp.get<User>(UserService.BASE_USERS_URL + userId) | 238 | const params = new HttpParams().append('withStats', withStats + '') |
239 | return this.authHttp.get<UserServerModel>(UserService.BASE_USERS_URL + userId, { params }) | ||
209 | .pipe(catchError(err => this.restExtractor.handleError(err))) | 240 | .pipe(catchError(err => this.restExtractor.handleError(err))) |
210 | } | 241 | } |
211 | 242 | ||
212 | getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<User>> { | 243 | getAnonymousUser () { |
244 | let videoLanguages | ||
245 | |||
246 | try { | ||
247 | videoLanguages = JSON.parse(this.localStorageService.getItem(User.KEYS.VIDEO_LANGUAGES)) | ||
248 | } catch (err) { | ||
249 | videoLanguages = null | ||
250 | console.error('Cannot parse desired video languages from localStorage.', err) | ||
251 | } | ||
252 | |||
253 | return new User({ | ||
254 | // local storage keys | ||
255 | nsfwPolicy: this.localStorageService.getItem(User.KEYS.NSFW_POLICY) as NSFWPolicyType, | ||
256 | webTorrentEnabled: this.localStorageService.getItem(User.KEYS.WEBTORRENT_ENABLED) !== 'false', | ||
257 | theme: this.localStorageService.getItem(User.KEYS.THEME) || 'default', | ||
258 | videoLanguages, | ||
259 | |||
260 | autoPlayNextVideoPlaylist: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false', | ||
261 | autoPlayVideo: this.localStorageService.getItem(User.KEYS.AUTO_PLAY_VIDEO) === 'true', | ||
262 | |||
263 | // session storage keys | ||
264 | autoPlayNextVideo: this.sessionStorageService.getItem(User.KEYS.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' | ||
265 | }) | ||
266 | } | ||
267 | |||
268 | getUsers (pagination: RestPagination, sort: SortMeta, search?: string): Observable<ResultList<UserServerModel>> { | ||
213 | let params = new HttpParams() | 269 | let params = new HttpParams() |
214 | params = this.restService.addRestGetParams(params, pagination, sort) | 270 | params = this.restService.addRestGetParams(params, pagination, sort) |
215 | 271 | ||
216 | if (search) params = params.append('search', search) | 272 | if (search) params = params.append('search', search) |
217 | 273 | ||
218 | return this.authHttp.get<ResultList<User>>(UserService.BASE_USERS_URL, { params }) | 274 | return this.authHttp.get<ResultList<UserServerModel>>(UserService.BASE_USERS_URL, { params }) |
219 | .pipe( | 275 | .pipe( |
220 | map(res => this.restExtractor.convertResultListDateToHuman(res)), | 276 | map(res => this.restExtractor.convertResultListDateToHuman(res)), |
221 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), | 277 | map(res => this.restExtractor.applyToResultListData(res, this.formatUser.bind(this))), |
@@ -223,7 +279,7 @@ export class UserService { | |||
223 | ) | 279 | ) |
224 | } | 280 | } |
225 | 281 | ||
226 | removeUser (usersArg: User | User[]) { | 282 | removeUser (usersArg: UserServerModel | UserServerModel[]) { |
227 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | 283 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] |
228 | 284 | ||
229 | return from(users) | 285 | return from(users) |
@@ -234,7 +290,7 @@ export class UserService { | |||
234 | ) | 290 | ) |
235 | } | 291 | } |
236 | 292 | ||
237 | banUsers (usersArg: User | User[], reason?: string) { | 293 | banUsers (usersArg: UserServerModel | UserServerModel[], reason?: string) { |
238 | const body = reason ? { reason } : {} | 294 | const body = reason ? { reason } : {} |
239 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | 295 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] |
240 | 296 | ||
@@ -246,7 +302,7 @@ export class UserService { | |||
246 | ) | 302 | ) |
247 | } | 303 | } |
248 | 304 | ||
249 | unbanUsers (usersArg: User | User[]) { | 305 | unbanUsers (usersArg: UserServerModel | UserServerModel[]) { |
250 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] | 306 | const users = Array.isArray(usersArg) ? usersArg : [ usersArg ] |
251 | 307 | ||
252 | return from(users) | 308 | return from(users) |
@@ -257,7 +313,7 @@ export class UserService { | |||
257 | ) | 313 | ) |
258 | } | 314 | } |
259 | 315 | ||
260 | private formatUser (user: User) { | 316 | private formatUser (user: UserServerModel) { |
261 | let videoQuota | 317 | let videoQuota |
262 | if (user.videoQuota === -1) { | 318 | if (user.videoQuota === -1) { |
263 | videoQuota = this.i18n('Unlimited') | 319 | videoQuota = this.i18n('Unlimited') |