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