diff --git a/net/web/src/Api/addContactChannelTopic.js b/net/web/src/Api/addContactChannelTopic.js new file mode 100644 index 00000000..942196bb --- /dev/null +++ b/net/web/src/Api/addContactChannelTopic.js @@ -0,0 +1,24 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function addContactChannelTopic(token, channelId, message, assets ) { + let topic = await fetchWithTimeout(`/content/channels/${channelId}/topics?contact=${token}`, + { method: 'POST', body: JSON.stringify({}) }); + checkResponse(topic); + let slot = await topic.json(); + + // add each asset + + let subject = { data: JSON.stringify(message, (key, value) => { + if (value !== null) return value + }), datatype: 'superbasictopic' }; + let unconfirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/subject?contact=${token}`, + { method: 'PUT', body: JSON.stringify(subject) }); + checkResponse(unconfirmed); + + let confirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/confirmed?contact=${token}`, + { method: 'PUT', body: JSON.stringify('confirmed') }); + checkResponse(confirmed); + + return; +} + diff --git a/net/web/src/Api/getContactChannel.js b/net/web/src/Api/getContactChannel.js new file mode 100644 index 00000000..c6a447b5 --- /dev/null +++ b/net/web/src/Api/getContactChannel.js @@ -0,0 +1,8 @@ +import { checkResponse, fetchWithTimeout } from './fetchUtil'; + +export async function getContactChannel(token, channelId) { + let channel = await fetchWithTimeout(`/content/channels/${channelId}/detail?contact=${token}`, { method: 'GET' }); + checkResponse(channel) + return await channel.json() +} + diff --git a/net/web/src/AppContext/useAppContext.hook.js b/net/web/src/AppContext/useAppContext.hook.js index 599d9153..080b39a5 100644 --- a/net/web/src/AppContext/useAppContext.hook.js +++ b/net/web/src/AppContext/useAppContext.hook.js @@ -3,6 +3,7 @@ import { getContactProfile, setCardProfile, getCards, getCardImageUrl, getCardPr import { getChannels } from '../Api/getChannels'; import { getChannel } from '../Api/getChannel'; import { getContactChannels } from '../Api/getContactChannels'; +import { getContactChannel } from '../Api/getContactChannel'; async function updateAccount(token, updateData) { let status = await getAccountStatus(token); @@ -134,10 +135,24 @@ async function updateCards(token, revision, cardMap, updateData, mergeChannels) async function updateContactChannels(token, viewRevision, channelRevision, channelMap) { let channels = await getContactChannels(token, viewRevision, channelRevision); - for (let channel of channels) { if (channel.data) { - channelMap.set(channel.id, channel); + let cur = channelMap.get(channel.id); + if (cur == null) { + cur = { id: channel.id, data: { } } + } + if (cur.data.detailRevision != channel.data.detailRevision) { + if (channel.data.channelDetail != null) { + cur.data.channelDetail = channel.data.channelDetail; + cur.data.detailRevision = channel.data.detailRevision; + } + else { + let slot = await getContactChannel(token, channel.id); + cur.data.channelDetail = slot.data.channelDetail; + cur.data.detailRevision = slot.data.detailRevision; + } + } + channelMap.set(channel.id, cur); } else { channelMap.delete(channel.id); diff --git a/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js b/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js index 2baade2c..4412ae9a 100644 --- a/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js +++ b/net/web/src/User/Conversation/AddTopic/useAddTopic.hook.js @@ -1,6 +1,7 @@ import { useContext, useState, useEffect } from 'react'; import { useParams } from 'react-router-dom'; import { addChannelTopic } from '../../../Api/addChannelTopic'; +import { addContactChannelTopic } from '../../../Api/addContactChannelTopic'; import { AppContext } from '../../../AppContext/AppContext'; export function useAddTopic() { @@ -53,11 +54,16 @@ export function useAddTopic() { if (!state.busy) { updateState({ busy: true }); try { -if (!contact) { let message = { text: state.messageText, textColor: state.messageColor, textSize: state.messageSize, backgroundColor: state.backgroundColor }; - await addChannelTopic(app.state.token, channel, message, []); -} + if (contact) { + let card = app.actions.getCard(contact); + let token = contact + '.' + card?.data?.cardDetail?.token; + await addContactChannelTopic(token, channel, message, []); + } + else { + await addChannelTopic(app.state.token, channel, message, []); + } } catch(err) { window.alert(err);