PeerTube/client/src/app/shared/users/user.service.ts

88 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-05-15 11:55:51 +02:00
import { catchError, map } from 'rxjs/operators'
import { HttpClient } from '@angular/common/http'
2017-12-11 17:36:46 +01:00
import { Injectable } from '@angular/core'
2018-05-16 11:00:57 +02:00
import { UserCreate, UserUpdateMe, UserVideoQuota } from '../../../../../shared'
2017-12-11 17:36:46 +01:00
import { environment } from '../../../environments/environment'
import { RestExtractor } from '../rest'
2018-05-16 11:00:57 +02:00
import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
2016-08-05 18:04:08 +02:00
@Injectable()
export class UserService {
2017-12-11 17:36:46 +01:00
static BASE_USERS_URL = environment.apiUrl + '/api/v1/users/'
2016-08-05 18:04:08 +02:00
constructor (
private authHttp: HttpClient,
private restExtractor: RestExtractor
2018-05-15 11:55:51 +02:00
) {
}
2016-08-05 18:04:08 +02:00
changePassword (newPassword: string) {
2017-09-05 21:29:39 +02:00
const url = UserService.BASE_USERS_URL + 'me'
const body: UserUpdateMe = {
2016-08-05 18:04:08 +02:00
password: newPassword
}
2016-08-05 18:04:08 +02:00
return this.authHttp.put(url, body)
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2016-08-05 18:04:08 +02:00
}
updateMyProfile (profile: UserUpdateMe) {
2017-09-05 21:29:39 +02:00
const url = UserService.BASE_USERS_URL + 'me'
return this.authHttp.put(url, profile)
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
}
2017-04-10 20:29:33 +02:00
2017-12-29 19:10:13 +01:00
changeAvatar (avatarForm: FormData) {
const url = UserService.BASE_USERS_URL + 'me/avatar/pick'
2018-05-16 11:00:57 +02:00
return this.authHttp.post<{ avatar: Avatar }>(url, avatarForm)
2018-07-09 15:56:02 +02:00
.pipe(catchError(err => this.restExtractor.handleError(err)))
2017-12-29 19:10:13 +01:00
}
signup (userCreate: UserCreate) {
return this.authHttp.post(UserService.BASE_USERS_URL + 'register', userCreate)
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2017-04-10 20:29:33 +02:00
}
2017-12-29 19:10:13 +01:00
2018-01-08 12:53:09 +01:00
getMyVideoQuotaUsed () {
const url = UserService.BASE_USERS_URL + '/me/video-quota-used'
2017-12-29 19:10:13 +01:00
2018-05-16 11:00:57 +02:00
return this.authHttp.get<UserVideoQuota>(url)
2018-07-09 15:56:02 +02:00
.pipe(catchError(err => this.restExtractor.handleError(err)))
2017-12-29 19:10:13 +01:00
}
2018-01-30 13:27:07 +01:00
askResetPassword (email: string) {
const url = UserService.BASE_USERS_URL + '/ask-reset-password'
return this.authHttp.post(url, { email })
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2018-01-30 13:27:07 +01:00
}
resetPassword (userId: number, verificationString: string, password: string) {
const url = `${UserService.BASE_USERS_URL}/${userId}/reset-password`
const body = {
verificationString,
password
}
return this.authHttp.post(url, body)
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
catchError(res => this.restExtractor.handleError(res))
)
2018-01-30 13:27:07 +01:00
}
2016-08-05 18:04:08 +02:00
}