]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/video/video-miniature.component.ts
Use default nsfw instance policy for search index
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / 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'
b7819090 13import { AuthService, ServerService } from '@app/core'
3a0fb65c 14import { ScreenService } from '@app/shared/misc/screen.service'
b7819090 15import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service'
5fb2e288
C
16import { VideoActionsDisplayType } from '@app/shared/video/video-actions-dropdown.component'
17import { I18n } from '@ngx-translate/i18n-polyfill'
18import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared'
19import { User } from '../users'
20import { Video } from './video.model'
501bc6c2 21
22a16e36 22export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto'
e2409062
C
23export type MiniatureDisplayOptions = {
24 date?: boolean
25 views?: boolean
26 by?: boolean
c2caa99b 27 avatar?: boolean
e2409062
C
28 privacyLabel?: boolean
29 privacyText?: boolean
30 state?: boolean
31 blacklistInfo?: boolean
32 nsfw?: boolean
33}
22a16e36 34
501bc6c2
C
35@Component({
36 selector: 'my-video-miniature',
ec8d8440 37 styleUrls: [ './video-miniature.component.scss' ],
89724816
C
38 templateUrl: './video-miniature.component.html',
39 changeDetection: ChangeDetectionStrategy.OnPush
501bc6c2 40})
22a16e36 41export class VideoMiniatureComponent implements OnInit {
df98563e
C
42 @Input() user: User
43 @Input() video: Video
e2409062 44
22a16e36 45 @Input() ownerDisplayType: OwnerDisplayType = 'account'
e2409062
C
46 @Input() displayOptions: MiniatureDisplayOptions = {
47 date: true,
48 views: true,
49 by: true,
c2caa99b 50 avatar: false,
e2409062
C
51 privacyLabel: false,
52 privacyText: false,
53 state: false,
54 blacklistInfo: false
55 }
56 @Input() displayAsRow = false
3a0fb65c 57 @Input() displayVideoActions = true
e66883b3 58 @Input() fitWidth = false
3a0fb65c 59
5fb2e288
C
60 @Input() useLazyLoadUrl = false
61
5baee5fc
RK
62 @Output() videoBlocked = new EventEmitter()
63 @Output() videoUnblocked = new EventEmitter()
3a0fb65c
C
64 @Output() videoRemoved = new EventEmitter()
65
66 videoActionsDisplayOptions: VideoActionsDisplayType = {
67 playlist: true,
68 download: false,
69 update: true,
70 blacklist: true,
71 delete: true,
b764380a 72 report: true,
988af781 73 duplicate: 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
5fb2e288
C
88 videoLink: any[] = []
89
22a16e36 90 private ownerDisplayTypeChosen: 'account' | 'videoChannel'
501bc6c2 91
e2409062 92 constructor (
3a0fb65c 93 private screenService: ScreenService,
e2409062
C
94 private serverService: ServerService,
95 private i18n: I18n,
b7819090
C
96 private authService: AuthService,
97 private videoPlaylistService: VideoPlaylistService,
98 private cd: ChangeDetectorRef,
e2409062 99 @Inject(LOCALE_ID) private localeId: string
435258ea 100 ) {}
0883b324 101
d1a63fc7 102 get isVideoBlur () {
ba430d75 103 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
d1a63fc7
C
104 }
105
22a16e36 106 ngOnInit () {
ba430d75
C
107 this.serverConfig = this.serverService.getTmpConfig()
108 this.serverService.getConfig()
5fb2e288
C
109 .subscribe(config => {
110 this.serverConfig = config
111 this.buildVideoLink()
112 })
ba430d75 113
3a0fb65c 114 this.setUpBy()
22a16e36 115
435258ea
RK
116 this.channelLinkTitle = this.i18n(
117 'Go to the channel page of {{name}} ({{handle}})',
118 { name: this.video.channel.name, handle: this.video.byVideoChannel }
119 )
120
8dfceec4
C
121 // We rely on mouseenter to lazy load actions
122 if (this.screenService.isInTouchScreen()) {
743f023c 123 this.loadActions()
22a16e36 124 }
92fb909c 125 }
22a16e36 126
5fb2e288
C
127 buildVideoLink () {
128 if (this.useLazyLoadUrl && this.video.url) {
129 const remoteUriConfig = this.serverConfig.search.remoteUri
130
131 // Redirect on the external instance if not allowed to fetch remote data
132 const externalRedirect = (!this.authService.isLoggedIn() && !remoteUriConfig.anonymous) || !remoteUriConfig.users
133 const fromPath = window.location.pathname + window.location.search
134
135 this.videoLink = [ '/search/lazy-load-video', { url: this.video.url, externalRedirect, fromPath } ]
136 return
137 }
138
139 this.videoLink = [ '/videos/watch', this.video.uuid ]
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
C
161 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
162 return this.i18n('Published')
163 }
164
165 if (video.scheduledUpdate) {
166 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
167 return this.i18n('Publication scheduled on ') + updateAt
168 }
169
170 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
171 return this.i18n('Waiting transcoding')
172 }
173
174 if (video.state.id === VideoState.TO_TRANSCODE) {
175 return this.i18n('To transcode')
176 }
177
178 if (video.state.id === VideoState.TO_IMPORT) {
179 return this.i18n('To import')
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
b7819090
C
211 isUserLoggedIn () {
212 return this.authService.isLoggedIn()
213 }
214
215 onWatchLaterClick (currentState: boolean) {
216 if (currentState === true) this.removeFromWatchLater()
217 else this.addToWatchLater()
218
219 this.inWatchLaterPlaylist = !currentState
220 }
221
222 addToWatchLater () {
223 const body = { videoId: this.video.id }
224
225 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
226 res => {
227 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
228 }
229 )
230 }
231
232 removeFromWatchLater () {
51b34a11 233 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
b7819090
C
234 .subscribe(
235 _ => { /* empty */ }
236 )
237 }
238
239 isWatchLaterPlaylistDisplayed () {
5fb2e288 240 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
b7819090
C
241 }
242
3a0fb65c
C
243 private setUpBy () {
244 if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
245 this.ownerDisplayTypeChosen = this.ownerDisplayType
246 return
247 }
248
249 // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
250 // -> Use the account name
251 if (
252 this.video.channel.name === `${this.video.account.name}_channel` ||
253 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}$/)
254 ) {
255 this.ownerDisplayTypeChosen = 'account'
256 } else {
257 this.ownerDisplayTypeChosen = 'videoChannel'
258 }
259 }
b7819090
C
260
261 private loadWatchLater () {
51b34a11
C
262 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
263
264 this.authService.userInformationLoaded
265 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
266 .subscribe(existResult => {
267 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
268 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
269 this.inWatchLaterPlaylist = false
270
271 this.watchLaterPlaylist = {
272 id: watchLaterPlaylist.id
273 }
274
275 if (existsInWatchLater) {
276 this.inWatchLaterPlaylist = true
277 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
278 }
279
280 this.cd.markForCheck()
281 })
282
283 this.videoPlaylistService.runPlaylistCheck(this.video.id)
b7819090 284 }
501bc6c2 285}