PeerTube/client/src/app/core/auth/auth-user.model.ts

80 lines
2.0 KiB
TypeScript
Raw Normal View History

import { Observable, of } from 'rxjs'
import { map } from 'rxjs/operators'
2020-06-23 14:10:17 +02:00
import { User } from '@app/core/users/user.model'
import { UserTokens } from '@root-helpers/users'
2020-08-06 14:58:01 +02:00
import { hasUserRight } from '@shared/core-utils/users'
2020-06-23 14:10:17 +02:00
import {
MyUser as ServerMyUserModel,
MyUserSpecialPlaylist,
User as ServerUserModel,
UserRight,
UserRole,
UserVideoQuota
2020-06-23 14:10:17 +02:00
} from '@shared/models'
2016-08-09 21:45:21 +02:00
2020-01-03 15:01:17 +01:00
export class AuthUser extends User implements ServerMyUserModel {
tokens: UserTokens
2020-01-03 15:01:17 +01:00
specialPlaylists: MyUserSpecialPlaylist[]
canSeeVideosLink = true
constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) {
super(userHash)
2020-01-03 15:01:17 +01:00
this.tokens = new UserTokens(hashTokens)
2020-01-03 15:01:17 +01:00
this.specialPlaylists = userHash.specialPlaylists
}
getAccessToken () {
return this.tokens.accessToken
}
getRefreshToken () {
return this.tokens.refreshToken
}
getTokenType () {
return this.tokens.tokenType
}
refreshTokens (accessToken: string, refreshToken: string) {
this.tokens.accessToken = accessToken
this.tokens.refreshToken = refreshToken
}
2017-10-27 17:27:06 +02:00
hasRight (right: UserRight) {
return hasUserRight(this.role, right)
}
2020-01-05 09:09:09 +01:00
canManage (user: ServerUserModel) {
2019-07-30 09:59:19 +02:00
const myRole = this.role
if (myRole === UserRole.ADMINISTRATOR) return true
// I'm a moderator: I can only manage users
return user.role === UserRole.USER
}
computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {
if (!this.isUploadDisabled()) {
this.canSeeVideosLink = true
return of(this.canSeeVideosLink)
}
// Check if the user has videos
return quotaObservable.pipe(
map(({ videoQuotaUsed }) => {
if (videoQuotaUsed !== 0) {
// User already uploaded videos, so it can see the link
this.canSeeVideosLink = true
} else {
// No videos, no upload so the user don't need to see the videos link
this.canSeeVideosLink = false
}
return this.canSeeVideosLink
})
)
}
}