]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/video-miniature.component.ts
Move to stylelint
[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
C
182
183 loadActions () {
184 if (this.displayVideoActions) this.showActions = true
b7819090
C
185
186 this.loadWatchLater()
3a0fb65c
C
187 }
188
5baee5fc
RK
189 onVideoBlocked () {
190 this.videoBlocked.emit()
3a0fb65c
C
191 }
192
5baee5fc
RK
193 onVideoUnblocked () {
194 this.videoUnblocked.emit()
3a0fb65c
C
195 }
196
197 onVideoRemoved () {
198 this.videoRemoved.emit()
199 }
200
d473fd94
RK
201 onVideoAccountMuted () {
202 this.videoAccountMuted.emit()
203 }
204
b7819090
C
205 isUserLoggedIn () {
206 return this.authService.isLoggedIn()
207 }
208
209 onWatchLaterClick (currentState: boolean) {
210 if (currentState === true) this.removeFromWatchLater()
211 else this.addToWatchLater()
212
213 this.inWatchLaterPlaylist = !currentState
214 }
215
216 addToWatchLater () {
217 const body = { videoId: this.video.id }
218
219 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
220 res => {
221 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
222 }
223 )
224 }
225
226 removeFromWatchLater () {
51b34a11 227 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
b7819090
C
228 .subscribe(
229 _ => { /* empty */ }
230 )
231 }
232
233 isWatchLaterPlaylistDisplayed () {
5fb2e288 234 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
b7819090
C
235 }
236
0f7407d9
C
237 getClasses () {
238 return {
239 'display-as-row': this.displayAsRow
240 }
241 }
242
3a0fb65c 243 private setUpBy () {
733dbc53 244 const accountName = this.video.account.name
3a0fb65c 245
deb8b9cd 246 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
733dbc53 247 // Or has not been customized (default created channel display name)
3a0fb65c
C
248 // -> Use the account name
249 if (
733dbc53
C
250 this.video.channel.displayName === `Default ${accountName} channel` ||
251 this.video.channel.displayName === `Main ${accountName} channel` ||
3a0fb65c
C
252 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}$/)
253 ) {
733dbc53 254 this.ownerDisplayType = 'account'
3a0fb65c 255 } else {
733dbc53 256 this.ownerDisplayType = 'videoChannel'
3a0fb65c
C
257 }
258 }
b7819090
C
259
260 private loadWatchLater () {
51b34a11
C
261 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
262
263 this.authService.userInformationLoaded
264 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
265 .subscribe(existResult => {
266 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
267 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
268 this.inWatchLaterPlaylist = false
269
270 this.watchLaterPlaylist = {
271 id: watchLaterPlaylist.id
272 }
273
274 if (existsInWatchLater) {
275 this.inWatchLaterPlaylist = true
276 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
277 }
278
279 this.cd.markForCheck()
280 })
281
282 this.videoPlaylistService.runPlaylistCheck(this.video.id)
b7819090 283 }
501bc6c2 284}