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