databag/net/web/src/api/addContactChannelTopic.js

81 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-04-15 04:07:11 +00:00
import { checkResponse, fetchWithTimeout } from './fetchUtil';
2022-04-25 17:29:22 +00:00
export async function addContactChannelTopic(server, token, channelId, message, assets ) {
2022-04-15 04:07:11 +00:00
2022-04-27 17:33:51 +00:00
if (assets == null || assets.length == 0) {
2022-05-02 07:56:58 +00:00
let subject = { data: JSON.stringify(message, (key, value) => {
if (value !== null) return value
}), datatype: 'superbasictopic' };
2022-04-27 17:33:51 +00:00
let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}&confirm=true`,
{ method: 'POST', body: JSON.stringify(subject) });
checkResponse(topic);
}
else {
2022-05-02 07:56:58 +00:00
2022-04-27 17:33:51 +00:00
let topic = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics?contact=${token}`,
{ method: 'POST', body: JSON.stringify({}) });
checkResponse(topic);
let slot = await topic.json();
// add each asset
2022-05-02 07:56:58 +00:00
message.assets = [];
2022-04-30 07:01:25 +00:00
for (let asset of assets) {
2022-05-02 07:56:58 +00:00
if (asset.image) {
const formData = new FormData();
formData.append('asset', asset.image);
let transform = encodeURIComponent(JSON.stringify(["ithumb;photo", "icopy;photo"]));
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 === '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);
2022-05-02 21:18:10 +00:00
let transform = encodeURIComponent(JSON.stringify(["vcopy;video"]));
2022-05-02 07:56:58 +00:00
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({
2022-05-02 21:18:10 +00:00
video: {
2022-05-02 07:56:58 +00:00
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({
2022-05-02 21:18:10 +00:00
audio: {
2022-05-02 07:56:58 +00:00
full: assetEntry.find(item => item.transform === 'acopy;audio').assetId,
}
});
}
2022-04-30 07:01:25 +00:00
}
2022-04-15 04:07:11 +00:00
2022-05-02 07:56:58 +00:00
let subject = { data: JSON.stringify(message, (key, value) => {
if (value !== null) return value
}), datatype: 'superbasictopic' };
2022-04-27 17:33:51 +00:00
let unconfirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/subject?contact=${token}`,
{ method: 'PUT', body: JSON.stringify(subject) });
checkResponse(unconfirmed);
2022-04-15 04:07:11 +00:00
2022-04-27 17:33:51 +00:00
let confirmed = await fetchWithTimeout(`https://${server}/content/channels/${channelId}/topics/${slot.id}/confirmed?contact=${token}`,
{ method: 'PUT', body: JSON.stringify('confirmed') });
checkResponse(confirmed);
}
2022-04-15 04:07:11 +00:00
}