0cef3eb8f1de5f17c2992c9a6134ae0806ae366b
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { User } from '../'
2 import { PlaylistElement, 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   playlistElement?: PlaylistElement
52
53   account: {
54     id: number
55     uuid: string
56     name: string
57     displayName: string
58     url: string
59     host: string
60     avatar?: Avatar
61   }
62
63   channel: {
64     id: number
65     uuid: string
66     name: string
67     displayName: string
68     url: string
69     host: string
70     avatar?: Avatar
71   }
72
73   userHistory?: {
74     currentTime: number
75   }
76
77   static buildClientUrl (videoUUID: string) {
78     return '/videos/watch/' + videoUUID
79   }
80
81   constructor (hash: VideoServerModel, translations = {}) {
82     const absoluteAPIUrl = getAbsoluteAPIUrl()
83
84     this.createdAt = new Date(hash.createdAt.toString())
85     this.publishedAt = new Date(hash.publishedAt.toString())
86     this.category = hash.category
87     this.licence = hash.licence
88     this.language = hash.language
89     this.privacy = hash.privacy
90     this.waitTranscoding = hash.waitTranscoding
91     this.state = hash.state
92     this.description = hash.description
93     this.duration = hash.duration
94     this.durationLabel = durationToString(hash.duration)
95     this.id = hash.id
96     this.uuid = hash.uuid
97     this.isLocal = hash.isLocal
98     this.name = hash.name
99     this.thumbnailPath = hash.thumbnailPath
100     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
101     this.previewPath = hash.previewPath
102     this.previewUrl = absoluteAPIUrl + hash.previewPath
103     this.embedPath = hash.embedPath
104     this.embedUrl = absoluteAPIUrl + hash.embedPath
105     this.views = hash.views
106     this.likes = hash.likes
107     this.dislikes = hash.dislikes
108     this.nsfw = hash.nsfw
109     this.account = hash.account
110     this.channel = hash.channel
111
112     this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
113     this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
114     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
115     this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
116
117     this.category.label = peertubeTranslate(this.category.label, translations)
118     this.licence.label = peertubeTranslate(this.licence.label, translations)
119     this.language.label = peertubeTranslate(this.language.label, translations)
120     this.privacy.label = peertubeTranslate(this.privacy.label, translations)
121
122     this.scheduledUpdate = hash.scheduledUpdate
123     this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
124
125     if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
126
127     this.blacklisted = hash.blacklisted
128     this.blacklistedReason = hash.blacklistedReason
129
130     this.userHistory = hash.userHistory
131
132     this.playlistElement = hash.playlistElement
133   }
134
135   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
136     // Video is not NSFW, skip
137     if (this.nsfw === false) return false
138
139     // Return user setting if logged in
140     if (user) return user.nsfwPolicy !== 'display'
141
142     // Return default instance config
143     return serverConfig.instance.defaultNSFWPolicy !== 'display'
144   }
145
146   isRemovableBy (user: AuthUser) {
147     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO))
148   }
149
150   isBlackistableBy (user: AuthUser) {
151     return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
152   }
153
154   isUnblacklistableBy (user: AuthUser) {
155     return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true
156   }
157
158   isUpdatableBy (user: AuthUser) {
159     return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO))
160   }
161 }