video-rate.component.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Observable } from 'rxjs'
  2. import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output } from '@angular/core'
  3. import { Notifier, ScreenService, Hotkey, HotkeysService } from '@app/core'
  4. import { VideoDetails, VideoService } from '@app/shared/shared-main'
  5. import { UserVideoRateType } from '@peertube/peertube-models'
  6. @Component({
  7. selector: 'my-video-rate',
  8. templateUrl: './video-rate.component.html',
  9. styleUrls: [ './video-rate.component.scss' ]
  10. })
  11. export class VideoRateComponent implements OnInit, OnChanges, OnDestroy {
  12. @Input() video: VideoDetails
  13. @Input() videoPassword: string
  14. @Input() isUserLoggedIn: boolean
  15. @Output() userRatingLoaded = new EventEmitter<UserVideoRateType>()
  16. @Output() rateUpdated = new EventEmitter<UserVideoRateType>()
  17. userRating: UserVideoRateType
  18. tooltipLike = ''
  19. tooltipDislike = ''
  20. private hotkeys: Hotkey[]
  21. constructor (
  22. private videoService: VideoService,
  23. private notifier: Notifier,
  24. private hotkeysService: HotkeysService,
  25. private screenService: ScreenService
  26. ) { }
  27. ngOnInit () {
  28. // Hide the tooltips for unlogged users in mobile view, this adds confusion with the popover
  29. if (this.isUserLoggedIn || !this.screenService.isInMobileView()) {
  30. this.tooltipLike = $localize`Like this video`
  31. this.tooltipDislike = $localize`Dislike this video`
  32. }
  33. if (this.isUserLoggedIn) {
  34. this.hotkeys = [
  35. new Hotkey('Shift+l', () => {
  36. this.setLike()
  37. return false
  38. }, $localize`Like the video`),
  39. new Hotkey('Shift+d', () => {
  40. this.setDislike()
  41. return false
  42. }, $localize`Dislike the video`)
  43. ]
  44. this.hotkeysService.add(this.hotkeys)
  45. }
  46. }
  47. ngOnChanges () {
  48. this.checkUserRating()
  49. }
  50. ngOnDestroy () {
  51. this.hotkeysService.remove(this.hotkeys)
  52. }
  53. setLike () {
  54. if (this.isUserLoggedIn === false) return
  55. // Already liked this video
  56. if (this.userRating === 'like') this.setRating('none')
  57. else this.setRating('like')
  58. }
  59. setDislike () {
  60. if (this.isUserLoggedIn === false) return
  61. // Already disliked this video
  62. if (this.userRating === 'dislike') this.setRating('none')
  63. else this.setRating('dislike')
  64. }
  65. getRatePopoverText () {
  66. if (this.isUserLoggedIn) return undefined
  67. return $localize`You need to be logged in to rate this video.`
  68. }
  69. private checkUserRating () {
  70. // Unlogged users do not have ratings
  71. if (this.isUserLoggedIn === false) return
  72. this.videoService.getUserVideoRating(this.video.uuid)
  73. .subscribe({
  74. next: ratingObject => {
  75. if (!ratingObject) return
  76. this.userRating = ratingObject.rating
  77. this.userRatingLoaded.emit(this.userRating)
  78. },
  79. error: err => this.notifier.error(err.message)
  80. })
  81. }
  82. private setRating (nextRating: UserVideoRateType) {
  83. const ratingMethods: { [id in UserVideoRateType]: (id: string, videoPassword: string) => Observable<any> } = {
  84. like: this.videoService.setVideoLike.bind(this.videoService),
  85. dislike: this.videoService.setVideoDislike.bind(this.videoService),
  86. none: this.videoService.unsetVideoLike.bind(this.videoService)
  87. }
  88. ratingMethods[nextRating](this.video.uuid, this.videoPassword)
  89. .subscribe({
  90. next: () => {
  91. // Update the video like attribute
  92. this.updateVideoRating(this.userRating, nextRating)
  93. this.userRating = nextRating
  94. this.rateUpdated.emit(this.userRating)
  95. },
  96. error: err => this.notifier.error(err.message)
  97. })
  98. }
  99. private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) {
  100. let likesToIncrement = 0
  101. let dislikesToIncrement = 0
  102. if (oldRating) {
  103. if (oldRating === 'like') likesToIncrement--
  104. if (oldRating === 'dislike') dislikesToIncrement--
  105. }
  106. if (newRating === 'like') likesToIncrement++
  107. if (newRating === 'dislike') dislikesToIncrement++
  108. this.video.likes += likesToIncrement
  109. this.video.dislikes += dislikesToIncrement
  110. this.video.buildLikeAndDislikePercents()
  111. }
  112. }