Merge branch 'feature/correctly-send-activities' into develop
[oweals/peertube.git] / client / src / app / +my-account / my-account-history / my-account-history.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Location } from '@angular/common'
4 import { immutableAssign } from '@app/shared/misc/utils'
5 import { ComponentPagination } from '@app/shared/rest/component-pagination.model'
6 import { AuthService } from '../../core/auth'
7 import { ConfirmService } from '../../core/confirm'
8 import { AbstractVideoList } from '../../shared/video/abstract-video-list'
9 import { VideoService } from '../../shared/video/video.service'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { ScreenService } from '@app/shared/misc/screen.service'
12 import { UserHistoryService } from '@app/shared/users/user-history.service'
13 import { UserService } from '@app/shared'
14 import { Notifier } from '@app/core'
15
16 @Component({
17   selector: 'my-account-history',
18   templateUrl: './my-account-history.component.html',
19   styleUrls: [ './my-account-history.component.scss' ]
20 })
21 export class MyAccountHistoryComponent extends AbstractVideoList implements OnInit, OnDestroy {
22   titlePage: string
23   currentRoute = '/my-account/history/videos'
24   pagination: ComponentPagination = {
25     currentPage: 1,
26     itemsPerPage: 5,
27     totalItems: null
28   }
29   videosHistoryEnabled: boolean
30
31   protected baseVideoWidth = -1
32   protected baseVideoHeight = 155
33
34   constructor (
35     protected router: Router,
36     protected route: ActivatedRoute,
37     protected authService: AuthService,
38     protected userService: UserService,
39     protected notifier: Notifier,
40     protected location: Location,
41     protected screenService: ScreenService,
42     protected i18n: I18n,
43     private confirmService: ConfirmService,
44     private videoService: VideoService,
45     private userHistoryService: UserHistoryService
46   ) {
47     super()
48
49     this.titlePage = this.i18n('My videos history')
50   }
51
52   ngOnInit () {
53     super.ngOnInit()
54
55     this.videosHistoryEnabled = this.authService.getUser().videosHistoryEnabled
56   }
57
58   ngOnDestroy () {
59     super.ngOnDestroy()
60   }
61
62   getVideosObservable (page: number) {
63     const newPagination = immutableAssign(this.pagination, { currentPage: page })
64
65     return this.userHistoryService.getUserVideosHistory(newPagination)
66   }
67
68   generateSyndicationList () {
69     throw new Error('Method not implemented.')
70   }
71
72   onVideosHistoryChange () {
73     this.userService.updateMyProfile({ videosHistoryEnabled: this.videosHistoryEnabled })
74       .subscribe(
75         () => {
76           const message = this.videosHistoryEnabled === true ?
77             this.i18n('Videos history is enabled') :
78             this.i18n('Videos history is disabled')
79
80           this.notifier.success(message)
81
82           this.authService.refreshUserInformation()
83         },
84
85         err => this.notifier.error(err.message)
86       )
87   }
88
89   async deleteHistory () {
90     const title = this.i18n('Delete videos history')
91     const message = this.i18n('Are you sure you want to delete all your videos history?')
92
93     const res = await this.confirmService.confirm(message, title)
94     if (res !== true) return
95
96     this.userHistoryService.deleteUserVideosHistory()
97         .subscribe(
98           () => {
99             this.notifier.success(this.i18n('Videos history deleted'))
100
101             this.reloadVideos()
102           },
103
104           err => this.notifier.error(err.message)
105         )
106   }
107 }