]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-library/my-history/my-history.component.ts
Add video filters to common video pages
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-history / my-history.component.ts
1 import { tap } from 'rxjs/operators'
2 import { Component, ComponentFactoryResolver, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import {
5 AuthService,
6 ComponentPagination,
7 ConfirmService,
8 DisableForReuseHook,
9 LocalStorageService,
10 Notifier,
11 ScreenService,
12 ServerService,
13 User,
14 UserService
15 } from '@app/core'
16 import { immutableAssign } from '@app/helpers'
17 import { UserHistoryService, Video } from '@app/shared/shared-main'
18 import { MiniatureDisplayOptions, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
19
20 @Component({
21 templateUrl: './my-history.component.html',
22 styleUrls: [ './my-history.component.scss' ]
23 })
24 export class MyHistoryComponent implements OnInit, DisableForReuseHook {
25 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
26
27 titlePage: string
28 pagination: ComponentPagination = {
29 currentPage: 1,
30 itemsPerPage: 5,
31 totalItems: null
32 }
33
34 videosHistoryEnabled: boolean
35
36 miniatureDisplayOptions: MiniatureDisplayOptions = {
37 date: true,
38 views: true,
39 by: true,
40 privacyLabel: false,
41 privacyText: true,
42 state: true,
43 blacklistInfo: true
44 }
45
46 getVideosObservableFunction = this.getVideosObservable.bind(this)
47
48 user: User
49
50 videos: Video[] = []
51 search: string
52
53 disabled = false
54
55 constructor (
56 protected router: Router,
57 protected serverService: ServerService,
58 protected route: ActivatedRoute,
59 protected authService: AuthService,
60 protected userService: UserService,
61 protected notifier: Notifier,
62 protected screenService: ScreenService,
63 protected storageService: LocalStorageService,
64 private confirmService: ConfirmService,
65 private userHistoryService: UserHistoryService,
66 protected cfr: ComponentFactoryResolver
67 ) {
68 this.titlePage = $localize`My watch history`
69 }
70
71 ngOnInit () {
72 this.user = this.authService.getUser()
73
74 this.authService.userInformationLoaded
75 .subscribe(() => this.videosHistoryEnabled = this.user.videosHistoryEnabled)
76 }
77
78 disableForReuse () {
79 this.disabled = true
80 }
81
82 enabledForReuse () {
83 this.disabled = false
84 }
85
86 reloadData () {
87 this.videosSelection.reloadVideos()
88 }
89
90 onSearch (search: string) {
91 this.search = search
92 this.reloadData()
93 }
94
95 getVideosObservable (page: number) {
96 const newPagination = immutableAssign(this.pagination, { currentPage: page })
97
98 return this.userHistoryService.getUserVideosHistory(newPagination, this.search)
99 .pipe(
100 tap(res => this.pagination.totalItems = res.total)
101 )
102 }
103
104 generateSyndicationList () {
105 /* method disabled */
106 throw new Error('Method not implemented.')
107 }
108
109 onVideosHistoryChange () {
110 this.userService.updateMyProfile({ videosHistoryEnabled: this.videosHistoryEnabled })
111 .subscribe({
112 next: () => {
113 const message = this.videosHistoryEnabled === true
114 ? $localize`Videos history is enabled`
115 : $localize`Videos history is disabled`
116
117 this.notifier.success(message)
118
119 this.authService.refreshUserInformation()
120 },
121
122 error: err => this.notifier.error(err.message)
123 })
124 }
125
126 async deleteHistory () {
127 const title = $localize`Delete videos history`
128 const message = $localize`Are you sure you want to delete all your videos history?`
129
130 const res = await this.confirmService.confirm(message, title)
131 if (res !== true) return
132
133 this.userHistoryService.deleteUserVideosHistory()
134 .subscribe({
135 next: () => {
136 this.notifier.success($localize`Videos history deleted`)
137
138 this.reloadData()
139 },
140
141 error: err => this.notifier.error(err.message)
142 })
143 }
144 }