2020-07-28 15:18:38 +02:00
|
|
|
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'
|
2022-10-12 16:09:02 +02:00
|
|
|
import { OAuthUserTokens } 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,
|
2020-07-28 15:18:38 +02:00
|
|
|
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 {
|
2022-10-12 16:09:02 +02:00
|
|
|
oauthTokens: OAuthUserTokens
|
2020-01-03 15:01:17 +01:00
|
|
|
specialPlaylists: MyUserSpecialPlaylist[]
|
2016-07-20 16:24:18 +02:00
|
|
|
|
2020-07-28 15:18:38 +02:00
|
|
|
canSeeVideosLink = true
|
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<OAuthUserTokens>) {
|
2017-06-16 14:32:15 +02:00
|
|
|
super(userHash)
|
2020-01-03 15:01:17 +01:00
|
|
|
|
2022-10-12 16:09:02 +02:00
|
|
|
this.oauthTokens = new OAuthUserTokens(hashTokens)
|
2020-01-03 15:01:17 +01:00
|
|
|
this.specialPlaylists = userHash.specialPlaylists
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
getAccessToken () {
|
2022-10-12 16:09:02 +02:00
|
|
|
return this.oauthTokens.accessToken
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
getRefreshToken () {
|
2022-10-12 16:09:02 +02:00
|
|
|
return this.oauthTokens.refreshToken
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
getTokenType () {
|
2022-10-12 16:09:02 +02:00
|
|
|
return this.oauthTokens.tokenType
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 14:32:15 +02:00
|
|
|
refreshTokens (accessToken: string, refreshToken: string) {
|
2022-10-12 16:09:02 +02:00
|
|
|
this.oauthTokens.accessToken = accessToken
|
|
|
|
this.oauthTokens.refreshToken = refreshToken
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|
|
|
|
|
2017-10-27 17:27:06 +02:00
|
|
|
hasRight (right: UserRight) {
|
2017-10-27 16:55:03 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:18:38 +02:00
|
|
|
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
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2016-07-20 16:24:18 +02:00
|
|
|
}
|