|
1 | | -import { |
2 | | - type NotificationClickForOpenHandlingSchema, |
3 | | - notificationClickFromDatabase, |
4 | | -} from '../helpers/serializer'; |
5 | | -import { |
6 | | - AppState, |
7 | | - type PendingNotificationClickEvents, |
8 | | -} from '../models/AppState'; |
9 | | -import { db } from './client'; |
| 1 | +import { AppState } from '../models/AppState'; |
| 2 | +import { db, getIdsValue, getOptionsValue } from './client'; |
10 | 3 |
|
11 | 4 | export const getDBAppConfig = async () => { |
12 | 5 | const config: any = {}; |
13 | | - const appIdStr: string = (await db.get('Ids', 'appId'))?.id as string; |
| 6 | + const appIdStr = await getIdsValue<string>('appId'); |
14 | 7 | config.appId = appIdStr; |
15 | | - config.vapidPublicKey = (await db.get('Options', 'vapidPublicKey'))?.value; |
| 8 | + config.vapidPublicKey = await getOptionsValue<string>('vapidPublicKey'); |
16 | 9 | return config; |
17 | 10 | }; |
18 | 11 |
|
19 | | -const getAllPendingNotificationClickEvents = |
20 | | - async (): Promise<PendingNotificationClickEvents> => { |
21 | | - const clickedNotifications: PendingNotificationClickEvents = {}; |
22 | | - const eventsFromDb = await db.getAll('NotificationOpened'); |
23 | | - for (const eventFromDb of eventsFromDb) { |
24 | | - const event = notificationClickFromDatabase(eventFromDb); |
25 | | - const url = event.result.url; |
26 | | - if (!url) { |
27 | | - continue; |
28 | | - } |
29 | | - clickedNotifications[url] = event; |
30 | | - } |
31 | | - return clickedNotifications; |
32 | | - }; |
33 | | - |
34 | 12 | export const getAppState = async (): Promise<AppState> => { |
35 | 13 | const state = new AppState(); |
36 | | - state.defaultNotificationUrl = (await db.get('Options', 'defaultUrl')) |
37 | | - ?.value as string; |
38 | | - state.defaultNotificationTitle = (await db.get('Options', 'defaultTitle')) |
39 | | - ?.value as string; |
40 | | - state.lastKnownPushEnabled = (await db.get('Options', 'isPushEnabled')) |
41 | | - ?.value as boolean; |
42 | | - state.pendingNotificationClickEvents = |
43 | | - await getAllPendingNotificationClickEvents(); |
| 14 | + state.defaultNotificationUrl = await getOptionsValue<string>('defaultUrl'); |
| 15 | + state.defaultNotificationTitle = |
| 16 | + await getOptionsValue<string>('defaultTitle'); |
| 17 | + state.lastKnownPushEnabled = await getOptionsValue<boolean>('isPushEnabled'); |
44 | 18 |
|
45 | 19 | // lastKnown<PushId|PushToken|OptedIn> are used to track changes to the user's subscription |
46 | 20 | // state. Displayed in the `current` & `previous` fields of the `subscriptionChange` event. |
47 | | - state.lastKnownPushId = (await db.get('Options', 'lastPushId')) |
48 | | - ?.value as string; |
49 | | - state.lastKnownPushToken = (await db.get('Options', 'lastPushToken')) |
50 | | - ?.value as string; |
51 | | - state.lastKnownOptedIn = (await db.get('Options', 'lastOptedIn')) |
52 | | - ?.value as boolean; |
| 21 | + state.lastKnownPushId = await getOptionsValue<string>('lastPushId'); |
| 22 | + state.lastKnownPushToken = await getOptionsValue<string>('lastPushToken'); |
| 23 | + state.lastKnownOptedIn = await getOptionsValue<boolean>('lastOptedIn'); |
53 | 24 | return state; |
54 | 25 | }; |
55 | 26 |
|
@@ -91,35 +62,8 @@ export const setAppState = async (appState: AppState) => { |
91 | 62 | key: 'lastOptedIn', |
92 | 63 | value: appState.lastKnownOptedIn, |
93 | 64 | }); |
94 | | - |
95 | | - if (appState.pendingNotificationClickEvents) { |
96 | | - const clickedNotificationUrls = Object.keys( |
97 | | - appState.pendingNotificationClickEvents, |
98 | | - ); |
99 | | - for (const url of clickedNotificationUrls) { |
100 | | - const notificationDetails = |
101 | | - appState.pendingNotificationClickEvents[ |
102 | | - url as keyof typeof appState.pendingNotificationClickEvents |
103 | | - ]; |
104 | | - |
105 | | - if (notificationDetails) { |
106 | | - await db.put('NotificationOpened', { |
107 | | - url: url, |
108 | | - data: (notificationDetails as any).data, |
109 | | - timestamp: (notificationDetails as any).timestamp, |
110 | | - } as NotificationClickForOpenHandlingSchema); |
111 | | - } else if (notificationDetails === null) { |
112 | | - // If we get an object like: |
113 | | - // { "http://site.com/page": null} |
114 | | - // It means we need to remove that entry |
115 | | - await db.delete('NotificationOpened', url); |
116 | | - } |
117 | | - } |
118 | | - } |
119 | 65 | }; |
120 | 66 |
|
121 | 67 | export const getConsentGiven = async () => { |
122 | | - return (await db.get('Options', 'consentGiven'))?.value as |
123 | | - | boolean |
124 | | - | undefined; |
| 68 | + return await getOptionsValue<boolean>('consentGiven'); |
125 | 69 | }; |
0 commit comments