1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00
pigallery2/src/frontend/app/model/notification.service.ts

102 lines
3.2 KiB
TypeScript
Raw Normal View History

2019-01-27 14:58:56 -05:00
import {Injectable} from '@angular/core';
2018-05-22 20:27:07 -04:00
import {ToastrService} from 'ngx-toastr';
2018-03-30 15:30:30 -04:00
import {NetworkService} from './network/network.service';
import {AuthenticationService} from './network/authentication.service';
import {NotificationDTO, NotificationType} from '../../../common/entities/NotificationDTO';
import {UserDTO, UserRoles} from '../../../common/entities/UserDTO';
2017-07-08 12:43:42 +02:00
export interface CountedNotificationDTO extends NotificationDTO {
count: number;
}
2017-07-08 12:43:42 +02:00
@Injectable()
export class NotificationService {
options = {
2018-03-30 15:30:30 -04:00
positionClass: 'toast-top-center',
animate: 'flyLeft'
2017-07-08 12:43:42 +02:00
};
countedNotifications: CountedNotificationDTO[] = [];
numberOfNotifications = 0;
2017-07-09 14:23:50 +02:00
lastUser: UserDTO = null;
2017-07-08 12:43:42 +02:00
constructor(private toastr: ToastrService,
private networkService: NetworkService,
private authService: AuthenticationService) {
2017-07-08 12:43:42 +02:00
this.authService.user.subscribe(() => {
if (this.authService.isAuthenticated() &&
2017-07-09 14:23:50 +02:00
(!this.lastUser ||
this.lastUser.id !== this.authService.user.value.id) &&
this.authService.user.value.role >= UserRoles.Guest) {
2017-07-09 14:23:50 +02:00
this.getServerNotifications();
}
this.lastUser = this.authService.user.value;
2017-07-09 14:23:50 +02:00
});
}
2019-12-10 14:50:20 +01:00
get Toastr(): ToastrService {
return this.toastr;
2019-12-10 14:50:20 +01:00
}
groupNotifications(notifications: NotificationDTO[]): void {
const groups: { [key: string]: { notification: NotificationDTO, count: number } } = {};
notifications.forEach(n => {
let key = n.message;
if (n.details) {
key += JSON.stringify(n.details);
}
groups[key] = groups[key] || {notification: n, count: 0};
groups[key].count++;
});
this.numberOfNotifications = notifications.length;
this.countedNotifications = [];
for (const key of Object.keys(groups)) {
(groups[key].notification as CountedNotificationDTO).count = groups[key].count;
this.countedNotifications.push(groups[key].notification as CountedNotificationDTO);
}
}
async getServerNotifications(): Promise<void> {
2019-12-10 14:50:20 +01:00
try {
this.groupNotifications((await this.networkService.getJson<NotificationDTO[]>('/notifications')) || []);
this.countedNotifications.forEach((noti) => {
let msg = '(' + noti.count + ') ' + noti.message;
2019-12-10 14:50:20 +01:00
if (noti.details) {
msg += ' Details: ' + JSON.stringify(noti.details);
}
switch (noti.type) {
case NotificationType.error:
this.error(msg, $localize`Server error`);
2019-12-10 14:50:20 +01:00
break;
case NotificationType.warning:
this.warning(msg, $localize`Server error`);
2019-12-10 14:50:20 +01:00
break;
case NotificationType.info:
this.info(msg, $localize`Server info`);
2019-12-10 14:50:20 +01:00
break;
}
});
} catch (e) {
console.error(e);
}
2017-07-08 12:43:42 +02:00
}
2018-11-28 23:49:33 +01:00
success(text: string, title: string = null): void {
this.toastr.success(text, title, this.options);
2017-07-08 12:43:42 +02:00
}
2018-11-28 23:49:33 +01:00
error(text: string, title?: string): void {
this.toastr.error(text, title, this.options);
2017-07-08 12:43:42 +02:00
}
2018-11-28 23:49:33 +01:00
warning(text: string, title?: string): void {
this.toastr.warning(text, title, this.options);
2017-07-08 12:43:42 +02:00
}
2018-11-28 23:49:33 +01:00
info(text: string, title: string = null): void {
this.toastr.info(text, title, this.options);
2017-07-08 12:43:42 +02:00
}
}