merging sealed topic add

This commit is contained in:
Roland Osborne 2023-03-02 21:30:17 -08:00
parent f5e27c80e1
commit 65c0afb313
8 changed files with 58 additions and 34 deletions

View File

@ -361,7 +361,7 @@ export function useCardContext() {
return await setCardCloseMessage(server, message); return await setCardCloseMessage(server, message);
}, },
getCardImageUrl: (cardId) => { getCardImageUrl: (cardId) => {
const { profileRevision } = cards.current.get(cardId)?.card || { }; const { profileRevision } = cards.current.get(cardId)?.card || {};
const { server, token } = access.current; const { server, token } = access.current;
return getCardImageUrl(server, token, cardId, profileRevision); return getCardImageUrl(server, token, cardId, profileRevision);
}, },
@ -371,17 +371,17 @@ export function useCardContext() {
return await removeContactChannel(profile?.node, cardToken, channelId); return await removeContactChannel(profile?.node, cardToken, channelId);
}, },
addTopic: async (cardId, channelId, type, message, files) => { addTopic: async (cardId, channelId, type, message, files) => {
const { detail, profile } = cards.current.get(cardId) || {}; const { detail, profile } = cards.current.get(cardId)?.card || {};
const cardToken = `${profile?.guid}.${detail?.token}`; const cardToken = `${profile?.guid}.${detail?.token}`;
const node = profile?.node; const node = profile?.node;
if (files?.length > 0) { if (files?.length > 0) {
const topicId = await addContactChannelTopic(node, token, channelId, null, null, null); const topicId = await addContactChannelTopic(node, cardToken, channelId, null, null, null);
upload.actions.addContactTopic(node, token, cardId, channelId, topicId, files, async (assets) => { upload.actions.addContactTopic(node, cardToken, cardId, channelId, topicId, files, async (assets) => {
const subject = message(assets); const subject = message(assets);
await setContactChannelTopicSubject(node, token, channelId, topicId, type, subject); await setContactChannelTopicSubject(node, cardToken, channelId, topicId, type, subject);
}, async () => { }, async () => {
try { try {
await removeContactChannelTopic(node, token, channelId, topicId); await removeContactChannelTopic(node, cardToken, channelId, topicId);
} }
catch (err) { catch (err) {
console.log(err); console.log(err);
@ -390,7 +390,7 @@ export function useCardContext() {
} }
else { else {
const subject = message([]); const subject = message([]);
await addContactChannelTopic(node, token, channelId, type, subject, []); await addContactChannelTopic(node, cardToken, channelId, type, subject, []);
} }
}, },
removeTopic: async (cardId, channelId, topicId) => { removeTopic: async (cardId, channelId, topicId) => {

View File

@ -61,7 +61,6 @@ export function useConversationContext() {
const cardValue = cardId ? card.state.cards.get(cardId) : null; const cardValue = cardId ? card.state.cards.get(cardId) : null;
const channelValue = cardId ? cardValue?.channels.get(channelId) : channel.state.channels.get(channelId); const channelValue = cardId ? cardValue?.channels.get(channelId) : channel.state.channels.get(channelId);
const { topicRevision } = channelValue || {}; const { topicRevision } = channelValue || {};
let setChannel = false;
if (channelValue) { if (channelValue) {
if (!loaded.current) { if (!loaded.current) {
@ -76,9 +75,6 @@ export function useConversationContext() {
curTopicMarker.current = topicMarker; curTopicMarker.current = topicMarker;
loaded.current = true; loaded.current = true;
} }
else {
setChannel = true;
}
} }
else { else {
console.log("failed to load conversation"); console.log("failed to load conversation");
@ -93,25 +89,20 @@ export function useConversationContext() {
await setMarkerAndSync(cardId, channelId, delta.marker, topicRevision); await setMarkerAndSync(cardId, channelId, delta.marker, topicRevision);
curTopicMarker.current = delta.marker; curTopicMarker.current = delta.marker;
curSyncRevision.current = topicRevision; curSyncRevision.current = topicRevision;
updateState({ offsync: false, topics: topics.current, card: cardValue, channel: channelValue });
} }
else if (loadMore && marker) { if (loadMore && marker) {
const delta = await getTopicDelta(cardId, channelId, null, COUNT, null, curTopicMarker.current); const delta = await getTopicDelta(cardId, channelId, null, COUNT, null, curTopicMarker.current);
await setTopicDelta(cardId, channelId, delta.topics); await setTopicDelta(cardId, channelId, delta.topics);
await setTopicMarker(cardId, channelId, delta.marker); await setTopicMarker(cardId, channelId, delta.marker);
curTopicMarker.current = delta.marker; curTopicMarker.current = delta.marker;
updateState({ offsync: false, topics: topics.current, card: cardValue, channel: channelValue });
} }
else if (ignoreRevision || topicRevision !== curSyncRevision.current) { if (ignoreRevision || topicRevision > curSyncRevision.current) {
const delta = await getTopicDelta(cardId, channelId, curSyncRevision.current, null, curTopicMarker.current, null); const delta = await getTopicDelta(cardId, channelId, curSyncRevision.current, null, curTopicMarker.current, null);
await setTopicDelta(cardId, channelId, delta.topics); await setTopicDelta(cardId, channelId, delta.topics);
await setSyncRevision(cardId, channelId, topicRevision); await setSyncRevision(cardId, channelId, delta.revision);
curSyncRevision.current = topicRevision; curSyncRevision.current = delta.revision;
updateState({ offsync: false, topics: topics.current, card: cardValue, channel: channelValue });
}
else if (setChannel) {
updateState({ offsync: false, topics: topics.current, card: cardValue, channel: channelValue });
} }
updateState({ offsync: false, topics: topics.current, card: cardValue, channel: channelValue });
} }
catch(err) { catch(err) {
console.log(err); console.log(err);

View File

@ -1,7 +1,7 @@
import { useEffect, useState, useRef, useContext } from 'react'; import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage"; import SQLite from "react-native-sqlite-storage";
const DATABAG_DB = 'db_v_115.db'; const DATABAG_DB = 'db_v_116.db';
export function useStoreContext() { export function useStoreContext() {
const [state, setState] = useState({}); const [state, setState] = useState({});

View File

@ -159,7 +159,7 @@ export function AddTopic({ sealed, sealKey }) {
<View style={styles.space} /> <View style={styles.space} />
<TouchableOpacity style={styles.addButton} onPress={sendMessage}> <TouchableOpacity style={styles.addButton} onPress={sendMessage}>
{ state.busy && ( { state.busy && (
<ActivityIndicator color={Colors.white} /> <ActivityIndicator color={Colors.primary} />
)} )}
{ !state.busy && (state.message || state.assets.length > 0) && ( { !state.busy && (state.message || state.assets.length > 0) && (
<MatIcons name="send-outline" size={20} color={Colors.text} /> <MatIcons name="send-outline" size={20} color={Colors.text} />

View File

@ -2,6 +2,8 @@ import { useState, useRef, useEffect, useContext } from 'react';
import { ConversationContext } from 'context/ConversationContext'; import { ConversationContext } from 'context/ConversationContext';
import { Image } from 'react-native'; import { Image } from 'react-native';
import Colors from 'constants/Colors'; import Colors from 'constants/Colors';
import { getChannelSeals, getContentKey, encryptTopicSubject } from 'context/sealUtil';
import { AccountContext } from 'context/AccountContext';
export function useAddTopic(sealed, sealKey) { export function useAddTopic(sealed, sealKey) {
@ -23,6 +25,7 @@ export function useAddTopic(sealed, sealKey) {
const assetId = useRef(0); const assetId = useRef(0);
const conversation = useContext(ConversationContext); const conversation = useContext(ConversationContext);
const account = useContext(AccountContext);
const updateState = (value) => { const updateState = (value) => {
setState((s) => ({ ...s, ...value })); setState((s) => ({ ...s, ...value }));
@ -107,17 +110,43 @@ export function useAddTopic(sealed, sealKey) {
if (!state.busy) { if (!state.busy) {
try { try {
updateState({ busy: true }); updateState({ busy: true });
let message = {
text: state.message, let contentKey;
textColor: state.colorSet ? state.color : null, const type = conversation.state.channel?.detail?.dataType === 'superbasic' ? 'superbasictopic' : 'sealedtopic';
textSize: state.sizeSet ? state.size : null, if (type === 'sealedtopic') {
const channelDetail = conversation.state.channel?.detail;
const seals = getChannelSeals(channelDetail?.data);
const sealKey = account.state.sealKey;
contentKey = await getContentKey(seals, sealKey);
}
const assemble = (assets) => {
if (type === 'superbasictopic') {
if (assets?.length) {
return {
assets,
text: state.message,
textColor: state.colorSet ? state.color : null,
textSize: state.sizeSet ? state.size : null,
}
}
else {
return {
text: state.message,
textColor: state.colorSet ? state.color : null,
textSize: state.sizeSet ? state.size : null,
}
}
}
else {
const message = {
text: state.message,
textColor: state.textColorSet ? state.textColor : null,
textSize: state.textSizeSet ? state.textSize : null,
}
return encryptTopicSubject({ message }, contentKey);
}
}; };
if (sealed) { await conversation.actions.addTopic(type, assemble, state.assets);
await conversation.actions.addSealedTopic(message, sealKey);
}
else {
await conversation.actions.addTopic(message, state.assets);
}
updateState({ busy: false, assets: [], message: null, updateState({ busy: false, assets: [], message: null,
size: 'medium', sizeSet: false, textSize: 14, size: 'medium', sizeSet: false, textSize: 14,
color: Colors.text, colorSet: false, color: Colors.text, colorSet: false,

View File

@ -205,7 +205,9 @@ export function Details({ channel, clearConversation }) {
<TouchableOpacity onPress={() => setNotifications(!state.notification)} activeOpacity={1}> <TouchableOpacity onPress={() => setNotifications(!state.notification)} activeOpacity={1}>
<Text style={styles.notifyText}>Enable Notifications</Text> <Text style={styles.notifyText}>Enable Notifications</Text>
</TouchableOpacity> </TouchableOpacity>
<Switch style={styles.switch} value={state.notification} onValueChange={setNotifications} trackColor={styles.track}/> { state.notification != null && (
<Switch style={styles.switch} value={state.notification} onValueChange={setNotifications} trackColor={styles.track}/>
)}
</View> </View>
</View> </View>

View File

@ -14,6 +14,7 @@ export const styles = StyleSheet.create({
paddingTop: 16, paddingTop: 16,
flexShrink: 1, flexShrink: 1,
minWidth: 0, minWidth: 0,
maxWidth: '80%',
}, },
info: { info: {
paddingLeft: 8, paddingLeft: 8,

View File

@ -29,6 +29,7 @@ export function useDetails() {
deleteBusy: false, deleteBusy: false,
blockBusy: false, blockBusy: false,
unknown: 0, unknown: 0,
notification: null,
}); });
const card = useContext(CardContext); const card = useContext(CardContext);