import { SortMeta } from 'primeng/api'
import { from, Observable, of } from 'rxjs'
-import { catchError, concatMap, filter, first, map, shareReplay, throttleTime, toArray } from 'rxjs/operators'
+import { catchError, concatMap, first, map, shareReplay, tap, toArray } from 'rxjs/operators'
import { HttpClient, HttpParams } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { AuthService } from '@app/core/auth'
import { getBytes } from '@root-helpers/bytes'
-import { UserLocalStorageKeys } from '@root-helpers/users'
import {
- Avatar,
- NSFWPolicyType,
+ ActorImage,
ResultList,
User as UserServerModel,
UserCreate,
} from '@shared/models'
import { environment } from '../../../environments/environment'
import { RestExtractor, RestPagination, RestService } from '../rest'
-import { LocalStorageService, SessionStorageService } from '../wrappers/storage.service'
+import { UserLocalStorageService } from './'
import { User } from './user.model'
@Injectable()
private userCache: { [ id: number ]: Observable<UserServerModel> } = {}
+ private signupInThisSession = false
+
constructor (
private authHttp: HttpClient,
private authService: AuthService,
private restExtractor: RestExtractor,
private restService: RestService,
- private localStorageService: LocalStorageService,
- private sessionStorageService: SessionStorageService
- ) { }
+ private userLocalStorageService: UserLocalStorageService
+ ) { }
+
+ hasSignupInThisSession () {
+ return this.signupInThisSession
+ }
changePassword (currentPassword: string, newPassword: string) {
const url = UserService.BASE_USERS_URL + 'me'
}
return this.authHttp.put(url, body)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
changeEmail (password: string, newEmail: string) {
}
return this.authHttp.put(url, body)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
- updateMyProfile (profile: UserUpdateMe) {
- const url = UserService.BASE_USERS_URL + 'me'
-
- return this.authHttp.put(url, profile)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
- }
+ // ---------------------------------------------------------------------------
updateMyAnonymousProfile (profile: UserUpdateMe) {
- const localStorageKeys: { [ id in keyof UserUpdateMe ]: string } = {
- nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY,
- webTorrentEnabled: UserLocalStorageKeys.WEBTORRENT_ENABLED,
- autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO,
- autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
- theme: UserLocalStorageKeys.THEME,
- videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES
- }
+ this.userLocalStorageService.setUserInfo(profile)
+ }
- const obj = Object.keys(localStorageKeys)
- .filter(key => key in profile)
- .map(key => ([ localStorageKeys[key], profile[key] ]))
+ listenAnonymousUpdate () {
+ return this.userLocalStorageService.listenUserInfoChange()
+ .pipe(map(() => this.getAnonymousUser()))
+ }
- for (const [ key, value ] of obj) {
- try {
- if (!value) {
- this.localStorageService.removeItem(key)
- continue
- }
+ getAnonymousUser () {
+ return new User(this.userLocalStorageService.getUserInfo())
+ }
- const localStorageValue = typeof value === 'string'
- ? value
- : JSON.stringify(value)
+ // ---------------------------------------------------------------------------
- this.localStorageService.setItem(key, localStorageValue)
- } catch (err) {
- console.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err)
- }
- }
- }
+ updateMyProfile (profile: UserUpdateMe) {
+ const url = UserService.BASE_USERS_URL + 'me'
- listenAnonymousUpdate () {
- return this.localStorageService.watch([
- UserLocalStorageKeys.NSFW_POLICY,
- UserLocalStorageKeys.WEBTORRENT_ENABLED,
- UserLocalStorageKeys.AUTO_PLAY_VIDEO,
- UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,
- UserLocalStorageKeys.THEME,
- UserLocalStorageKeys.VIDEO_LANGUAGES
- ]).pipe(
- throttleTime(200),
- filter(() => this.authService.isLoggedIn() !== true),
- map(() => this.getAnonymousUser())
- )
+ return this.authHttp.put(url, profile)
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
deleteMe () {
const url = UserService.BASE_USERS_URL + 'me'
return this.authHttp.delete(url)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
changeAvatar (avatarForm: FormData) {
const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
- return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
+ return this.authHttp.post<{ avatar: ActorImage }>(url, avatarForm)
.pipe(catchError(err => this.restExtractor.handleError(err)))
}
const url = UserService.BASE_USERS_URL + 'me/avatar'
return this.authHttp.delete(url)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
signup (userCreate: UserRegister) {
return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
.pipe(
- map(this.restExtractor.extractDataBool),
+ tap(() => this.signupInThisSession = true),
catchError(err => this.restExtractor.handleError(err))
)
}
const url = UserService.BASE_USERS_URL + '/ask-reset-password'
return this.authHttp.post(url, { email })
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
resetPassword (userId: number, verificationString: string, password: string) {
}
return this.authHttp.post(url, body)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(res => this.restExtractor.handleError(res))
- )
+ .pipe(catchError(res => this.restExtractor.handleError(res)))
}
verifyEmail (userId: number, verificationString: string, isPendingEmail: boolean) {
}
return this.authHttp.post(url, body)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(res => this.restExtractor.handleError(res))
- )
+ .pipe(catchError(res => this.restExtractor.handleError(res)))
}
askSendVerifyEmail (email: string) {
const url = UserService.BASE_USERS_URL + '/ask-send-verify-email'
return this.authHttp.post(url, { email })
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
autocomplete (search: string): Observable<string[]> {
addUser (userCreate: UserCreate) {
return this.authHttp.post(UserService.BASE_USERS_URL, userCreate)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
updateUser (userId: number, userUpdate: UserUpdate) {
return this.authHttp.put(UserService.BASE_USERS_URL + userId, userUpdate)
- .pipe(
- map(this.restExtractor.extractDataBool),
- catchError(err => this.restExtractor.handleError(err))
- )
+ .pipe(catchError(err => this.restExtractor.handleError(err)))
}
updateUsers (users: UserServerModel[], userUpdate: UserUpdate) {
getUserWithCache (userId: number) {
if (!this.userCache[userId]) {
- this.userCache[ userId ] = this.getUser(userId).pipe(shareReplay())
+ this.userCache[userId] = this.getUser(userId).pipe(shareReplay())
}
return this.userCache[userId]
.pipe(catchError(err => this.restExtractor.handleError(err)))
}
- getAnonymousUser () {
- let videoLanguages: string[]
-
- try {
- const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)
- videoLanguages = languagesString && languagesString !== 'undefined'
- ? JSON.parse(languagesString)
- : null
- } catch (err) {
- videoLanguages = null
- console.error('Cannot parse desired video languages from localStorage.', err)
- }
-
- return new User({
- // local storage keys
- nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY),
- webTorrentEnabled: this.localStorageService.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) !== 'false',
- theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default',
- videoLanguages,
-
- autoPlayNextVideoPlaylist: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',
- autoPlayVideo: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',
-
- // session storage keys
- autoPlayNextVideo: this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'
- })
- }
-
getUsers (parameters: {
pagination: RestPagination
sort: SortMeta
const filters = this.restService.parseQueryStringFilter(search, {
blocked: {
prefix: 'banned:',
- isBoolean: true,
- handler: v => {
- if (v === 'true') return v
- if (v === 'false') return v
-
- return undefined
- }
+ isBoolean: true
}
})