]>
Commit | Line | Data |
---|---|---|
bc6f8863 | 1 | import { Subject, Subscription } from 'rxjs' |
2f1548fd | 2 | import { filter } from 'rxjs/operators' |
30d55e75 | 3 | import { Component, EventEmitter, Input, Output, OnDestroy, OnInit, ViewChild } from '@angular/core' |
67ed6552 | 4 | import { NavigationEnd, Router } from '@angular/router' |
a5cf76af | 5 | import { Notifier, User, PeerTubeSocket } from '@app/core' |
67ed6552 C |
6 | import { UserNotificationService } from '@app/shared/shared-main' |
7 | import { NgbPopover } from '@ng-bootstrap/ng-bootstrap' | |
2f1548fd C |
8 | |
9 | @Component({ | |
10 | selector: 'my-avatar-notification', | |
11 | templateUrl: './avatar-notification.component.html', | |
12 | styleUrls: [ './avatar-notification.component.scss' ] | |
13 | }) | |
14 | export class AvatarNotificationComponent implements OnInit, OnDestroy { | |
f36da21e | 15 | @ViewChild('popover', { static: true }) popover: NgbPopover |
bc6f8863 | 16 | |
2f1548fd | 17 | @Input() user: User |
30d55e75 | 18 | @Output() navigate = new EventEmitter<HTMLAnchorElement>() |
2f1548fd C |
19 | |
20 | unreadNotifications = 0 | |
b28e4e5e | 21 | loaded = false |
2f1548fd | 22 | |
bc6f8863 C |
23 | markAllAsReadSubject = new Subject<boolean>() |
24 | ||
2f1548fd C |
25 | private notificationSub: Subscription |
26 | private routeSub: Subscription | |
27 | ||
28 | constructor ( | |
29 | private userNotificationService: UserNotificationService, | |
a5cf76af | 30 | private peertubeSocket: PeerTubeSocket, |
2f1548fd C |
31 | private notifier: Notifier, |
32 | private router: Router | |
41d71344 C |
33 | ) { |
34 | } | |
2f1548fd C |
35 | |
36 | ngOnInit () { | |
37 | this.userNotificationService.countUnreadNotifications() | |
41d71344 C |
38 | .subscribe( |
39 | result => { | |
40 | this.unreadNotifications = Math.min(result, 99) // Limit number to 99 | |
41 | this.subscribeToNotifications() | |
42 | }, | |
2f1548fd | 43 | |
41d71344 C |
44 | err => this.notifier.error(err.message) |
45 | ) | |
2f1548fd C |
46 | |
47 | this.routeSub = this.router.events | |
48 | .pipe(filter(event => event instanceof NavigationEnd)) | |
49 | .subscribe(() => this.closePopover()) | |
50 | } | |
51 | ||
52 | ngOnDestroy () { | |
53 | if (this.notificationSub) this.notificationSub.unsubscribe() | |
54 | if (this.routeSub) this.routeSub.unsubscribe() | |
55 | } | |
56 | ||
57 | closePopover () { | |
58 | this.popover.close() | |
59 | } | |
60 | ||
b28e4e5e C |
61 | onPopoverHidden () { |
62 | this.loaded = false | |
63 | } | |
64 | ||
65 | onNotificationLoaded () { | |
66 | this.loaded = true | |
67 | } | |
68 | ||
30d55e75 K |
69 | onNavigate (link: HTMLAnchorElement) { |
70 | this.navigate.emit(link) | |
71 | } | |
72 | ||
10475dea | 73 | markAllAsRead () { |
bc6f8863 | 74 | this.markAllAsReadSubject.next(true) |
10475dea RK |
75 | } |
76 | ||
41d71344 | 77 | private async subscribeToNotifications () { |
a5cf76af | 78 | const obs = await this.peertubeSocket.getMyNotificationsSocket() |
41d71344 C |
79 | |
80 | this.notificationSub = obs.subscribe(data => { | |
81 | if (data.type === 'new') return this.unreadNotifications++ | |
82 | if (data.type === 'read') return this.unreadNotifications-- | |
83 | if (data.type === 'read-all') return this.unreadNotifications = 0 | |
84 | }) | |
2f1548fd C |
85 | } |
86 | ||
87 | } |