From 05eb457e114b441fe86fc1b2ae13f0ea147bfc01 Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Tue, 10 Sep 2024 18:57:37 +0800 Subject: [PATCH] chore: improve message app --- .../components/messages/ChannelDetail.svelte | 23 ++++++-- .../messages/ChannelMessages.svelte | 29 +++++----- .../components/messages/ChannelSetting.svelte | 7 +-- .../src/lib/components/messages/Chat.svelte | 4 +- .../src/lib/components/messages/Home.svelte | 10 +--- .../src/lib/components/messages/Index.svelte | 7 ++- .../components/messages/PasswordModel.svelte | 6 +-- .../components/messages/ProfileDetail.svelte | 17 +++--- .../messages/UserRegisterModel.svelte | 22 ++++++-- .../messages/UserSelectModel.svelte | 21 ++++++-- src/ic_panda_frontend/src/lib/constants.ts | 1 + .../src/lib/stores/message.ts | 54 +++++++++++++------ src/ic_panda_frontend/src/lib/utils/token.ts | 11 ++-- 13 files changed, 140 insertions(+), 72 deletions(-) diff --git a/src/ic_panda_frontend/src/lib/components/messages/ChannelDetail.svelte b/src/ic_panda_frontend/src/lib/components/messages/ChannelDetail.svelte index edd6fb0..e77d315 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/ChannelDetail.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/ChannelDetail.svelte @@ -11,6 +11,7 @@ type ChannelInfoEx, type MyMessageState } from '$src/lib/stores/message' + import { sleep } from '$src/lib/utils/helper' import { Avatar, getToastStore } from '@skeletonlabs/skeleton' import { onMount } from 'svelte' import { type Readable } from 'svelte/store' @@ -20,20 +21,31 @@ export let channelId: string const toastStore = getToastStore() + const { canister, id } = ChannelAPI.parseChannelParam(channelId) let myState: MyMessageState let myInfo: Readable let channelInfo: Readable let openSettings = false - function onCreateChannelHandler() { + let switching = false + async function onClickChannelSetting() { + if (!canister) return + switching = true + await Promise.all([ + async () => { + // load latest channel info + channelInfo = await myState.loadChannelInfo(canister, id) + }, + sleep(200) + ]) openSettings = !openSettings + switching = false } onMount(() => { const { abort } = toastRun(async (signal: AbortSignal) => { - const { canister, id } = ChannelAPI.parseChannelParam(channelId) - if (canister && !signal.aborted) { + if (canister) { myState = await myMessageStateAsync() myInfo = myState.info as Readable channelInfo = await myState.loadChannelInfo(canister, id) @@ -78,7 +90,8 @@ type="button" class="btn-icon" title="Channel settings" - on:click={onCreateChannelHandler} + disabled={switching} + on:click={onClickChannelSetting} > {#if openSettings} @@ -92,7 +105,7 @@ {#if openSettings} {:else} - + {/if} {:else}
diff --git a/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte b/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte index 6465290..73e795b 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte @@ -27,10 +27,10 @@ export let myState: MyMessageState export let myInfo: Readable - export let channelInfo: Readable + export let channelInfo: ChannelInfoEx const toastStore = getToastStore() - const { canister, id } = $channelInfo + const { canister, id } = channelInfo let messageFeed: Writable = writable([]) let latestMessage: Readable @@ -117,29 +117,29 @@ const debouncedUpdateMyLastRead = debounce(async () => { await myState.updateMyLastRead(canister, id, latestMessageId) - }, 10000) + }, 6000) async function loadMessages(canister: Principal, id: number) { channelAPI = await myState.api.channelAPI(canister) - dek = await myState.decryptChannelDEK($channelInfo) - latestMessage = await myState.loadLatestMessageStream( - canister, - id, - dek, - $channelInfo.latest_message_id - ) - + dek = await myState.decryptChannelDEK(channelInfo) const prevMessages = await myState.loadPrevMessages( canister, id, dek, - $channelInfo.latest_message_id + channelInfo.latest_message_id + 1 ) if (prevMessages.length > 0) { messageFeed.update((prev) => [...prevMessages, ...prev]) } + latestMessage = await myState.loadLatestMessageStream( + canister, + id, + dek, + channelInfo.latest_message_id + 1 + ) + await tick() debouncedUpdateMyLastRead() debouncedUpdateMyLastRead.trigger() @@ -149,6 +149,9 @@ onMount(() => { const { abort } = toastRun(async (signal: AbortSignal) => { if (!signal.aborted) { + if (!channelInfo._kek) { + channelInfo = await myState.refreshMyChannel(channelInfo) + } await loadMessages(canister, id) } else { goto('/_/messages') @@ -174,7 +177,7 @@
-
+
{#each $messageFeed as msg (msg.id)} {#if msg.created_by.compareTo(myState.principal) !== 'eq'}
{ + // fetch the latest ECDH request + channelInfo = await myState.refreshMyChannel(channelInfo) await myState.adminExchangeKEK(channelInfo) channelInfo = await myState.refreshMyChannel(channelInfo) await loadMembers() @@ -209,9 +211,8 @@ onMount(() => { const { abort } = toastRun(async (signal: AbortSignal) => { - if (!signal.aborted) { - await loadMembers() - } + channelInfo = await myState.refreshMyChannel(channelInfo) + await loadMembers() }, toastStore) return abort diff --git a/src/ic_panda_frontend/src/lib/components/messages/Chat.svelte b/src/ic_panda_frontend/src/lib/components/messages/Chat.svelte index e8592b8..43f134d 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/Chat.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/Chat.svelte @@ -2,6 +2,7 @@ import { goto } from '$app/navigation' import { page } from '$app/stores' import { type UserInfo } from '$lib/canisters/message' + import { MyMessageState } from '$src/lib/stores/message' import { Avatar } from '@skeletonlabs/skeleton' import { type Readable } from 'svelte/store' @@ -9,6 +10,7 @@ import MyChannelList from './MyChannelList.svelte' import ProfileDetail from './ProfileDetail.svelte' + export let myState: MyMessageState // force reactivity when 'myState' updated export let myInfo: Readable function onMeHandler() { @@ -60,7 +62,7 @@
- +
{/if}
diff --git a/src/ic_panda_frontend/src/lib/components/messages/Home.svelte b/src/ic_panda_frontend/src/lib/components/messages/Home.svelte index 286b117..c40fe70 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/Home.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/Home.svelte @@ -7,7 +7,6 @@ type MyMessageState } from '$src/lib/stores/message' import { getModalStore, getToastStore } from '@skeletonlabs/skeleton' - import { getContext } from 'svelte' import { type Readable } from 'svelte/store' import UserRegisterModel from './UserRegisterModel.svelte' @@ -16,7 +15,6 @@ const toastStore = getToastStore() const modalStore = getModalStore() - const initState = getContext('InitState') function getStartedHandler() { toastRun(async () => { @@ -29,9 +27,7 @@ type: 'component', component: { ref: UserRegisterModel, - props: { - initState: initState || null - } + props: {} } }) } @@ -40,9 +36,7 @@ type: 'component', component: { ref: UserRegisterModel, - props: { - initState: initState || null - } + props: {} } }) } diff --git a/src/ic_panda_frontend/src/lib/components/messages/Index.svelte b/src/ic_panda_frontend/src/lib/components/messages/Index.svelte index f764208..c6d65af 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/Index.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/Index.svelte @@ -7,7 +7,7 @@ type MyMessageState } from '$src/lib/stores/message' import { getModalStore, getToastStore } from '@skeletonlabs/skeleton' - import { onMount, setContext } from 'svelte' + import { onMount } from 'svelte' import { type Readable } from 'svelte/store' import Chat from './Chat.svelte' import Home from './Home.svelte' @@ -22,6 +22,7 @@ async function initState() { myState = await myMessageStateAsync() + await myState.refreshState() myInfo = myState.info myInfo_ = myInfo as Readable if ($myInfo) { @@ -41,8 +42,6 @@ } } - setContext('InitState', initState) - onMount(() => { const { abort } = toastRun(initState, toastStore) return abort @@ -60,7 +59,7 @@ {#if $myInfo} - + {:else} {/if} diff --git a/src/ic_panda_frontend/src/lib/components/messages/PasswordModel.svelte b/src/ic_panda_frontend/src/lib/components/messages/PasswordModel.svelte index c6c864d..7d08a3f 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/PasswordModel.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/PasswordModel.svelte @@ -15,7 +15,7 @@ const toastStore = getToastStore() const keyId = encodeCBOR('ICPanda_Messages_Master_Key') - const PasswordExpire = 7 * 24 * 60 * 60 * 1000 + const PasswordExpire = 14 * 24 * 60 * 60 * 1000 let validating = false let submitting = false @@ -51,7 +51,7 @@ ) if (masterKey.kind !== kind) { - processingTip = 'Migrate keys...' + processingTip = 'Migrate encrypted keys to ICP chain ...' await myState.migrateKeys() } else { await myState.initStaticECDHKey() @@ -164,7 +164,7 @@ {/if}