Notification settings (#807)

* Add account data hook

* Add options to change global notification

* Add options to add keywords

* Fix wrong notification count

* Show unread badge in favicon (#251)

* Fix favicon badge

* Fix bug

* Fix favicon not updating

* Add favicon svg's

* remove dead code
This commit is contained in:
Ajay Bura 2022-09-04 13:10:07 +05:30 committed by GitHub
parent ac155bbf4c
commit 584fa87bbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 587 additions and 39 deletions

View file

@ -5,6 +5,11 @@ import { selectRoom } from '../action/navigation';
import cons from './cons';
import navigation from './navigation';
import settings from './settings';
import { setFavicon } from '../../util/common';
import LogoSVG from '../../../public/res/svg/cinny.svg';
import LogoUnreadSVG from '../../../public/res/svg/cinny-unread.svg';
import LogoHighlightSVG from '../../../public/res/svg/cinny-highlight.svg';
function isNotifEvent(mEvent) {
const eType = mEvent.getType();
@ -37,17 +42,16 @@ class Notifications extends EventEmitter {
this.roomIdToNoti = new Map();
this._initNoti();
// this._initNoti();
this._listenEvents();
// Ask for permission by default after loading
window.Notification?.requestPermission();
// TODO:
window.notifications = this;
}
_initNoti() {
async _initNoti() {
this.roomIdToNoti = new Map();
const addNoti = (roomId) => {
const room = this.matrixClient.getRoom(roomId);
if (this.getNotiType(room.roomId) === cons.notifs.MUTE) return;
@ -59,6 +63,7 @@ class Notifications extends EventEmitter {
};
[...this.roomList.rooms].forEach(addNoti);
[...this.roomList.directs].forEach(addNoti);
this._updateFavicon();
}
doesRoomHaveUnread(room) {
@ -104,7 +109,8 @@ class Notifications extends EventEmitter {
}
getTotalNoti(roomId) {
const { total } = this.getNoti(roomId);
const { total, highlight } = this.getNoti(roomId);
if (highlight > total) return highlight;
return total;
}
@ -129,6 +135,24 @@ class Notifications extends EventEmitter {
}
}
async _updateFavicon() {
let unread = false;
let highlight = false;
[...this.roomIdToNoti.values()].find((noti) => {
if (!unread) {
unread = noti.total > 0 || noti.highlight > 0;
}
highlight = noti.highlight > 0;
if (unread && highlight) return true;
return false;
});
if (!unread) {
setFavicon(LogoSVG);
return;
}
setFavicon(highlight ? LogoHighlightSVG : LogoUnreadSVG);
}
_setNoti(roomId, total, highlight) {
const addNoti = (id, t, h, fromId) => {
const prevTotal = this.roomIdToNoti.get(id)?.total ?? null;
@ -155,6 +179,7 @@ class Notifications extends EventEmitter {
allParentSpaces.forEach((spaceId) => {
addNoti(spaceId, addT, addH, roomId);
});
this._updateFavicon();
}
_deleteNoti(roomId, total, highlight) {
@ -187,6 +212,7 @@ class Notifications extends EventEmitter {
allParentSpaces.forEach((spaceId) => {
removeNoti(spaceId, total, highlight, roomId);
});
this._updateFavicon();
}
async _displayPopupNoti(mEvent, room) {