From 753ca95bf1911c2e4be00af28c0b07d7a727c906 Mon Sep 17 00:00:00 2001 From: phoebus-84 Date: Fri, 3 May 2024 15:46:54 +0200 Subject: [PATCH 1/4] feat: end of verification flow --- src/lib/preferences/verifiedSid.ts | 34 +++++++++++++++++++ src/routes/+layout.svelte | 4 +-- .../(protected)/[id]/verify/+page.svelte | 17 +++------- .../(protected)/[id]/verify/_lib/tools.ts | 25 +++++++++++--- .../[[lang]]/(protected)/[sid]/+layout.ts | 12 +++++++ .../(protected)/[sid]/verified/+page.svelte | 26 ++++++++++++++ 6 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 src/lib/preferences/verifiedSid.ts create mode 100644 src/routes/[[lang]]/(protected)/[sid]/+layout.ts create mode 100644 src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte diff --git a/src/lib/preferences/verifiedSid.ts b/src/lib/preferences/verifiedSid.ts new file mode 100644 index 0000000..82256c7 --- /dev/null +++ b/src/lib/preferences/verifiedSid.ts @@ -0,0 +1,34 @@ +import { getStructuredPreferences, setStructuredPreferences } from '.'; +import dayjs from 'dayjs'; + +export type VerifiedSid = { + sid: string; + success: boolean; + at: number; + message?: string; +}; + +export const VERIFIED_SID_KEY = 'verifiedSid'; + +export const saveVerifiedSid = async (sid: string, success: boolean, message?: string) => { + const at = dayjs().unix(); + const verifiedSid = await getVerifiedSids(); + + const r = { sid, success, at, message }; + if (verifiedSid) { + const newVerifiedSids = [...verifiedSid, r]; + await setStructuredPreferences(VERIFIED_SID_KEY, newVerifiedSids); + return r; + } + await setStructuredPreferences(VERIFIED_SID_KEY, [r]); + return r; +}; + +export const getVerifiedSids = async () => { + return (await getStructuredPreferences(VERIFIED_SID_KEY)) as VerifiedSid[]; +}; + +export const getVerifiedSid = async (sid: string) => { + const verifiedSids = await getVerifiedSids(); + return verifiedSids.find((r) => r.sid === sid); +}; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index f26dc24..0abd885 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -30,11 +30,11 @@ /> diff --git a/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte b/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte index 0236a4d..7058be4 100644 --- a/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte +++ b/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte @@ -3,7 +3,6 @@ import { Slangroom } from '@slangroom/core'; import { qrcode } from '@slangroom/qrcode'; import { helpers } from '@slangroom/helpers'; - import { thumbsDownOutline, thumbsUpOutline } from 'ionicons/icons'; import Header from '$lib/components/molecules/Header.svelte'; import { m } from '$lib/i18n'; import dayjs from 'dayjs'; @@ -14,8 +13,9 @@ import cardToQrKeys from '$lib/mobile_zencode/verifier/card_to_qr.keys.json?raw'; import { backendUri } from '$lib/backendUri'; import { saveRuAndSid } from '$lib/preferences/sidRu'; - import { jwsToId, jwsToIdSuccess } from './_lib/tools'; + // import { jwsToId, jwsToIdSuccess } from './_lib/tools'; import { log } from '$lib/log'; + import { onIncomingNotification } from './_lib/tools'; export let data: any; @@ -28,7 +28,6 @@ let error: string; let tok: string; - //@ts-expect-error qrcode should be of type plugins const slangroom = new Slangroom(qrcode, helpers); let incomingNotification: any; @@ -42,9 +41,10 @@ error = err.error; }); - await PushNotifications.addListener('pushNotificationReceived', (notification) => { + await PushNotifications.addListener('pushNotificationReceived', async (notification) => { incomingNotification = notification; log(`Push notification received: /n ${JSON.stringify(notification)}`); + await onIncomingNotification(notification); }); }; @@ -126,15 +126,6 @@ > {/await} - {:else if incomingNotification} - {#await jwsToId(incomingNotification.data.message) then res} - {res.message} - - SESSION ID: {res.id} - {/await} {:else if qr}
=> { @@ -35,10 +39,23 @@ export const jwsToId = async (jws: string): Promise => { }; const res = await slangroom.execute(verify, { data: dataVerify, keys: JSON.parse(verifyKeys) }); log(JSON.stringify(res)); - const message = res.result.result as typeof jwsToIdSuccess | typeof jwsToIdFailure; - return { message, id }; + const result = res.result.result as jwsToIdResult; + return { result, id }; } catch (e) { log(JSON.stringify(e)); - return {message:jwsToIdFailure, id} + return { result: jwsToIdFailure, id, message: JSON.stringify(e) }; } }; + +export type Notification = { + id: string; + data: { + message: string; + }; +}; + +export const onIncomingNotification = async (notification: Notification) => { + const { id, result, message } = await jwsToId(notification.data.message); + await saveVerifiedSid(id, result === jwsToIdSuccess, message); + await goto(`/${id}/verified`); +}; diff --git a/src/routes/[[lang]]/(protected)/[sid]/+layout.ts b/src/routes/[[lang]]/(protected)/[sid]/+layout.ts new file mode 100644 index 0000000..4ed36d3 --- /dev/null +++ b/src/routes/[[lang]]/(protected)/[sid]/+layout.ts @@ -0,0 +1,12 @@ +import { getVerifiedSid } from '$lib/preferences/verifiedSid.js'; +import { error } from '@sveltejs/kit'; + +export const load = async ({ params }) => { + const verifiedSid = await getVerifiedSid(params.sid); + if (!verifiedSid) { + error(404, { + message: 'Not found' + }); + } + return { verifiedSid }; +}; diff --git a/src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte b/src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte new file mode 100644 index 0000000..d1aadf8 --- /dev/null +++ b/src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte @@ -0,0 +1,26 @@ + + +
Verification
+ + +
+ +
+
From 46efcb485552e8e0f09103858ad5493d3276f14d Mon Sep 17 00:00:00 2001 From: phoebus-84 Date: Fri, 3 May 2024 15:53:27 +0200 Subject: [PATCH 2/4] fix: feedback date --- src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte b/src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte index d1aadf8..8e2fe93 100644 --- a/src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte +++ b/src/routes/[[lang]]/(protected)/[sid]/verified/+page.svelte @@ -21,6 +21,6 @@
- +
From 375af85a4391a26963bc5681342c670abc2a343f Mon Sep 17 00:00:00 2001 From: phoebus-84 Date: Fri, 3 May 2024 16:12:49 +0200 Subject: [PATCH 3/4] feat: dumb notifications page --- .../(tabs)/notifications/+page.svelte | 23 ++++++++++++++++++- .../(protected)/(tabs)/notifications/+page.ts | 6 +++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.ts diff --git a/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.svelte b/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.svelte index 2a28f1e..a1e6ef0 100644 --- a/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.svelte +++ b/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.svelte @@ -1,5 +1,26 @@ -be patient + +
+ {#each verifiedSids as verifiedSid} +
+
+ {verifiedSid.sid} + {verifiedSid.success ? 'verified' : 'failure'} +
+
+ {dayjs.unix(verifiedSid.at).toString()} +
+
+
+ {/each} +
+ diff --git a/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.ts b/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.ts new file mode 100644 index 0000000..65a91f8 --- /dev/null +++ b/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.ts @@ -0,0 +1,6 @@ +import { getVerifiedSids } from '$lib/preferences/verifiedSid.js'; + +export const load = async () => { + const verifiedSids = await getVerifiedSids(); + return { verifiedSids }; +}; From 59c9f39dcaf50ea925c3d049ffa328bcf380cc53 Mon Sep 17 00:00:00 2001 From: phoebus-84 Date: Fri, 3 May 2024 19:34:59 +0200 Subject: [PATCH 4/4] fix: notifications to history --- src/lib/components/tabs/TabPage.svelte | 3 +-- src/lib/components/tabs/index.ts | 2 +- src/routes/[[lang]]/(protected)/(tabs)/+layout.svelte | 2 +- .../(tabs)/{notifications => history}/+page.svelte | 8 +++++--- .../(tabs)/{notifications => history}/+page.ts | 0 5 files changed, 8 insertions(+), 7 deletions(-) rename src/routes/[[lang]]/(protected)/(tabs)/{notifications => history}/+page.svelte (74%) rename src/routes/[[lang]]/(protected)/(tabs)/{notifications => history}/+page.ts (100%) diff --git a/src/lib/components/tabs/TabPage.svelte b/src/lib/components/tabs/TabPage.svelte index 879ebe7..2c10a4a 100644 --- a/src/lib/components/tabs/TabPage.svelte +++ b/src/lib/components/tabs/TabPage.svelte @@ -4,11 +4,10 @@ export let tab: Tab; export let title: string; - export let settings = false -
{title}
+
{title}
diff --git a/src/lib/components/tabs/index.ts b/src/lib/components/tabs/index.ts index 60cff81..6c90870 100644 --- a/src/lib/components/tabs/index.ts +++ b/src/lib/components/tabs/index.ts @@ -1,6 +1,6 @@ export const Tabs = { home: 'home', - notifications: 'notifications', + history: 'history', profile: 'profile' } as const; diff --git a/src/routes/[[lang]]/(protected)/(tabs)/+layout.svelte b/src/routes/[[lang]]/(protected)/(tabs)/+layout.svelte index ed8f551..ab92820 100644 --- a/src/routes/[[lang]]/(protected)/(tabs)/+layout.svelte +++ b/src/routes/[[lang]]/(protected)/(tabs)/+layout.svelte @@ -11,7 +11,7 @@ const tabs: IonTabProps[] = [ { label: 'Home', icon: home, tab: Tabs.home }, - { label: 'Notifications', icon: notificationsOutline, tab: Tabs.notifications }, + { label: 'History', icon: notificationsOutline, tab: Tabs.history }, { label: 'Profile', icon: personOutline, tab: Tabs.profile } ]; diff --git a/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.svelte b/src/routes/[[lang]]/(protected)/(tabs)/history/+page.svelte similarity index 74% rename from src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.svelte rename to src/routes/[[lang]]/(protected)/(tabs)/history/+page.svelte index a1e6ef0..d40813b 100644 --- a/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.svelte +++ b/src/routes/[[lang]]/(protected)/(tabs)/history/+page.svelte @@ -1,14 +1,16 @@ - +
- {#each verifiedSids as verifiedSid} + {#each verifiedSids.reverse() as verifiedSid}
{verifiedSid.sid} @@ -17,7 +19,7 @@
- {dayjs.unix(verifiedSid.at).toString()} + {dayjs().to(dayjs.unix(verifiedSid.at))}
diff --git a/src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.ts b/src/routes/[[lang]]/(protected)/(tabs)/history/+page.ts similarity index 100% rename from src/routes/[[lang]]/(protected)/(tabs)/notifications/+page.ts rename to src/routes/[[lang]]/(protected)/(tabs)/history/+page.ts