Add i18n attributes
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { User } from '../'
2 import { Video as VideoServerModel, VideoPrivacy } 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
9 export class Video implements VideoServerModel {
10   by: string
11   accountAvatarUrl: string
12   createdAt: Date
13   updatedAt: Date
14   publishedAt: Date
15   category: VideoConstant<number>
16   licence: VideoConstant<number>
17   language: VideoConstant<string>
18   privacy: VideoConstant<VideoPrivacy>
19   description: string
20   duration: number
21   durationLabel: string
22   id: number
23   uuid: string
24   isLocal: boolean
25   name: string
26   serverHost: string
27   thumbnailPath: string
28   thumbnailUrl: string
29   previewPath: string
30   previewUrl: string
31   embedPath: string
32   embedUrl: string
33   views: number
34   likes: number
35   dislikes: number
36   nsfw: boolean
37
38   account: {
39     id: number
40     uuid: string
41     name: string
42     displayName: string
43     url: string
44     host: string
45     avatar: Avatar
46   }
47
48   channel: {
49     id: number
50     uuid: string
51     name: string
52     displayName: string
53     url: string
54     host: string
55     avatar: Avatar
56   }
57
58   private static createDurationString (duration: number) {
59     const hours = Math.floor(duration / 3600)
60     const minutes = Math.floor(duration % 3600 / 60)
61     const seconds = duration % 60
62
63     const minutesPadding = minutes >= 10 ? '' : '0'
64     const secondsPadding = seconds >= 10 ? '' : '0'
65     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
66
67     return displayedHours + minutesPadding +
68         minutes.toString() + ':' + secondsPadding + seconds.toString()
69   }
70
71   constructor (hash: VideoServerModel) {
72     const absoluteAPIUrl = getAbsoluteAPIUrl()
73
74     this.createdAt = new Date(hash.createdAt.toString())
75     this.publishedAt = new Date(hash.publishedAt.toString())
76     this.category = hash.category
77     this.licence = hash.licence
78     this.language = hash.language
79     this.privacy = hash.privacy
80     this.description = hash.description
81     this.duration = hash.duration
82     this.durationLabel = Video.createDurationString(hash.duration)
83     this.id = hash.id
84     this.uuid = hash.uuid
85     this.isLocal = hash.isLocal
86     this.name = hash.name
87     this.thumbnailPath = hash.thumbnailPath
88     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
89     this.previewPath = hash.previewPath
90     this.previewUrl = absoluteAPIUrl + hash.previewPath
91     this.embedPath = hash.embedPath
92     this.embedUrl = absoluteAPIUrl + hash.embedPath
93     this.views = hash.views
94     this.likes = hash.likes
95     this.dislikes = hash.dislikes
96     this.nsfw = hash.nsfw
97     this.account = hash.account
98
99     this.by = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
100     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
101   }
102
103   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
104     // Video is not NSFW, skip
105     if (this.nsfw === false) return false
106
107     // Return user setting if logged in
108     if (user) return user.nsfwPolicy !== 'display'
109
110     // Return default instance config
111     return serverConfig.instance.defaultNSFWPolicy !== 'display'
112   }
113 }