optimize add topic for message only

This commit is contained in:
Roland Osborne 2022-04-27 10:33:51 -07:00
parent 8cf1d2f24b
commit 91621b8a58
4 changed files with 49 additions and 34 deletions

View File

@ -19,7 +19,7 @@ export function Home() {
navigate('/admin') navigate('/admin')
} }
} }
}, []) }, [app])
return <></> return <></>
} }

View File

@ -1,16 +1,25 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil'; import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function addChannelTopic(token, channelId, message, assets ) { export async function addChannelTopic(token, channelId, message, assets ) {
let topic = await fetchWithTimeout(`/content/channels/${channelId}/topics?agent=${token}`,
{ method: 'POST', body: JSON.stringify({}) });
checkResponse(topic);
let slot = await topic.json();
// add each asset
let subject = { data: JSON.stringify(message, (key, value) => { let subject = { data: JSON.stringify(message, (key, value) => {
if (value !== null) return value if (value !== null) return value
}), datatype: 'superbasictopic' }; }), datatype: 'superbasictopic' };
if (assets == null || assets.length == 0) {
let topic = await fetchWithTimeout(`/content/channels/${channelId}/topics?agent=${token}&confirm=true`,
{ method: 'POST', body: JSON.stringify(subject) });
checkResponse(topic);
}
else {
let topic = await fetchWithTimeout(`/content/channels/${channelId}/topics?agent=${token}`,
{ method: 'POST', body: JSON.stringify({}) });
checkResponse(topic);
let slot = await topic.json();
// add each asset
let unconfirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/subject?agent=${token}`, let unconfirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/subject?agent=${token}`,
{ method: 'PUT', body: JSON.stringify(subject) }); { method: 'PUT', body: JSON.stringify(subject) });
checkResponse(unconfirmed); checkResponse(unconfirmed);
@ -18,7 +27,6 @@ export async function addChannelTopic(token, channelId, message, assets ) {
let confirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/confirmed?agent=${token}`, let confirmed = await fetchWithTimeout(`/content/channels/${channelId}/topics/${slot.id}/confirmed?agent=${token}`,
{ method: 'PUT', body: JSON.stringify('confirmed') }); { method: 'PUT', body: JSON.stringify('confirmed') });
checkResponse(confirmed); checkResponse(confirmed);
}
return;
} }

View File

@ -1,6 +1,17 @@
import { checkResponse, fetchWithTimeout } from './fetchUtil'; import { checkResponse, fetchWithTimeout } from './fetchUtil';
export async function addContactChannelTopic(server, token, channelId, message, assets ) { export async function addContactChannelTopic(server, token, channelId, message, assets ) {
let subject = { data: JSON.stringify(message, (key, value) => {
if (value !== null) return value
}), datatype: 'superbasictopic' };
if (assets == null || assets.length == 0) {
let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}&confirm=true`,
{ method: 'POST', body: JSON.stringify(subject) });
checkResponse(topic);
}
else {
let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}`, let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}`,
{ method: 'POST', body: JSON.stringify({}) }); { method: 'POST', body: JSON.stringify({}) });
checkResponse(topic); checkResponse(topic);
@ -8,9 +19,6 @@ export async function addContactChannelTopic(server, token, channelId, message,
// add each asset // add each asset
let subject = { data: JSON.stringify(message, (key, value) => {
if (value !== null) return value
}), datatype: 'superbasictopic' };
let unconfirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/subject?contact=${token}`, let unconfirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/subject?contact=${token}`,
{ method: 'PUT', body: JSON.stringify(subject) }); { method: 'PUT', body: JSON.stringify(subject) });
checkResponse(unconfirmed); checkResponse(unconfirmed);
@ -18,7 +26,6 @@ export async function addContactChannelTopic(server, token, channelId, message,
let confirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/confirmed?contact=${token}`, let confirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/confirmed?contact=${token}`,
{ method: 'PUT', body: JSON.stringify('confirmed') }); { method: 'PUT', body: JSON.stringify('confirmed') });
checkResponse(confirmed); checkResponse(confirmed);
}
return;
} }

View File

@ -163,8 +163,8 @@ export function useAppContext() {
} }
}, []); }, []);
if (!state) { if (state == null) {
return {} return {};
} }
if (state.access === 'user') { if (state.access === 'user') {
return { state, actions: userActions } return { state, actions: userActions }