1
0
mirror of https://github.com/NicolasConstant/sengi synced 2025-01-23 23:11:04 +01:00

wirering of notifications with settings state

This commit is contained in:
Nicolas Constant 2019-03-31 18:53:11 -04:00
parent b096ced237
commit 5547b19727
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688

View File

@ -6,6 +6,7 @@ import { Status, Notification } from './models/mastodon.interfaces';
import { MastodonService } from './mastodon.service';
import { AccountInfo } from '../states/accounts.state';
import { NotificationService } from './notification.service';
import { ToolsService } from './tools.service';
@Injectable({
providedIn: 'root'
@ -16,6 +17,7 @@ export class UserNotificationService {
private sinceIds: { [id: string]: string } = {};
constructor(
private readonly toolsService: ToolsService,
private readonly notificationService: NotificationService,
private readonly mastodonService: MastodonService,
private readonly store: Store) {
@ -65,7 +67,7 @@ export class UserNotificationService {
if (currentAccountNotifications) {
currentAccountNotifications.allNotifications = [...notifications, ...currentAccountNotifications.allNotifications];
currentAccountNotifications = this.analyseNotifications(currentAccountNotifications);
currentAccountNotifications = this.analyseNotifications(account, currentAccountNotifications);
if (currentAccountNotifications.hasNewMentions || currentAccountNotifications.hasNewNotifications) {
currentNotifications = currentNotifications.filter(x => x.account.id !== account.id);
@ -77,14 +79,14 @@ export class UserNotificationService {
newNotifications.account = account;
newNotifications.allNotifications = notifications;
newNotifications = this.analyseNotifications(newNotifications);
newNotifications = this.analyseNotifications(account, newNotifications);
currentNotifications.push(newNotifications);
this.userNotifications.next(currentNotifications);
}
}
private analyseNotifications(userNotification: UserNotification): UserNotification {
private analyseNotifications(account: AccountInfo, userNotification: UserNotification): UserNotification {
if (userNotification.allNotifications.length > 30) {
userNotification.allNotifications.length = 30;
}
@ -96,34 +98,58 @@ export class UserNotificationService {
const currentNotifications = userNotification.notifications;
const currentMentions = userNotification.mentions;
if (!currentNotifications) {
userNotification.notifications = newNotifications;
userNotification.notifications = [...newNotifications, ...currentNotifications];
userNotification.mentions = [...newMentions, ...currentMentions];
const accountSettings = this.toolsService.getAccountSettings(account);
} else if (currentNotifications.length === 0) {
if (newNotifications.length > 0) {
userNotification.hasNewNotifications = true;
}
userNotification.notifications = newNotifications;
} else if (newNotifications.length > 0) {
userNotification.hasNewNotifications = currentNotifications[0].id !== newNotifications[0].id;
userNotification.notifications = [...newNotifications, ...currentNotifications];
if(accountSettings.lastMentionReadId && userNotification.mentions[0] && accountSettings.lastMentionReadId !== userNotification.mentions[0].id){
userNotification.hasNewMentions = true;
} else {
userNotification.hasNewMentions = false;
}
if (!currentNotifications) {
userNotification.mentions = newMentions;
} else if (currentMentions.length === 0) {
if (newMentions.length > 0) {
userNotification.hasNewMentions = true;
}
userNotification.mentions = newMentions;
} else if (newMentions.length > 0) {
userNotification.hasNewMentions = currentMentions[0].id !== newMentions[0].id;
userNotification.mentions = [...newMentions, ...currentMentions];
if(accountSettings.lastNotificationReadId && userNotification.notifications[0] && accountSettings.lastNotificationReadId !== userNotification.notifications[0].id){
userNotification.hasNewNotifications = true;
} else {
userNotification.hasNewNotifications = false;
}
if((!accountSettings.lastMentionReadId && userNotification.mentions[0])
|| (!accountSettings.lastNotificationReadId && userNotification.notifications[0])){
accountSettings.lastMentionReadId = userNotification.mentions[0].id;
accountSettings.lastNotificationReadId = userNotification.notifications[0].id;
this.toolsService.saveAccountSettings(accountSettings);
}
// if (!currentNotifications) {
// userNotification.notifications = newNotifications;
// } else if (currentNotifications.length === 0) {
// if (newNotifications.length > 0) {
// userNotification.hasNewNotifications = true;
// }
// userNotification.notifications = newNotifications;
// } else if (newNotifications.length > 0) {
// userNotification.hasNewNotifications = currentNotifications[0].id !== newNotifications[0].id;
// userNotification.notifications = [...newNotifications, ...currentNotifications];
// }
// if (!currentNotifications) {
// userNotification.mentions = newMentions;
// } else if (currentMentions.length === 0) {
// if (newMentions.length > 0) {
// userNotification.hasNewMentions = true;
// }
// userNotification.mentions = newMentions;
// } else if (newMentions.length > 0) {
// userNotification.hasNewMentions = currentMentions[0].id !== newMentions[0].id;
// userNotification.mentions = [...newMentions, ...currentMentions];
// }
return userNotification;
}
@ -131,6 +157,14 @@ export class UserNotificationService {
let currentNotifications = this.userNotifications.value;
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
const lastMention = currentAccountNotifications.mentions[0];
if(lastMention){
// const lastNotification = currentAccountNotifications.allNotifications.find(x => x.status && x.status.id === lastMention.id);
const settings = this.toolsService.getAccountSettings(account);
settings.lastMentionReadId = lastMention.id;
this.toolsService.saveAccountSettings(settings);
}
if (currentAccountNotifications.hasNewMentions === true) {
currentAccountNotifications.hasNewMentions = false;
this.userNotifications.next(currentNotifications);
@ -141,6 +175,13 @@ export class UserNotificationService {
let currentNotifications = this.userNotifications.value;
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
const lastNotification = currentAccountNotifications.notifications[0];
if(lastNotification){
const settings = this.toolsService.getAccountSettings(account);
settings.lastNotificationReadId = lastNotification.id;
this.toolsService.saveAccountSettings(settings);
}
if (currentAccountNotifications.hasNewNotifications === true) {
currentAccountNotifications.hasNewNotifications = false;
this.userNotifications.next(currentNotifications);