Add ability to list redundancies
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { User } from '../'
2 import { UserRight, Video as VideoServerModel, VideoPrivacy, VideoState } from '../../../../../shared'
3 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
4 import { VideoConstant } from '../../../../../shared/models/videos/video-constant.model'
5 import { durationToString, getAbsoluteAPIUrl } from '../misc/utils'
6 import { peertubeTranslate, ServerConfig } from '../../../../../shared/models'
7 import { Actor } from '@app/shared/actor/actor.model'
8 import { VideoScheduleUpdate } from '../../../../../shared/models/videos/video-schedule-update.model'
9 import { AuthUser } from '@app/core'
10
11 export class Video implements VideoServerModel {
12   byVideoChannel: string
13   byAccount: string
14
15   accountAvatarUrl: string
16   videoChannelAvatarUrl: string
17
18   createdAt: Date
19   updatedAt: Date
20   publishedAt: Date
21   originallyPublishedAt: Date | string
22   category: VideoConstant<number>
23   licence: VideoConstant<number>
24   language: VideoConstant<string>
25   privacy: VideoConstant<VideoPrivacy>
26   description: string
27   duration: number
28   durationLabel: string
29   id: number
30   uuid: string
31   isLocal: boolean
32   name: string
33   serverHost: string
34   thumbnailPath: string
35   thumbnailUrl: string
36   previewPath: string
37   previewUrl: string
38   embedPath: string
39   embedUrl: string
40   views: number
41   likes: number
42   dislikes: number
43   nsfw: boolean
44
45   waitTranscoding?: boolean
46   state?: VideoConstant<VideoState>
47   scheduledUpdate?: VideoScheduleUpdate
48   blacklisted?: boolean
49   blacklistedReason?: string
50
51   account: {
52     id: number
53     name: string
54     displayName: string
55     url: string
56     host: string
57     avatar?: Avatar
58   }
59
60   channel: {
61     id: number
62     name: string
63     displayName: string
64     url: string
65     host: string
66     avatar?: Avatar
67   }
68
69   userHistory?: {
70     currentTime: number
71   }
72
73   static buildClientUrl (videoUUID: string) {
74     return '/videos/watch/' + videoUUID
75   }
76
77   constructor (hash: VideoServerModel, translations = {}) {
78     const absoluteAPIUrl = getAbsoluteAPIUrl()
79
80     this.createdAt = new Date(hash.createdAt.toString())
81     this.publishedAt = new Date(hash.publishedAt.toString())
82     this.category = hash.category
83     this.licence = hash.licence
84     this.language = hash.language
85     this.privacy = hash.privacy
86     this.waitTranscoding = hash.waitTranscoding
87     this.state = hash.state
88     this.description = hash.description
89     this.duration = hash.duration
90     this.durationLabel = durationToString(hash.duration)
91     this.id = hash.id
92     this.uuid = hash.uuid
93     this.isLocal = hash.isLocal
94     this.name = hash.name
95     this.thumbnailPath = hash.thumbnailPath
96     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
97     this.previewPath = hash.previewPath
98     this.previewUrl = absoluteAPIUrl + hash.previewPath
99     this.embedPath = hash.embedPath
100     this.embedUrl = absoluteAPIUrl + hash.embedPath
101     this.views = hash.views
102     this.likes = hash.likes
103     this.dislikes = hash.dislikes
104     this.nsfw = hash.nsfw
105     this.account = hash.account
106     this.channel = hash.channel
107
108     this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
109     this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
110     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
111     this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
112
113     this.category.label = peertubeTranslate(this.category.label, translations)
114     this.licence.label = peertubeTranslate(this.licence.label, translations)
115     this.language.label = peertubeTranslate(this.language.label, translations)
116     this.privacy.label = peertubeTranslate(this.privacy.label, translations)
117
118     this.scheduledUpdate = hash.scheduledUpdate
119     this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
120
121     if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
122
123     this.blacklisted = hash.blacklisted
124     this.blacklistedReason = hash.blacklistedReason
125
126     this.userHistory = hash.userHistory
127   }
128
129   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
130     // Video is not NSFW, skip
131     if (this.nsfw === false) return false
132
133     // Return user setting if logged in
134     if (user) return user.nsfwPolicy !== 'display'
135
136     // Return default instance config
137     return serverConfig.instance.defaultNSFWPolicy !== 'display'
138   }
139
140   isRemovableBy (user: AuthUser) {
141     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
142   }
143
144   isBlackistableBy (user: AuthUser) {
145     return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
146   }
147
148   isUnblacklistableBy (user: AuthUser) {
149     return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
150   }
151
152   isUpdatableBy (user: AuthUser) {
153     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
154   }
155
156   canBeDuplicatedBy (user: AuthUser) {
157     return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES)
158   }
159 }