Fix client error logging
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { User } from '../'
2 import { Video as VideoServerModel, VideoPrivacy, VideoState } from '../../../../../shared'
3 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
4 import { VideoConstant } from '../../../../../shared/models/videos/video.model'
5 import { getAbsoluteAPIUrl } from '../misc/utils'
6 import { ServerConfig } from '../../../../../shared/models'
7 import { Actor } from '@app/shared/actor/actor.model'
8 import { peertubeTranslate } from '@app/shared/i18n/i18n-utils'
9 import { VideoScheduleUpdate } from '../../../../../shared/models/videos/video-schedule-update.model'
10
11 export class Video implements VideoServerModel {
12   by: string
13   accountAvatarUrl: string
14   videoChannelAvatarUrl: string
15   createdAt: Date
16   updatedAt: Date
17   publishedAt: Date
18   category: VideoConstant<number>
19   licence: VideoConstant<number>
20   language: VideoConstant<string>
21   privacy: VideoConstant<VideoPrivacy>
22   description: string
23   duration: number
24   durationLabel: string
25   id: number
26   uuid: string
27   isLocal: boolean
28   name: string
29   serverHost: string
30   thumbnailPath: string
31   thumbnailUrl: string
32   previewPath: string
33   previewUrl: string
34   embedPath: string
35   embedUrl: string
36   views: number
37   likes: number
38   dislikes: number
39   nsfw: boolean
40
41   waitTranscoding?: boolean
42   state?: VideoConstant<VideoState>
43   scheduledUpdate?: VideoScheduleUpdate
44
45   account: {
46     id: number
47     uuid: string
48     name: string
49     displayName: string
50     url: string
51     host: string
52     avatar: Avatar
53   }
54
55   channel: {
56     id: number
57     uuid: string
58     name: string
59     displayName: string
60     url: string
61     host: string
62     avatar: Avatar
63   }
64
65   private static createDurationString (duration: number) {
66     const hours = Math.floor(duration / 3600)
67     const minutes = Math.floor((duration % 3600) / 60)
68     const seconds = duration % 60
69
70     const minutesPadding = minutes >= 10 ? '' : '0'
71     const secondsPadding = seconds >= 10 ? '' : '0'
72     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
73
74     return displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
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 = Video.createDurationString(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.by = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
109     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
110     this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
111
112     this.category.label = peertubeTranslate(this.category.label, translations)
113     this.licence.label = peertubeTranslate(this.licence.label, translations)
114     this.language.label = peertubeTranslate(this.language.label, translations)
115     this.privacy.label = peertubeTranslate(this.privacy.label, translations)
116
117     this.scheduledUpdate = hash.scheduledUpdate
118     if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
119   }
120
121   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
122     // Video is not NSFW, skip
123     if (this.nsfw === false) return false
124
125     // Return user setting if logged in
126     if (user) return user.nsfwPolicy !== 'display'
127
128     // Return default instance config
129     return serverConfig.instance.defaultNSFWPolicy !== 'display'
130   }
131 }