1
0
mirror of https://github.com/NicolasConstant/sengi synced 2025-02-09 08:28:40 +01:00

better analysis of new mentions/notifications #55

This commit is contained in:
Nicolas Constant 2019-03-28 00:46:37 -04:00
parent 18794ba0c7
commit e61a7cd49a
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 60 additions and 33 deletions

View File

@ -86,12 +86,9 @@ export class MentionsComponent implements OnInit, OnDestroy {
this.isLoading = true; this.isLoading = true;
console.warn(`this.lastId ${this.lastId}`);
this.mastodonService.getNotifications(this.account.info, ['follow', 'favourite', 'reblog'], this.lastId) this.mastodonService.getNotifications(this.account.info, ['follow', 'favourite', 'reblog'], this.lastId)
.then((result: Notification[]) => { .then((result: Notification[]) => {
console.warn(result);
const statuses = result.map(x => x.status); const statuses = result.map(x => x.status);
if (statuses.length === 0) { if (statuses.length === 0) {
this.maxReached = true; this.maxReached = true;

View File

@ -29,7 +29,7 @@ export class UserNotificationService {
accounts.forEach((account: AccountInfo) => { accounts.forEach((account: AccountInfo) => {
let sinceId = null; let sinceId = null;
if(this.sinceIds[account.id]){ if (this.sinceIds[account.id]) {
sinceId = this.sinceIds[account.id]; sinceId = this.sinceIds[account.id];
} }
@ -48,58 +48,85 @@ export class UserNotificationService {
setTimeout(() => { setTimeout(() => {
this.fetchNotifications(); this.fetchNotifications();
}, 15 * 1000); }, 15 * 1000);
}); });
} }
private processNotifications(account: AccountInfo, notifications: Notification[]) { private processNotifications(account: AccountInfo, notifications: Notification[]) {
if(notifications.length === 0){ if (notifications.length === 0) {
return; return;
} }
let currentNotifications = this.userNotifications.value; let currentNotifications = this.userNotifications.value;
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id); let currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
const userNotifications = notifications.filter(x => x.type !== 'mention');
const userMentions = notifications.filter(x => x.type === 'mention').map(x => x.status);
const lastId = notifications[notifications.length - 1].id;
const sinceId = notifications[0].id; const sinceId = notifications[0].id;
this.sinceIds[account.id] = sinceId; this.sinceIds[account.id] = sinceId;
if (currentAccountNotifications) { if (currentAccountNotifications) {
const currentUserNotifications = currentAccountNotifications.notifications; currentAccountNotifications.allNotifications = [...notifications, ...currentAccountNotifications.allNotifications];
const currentUserMentions = currentAccountNotifications.mentions;
const hasNewNotifications = (userNotifications.length === 0 && currentUserNotifications.length > 0) currentAccountNotifications = this.analyseNotifications(currentAccountNotifications);
|| (userNotifications.length > 0 && currentUserNotifications.length > 0) && (userNotifications[0].id !== currentUserNotifications[0].id);
const hasNewMentions = (userMentions.length === 0 && currentUserMentions.length > 0)
|| (userMentions.length > 0 && currentUserMentions.length > 0) && (userMentions[0].id !== currentUserMentions[0].id);
if (hasNewNotifications || hasNewMentions) {
currentAccountNotifications.hasNewMentions = hasNewMentions;
currentAccountNotifications.hasNewNotifications = hasNewNotifications;
currentAccountNotifications.notifications = userNotifications;
currentAccountNotifications.mentions = userMentions;
currentAccountNotifications.lastId = lastId;
if (currentAccountNotifications.hasNewMentions || currentAccountNotifications.hasNewNotifications) {
currentNotifications = currentNotifications.filter(x => x.account.id !== account.id); currentNotifications = currentNotifications.filter(x => x.account.id !== account.id);
currentNotifications.push(currentAccountNotifications); currentNotifications.push(currentAccountNotifications);
this.userNotifications.next(currentNotifications); this.userNotifications.next(currentNotifications);
} }
} else { } else {
const newNotifications = new UserNotification(); let newNotifications = new UserNotification();
newNotifications.account = account; newNotifications.account = account;
newNotifications.hasNewNotifications = false; //TODO: check in local settings newNotifications.allNotifications = notifications;
newNotifications.hasNewMentions = false; //TODO: check in local settings
newNotifications.notifications = userNotifications; newNotifications = this.analyseNotifications(newNotifications);
newNotifications.mentions = userMentions;
newNotifications.lastId = lastId;
currentNotifications.push(newNotifications); currentNotifications.push(newNotifications);
this.userNotifications.next(currentNotifications); this.userNotifications.next(currentNotifications);
} }
} }
private analyseNotifications(userNotification: UserNotification): UserNotification {
if(userNotification.allNotifications.length > 30){
userNotification.allNotifications.length = 30;
}
userNotification.lastId = userNotification.allNotifications[userNotification.allNotifications.length - 1].id;
const newNotifications = userNotification.allNotifications.filter(x => x.type !== 'mention');
const newMentions = userNotification.allNotifications.filter(x => x.type === 'mention').map(x => x.status);
const currentNotifications = userNotification.notifications;
const currentMentions = userNotification.mentions;
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;
}
markMentionsAsRead(account: AccountInfo) { markMentionsAsRead(account: AccountInfo) {
let currentNotifications = this.userNotifications.value; let currentNotifications = this.userNotifications.value;
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id); const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
@ -117,9 +144,12 @@ export class UserNotificationService {
export class UserNotification { export class UserNotification {
account: AccountInfo; account: AccountInfo;
allNotifications: Notification[] = [];
hasNewNotifications: boolean; hasNewNotifications: boolean;
hasNewMentions: boolean; hasNewMentions: boolean;
notifications: Notification[] = [];
mentions: Status[] = []; notifications: Notification[];
mentions: Status[];
lastId: string; lastId: string;
} }