1
0
mirror of https://github.com/NicolasConstant/sengi synced 2025-02-08 07:58:47 +01:00

make distinction between mention and notification

This commit is contained in:
Nicolas Constant 2019-11-07 00:19:06 -05:00
parent c846859461
commit a9bee0f9fb
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
5 changed files with 132 additions and 75 deletions

View File

@ -72,10 +72,10 @@ export class MentionsComponent implements OnInit, OnDestroy {
});
}
private processNewMentions(userNotifications: UserNotification[]) {
private processNewMentions(userNotifications: UserNotification[]) {
const userNotification = userNotifications.find(x => x.account.id === this.account.info.id);
if (userNotification && userNotification.mentions) {
let orderedMentions = [...userNotification.mentions].reverse();
let orderedMentions = [...userNotification.mentions.map(x => x.status)].reverse();
for (let m of orderedMentions) {
if (!this.statuses.find(x => x.status.id === m.id)) {
const statusWrapper = new StatusWrapper(m, this.account.info);
@ -83,7 +83,7 @@ export class MentionsComponent implements OnInit, OnDestroy {
}
}
}
this.lastId = userNotification.lastId;
this.lastId = userNotification.lastMentionsId;
this.userNotificationService.markMentionsAsRead(this.account.info);
}

View File

@ -85,7 +85,7 @@ export class NotificationsComponent implements OnInit, OnDestroy {
}
}
}
this.lastId = userNotification.lastId;
this.lastId = userNotification.lastNotificationsId;
this.userNotificationService.markNotificationAsRead(this.account.info);
}

View File

