render contact channel images

This commit is contained in:
Roland Osborne 2022-05-02 00:56:58 -07:00
parent 80c0b0c97d
commit 9782c3961e

View File

@ -2,31 +2,73 @@ 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) { if (assets == null || assets.length == 0) {
let subject = { data: JSON.stringify(message, (key, value) => {
if (value !== null) return value
}), datatype: 'superbasictopic' };
let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}&confirm=true`, let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}&confirm=true`,
{ method: 'POST', body: JSON.stringify(subject) }); { method: 'POST', body: JSON.stringify(subject) });
checkResponse(topic); checkResponse(topic);
} }
else { 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);
let slot = await topic.json(); let slot = await topic.json();
// add each asset // add each asset
message.assets = [];
for (let asset of assets) { for (let asset of assets) {
const formData = new FormData(); if (asset.image) {
formData.append('asset', asset.image); const formData = new FormData();
let transform = encodeURIComponent(JSON.stringify(["ithumb;photo"])); formData.append('asset', asset.image);
let topicAsset = await fetch(`https://${server}/content/channels/${channelId}/topics/${slot.id}/assets?transforms=${transform}&contact=${token}`, { method: 'POST', body: formData }); let transform = encodeURIComponent(JSON.stringify(["ithumb;photo", "icopy;photo"]));
checkResponse(topicAsset); let topicAsset = await fetch(`https://${server}/content/channels/${channelId}/topics/${slot.id}/assets?transforms=${transform}&contact=${token}`, { method: 'POST', body: formData });
console.log(await topicAsset.json()); checkResponse(topicAsset);
let assetEntry = await topicAsset.json();
message.assets.push({
image: {
thumb: assetEntry.find(item => item.transform === 'ithumb;photo').assetId,
full: assetEntry.find(item => item.transform === 'icopy;photo').assetId,
}
});
}
else if (asset.video) {
const formData = new FormData();
formData.append('asset', asset.video);
let transform = encodeURIComponent(JSON.stringify(["vthumb;video", "vcopy;video"]));
let topicAsset = await fetch(`https://${server}/content/channels/${channelId}/topics/${slot.id}/assets?transforms=${transform}&contact=${token}`, { method: 'POST', body: formData });
checkResponse(topicAsset);
let assetEntry = await topicAsset.json();
message.assets.push({
image: {
thumb: assetEntry.find(item => item.transform === 'vthumb;video').assetId,
full: assetEntry.find(item => item.transform === 'vcopy;video').assetId,
}
});
}
else if (asset.audio) {
const formData = new FormData();
formData.append('asset', asset.audio);
let transform = encodeURIComponent(JSON.stringify(["acopy;audio"]));
let topicAsset = await fetch(`https://${server}/content/channels/${channelId}/topics/${slot.id}/assets?transforms=${transform}&contact=${token}`, { method: 'POST', body: formData });
checkResponse(topicAsset);
let assetEntry = await topicAsset.json();
message.assets.push({
image: {
full: assetEntry.find(item => item.transform === 'acopy;audio').assetId,
}
});
}
} }
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);