2020-01-06 14:59:17 +00:00
|
|
|
import { Subject, Subscription } from 'rxjs'
|
2019-01-08 10:26:41 +00:00
|
|
|
import { filter } from 'rxjs/operators'
|
2020-12-13 13:54:12 +00:00
|
|
|
import { Component, EventEmitter, Output, OnDestroy, OnInit, ViewChild } from '@angular/core'
|
2020-06-23 12:10:17 +00:00
|
|
|
import { NavigationEnd, Router } from '@angular/router'
|
2020-12-13 13:54:12 +00:00
|
|
|
import { Notifier, PeerTubeSocket, ScreenService } from '@app/core'
|
2020-06-23 12:10:17 +00:00
|
|
|
import { UserNotificationService } from '@app/shared/shared-main'
|
|
|
|
import { NgbPopover } from '@ng-bootstrap/ng-bootstrap'
|
2019-01-08 10:26:41 +00:00
|
|
|
|
|
|
|
@Component({
|
2020-12-13 13:54:12 +00:00
|
|
|
selector: 'my-notification',
|
|
|
|
templateUrl: './notification.component.html',
|
|
|
|
styleUrls: [ './notification.component.scss' ]
|
2019-01-08 10:26:41 +00:00
|
|
|
})
|
2020-12-13 13:54:12 +00:00
|
|
|
export class NotificationComponent implements OnInit, OnDestroy {
|
2019-07-24 14:05:59 +00:00
|
|
|
@ViewChild('popover', { static: true }) popover: NgbPopover
|
2020-01-06 14:59:17 +00:00
|
|
|
|
2020-08-17 08:13:31 +00:00
|
|
|
@Output() navigate = new EventEmitter<HTMLAnchorElement>()
|
2019-01-08 10:26:41 +00:00
|
|
|
|
|
|
|
unreadNotifications = 0
|
2019-02-20 09:16:04 +00:00
|
|
|
loaded = false
|
2020-12-13 13:54:12 +00:00
|
|
|
opened = false
|
2019-01-08 10:26:41 +00:00
|
|
|
|
2020-01-06 14:59:17 +00:00
|
|
|
markAllAsReadSubject = new Subject<boolean>()
|
|
|
|
|
2019-01-08 10:26:41 +00:00
|
|
|
private notificationSub: Subscription
|
|
|
|
private routeSub: Subscription
|
|
|
|
|
|
|
|
constructor (
|
|
|
|
private userNotificationService: UserNotificationService,
|
2020-12-13 13:54:12 +00:00
|
|
|
private screenService: ScreenService,
|
2020-09-25 08:04:21 +00:00
|
|
|
private peertubeSocket: PeerTubeSocket,
|
2019-01-08 10:26:41 +00:00
|
|
|
private notifier: Notifier,
|
|
|
|
private router: Router
|
2019-02-15 14:52:18 +00:00
|
|
|
) {
|
|
|
|
}
|
2019-01-08 10:26:41 +00:00
|
|
|
|
|
|
|
ngOnInit () {
|
|
|
|
this.userNotificationService.countUnreadNotifications()
|
2021-08-17 09:27:47 +00:00
|
|
|
.subscribe({
|
|
|
|
next: result => {
|
2019-02-15 14:52:18 +00:00
|
|
|
this.unreadNotifications = Math.min(result, 99) // Limit number to 99
|
|
|
|
this.subscribeToNotifications()
|
|
|
|
},
|
2019-01-08 10:26:41 +00:00
|
|
|
|
2021-08-17 09:27:47 +00:00
|
|
|
error: err => this.notifier.error(err.message)
|
|
|
|
})
|
2019-01-08 10:26:41 +00:00
|
|
|
|
|
|
|
this.routeSub = this.router.events
|
|
|
|
.pipe(filter(event => event instanceof NavigationEnd))
|
|
|
|
.subscribe(() => this.closePopover())
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy () {
|
|
|
|
if (this.notificationSub) this.notificationSub.unsubscribe()
|
|
|
|
if (this.routeSub) this.routeSub.unsubscribe()
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:54:12 +00:00
|
|
|
get isInMobileView () {
|
|
|
|
return this.screenService.isInMobileView()
|
|
|
|
}
|
|
|
|
|
2019-01-08 10:26:41 +00:00
|
|
|
closePopover () {
|
|
|
|
this.popover.close()
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:54:12 +00:00
|
|
|
onPopoverShown () {
|
|
|
|
this.opened = true
|
|
|
|
|
|
|
|
document.querySelector('menu').scrollTo(0, 0) // Reset menu scroll to easy lock
|
|
|
|
document.querySelector('menu').addEventListener('scroll', this.onMenuScrollEvent)
|
|
|
|
}
|
|
|
|
|
2019-02-20 09:16:04 +00:00
|
|
|
onPopoverHidden () {
|
|
|
|
this.loaded = false
|
2020-12-13 13:54:12 +00:00
|
|
|
this.opened = false
|
|
|
|
|
|
|
|
document.querySelector('menu').removeEventListener('scroll', this.onMenuScrollEvent)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock menu scroll when menu scroll to avoid fleeing / detached dropdown
|
|
|
|
onMenuScrollEvent () {
|
|
|
|
document.querySelector('menu').scrollTo(0, 0)
|
2019-02-20 09:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
onNotificationLoaded () {
|
|
|
|
this.loaded = true
|
|
|
|
}
|
|
|
|
|
2020-08-17 08:13:31 +00:00
|
|
|
onNavigate (link: HTMLAnchorElement) {
|
2020-12-13 13:54:12 +00:00
|
|
|
this.closePopover()
|
2020-08-17 08:13:31 +00:00
|
|
|
this.navigate.emit(link)
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:51:35 +00:00
|
|
|
markAllAsRead () {
|
2020-01-06 14:59:17 +00:00
|
|
|
this.markAllAsReadSubject.next(true)
|
2019-12-19 13:51:35 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 14:52:18 +00:00
|
|
|
private async subscribeToNotifications () {
|
2020-09-25 08:04:21 +00:00
|
|
|
const obs = await this.peertubeSocket.getMyNotificationsSocket()
|
2019-02-15 14:52:18 +00:00
|
|
|
|
|
|
|
this.notificationSub = obs.subscribe(data => {
|
|
|
|
if (data.type === 'new') return this.unreadNotifications++
|
|
|
|
if (data.type === 'read') return this.unreadNotifications--
|
|
|
|
if (data.type === 'read-all') return this.unreadNotifications = 0
|
|
|
|
})
|
2019-01-08 10:26:41 +00:00
|
|
|
}
|
|
|
|
}
|