Don't forget to clean up subscriptions
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { Account } from '@app/shared/account/account.model'
2 import { User } from '../'
3 import { Video as VideoServerModel } from '../../../../../shared'
4 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
5 import { VideoConstant } from '../../../../../shared/models/videos/video.model'
6 import { getAbsoluteAPIUrl } from '../misc/utils'
7
8 export class Video implements VideoServerModel {
9   by: string
10   createdAt: Date
11   updatedAt: Date
12   category: VideoConstant<number>
13   licence: VideoConstant<number>
14   language: VideoConstant<number>
15   description: string
16   duration: number
17   durationLabel: string
18   id: number
19   uuid: string
20   isLocal: boolean
21   name: string
22   serverHost: string
23   thumbnailPath: string
24   thumbnailUrl: string
25   previewPath: string
26   previewUrl: string
27   embedPath: string
28   embedUrl: string
29   views: number
30   likes: number
31   dislikes: number
32   nsfw: boolean
33
34   account: {
35     name: string
36     displayName: string
37     url: string
38     host: string
39     avatar: Avatar
40   }
41
42   private static createDurationString (duration: number) {
43     const hours = Math.floor(duration / 3600)
44     const minutes = Math.floor(duration % 3600 / 60)
45     const seconds = duration % 60
46
47     const minutesPadding = minutes >= 10 ? '' : '0'
48     const secondsPadding = seconds >= 10 ? '' : '0'
49     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
50
51     return displayedHours + minutesPadding +
52         minutes.toString() + ':' + secondsPadding + seconds.toString()
53   }
54
55   constructor (hash: VideoServerModel) {
56     const absoluteAPIUrl = getAbsoluteAPIUrl()
57
58     this.createdAt = new Date(hash.createdAt.toString())
59     this.category = hash.category
60     this.licence = hash.licence
61     this.language = hash.language
62     this.description = hash.description
63     this.duration = hash.duration
64     this.durationLabel = Video.createDurationString(hash.duration)
65     this.id = hash.id
66     this.uuid = hash.uuid
67     this.isLocal = hash.isLocal
68     this.name = hash.name
69     this.thumbnailPath = hash.thumbnailPath
70     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
71     this.previewPath = hash.previewPath
72     this.previewUrl = absoluteAPIUrl + hash.previewPath
73     this.embedPath = hash.embedPath
74     this.embedUrl = absoluteAPIUrl + hash.embedPath
75     this.views = hash.views
76     this.likes = hash.likes
77     this.dislikes = hash.dislikes
78     this.nsfw = hash.nsfw
79     this.account = hash.account
80
81     this.by = Account.CREATE_BY_STRING(hash.account.name, hash.account.host)
82   }
83
84   isVideoNSFWForUser (user: User) {
85     // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
86     return (this.nsfw && (!user || user.displayNSFW === false))
87   }
88 }