From a86d84de33b9d11bb218525d81bed51feb43b6bd Mon Sep 17 00:00:00 2001 From: balzack Date: Wed, 1 Mar 2023 00:42:57 -0800 Subject: [PATCH] refactoring details screen --- app/mobile/src/context/sealUtil.js | 1 - .../src/context/useChannelContext.hook.js | 2 +- .../context/useConversationContext.hook.js | 2 +- app/mobile/src/session/Session.jsx | 3 +- .../src/session/channels/useChannels.hook.js | 26 +++++---- app/mobile/src/session/details/Details.jsx | 4 +- .../src/session/details/useDetails.hook.js | 56 +++++++++++++------ 7 files changed, 59 insertions(+), 35 deletions(-) diff --git a/app/mobile/src/context/sealUtil.js b/app/mobile/src/context/sealUtil.js index 1af3fe0f..47027d8b 100644 --- a/app/mobile/src/context/sealUtil.js +++ b/app/mobile/src/context/sealUtil.js @@ -54,7 +54,6 @@ export function updateChannelSubject(subject, contentKey) { const encrypted = CryptoJS.AES.encrypt(JSON.stringify({ subject }), key, { iv: iv }); const subjectEncrypted = encrypted.ciphertext.toString(CryptoJS.enc.Base64) const subjectIv = iv.toString(); - return { subjectEncrypted, subjectIv }; } diff --git a/app/mobile/src/context/useChannelContext.hook.js b/app/mobile/src/context/useChannelContext.hook.js index c8d15361..e2da65bf 100644 --- a/app/mobile/src/context/useChannelContext.hook.js +++ b/app/mobile/src/context/useChannelContext.hook.js @@ -147,7 +147,7 @@ export function useChannelContext() { }, removeChannel: async (channelId) => { const { server, token } = access.current; - return await removeChannel(access.current, channelId); + return await removeChannel(server, token, channelId); }, setChannelSubject: async (channelId, type, subject) => { const { server, token } = access.current; diff --git a/app/mobile/src/context/useConversationContext.hook.js b/app/mobile/src/context/useConversationContext.hook.js index 607cf9ca..ec7ac727 100644 --- a/app/mobile/src/context/useConversationContext.hook.js +++ b/app/mobile/src/context/useConversationContext.hook.js @@ -165,7 +165,7 @@ export function useConversationContext() { throw new Error("can only set hosted channel subjects"); } else if(channelId) { - await channel.actions.setSubject(channelId, type, subject); + await channel.actions.setChannelSubject(channelId, type, subject); } }, removeChannel: async () => { diff --git a/app/mobile/src/session/Session.jsx b/app/mobile/src/session/Session.jsx index 75c321dc..61fffa12 100644 --- a/app/mobile/src/session/Session.jsx +++ b/app/mobile/src/session/Session.jsx @@ -54,6 +54,7 @@ export function Session() { const closeConversation = (navigation) => { setCardId(null); setChannelId(null); + navigation.popToTop(); } const openDetails = (navigation) => { navigation.navigate('details'); @@ -73,7 +74,7 @@ export function Session() { ( Details )}}> - {(props) =>
clearConversation(props.navigation)} />} + {(props) =>
closeConversation(props.navigation)} />} diff --git a/app/mobile/src/session/channels/useChannels.hook.js b/app/mobile/src/session/channels/useChannels.hook.js index a81286ce..2339415a 100644 --- a/app/mobile/src/session/channels/useChannels.hook.js +++ b/app/mobile/src/session/channels/useChannels.hook.js @@ -37,7 +37,7 @@ export function useChannels() { const setChannelItem = async (loginTimestamp, cardId, channelId, item) => { const timestamp = item.summary.lastTopic.created; const { readRevision, topicRevision } = item; - + // decrypt subject and message let locked = false; let unlocked = false; @@ -50,11 +50,13 @@ export function useChannels() { try { const contentKey = await getContentKey(seals, account.state.sealKey); const unsealed = decryptChannelSubject(item.detail.data, contentKey); - if (cardId) { - await card.actions.setUnsealedChannelSubject(cardId, channelId, item.detailRevision, unsealed); - } - else { - await channel.actions.setUnsealedChannelSubject(channelId, item.detailRevision, unsealed); + if (unsealed) { + if (cardId) { + await card.actions.setUnsealedChannelSubject(cardId, channelId, item.detailRevision, unsealed); + } + else { + await channel.actions.setUnsealedChannelSubject(channelId, item.detailRevision, unsealed); + } } } catch(err) { @@ -66,11 +68,13 @@ export function useChannels() { try { const contentKey = await getContentKey(seals, account.state.sealKey); const unsealed = decryptTopicSubject(item.summary.lastTopic.data, contentKey); - if (cardId) { - await card.actions.setUnsealedChannelSummary(cardId, channelId, item.topicRevision, unsealed); - } - else { - await channel.actions.setUnsealedChannelSummary(channelId, item.topicRevision, unsealed); + if (unsealed) { + if (cardId) { + await card.actions.setUnsealedChannelSummary(cardId, channelId, item.topicRevision, unsealed); + } + else { + await channel.actions.setUnsealedChannelSummary(channelId, item.topicRevision, unsealed); + } } } catch(err) { diff --git a/app/mobile/src/session/details/Details.jsx b/app/mobile/src/session/details/Details.jsx index b237a318..b7d32cce 100644 --- a/app/mobile/src/session/details/Details.jsx +++ b/app/mobile/src/session/details/Details.jsx @@ -131,13 +131,13 @@ export function Details({ channel, clearConversation }) { )} { state.subject } - { !state.hostId && (!state.locked || state.sealable) && ( + { !state.hostId && (!state.locked || state.unlocked) && ( )} - { state.created } + { state.timestamp } { state.hostId ? 'guest' : 'host' } diff --git a/app/mobile/src/session/details/useDetails.hook.js b/app/mobile/src/session/details/useDetails.hook.js index 42cba054..6ae697c9 100644 --- a/app/mobile/src/session/details/useDetails.hook.js +++ b/app/mobile/src/session/details/useDetails.hook.js @@ -5,14 +5,14 @@ import { CardContext } from 'context/CardContext'; import { AccountContext } from 'context/AccountContext'; import { ProfileContext } from 'context/ProfileContext'; import { getChannelSubjectLogo } from 'context/channelUtil'; -import { getChannelSeals, isUnsealed } from 'context/sealUtil'; +import { getChannelSeals, isUnsealed, getContentKey, updateChannelSubject } from 'context/sealUtil'; import moment from 'moment'; export function useDetails() { const [state, setState] = useState({ subject: null, - created: null, + timestamp: null, logo: null, hostId: null, connected: [], @@ -24,6 +24,8 @@ export function useDetails() { locked: false, unlocked: false, count: 0, + seals: null, + sealKey: null, }); const card = useContext(CardContext); @@ -38,12 +40,14 @@ export function useDetails() { useEffect(() => { let locked; let unlocked; + let seals; + let sealKey; const { channel, notification } = conversation.state; - if (channel.detail.dataType === 'sealed') { + if (channel?.detail?.dataType === 'sealed') { locked = true; try { - const { sealKey } = account.state; - const seals = getChannelSeals(channel.detail.data); + sealKey = account.state.sealKey; + seals = getChannelSeals(channel.detail.data); unlocked = isUnsealed(seals, sealKey); } catch(err) { @@ -55,7 +59,7 @@ export function useDetails() { locked = false; unlocked = false; } - updateState({ locked, unlocked, notification }); + updateState({ locked, unlocked, seals, sealKey, notification }); }, [account.state, conversation.state]); useEffect(() => { @@ -69,30 +73,42 @@ export function useDetails() { }, [card.state]); useEffect(() => { - const cardId = conversation.state.card?.cardId; + const hostId = conversation.state.card?.cardId; const profileGuid = profile.state.identity?.guid; const channel = conversation.state.channel; const cards = card.state.cards; const cardImageUrl = card.actions.getCardImageUrl; - const { logo, subject } = getChannelSubjectLogo(cardId, profileGuid, channel, cards, cardImageUrl); - const timestamp = conversation.state.channel?.detail?.created; + const { logo, subject } = getChannelSubjectLogo(hostId, profileGuid, channel, cards, cardImageUrl); - let created; - const date = new Date(item.detail.created * 1000); + let timestamp; + const { created, data, dataType } = conversation.state.channel?.detail || {} + const date = new Date(created * 1000); const now = new Date(); const offset = now.getTime() - date.getTime(); if(offset < 86400000) { - created = moment(date).format('h:mma'); + timestamp = moment(date).format('h:mma'); } else if (offset < 31449600000) { - created = moment(date).format('M/DD'); + timestamp = moment(date).format('M/DD'); } else { - created = moment(date).format('M/DD/YYYY'); + timestamp = moment(date).format('M/DD/YYYY'); } - updateState({ logo, subject, created }); - }, [conversation]); + let subjectUpdate; + try { + if (dataType === 'superbasic') { + subjectUpdate = JSON.parse(data).subject; + } + else if (conversation.state?.channel?.unsealedDetail) { + subjectUpdate = conversation.state?.channel?.unsealedDetail?.subject; + } + } + catch(err) { + console.log(err); + } + updateState({ hostId, logo, subject, timestamp, subjectUpdate }); + }, [conversation.state]); const actions = { showEditMembers: () => { @@ -112,10 +128,14 @@ export function useDetails() { }, saveSubject: async () => { if (state.locked) { - await conversation.actions.setSealedSubject(state.subjectUpdate, account.state.sealKey); + const contentKey = await getContentKey(state.seals, state.sealKey); + const sealed = updateChannelSubject(state.subjectUpdate, contentKey); + sealed.seals = state.seals; + await conversation.actions.setChannelSubject('sealed', sealed); } else { - await conversation.actions.setSubject(state.subjectUpdate); + const subject = { subject: state.subjectUpdate }; + await conversation.actions.setChannelSubject('superbasic', subject); } }, remove: async () => {