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