1
0
mirror of https://github.com/NicolasConstant/sengi synced 2025-02-01 19:16:52 +01:00

fix notification sounds glitches

This commit is contained in:
Nicolas Constant 2019-11-13 22:03:56 -05:00
parent 93947744be
commit 423a32a888
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
2 changed files with 36 additions and 95 deletions

View File

@ -64,7 +64,7 @@ export class StreamingWrapper {
this.eventSource.onerror = x => this.webSocketGotError(x);
this.eventSource.onopen = x => { };
this.eventSource.onclose = x => this.webSocketClosed(refreshedAccount, stream, x);
});
});
}
private webSocketGotError(x: Event) {
@ -74,11 +74,11 @@ export class StreamingWrapper {
private webSocketClosed(account: AccountInfo, stream: StreamElement, x: Event) {
if (this.errorClosing) {
setTimeout(() => {
if(stream.type === StreamTypeEnum.personnal) {
if (stream.type === StreamTypeEnum.personnal) {
this.pullNewNotifications();
} else {
this.pullNewStatuses();
}
}
this.errorClosing = false;
}, 60 * 1000);
} else if (!this.disposed) {
@ -86,15 +86,20 @@ export class StreamingWrapper {
}
}
private pullNewNotifications(){
this.mastodonService.getNotifications(this.account, null, this.since_id, null, 10)
private pullNewNotifications() {
this.mastodonService.getNotifications(this.account, null, null, this.since_id, 10)
.then((notifications: Notification[]) => {
//notifications = notifications.sort((a, b) => a.id.localeCompare(b.id));
let soundMuted = !this.since_id;
notifications = notifications.reverse();
for (const n of notifications) {
const update = new StatusUpdate();
update.notification = n;
update.type = EventEnum.notification;
update.muteSound = soundMuted;
this.since_id = n.id;
this.statusUpdateSubjet.next(update);
}
@ -148,8 +153,8 @@ export class StreamingWrapper {
break;
case 'notification':
newUpdate.type = EventEnum.notification;
newUpdate.notification = <Notification>JSON.parse(event.payload);
break;
newUpdate.notification = <Notification>JSON.parse(event.payload);
break;
default:
newUpdate.type = EventEnum.unknow;
}
@ -194,11 +199,11 @@ export class EventSourceStreaminWrapper {
constructor(
private readonly account: AccountInfo,
private readonly stream: StreamElement
){
) {
this.start();
}
private start(){
private start() {
const route = this.getRoute();
this.eventSource = new EventSource(route);
this.eventSource.addEventListener('update', u => {
@ -211,20 +216,20 @@ export class EventSourceStreaminWrapper {
});
this.eventSource.onmessage = x => {
console.log(x);
if(x.data !== ''){
if (x.data !== '') {
this.onMessage(JSON.parse(x.data));
}
};
this.eventSource.onerror = x => {
this.onError(x);
};
};
console.warn('this.eventSource.CONNECTING');
console.warn(this.eventSource.CONNECTING);
console.warn('this.eventSource.OPEN');
console.warn(this.eventSource.OPEN);
}
}
private onMessage(data) {
console.warn('onMessage');
@ -275,6 +280,7 @@ export class StatusUpdate {
messageId: string;
account: AccountInfo;
notification: Notification;
muteSound: boolean;
}
export enum EventEnum {

View File

@ -19,8 +19,8 @@ import { StreamElement, StreamTypeEnum } from '../states/streams.state';
export class UserNotificationService {
userNotifications = new BehaviorSubject<UserNotification[]>([]);
private mentionsSinceIds: { [id: string]: string } = {};
private notificationsSinceIds: { [id: string]: string } = {};
// private mentionsSinceIds: { [id: string]: string } = {};
// private notificationsSinceIds: { [id: string]: string } = {};
private sound: Howl;
@ -39,9 +39,9 @@ export class UserNotificationService {
private fetchNotifications() {
this.sound = new Howl({
// src: ['assets/audio/exquisite.mp3']
//src: ['assets/audio/all-eyes-on-me.mp3']
src: ['assets/audio/all-eyes-on-me.mp3']
//src: ['assets/audio/appointed.mp3']
src: ['assets/audio/boop.mp3']
//src: ['assets/audio/boop.mp3']
});
@ -62,7 +62,7 @@ export class UserNotificationService {
})
.catch(err => {
this.notificationService.notifyHttpError(err, account);
});;
});
let getNotificationPromise = this.mastodonService.getNotifications(account, ['mention'], null, null, 10)
.then((notifications: Notification[]) => {
@ -70,17 +70,13 @@ export class UserNotificationService {
})
.catch(err => {
this.notificationService.notifyHttpError(err, account);
});;
});
Promise.all([getMentionsPromise, getNotificationPromise])
.then(() => {
let streamElement = new StreamElement(StreamTypeEnum.personnal, 'activity', account.id, null, null, null, account.instance);
let mentionId = parseInt(this.mentionsSinceIds[account.id]);
let notificationId = parseInt(this.mentionsSinceIds[account.id]);
let lastId = Math.max(mentionId, notificationId).toString();
let streaming = this.streamingService.getStreaming(account, streamElement, lastId);
let streaming = this.streamingService.getStreaming(account, streamElement);
streaming.statusUpdateSubjet.subscribe((notification: StatusUpdate) => {
if (notification && notification.type === EventEnum.notification) {
this.processNewUpdate(account, notification);
@ -88,45 +84,28 @@ export class UserNotificationService {
});
})
.catch(err => { });
// 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);
// });
}
private soundJustPlayed = false;
private playSoundNotification() {
if(this.soundJustPlayed) return;
this.soundJustPlayed = true;
console.warn('play audio');
this.sound.play();
setTimeout(() => {
this.soundJustPlayed = false;
}, 2000);
}
private processNewUpdate(account: AccountInfo, notification: StatusUpdate) {
if(!notification && !notification.notification) return;
this.playSoundNotification();
console.warn(account);
console.warn(notification);
if(!notification.muteSound){
this.playSoundNotification();
}
if (notification.notification.type === 'mention') {
this.processMentionsAndNotifications(account, [notification.notification], NotificationTypeEnum.UserMention);
@ -135,17 +114,13 @@ export class UserNotificationService {
}
}
private processMentionsAndNotifications(account: AccountInfo, notifications: Notification[], type: NotificationTypeEnum) {
if (notifications.length === 0) {
return;
}
let currentNotifications = this.userNotifications.value;
let currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
const sinceId = notifications[0].id;
this.notificationsSinceIds[account.id] = sinceId;
let currentAccountNotifications = currentNotifications.find(x => x.account.id === account.id);
if (currentAccountNotifications) {
currentAccountNotifications = this.analyseNotifications(account, currentAccountNotifications, notifications, type);
@ -191,24 +166,7 @@ export class UserNotificationService {
userNotification.hasNewNotifications = this.hasNewNotifications(userNotification.notifications[0], accountSettings.lastNotificationCreationDate);
}
// 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;
// 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];
// Set settings if needed
if (type === NotificationTypeEnum.UserMention && !accountSettings.lastMentionCreationDate && newNotifications.length > 0) {
accountSettings.lastMentionCreationDate = newNotifications[0].created_at;
this.toolsService.saveAccountSettings(accountSettings);
@ -219,29 +177,6 @@ export class UserNotificationService {
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);
// }
return userNotification;
}