Small style fixes
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { User } from '../'
2 import { Video as VideoServerModel } from '../../../../../shared'
3 import { Account } from '../../../../../shared/models/accounts'
4 import { environment } from '../../../environments/environment'
5
6 export class Video implements VideoServerModel {
7   accountName: string
8   by: string
9   createdAt: Date
10   updatedAt: Date
11   categoryLabel: string
12   category: number
13   licenceLabel: string
14   licence: number
15   languageLabel: string
16   language: number
17   description: string
18   duration: number
19   durationLabel: string
20   id: number
21   uuid: string
22   isLocal: boolean
23   name: string
24   serverHost: string
25   tags: string[]
26   thumbnailPath: string
27   thumbnailUrl: string
28   previewPath: string
29   previewUrl: string
30   embedPath: string
31   embedUrl: string
32   views: number
33   likes: number
34   dislikes: number
35   nsfw: boolean
36   account: Account
37
38   private static createByString (account: string, serverHost: string) {
39     return account + '@' + serverHost
40   }
41
42   private static createDurationString (duration: number) {
43     const minutes = Math.floor(duration / 60)
44     const seconds = duration % 60
45     const minutesPadding = minutes >= 10 ? '' : '0'
46     const secondsPadding = seconds >= 10 ? '' : '0'
47
48     return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
49   }
50
51   constructor (hash: VideoServerModel) {
52     let absoluteAPIUrl = environment.apiUrl
53     if (!absoluteAPIUrl) {
54       // The API is on the same domain
55       absoluteAPIUrl = window.location.origin
56     }
57
58     this.accountName = hash.accountName
59     this.createdAt = new Date(hash.createdAt.toString())
60     this.categoryLabel = hash.categoryLabel
61     this.category = hash.category
62     this.licenceLabel = hash.licenceLabel
63     this.licence = hash.licence
64     this.languageLabel = hash.languageLabel
65     this.language = hash.language
66     this.description = hash.description
67     this.duration = hash.duration
68     this.durationLabel = Video.createDurationString(hash.duration)
69     this.id = hash.id
70     this.uuid = hash.uuid
71     this.isLocal = hash.isLocal
72     this.name = hash.name
73     this.serverHost = hash.serverHost
74     this.tags = hash.tags
75     this.thumbnailPath = hash.thumbnailPath
76     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
77     this.previewPath = hash.previewPath
78     this.previewUrl = absoluteAPIUrl + hash.previewPath
79     this.embedPath = hash.embedPath
80     this.embedUrl = absoluteAPIUrl + hash.embedPath
81     this.views = hash.views
82     this.likes = hash.likes
83     this.dislikes = hash.dislikes
84     this.nsfw = hash.nsfw
85
86     this.by = Video.createByString(hash.accountName, hash.serverHost)
87   }
88
89   isVideoNSFWForUser (user: User) {
90     // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
91     return (this.nsfw && (!user || user.displayNSFW === false))
92   }
93 }