]>
Commit | Line | Data |
---|---|---|
3a0fb65c | 1 | import { AuthUser } from '@app/core' |
67ed6552 | 2 | import { User } from '@app/core/users/user.model' |
72493e44 | 3 | import { durationToString, getAbsoluteAPIUrl, getAbsoluteEmbedUrl } from '@app/helpers' |
69524f6e | 4 | import { Actor } from '@app/shared/shared-main/account/actor.model' |
15a7eafb | 5 | import { buildVideoWatchPath } from '@shared/core-utils' |
bd45d503 | 6 | import { peertubeTranslate } from '@shared/core-utils/i18n' |
67ed6552 | 7 | import { |
f4796856 | 8 | ActorImage, |
2989628b | 9 | HTMLServerConfig, |
67ed6552 C |
10 | UserRight, |
11 | Video as VideoServerModel, | |
12 | VideoConstant, | |
3c10840f | 13 | VideoFile, |
67ed6552 C |
14 | VideoPrivacy, |
15 | VideoScheduleUpdate, | |
3c10840f | 16 | VideoState, |
b46cf4b9 C |
17 | VideoStreamingPlaylist, |
18 | VideoStreamingPlaylistType | |
67ed6552 | 19 | } from '@shared/models' |
92fb909c | 20 | |
69f616ab | 21 | export class Video implements VideoServerModel { |
22a16e36 C |
22 | byVideoChannel: string |
23 | byAccount: string | |
24 | ||
df98563e | 25 | createdAt: Date |
6d33593a | 26 | updatedAt: Date |
2922e048 | 27 | publishedAt: Date |
c8034165 | 28 | originallyPublishedAt: Date | string |
09700934 C |
29 | category: VideoConstant<number> |
30 | licence: VideoConstant<number> | |
9d3ef9fe | 31 | language: VideoConstant<string> |
2243730c | 32 | privacy: VideoConstant<VideoPrivacy> |
d4a8e7a6 | 33 | |
df98563e | 34 | description: string |
d4a8e7a6 | 35 | |
df98563e C |
36 | duration: number |
37 | durationLabel: string | |
d4a8e7a6 | 38 | |
0a6658fd C |
39 | id: number |
40 | uuid: string | |
d4a8e7a6 C |
41 | shortUUID: string |
42 | ||
df98563e | 43 | isLocal: boolean |
d4a8e7a6 | 44 | |
df98563e | 45 | name: string |
60862425 | 46 | serverHost: string |
df98563e C |
47 | thumbnailPath: string |
48 | thumbnailUrl: string | |
5fb2e288 | 49 | |
c6c0fa6c C |
50 | isLive: boolean |
51 | ||
43f61d26 C |
52 | previewPath: string |
53 | previewUrl: string | |
5fb2e288 | 54 | |
d8755eed C |
55 | embedPath: string |
56 | embedUrl: string | |
5fb2e288 | 57 | |
ab4001aa | 58 | url: string |
5fb2e288 | 59 | |
df98563e | 60 | views: number |
384ba8b7 | 61 | viewers: number |
51353d9a | 62 | |
df98563e C |
63 | likes: number |
64 | dislikes: number | |
65 | nsfw: boolean | |
b64c950a | 66 | |
c910f667 C |
67 | originInstanceUrl: string |
68 | originInstanceHost: string | |
69 | ||
2186386c C |
70 | waitTranscoding?: boolean |
71 | state?: VideoConstant<VideoState> | |
bbe0f064 | 72 | scheduledUpdate?: VideoScheduleUpdate |
2760b454 | 73 | |
191764f3 | 74 | blacklisted?: boolean |
2760b454 C |
75 | blacklistedReason?: string |
76 | ||
77 | blockedOwner?: boolean | |
78 | blockedServer?: boolean | |
2186386c | 79 | |
b64c950a | 80 | account: { |
03e12d7c | 81 | id: number |
b64c950a C |
82 | name: string |
83 | displayName: string | |
84 | url: string | |
0f320037 | 85 | host: string |
d0800f76 | 86 | |
87 | // TODO: remove, deprecated in 4.2 | |
88 | avatar: ActorImage | |
89 | ||
90 | avatars: ActorImage[] | |
0f320037 C |
91 | } |
92 | ||
93 | channel: { | |
94 | id: number | |
0f320037 C |
95 | name: string |
96 | displayName: string | |
97 | url: string | |
b64c950a | 98 | host: string |
d0800f76 | 99 | |
100 | // TODO: remove, deprecated in 4.2 | |
101 | avatar: ActorImage | |
102 | ||
103 | avatars: ActorImage[] | |
b64c950a | 104 | } |
aff038cd | 105 | |
6e46de09 C |
106 | userHistory?: { |
107 | currentTime: number | |
108 | } | |
109 | ||
7294aab0 C |
110 | pluginData?: any |
111 | ||
3c10840f C |
112 | streamingPlaylists?: VideoStreamingPlaylist[] |
113 | files?: VideoFile[] | |
114 | ||
d4a8e7a6 | 115 | static buildWatchUrl (video: Partial<Pick<Video, 'uuid' | 'shortUUID'>>) { |
15a7eafb | 116 | return buildVideoWatchPath({ shortUUID: video.shortUUID || video.uuid }) |
d4a8e7a6 C |
117 | } |
118 | ||
119 | static buildUpdateUrl (video: Pick<Video, 'uuid'>) { | |
120 | return '/videos/update/' + video.uuid | |
191764f3 C |
121 | } |
122 | ||
9df52d66 | 123 | constructor (hash: VideoServerModel, translations: { [ id: string ]: string } = {}) { |
c5911fd3 | 124 | const absoluteAPIUrl = getAbsoluteAPIUrl() |
c6e0bfbf | 125 | |
d592e0a9 | 126 | this.createdAt = new Date(hash.createdAt.toString()) |
2922e048 | 127 | this.publishedAt = new Date(hash.publishedAt.toString()) |
df98563e | 128 | this.category = hash.category |
df98563e | 129 | this.licence = hash.licence |
df98563e | 130 | this.language = hash.language |
2243730c | 131 | this.privacy = hash.privacy |
2186386c C |
132 | this.waitTranscoding = hash.waitTranscoding |
133 | this.state = hash.state | |
df98563e | 134 | this.description = hash.description |
c910f667 | 135 | |
c6c0fa6c C |
136 | this.isLive = hash.isLive |
137 | ||
df98563e | 138 | this.duration = hash.duration |
11b8762f | 139 | this.durationLabel = durationToString(hash.duration) |
c910f667 | 140 | |
df98563e | 141 | this.id = hash.id |
0a6658fd | 142 | this.uuid = hash.uuid |
d4a8e7a6 | 143 | this.shortUUID = hash.shortUUID |
c910f667 | 144 | |
df98563e | 145 | this.isLocal = hash.isLocal |
df98563e | 146 | this.name = hash.name |
c910f667 | 147 | |
df98563e | 148 | this.thumbnailPath = hash.thumbnailPath |
c6c0fa6c C |
149 | this.thumbnailUrl = this.thumbnailPath |
150 | ? hash.thumbnailUrl || (absoluteAPIUrl + hash.thumbnailPath) | |
151 | : null | |
c910f667 | 152 | |
43f61d26 | 153 | this.previewPath = hash.previewPath |
c6c0fa6c C |
154 | this.previewUrl = this.previewPath |
155 | ? hash.previewUrl || (absoluteAPIUrl + hash.previewPath) | |
156 | : null | |
c910f667 | 157 | |
d8755eed | 158 | this.embedPath = hash.embedPath |
72493e44 | 159 | this.embedUrl = hash.embedUrl || (getAbsoluteEmbedUrl() + hash.embedPath) |
5fb2e288 C |
160 | |
161 | this.url = hash.url | |
c910f667 | 162 | |
df98563e | 163 | this.views = hash.views |
51353d9a | 164 | this.viewers = hash.viewers |
df98563e C |
165 | this.likes = hash.likes |
166 | this.dislikes = hash.dislikes | |
c910f667 | 167 | |
df98563e | 168 | this.nsfw = hash.nsfw |
c910f667 | 169 | |
b64c950a | 170 | this.account = hash.account |
52d9f792 | 171 | this.channel = hash.channel |
4fd8aa32 | 172 | |
22a16e36 C |
173 | this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host) |
174 | this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host) | |
7ce44a74 C |
175 | |
176 | this.category.label = peertubeTranslate(this.category.label, translations) | |
177 | this.licence.label = peertubeTranslate(this.licence.label, translations) | |
178 | this.language.label = peertubeTranslate(this.language.label, translations) | |
179 | this.privacy.label = peertubeTranslate(this.privacy.label, translations) | |
2186386c | 180 | |
bbe0f064 | 181 | this.scheduledUpdate = hash.scheduledUpdate |
830b4faf C |
182 | this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null |
183 | ||
2186386c | 184 | if (this.state) this.state.label = peertubeTranslate(this.state.label, translations) |
191764f3 C |
185 | |
186 | this.blacklisted = hash.blacklisted | |
2760b454 C |
187 | this.blacklistedReason = hash.blacklistedReason |
188 | ||
189 | this.blockedOwner = hash.blockedOwner | |
190 | this.blockedServer = hash.blockedServer | |
6e46de09 | 191 | |
3c10840f C |
192 | this.streamingPlaylists = hash.streamingPlaylists |
193 | this.files = hash.files | |
194 | ||
6e46de09 | 195 | this.userHistory = hash.userHistory |
c910f667 C |
196 | |
197 | this.originInstanceHost = this.account.host | |
198 | this.originInstanceUrl = 'https://' + this.originInstanceHost | |
7294aab0 C |
199 | |
200 | this.pluginData = hash.pluginData | |
501bc6c2 C |
201 | } |
202 | ||
2989628b | 203 | isVideoNSFWForUser (user: User, serverConfig: HTMLServerConfig) { |
0883b324 C |
204 | // Video is not NSFW, skip |
205 | if (this.nsfw === false) return false | |
206 | ||
207 | // Return user setting if logged in | |
208 | if (user) return user.nsfwPolicy !== 'display' | |
209 | ||
210 | // Return default instance config | |
211 | return serverConfig.instance.defaultNSFWPolicy !== 'display' | |
92fb909c | 212 | } |
3a0fb65c C |
213 | |
214 | isRemovableBy (user: AuthUser) { | |
215 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.REMOVE_ANY_VIDEO)) | |
216 | } | |
217 | ||
5baee5fc | 218 | isBlockableBy (user: AuthUser) { |
3487330d | 219 | return this.blacklisted !== true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true |
3a0fb65c C |
220 | } |
221 | ||
5baee5fc | 222 | isUnblockableBy (user: AuthUser) { |
3487330d | 223 | return this.blacklisted === true && user && user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) === true |
3a0fb65c C |
224 | } |
225 | ||
226 | isUpdatableBy (user: AuthUser) { | |
227 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.UPDATE_ANY_VIDEO)) | |
228 | } | |
b764380a | 229 | |
92e66e04 C |
230 | isEditableBy (user: AuthUser, videoStudioEnabled: boolean) { |
231 | return videoStudioEnabled && | |
22e90922 C |
232 | this.state?.id === VideoState.PUBLISHED && |
233 | this.isUpdatableBy(user) | |
234 | } | |
235 | ||
384ba8b7 C |
236 | canSeeStats (user: AuthUser) { |
237 | return user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.SEE_ALL_VIDEOS)) | |
238 | } | |
239 | ||
ad5db104 | 240 | canRemoveFiles (user: AuthUser) { |
7aeb82ea | 241 | return this.isLocal && |
384ba8b7 | 242 | user && user.hasRight(UserRight.MANAGE_VIDEO_FILES) && |
ad5db104 C |
243 | this.state.id !== VideoState.TO_TRANSCODE && |
244 | this.hasHLS() && | |
245 | this.hasWebTorrent() | |
246 | } | |
247 | ||
248 | canRunTranscoding (user: AuthUser) { | |
7aeb82ea | 249 | return this.isLocal && |
384ba8b7 | 250 | user && user.hasRight(UserRight.RUN_VIDEO_TRANSCODING) && |
ad5db104 C |
251 | this.state.id !== VideoState.TO_TRANSCODE |
252 | } | |
253 | ||
b46cf4b9 C |
254 | hasHLS () { |
255 | return this.streamingPlaylists?.some(p => p.type === VideoStreamingPlaylistType.HLS) | |
256 | } | |
257 | ||
258 | hasWebTorrent () { | |
259 | return this.files && this.files.length !== 0 | |
260 | } | |
261 | ||
f8c00564 C |
262 | isLiveInfoAvailableBy (user: AuthUser) { |
263 | return this.isLive && | |
264 | user && this.isLocal === true && (this.account.name === user.username || user.hasRight(UserRight.GET_ANY_LIVE)) | |
265 | } | |
266 | ||
b764380a C |
267 | canBeDuplicatedBy (user: AuthUser) { |
268 | return user && this.isLocal === false && user.hasRight(UserRight.MANAGE_VIDEOS_REDUNDANCIES) | |
269 | } | |
4ec25ae8 C |
270 | |
271 | getExactNumberOfViews () { | |
272 | if (this.views < 1000) return '' | |
273 | ||
274 | if (this.isLive) { | |
275 | return $localize`${this.views} viewers` | |
276 | } | |
277 | ||
278 | return $localize`${this.views} views` | |
279 | } | |
501bc6c2 | 280 | } |