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