39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
let intervalId = null;
|
|
|
|
exports.runQuery = function (roomId, disable) {
|
|
|
|
if (intervalId) {
|
|
clearInterval(intervalId);
|
|
intervalId = null;
|
|
}
|
|
|
|
if (disable) return;
|
|
|
|
intervalId = setInterval(() => {
|
|
axios({
|
|
method: 'GET',
|
|
url: `https://${fediverse.auth[event.getSender()].domain}/api/v1/notifications`,
|
|
headers: { Authorization: `Bearer ${fediverse.auth[event.getSender()].access_token}` },
|
|
})
|
|
.then((res) => {
|
|
let notifications = JSON.parse(localStorage.getItem('notifications'));
|
|
let past = notifications[event.getSender()] || {}
|
|
if (past.length === 0) past = res.data;
|
|
const events = res.data;
|
|
const len = events.length;
|
|
for (let i = len - 1; i >= 0; i--) {
|
|
if (past.findIndex((x) => x.created_at === events[i].created_at) === -1) {
|
|
const lastStored = past.slice(past.length - 1, past.length);
|
|
if (events[i].created_at < lastStored[0].created_at) return;
|
|
events[i].label = 'notifications';
|
|
fediverse.utils.formatter(events[i], roomId);
|
|
}
|
|
}
|
|
notifications[event.getSender()] = events
|
|
localStorage.setItem('notifications', JSON.stringify(notifications, null, 2));
|
|
})
|
|
.catch((e) => {
|
|
matrix.utils.sendError(null, roomId, e);
|
|
});
|
|
}, 30000);
|
|
};
|