addTopic to contact channel

This commit is contained in:
Roland Osborne 2022-04-14 21:07:11 -07:00
parent 6e2801d705
commit 34eceaa533
4 changed files with 58 additions and 5 deletions

View File

@ -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;
}

View File

@ -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()
}

View File

@ -3,6 +3,7 @@ import { getContactProfile, setCardProfile, getCards, getCardImageUrl, getCardPr
import { getChannels } from '../Api/getChannels'; import { getChannels } from '../Api/getChannels';
import { getChannel } from '../Api/getChannel'; import { getChannel } from '../Api/getChannel';
import { getContactChannels } from '../Api/getContactChannels'; import { getContactChannels } from '../Api/getContactChannels';
import { getContactChannel } from '../Api/getContactChannel';
async function updateAccount(token, updateData) { async function updateAccount(token, updateData) {
let status = await getAccountStatus(token); let status = await getAccountStatus(token);
@ -134,10 +135,24 @@ async function updateCards(token, revision, cardMap, updateData, mergeChannels)
async function updateContactChannels(token, viewRevision, channelRevision, channelMap) { async function updateContactChannels(token, viewRevision, channelRevision, channelMap) {
let channels = await getContactChannels(token, viewRevision, channelRevision); let channels = await getContactChannels(token, viewRevision, channelRevision);
for (let channel of channels) { for (let channel of channels) {
if (channel.data) { 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 { else {
channelMap.delete(channel.id); channelMap.delete(channel.id);

View File

@ -1,6 +1,7 @@
import { useContext, useState, useEffect } from 'react'; import { useContext, useState, useEffect } from 'react';
import { useParams } from 'react-router-dom'; import { useParams } from 'react-router-dom';
import { addChannelTopic } from '../../../Api/addChannelTopic'; import { addChannelTopic } from '../../../Api/addChannelTopic';
import { addContactChannelTopic } from '../../../Api/addContactChannelTopic';
import { AppContext } from '../../../AppContext/AppContext'; import { AppContext } from '../../../AppContext/AppContext';
export function useAddTopic() { export function useAddTopic() {
@ -53,11 +54,16 @@ export function useAddTopic() {
if (!state.busy) { if (!state.busy) {
updateState({ busy: true }); updateState({ busy: true });
try { try {
if (!contact) {
let message = { text: state.messageText, textColor: state.messageColor, let message = { text: state.messageText, textColor: state.messageColor,
textSize: state.messageSize, backgroundColor: state.backgroundColor }; 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) { catch(err) {
window.alert(err); window.alert(err);