merging refactored add channels

This commit is contained in:
balzack 2023-02-27 21:29:10 -08:00
parent aaf7d38836
commit 0cdf493682
5 changed files with 64 additions and 14 deletions

View File

@ -187,7 +187,7 @@ export function useStoreContext() {
await db.current.executeSql(`UPDATE channel_${guid} set topic_marker=? where channel_id=?`, [marker, channelId]);
},
setChannelItemMarkerAndSync: async (guid, channelId, marker, revision) => {
await db.current.executeSql(`UPDATE channel_${guid} set sync_revision=?, topic_marker=? where channel_id=?`, [revision, maker, channelId]);
await db.current.executeSql(`UPDATE channel_${guid} set sync_revision=?, topic_marker=? where channel_id=?`, [revision, marker, channelId]);
},
setChannelItemBlocked: async (guid, channelId) => {
await db.current.executeSql(`UPDATE channel_${guid} set blocked=? where channel_id=?`, [1, channelId]);
@ -281,7 +281,7 @@ export function useStoreContext() {
setCardChannelItemTopicMarker: async (guid, cardId, channelId, marker) => {
await db.current.executeSql(`UPDATE card_channel_${guid} set topic_marker=? where card_id=? and channel_id=?`, [marker, cardId, channelId]);
},
setCardChannelItemMakerAndSync: async (guid, cardId, channelId, marker, revision) => {
setCardChannelItemMarkerAndSync: async (guid, cardId, channelId, marker, revision) => {
await db.current.executeSql(`UPDATE card_channel_${guid} set topic_marker=?, sync_revision=? where card_id=? and channel_id=?`, [marker, revision, cardId, channelId]);
},
setCardChannelItemDetail: async (guid, cardId, channelId, revision, detail) => {

View File

@ -45,9 +45,9 @@ export function Session() {
const ConversationStackScreen = () => {
const conversation = useContext(ConversationContext);
const setConversation = (navigation, cardId, channelId) => {
const setConversation = async (navigation, cardId, channelId) => {
navigation.navigate('conversation');
conversation.actions.setConversation(cardId, channelId);
await conversation.actions.setConversation(cardId, channelId);
}
const clearConversation = (navigation) => {
navigation.dispatch(
@ -55,7 +55,7 @@ export function Session() {
);
conversation.actions.clearConversation();
}
const setDetail = (navigation) => {
const openDetails = (navigation) => {
navigation.navigate('details');
}

View File

@ -1,5 +1,5 @@
import { useEffect } from 'react';
import { Switch, KeyboardAvoidingView, Modal, View, FlatList, Text, TextInput, TouchableOpacity } from 'react-native';
import { Alert, ActivityIndicator, Switch, KeyboardAvoidingView, Modal, View, FlatList, Text, TextInput, TouchableOpacity } from 'react-native';
import Ionicons from 'react-native-vector-icons/AntDesign';
import { styles } from './Channels.styled';
import { useChannels } from './useChannels.hook';
@ -11,7 +11,18 @@ export function Channels({ navigation, openConversation }) {
const { state, actions } = useChannels();
const addTopic = async () => {
const addChannel = async () => {
try {
await actions.addChannel();
actions.hideAdding();
}
catch (err) {
console.log(err);
Alert.alert(
'Failed to Add Topic',
'Please try again.'
)
}
};
useEffect(() => {
@ -108,7 +119,10 @@ export function Channels({ navigation, openConversation }) {
<TouchableOpacity style={styles.cancel} onPress={actions.hideAdding}>
<Text>Cancel</Text>
</TouchableOpacity>
<TouchableOpacity style={styles.save} onPress={addTopic}>
<TouchableOpacity style={styles.save} onPress={addChannel}>
{ state.busy && (
<ActivityIndicator color={Colors.white} />
)}
<Text style={styles.saveText}>Create</Text>
</TouchableOpacity>
</View>

View File

@ -143,15 +143,21 @@ export const styles = StyleSheet.create({
save: {
backgroundColor: Colors.primary,
borderRadius: 4,
padding: 4,
paddingTop: 4,
paddingBottom: 4,
paddingLeft: 8,
paddingRight: 8,
marginRight: 8,
width: 72,
minWidth: 72,
display: 'flex',
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
},
saveText: {
color: Colors.white,
paddingRight: 4,
paddingLeft: 4,
},
addControls: {
display: 'flex',

View File

@ -4,7 +4,7 @@ import { CardContext } from 'context/CardContext';
import { AccountContext } from 'context/AccountContext';
import { AppContext } from 'context/AppContext';
import { ProfileContext } from 'context/ProfileContext';
import { getChannelSeals, isUnsealed, getContentKey, decryptChannelSubject, decryptTopicSubject } from 'context/sealUtil';
import { getChannelSeals, isUnsealed, getContentKey, encryptChannelSubject, decryptChannelSubject, decryptTopicSubject } from 'context/sealUtil';
import { getCardByGuid } from 'context/cardUtil';
export function useChannels() {
@ -17,6 +17,7 @@ export function useChannels() {
addSubject: null,
sealed: false,
sealable: false,
busy: false,
});
const channel = useContext(ChannelContext);
@ -111,7 +112,7 @@ export function useChannels() {
}
item.detail.members.forEach(guid => {
if (guid !== profile.state.identity.guid) {
contacts.push(getCardByGuid(card.state.cards, guid)?.cardId);
contacts.push(getCardByGuid(card.state.cards, guid)?.card?.cardId);
}
})
@ -257,7 +258,7 @@ export function useChannels() {
updateState({ filter });
},
showAdding: () => {
updateState({ adding: true });
updateState({ adding: true, addSubject: null, addMembers: [] });
},
hideAdding: () => {
updateState({ adding: false });
@ -271,7 +272,36 @@ export function useChannels() {
clearAddMember: (cardId) => {
updateState({ addMembers: state.addMembers.filter(item => item !== cardId) });
},
addTopic: () => {
addChannel: async () => {
let conversation;
if (!state.busy) {
try {
updateState({ busy: true });
if (state.sealed) {
const keys = [ account.state.sealKey.public ];
state.addMembers.forEach(id => {
const contact = card.state.cards.get(id);
keys.push(contact.card.profile.seal);
});
const sealed = encryptChannelSubject(state.addSubject, keys);
conversation = await channel.actions.addChannel('sealed', sealed, state.addMembers);
}
else {
const subject = { subject: state.addSubject };
conversation = await channel.actions.addChannel('superbasic', subject, state.addMembers);
}
updateState({ busy: false });
}
catch(err) {
console.log(err);
updateState({ busy: false });
throw new Error("failed to create new channel");
}
}
else {
throw new Error("operation in progress");
}
return conversation.id;
},
};