]>
Commit | Line | Data |
---|---|---|
5fb2e288 | 1 | import { switchMap } from 'rxjs/operators' |
b7819090 C |
2 | import { |
3 | ChangeDetectionStrategy, | |
4 | ChangeDetectorRef, | |
5 | Component, | |
6 | EventEmitter, | |
7 | Inject, | |
8 | Input, | |
9 | LOCALE_ID, | |
10 | OnInit, | |
11 | Output | |
12 | } from '@angular/core' | |
67ed6552 | 13 | import { AuthService, ScreenService, ServerService, User } from '@app/core' |
583eb04b | 14 | import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '@shared/models' |
67ed6552 C |
15 | import { Video } from '../shared-main' |
16 | import { VideoPlaylistService } from '../shared-video-playlist' | |
17 | import { VideoActionsDisplayType } from './video-actions-dropdown.component' | |
501bc6c2 | 18 | |
22a16e36 | 19 | export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto' |
e2409062 C |
20 | export type MiniatureDisplayOptions = { |
21 | date?: boolean | |
22 | views?: boolean | |
23 | by?: boolean | |
c2caa99b | 24 | avatar?: boolean |
e2409062 C |
25 | privacyLabel?: boolean |
26 | privacyText?: boolean | |
27 | state?: boolean | |
28 | blacklistInfo?: boolean | |
29 | nsfw?: boolean | |
30 | } | |
0bdad52f | 31 | export type VideoLinkType = 'internal' | 'lazy-load' | 'external' |
22a16e36 | 32 | |
501bc6c2 C |
33 | @Component({ |
34 | selector: 'my-video-miniature', | |
ec8d8440 | 35 | styleUrls: [ './video-miniature.component.scss' ], |
89724816 C |
36 | templateUrl: './video-miniature.component.html', |
37 | changeDetection: ChangeDetectionStrategy.OnPush | |
501bc6c2 | 38 | }) |
22a16e36 | 39 | export class VideoMiniatureComponent implements OnInit { |
df98563e C |
40 | @Input() user: User |
41 | @Input() video: Video | |
e2409062 | 42 | |
22a16e36 | 43 | @Input() ownerDisplayType: OwnerDisplayType = 'account' |
e2409062 C |
44 | @Input() displayOptions: MiniatureDisplayOptions = { |
45 | date: true, | |
46 | views: true, | |
47 | by: true, | |
c2caa99b | 48 | avatar: false, |
e2409062 C |
49 | privacyLabel: false, |
50 | privacyText: false, | |
51 | state: false, | |
52 | blacklistInfo: false | |
53 | } | |
54 | @Input() displayAsRow = false | |
3a0fb65c | 55 | @Input() displayVideoActions = true |
e66883b3 | 56 | @Input() fitWidth = false |
3a0fb65c | 57 | |
0bdad52f | 58 | @Input() videoLinkType: VideoLinkType = 'internal' |
5fb2e288 | 59 | |
5baee5fc RK |
60 | @Output() videoBlocked = new EventEmitter() |
61 | @Output() videoUnblocked = new EventEmitter() | |
3a0fb65c | 62 | @Output() videoRemoved = new EventEmitter() |
d473fd94 | 63 | @Output() videoAccountMuted = new EventEmitter() |
3a0fb65c C |
64 | |
65 | videoActionsDisplayOptions: VideoActionsDisplayType = { | |
66 | playlist: true, | |
67 | download: false, | |
68 | update: true, | |
69 | blacklist: true, | |
70 | delete: true, | |
b764380a | 71 | report: true, |
d473fd94 RK |
72 | duplicate: true, |
73 | mute: true | |
3a0fb65c C |
74 | } |
75 | showActions = false | |
ba430d75 | 76 | serverConfig: ServerConfig |
22a16e36 | 77 | |
b7819090 C |
78 | addToWatchLaterText: string |
79 | addedToWatchLaterText: string | |
80 | inWatchLaterPlaylist: boolean | |
435258ea | 81 | channelLinkTitle = '' |
b7819090 C |
82 | |
83 | watchLaterPlaylist: { | |
84 | id: number | |
85 | playlistElementId?: number | |
86 | } | |
87 | ||
0bdad52f C |
88 | videoRouterLink: any[] = [] |
89 | videoHref: string | |
90 | videoTarget: string | |
5fb2e288 | 91 | |
22a16e36 | 92 | private ownerDisplayTypeChosen: 'account' | 'videoChannel' |
501bc6c2 | 93 | |
e2409062 | 94 | constructor ( |
3a0fb65c | 95 | private screenService: ScreenService, |
e2409062 | 96 | private serverService: ServerService, |
b7819090 C |
97 | private authService: AuthService, |
98 | private videoPlaylistService: VideoPlaylistService, | |
99 | private cd: ChangeDetectorRef, | |
e2409062 | 100 | @Inject(LOCALE_ID) private localeId: string |
435258ea | 101 | ) {} |
0883b324 | 102 | |
d1a63fc7 | 103 | get isVideoBlur () { |
ba430d75 | 104 | return this.video.isVideoNSFWForUser(this.user, this.serverConfig) |
d1a63fc7 C |
105 | } |
106 | ||
22a16e36 | 107 | ngOnInit () { |
ba430d75 C |
108 | this.serverConfig = this.serverService.getTmpConfig() |
109 | this.serverService.getConfig() | |
5fb2e288 C |
110 | .subscribe(config => { |
111 | this.serverConfig = config | |
112 | this.buildVideoLink() | |
113 | }) | |
ba430d75 | 114 | |
3a0fb65c | 115 | this.setUpBy() |
22a16e36 | 116 | |
66357162 | 117 | this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)` |
435258ea | 118 | |
8dfceec4 C |
119 | // We rely on mouseenter to lazy load actions |
120 | if (this.screenService.isInTouchScreen()) { | |
743f023c | 121 | this.loadActions() |
22a16e36 | 122 | } |
92fb909c | 123 | } |
22a16e36 | 124 | |
5fb2e288 | 125 | buildVideoLink () { |
0bdad52f C |
126 | if (this.videoLinkType === 'internal' || !this.video.url) { |
127 | this.videoRouterLink = [ '/videos/watch', this.video.uuid ] | |
128 | return | |
129 | } | |
5fb2e288 | 130 | |
0bdad52f C |
131 | if (this.videoLinkType === 'external') { |
132 | this.videoRouterLink = null | |
133 | this.videoHref = this.video.url | |
134 | this.videoTarget = '_blank' | |
5fb2e288 C |
135 | return |
136 | } | |
137 | ||
0bdad52f C |
138 | // Lazy load |
139 | this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ] | |
5fb2e288 C |
140 | } |
141 | ||
22a16e36 C |
142 | displayOwnerAccount () { |
143 | return this.ownerDisplayTypeChosen === 'account' | |
144 | } | |
145 | ||
146 | displayOwnerVideoChannel () { | |
147 | return this.ownerDisplayTypeChosen === 'videoChannel' | |
148 | } | |
017c3dca C |
149 | |
150 | isUnlistedVideo () { | |
151 | return this.video.privacy.id === VideoPrivacy.UNLISTED | |
152 | } | |
153 | ||
154 | isPrivateVideo () { | |
155 | return this.video.privacy.id === VideoPrivacy.PRIVATE | |
156 | } | |
e2409062 C |
157 | |
158 | getStateLabel (video: Video) { | |
dedc7abb C |
159 | if (!video.state) return '' |
160 | ||
e2409062 | 161 | if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) { |
66357162 | 162 | return $localize`Published` |
e2409062 C |
163 | } |
164 | ||
165 | if (video.scheduledUpdate) { | |
166 | const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId) | |
66357162 | 167 | return $localize`Publication scheduled on ` + updateAt |
e2409062 C |
168 | } |
169 | ||
170 | if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) { | |
66357162 | 171 | return $localize`Waiting transcoding` |
e2409062 C |
172 | } |
173 | ||
174 | if (video.state.id === VideoState.TO_TRANSCODE) { | |
66357162 | 175 | return $localize`To transcode` |
e2409062 C |
176 | } |
177 | ||
178 | if (video.state.id === VideoState.TO_IMPORT) { | |
66357162 | 179 | return $localize`To import` |
e2409062 C |
180 | } |
181 | ||
182 | return '' | |
183 | } | |
3a0fb65c | 184 | |
cf78883c C |
185 | getAvatarUrl () { |
186 | if (this.ownerDisplayTypeChosen === 'account') { | |
187 | return this.video.accountAvatarUrl | |
188 | } | |
189 | ||
190 | return this.video.videoChannelAvatarUrl | |
191 | } | |
192 | ||
3a0fb65c C |
193 | loadActions () { |
194 | if (this.displayVideoActions) this.showActions = true | |
b7819090 C |
195 | |
196 | this.loadWatchLater() | |
3a0fb65c C |
197 | } |
198 | ||
5baee5fc RK |
199 | onVideoBlocked () { |
200 | this.videoBlocked.emit() | |
3a0fb65c C |
201 | } |
202 | ||
5baee5fc RK |
203 | onVideoUnblocked () { |
204 | this.videoUnblocked.emit() | |
3a0fb65c C |
205 | } |
206 | ||
207 | onVideoRemoved () { | |
208 | this.videoRemoved.emit() | |
209 | } | |
210 | ||
d473fd94 RK |
211 | onVideoAccountMuted () { |
212 | this.videoAccountMuted.emit() | |
213 | } | |
214 | ||
b7819090 C |
215 | isUserLoggedIn () { |
216 | return this.authService.isLoggedIn() | |
217 | } | |
218 | ||
219 | onWatchLaterClick (currentState: boolean) { | |
220 | if (currentState === true) this.removeFromWatchLater() | |
221 | else this.addToWatchLater() | |
222 | ||
223 | this.inWatchLaterPlaylist = !currentState | |
224 | } | |
225 | ||
226 | addToWatchLater () { | |
227 | const body = { videoId: this.video.id } | |
228 | ||
229 | this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe( | |
230 | res => { | |
231 | this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id | |
232 | } | |
233 | ) | |
234 | } | |
235 | ||
236 | removeFromWatchLater () { | |
51b34a11 | 237 | this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id) |
b7819090 C |
238 | .subscribe( |
239 | _ => { /* empty */ } | |
240 | ) | |
241 | } | |
242 | ||
243 | isWatchLaterPlaylistDisplayed () { | |
5fb2e288 | 244 | return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined |
b7819090 C |
245 | } |
246 | ||
3a0fb65c C |
247 | private setUpBy () { |
248 | if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') { | |
249 | this.ownerDisplayTypeChosen = this.ownerDisplayType | |
250 | return | |
251 | } | |
252 | ||
253 | // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12) | |
254 | // -> Use the account name | |
255 | if ( | |
256 | this.video.channel.name === `${this.video.account.name}_channel` || | |
257 | this.video.channel.name.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) | |
258 | ) { | |
259 | this.ownerDisplayTypeChosen = 'account' | |
260 | } else { | |
261 | this.ownerDisplayTypeChosen = 'videoChannel' | |
262 | } | |
263 | } | |
b7819090 C |
264 | |
265 | private loadWatchLater () { | |
51b34a11 C |
266 | if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return |
267 | ||
268 | this.authService.userInformationLoaded | |
269 | .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id))) | |
270 | .subscribe(existResult => { | |
271 | const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER) | |
272 | const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id) | |
273 | this.inWatchLaterPlaylist = false | |
274 | ||
275 | this.watchLaterPlaylist = { | |
276 | id: watchLaterPlaylist.id | |
277 | } | |
278 | ||
279 | if (existsInWatchLater) { | |
280 | this.inWatchLaterPlaylist = true | |
281 | this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId | |
282 | } | |
283 | ||
284 | this.cd.markForCheck() | |
285 | }) | |
286 | ||
287 | this.videoPlaylistService.runPlaylistCheck(this.video.id) | |
b7819090 | 288 | } |
501bc6c2 | 289 | } |