forked from galaxyproject/galaxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotificationsStore.ts
More file actions
112 lines (93 loc) · 4.01 KB
/
notificationsStore.ts
File metadata and controls
112 lines (93 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { defineStore } from "pinia";
import { computed, ref } from "vue";
import { GalaxyApi } from "@/api";
import type { NotificationChanges, UserNotification, UserNotificationsBatchUpdateRequest } from "@/api/notifications";
import { useResourceWatcher } from "@/composables/resourceWatcher";
import { rethrowSimple } from "@/utils/simple-error";
import { mergeObjectListsById } from "@/utils/utils";
import { useBroadcastsStore } from "./broadcastsStore";
const ACTIVE_POLLING_INTERVAL = 30000; // 30 seconds
const INACTIVE_POLLING_INTERVAL = ACTIVE_POLLING_INTERVAL * 20; // 10 minutes
export const useNotificationsStore = defineStore("notificationsStore", () => {
const { startWatchingResource: startWatchingNotifications, stopWatchingResource: stopWatchingNotifications } =
useResourceWatcher(getNotificationStatus, {
shortPollingInterval: ACTIVE_POLLING_INTERVAL,
longPollingInterval: INACTIVE_POLLING_INTERVAL,
});
const broadcastsStore = useBroadcastsStore();
const totalUnreadCount = ref<number>(0);
const notifications = ref<UserNotification[]>([]);
const loadingNotifications = ref<boolean>(false);
const lastNotificationUpdate = ref<Date | null>(null);
const unreadNotifications = computed(() => notifications.value.filter((n) => !n.seen_time));
async function loadNotifications() {
const { data, error } = await GalaxyApi().GET("/api/notifications");
if (error) {
rethrowSimple(error);
}
const useNotifications = data as UserNotification[]; // We are sure this cannot be a broadcast
notifications.value = mergeObjectListsById(useNotifications, [], "create_time", "desc");
}
async function getNotificationStatus() {
try {
if (!lastNotificationUpdate.value) {
loadingNotifications.value = true;
await broadcastsStore.loadBroadcasts();
await loadNotifications();
updateUnreadCount();
} else {
const { data, error } = await GalaxyApi().GET("/api/notifications/status", {
params: {
query: {
since: lastNotificationUpdate.value.toISOString().replace("Z", ""),
},
},
});
if (error) {
rethrowSimple(error);
}
totalUnreadCount.value = data.total_unread_count;
notifications.value = mergeObjectListsById(
notifications.value,
data.notifications as UserNotification[],
"create_time",
"desc",
);
broadcastsStore.updateBroadcasts(data.broadcasts);
}
lastNotificationUpdate.value = new Date();
} catch (e) {
console.error(e);
} finally {
loadingNotifications.value = false;
}
}
async function updateBatchNotification(request: UserNotificationsBatchUpdateRequest) {
const { error } = await GalaxyApi().PUT("/api/notifications", {
body: request,
});
if (error) {
rethrowSimple(error);
}
if (request.changes.deleted) {
notifications.value = notifications.value.filter((n) => !request.notification_ids.includes(n.id));
}
startWatchingNotifications();
}
async function updateNotification(notification: UserNotification, changes: NotificationChanges) {
return updateBatchNotification({ notification_ids: [notification.id], changes });
}
function updateUnreadCount() {
totalUnreadCount.value = notifications.value.filter((n) => !n.seen_time).length;
}
return {
notifications,
totalUnreadCount,
unreadNotifications,
loadingNotifications,
updateNotification,
updateBatchNotification,
startWatchingNotifications,
stopWatchingNotifications,
};
});