@ -216,7 +216,7 @@ export class MastodonWrapperService {
});
}
getNotifications(account: AccountInfo, excludeTypes: string[] = null, maxId: string = null, sinceId: string = null, limit: number = 15): Promise<Notification[]> {
getNotifications(account: AccountInfo, excludeTypes: ('follow' | 'favourite' | 'reblog' | 'mention')[] = null, maxId: string = null, sinceId: string = null, limit: number = 15): Promise<Notification[]> {
return this.refreshAccountIfNeeded(account)
.then((refreshedAccount: AccountInfo) => {
return this.mastodonService.getNotifications(refreshedAccount, excludeTypes, maxId, sinceId, limit);

View File

@ -262,7 +262,7 @@ export class MastodonService {
return this.httpClient.put<Attachment>(route, input, { headers: headers }).toPromise();
}
getNotifications(account: AccountInfo, excludeTypes: string[] = null, maxId: string = null, sinceId: string = null, limit: number = 15): Promise<Notification[]> {
getNotifications(account: AccountInfo, excludeTypes: ('follow' | 'favourite' | 'reblog' | 'mention')[] = null, maxId: string = null, sinceId: string = null, limit: number = 15): Promise<Notification[]> {
let route = `https://${account.instance}${this.apiRoutes.getNotifications}?limit=${limit}`;
if(maxId){

View File

@ -7,6 +7,8 @@ import { MastodonWrapperService } from './mastodon-wrapper.service';
import { AccountInfo } from '../states/accounts.state';
import { NotificationService } from './notification.service';
import { ToolsService } from './tools.service';
import { StreamingService } from './streaming.service';
import { NotificationsComponent } from '../components/floating-column/manage-account/notifications/notifications.component';
@Injectable({
providedIn: 'root'
@ -14,9 +16,11 @@ import { ToolsService } from './tools.service';
export class UserNotificationService {
userNotifications = new BehaviorSubject<UserNotification[]>([]);
private sinceIds: { [id: string]: string } = {};
private mentionsSinceIds: { [id: string]: string } = {};
private notificationsSinceIds: { [id: string]: string } = {};
constructor(
private readonly streamingService: StreamingService,
private readonly toolsService: ToolsService,
private readonly notificationService: NotificationService,
private readonly mastodonService: MastodonWrapperService,
@ -27,33 +31,56 @@ export class UserNotificationService {
private fetchNotifications() {
let accounts = this.store.snapshot().registeredaccounts.accounts;
let promises: Promise<any>[] = [];
// let promises: Promise<any>[] = [];
accounts.forEach((account: AccountInfo) => {
let sinceId = null;
if (this.sinceIds[account.id]) {
sinceId = this.sinceIds[account.id];
}
// let sinceId = null;
// if (this.sinceIds[account.id]) {
// sinceId = this.sinceIds[account.id];
// }
let getNotificationPromise = this.mastodonService.getNotifications(account, null, null, sinceId, 30)
this.mastodonService.getNotifications(account, ['follow', 'follow', 'reblog'], null, null, 10)
.then((notifications: Notification[]) => {
this.processNotifications(account, notifications);
this.processMentionsAndNotifications(account, notifications, NotificationTypeEnum.UserMention);
})
.catch(err => {
this.notificationService.notifyHttpError(err, account);
});
promises.push(getNotificationPromise);
});;
this.mastodonService.getNotifications(account, ['mention'], null, null, 10)
.then((notifications: Notification[]) => {
this.processMentionsAndNotifications(account, notifications, NotificationTypeEnum.UserNotification);
})
.catch(err => {
this.notificationService.notifyHttpError(err, account);
});;
//TODO: start streaming services
// let getNotificationPromise = this.mastodonService.getNotifications(account, null, null, sinceId, 30)
// .then((notifications: Notification[]) => {
// this.processNotifications(account, notifications);
// })
// .catch(err => {
// this.notificationService.notifyHttpError(err, account);
// });
// promises.push(getNotificationPromise);
});
Promise.all(promises)
.then(() => {
setTimeout(() => {
this.fetchNotifications();
}, 15 * 1000);
});
// Promise.all(promises)
// .then(() => {
// setTimeout(() => {
// this.fetchNotifications();
// }, 15 * 1000);
// });
}
private processNotifications(account: AccountInfo, notifications: Notification[]) {
private processMentionsAndNotifications(account: AccountInfo, notifications: Notification[], type: NotificationTypeEnum) {
if (notifications.length === 0) {
return;
}
@ -62,12 +89,10 @@ export class UserNotificationService {
let currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
const sinceId = notifications[0].id;
this.sinceIds[account.id] = sinceId;
this.notificationsSinceIds[account.id] = sinceId;
if (currentAccountNotifications) {
currentAccountNotifications.allNotifications = [...notifications, ...currentAccountNotifications.allNotifications];
currentAccountNotifications = this.analyseNotifications(account, currentAccountNotifications);
currentAccountNotifications = this.analyseNotifications(account, currentAccountNotifications, notifications, type);
if (currentAccountNotifications.hasNewMentions || currentAccountNotifications.hasNewNotifications) {
currentNotifications = currentNotifications.filter(x => x.account.id !== account.id);
@ -77,66 +102,91 @@ export class UserNotificationService {
} else {
let newNotifications = new UserNotification();
newNotifications.account = account;
newNotifications.allNotifications = notifications;
newNotifications.mentions = [];
newNotifications.notifications = [];
newNotifications = this.analyseNotifications(account, newNotifications);
newNotifications = this.analyseNotifications(account, newNotifications, notifications, type);
currentNotifications.push(newNotifications);
this.userNotifications.next(currentNotifications);
}
}
private analyseNotifications(account: AccountInfo, userNotification: UserNotification): UserNotification {
if (userNotification.allNotifications.length > 30) {
userNotification.allNotifications.length = 30;
}
userNotification.lastId = userNotification.allNotifications[userNotification.allNotifications.length - 1].id;
private hasNewNotifications(lastNotification: Notification, lastCreationDate: string): boolean {
if(!lastNotification) return false;
if(!lastCreationDate) return false;
return new Date(lastNotification.created_at) > new Date(lastCreationDate);
}
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;
const lastMention = newMentions[0];
let lastMentionNotification: Notification;
if (lastMention) {
lastMentionNotification = userNotification.allNotifications.find(x => x.type === 'mention' && x.status.id === lastMention.id);
}
const lastNotification = newNotifications[0];
userNotification.notifications = [...newNotifications, ...currentNotifications];
userNotification.mentions = [...newMentions, ...currentMentions];
private analyseNotifications(account: AccountInfo, userNotification: UserNotification, newNotifications: Notification[], type: NotificationTypeEnum): UserNotification {
// if (userNotification.allNotifications.length > 30) {
// userNotification.allNotifications.length = 30;
// }
let lastNotificationId = newNotifications[newNotifications.length - 1].id; //FIXME: wtf? check the id retrieval
//let lastNotificationId = newNotifications[0].id; //FIXME: wtf? check the id retrieval
const accountSettings = this.toolsService.getAccountSettings(account);
if (lastMentionNotification && accountSettings.lastMentionCreationDate && new Date(lastMentionNotification.created_at) > new Date(accountSettings.lastMentionCreationDate)) {
userNotification.hasNewMentions = true;
if (type === NotificationTypeEnum.UserMention) {
userNotification.lastMentionsId = lastNotificationId;
// let status = newNotifications.map(x => x.status);
userNotification.mentions = [...newNotifications, ...userNotification.mentions];
userNotification.hasNewMentions = this.hasNewNotifications(userNotification.mentions[0], accountSettings.lastMentionCreationDate);
} else {
userNotification.hasNewMentions = false;
userNotification.lastNotificationsId = lastNotificationId;
userNotification.notifications = [...newNotifications, ...userNotification.notifications];
userNotification.hasNewNotifications = this.hasNewNotifications(userNotification.notifications[0], accountSettings.lastNotificationCreationDate);
}
if (lastNotification && accountSettings.lastNotificationCreationDate && (new Date(lastNotification.created_at)) > new Date(accountSettings.lastNotificationCreationDate)) {
userNotification.hasNewNotifications = true;
} else {
userNotification.hasNewNotifications = false;
}
// 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);
if (!accountSettings.lastMentionCreationDate && lastMentionNotification) {
accountSettings.lastMentionCreationDate = lastMentionNotification.created_at;
// const currentNotifications = userNotification.notifications;
// const currentMentions = userNotification.mentions;
// const lastMention = newMentions[0];
// let lastMentionNotification: Notification;
// if (lastMention) {
// lastMentionNotification = userNotification.allNotifications.find(x => x.type === 'mention' && x.status.id === lastMention.id);
// }
// const lastNotification = newNotifications[0];
// userNotification.notifications = [...newNotifications, ...currentNotifications];
// userNotification.mentions = [...newMentions, ...currentMentions];
if (type === NotificationTypeEnum.UserMention && !accountSettings.lastMentionCreationDate && newNotifications.length > 0) {
accountSettings.lastMentionCreationDate = newNotifications[0].created_at;
this.toolsService.saveAccountSettings(accountSettings);
}
// else if(!accountSettings.lastMentionCreationDate){
// accountSettings.lastMentionCreationDate = "2000-01-01T01:01:01.000Z";
}
if (type === NotificationTypeEnum.UserNotification && !accountSettings.lastNotificationCreationDate && newNotifications.length > 0) {
accountSettings.lastNotificationCreationDate = newNotifications[0].created_at;
this.toolsService.saveAccountSettings(accountSettings);
}
// if (lastMentionNotification && accountSettings.lastMentionCreationDate && new Date(lastMentionNotification.created_at) > new Date(accountSettings.lastMentionCreationDate)) {
// userNotification.hasNewMentions = true;
// } else {
// userNotification.hasNewMentions = false;
// }
// if (lastNotification && accountSettings.lastNotificationCreationDate && (new Date(lastNotification.created_at)) > new Date(accountSettings.lastNotificationCreationDate)) {
// userNotification.hasNewNotifications = true;
// } else {
// userNotification.hasNewNotifications = false;
// }
// if (!accountSettings.lastMentionCreationDate && lastMentionNotification) {
// accountSettings.lastMentionCreationDate = lastMentionNotification.created_at;
// this.toolsService.saveAccountSettings(accountSettings);
// }
if(!accountSettings.lastNotificationCreationDate && lastNotification){
accountSettings.lastNotificationCreationDate = lastNotification.created_at;
this.toolsService.saveAccountSettings(accountSettings);
}
// else if(!accountSettings.lastNotificationCreationDate){
// accountSettings.lastNotificationCreationDate = "2000-01-01T01:01:01.000Z";
// if (!accountSettings.lastNotificationCreationDate && lastNotification) {
// accountSettings.lastNotificationCreationDate = lastNotification.created_at;
// this.toolsService.saveAccountSettings(accountSettings);
// }
@ -147,12 +197,12 @@ export class UserNotificationService {
let currentNotifications = this.userNotifications.value;
const currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
const lastMention = currentAccountNotifications.mentions[0];
const lastMention = currentAccountNotifications.mentions[0];
if (lastMention) {
if (lastMention) {
const settings = this.toolsService.getAccountSettings(account);
const lastMentionNotification = currentAccountNotifications.allNotifications.find(x => x.type === 'mention' && x.status.id === lastMention.id);
settings.lastMentionCreationDate = lastMentionNotification.created_at;
// const lastMentionNotification = currentAccountNotifications.mentions[0];
settings.lastMentionCreationDate = lastMention.created_at;
this.toolsService.saveAccountSettings(settings);
}
@ -182,12 +232,19 @@ export class UserNotificationService {
export class UserNotification {
account: AccountInfo;
allNotifications: Notification[] = [];
//allNotifications: Notification[] = [];
hasNewNotifications: boolean;
hasNewMentions: boolean;
notifications: Notification[] = [];
mentions: Status[] = [];
lastId: string;
mentions: Notification[] = [];
lastMentionsId: string;
lastNotificationsId: string;
}
enum NotificationTypeEnum {
UserNotification,
UserMention
}