Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

refactor(publishing): split igPublishFlow into polling and single post flow

spacesdrive1926b4e7cef5
diff --git a/docs/architecture/frontend/API_LAYER.md b/docs/architecture/frontend/API_LAYER.md
index d33ac50..6ec6862 100644
--- a/docs/architecture/frontend/API_LAYER.md
+++ b/docs/architecture/frontend/API_LAYER.md
@@ -131,17 +131,16 @@ uploadImage(file, cloudConfig)
Used for tracked item screenshots. It posts the image with `fetch` to `https://api.cloudinary.com/v1_1/<cloudName>/image/upload` and resolves with `secure_url`. It throws an `Error` with Cloudinary's own message, or `'Cloudinary did not return an image URL'`, and network or JSON failures reject with the browser's own error. The tracked content editor shows that `err.message` after `Image upload failed.` in an error toast.
```js
-uploadToCloudinary(file, cloudConfig, { onProgress, resourceType })
+uploadToCloudinary(file, cloudConfig, { onProgress })
```
Used by publishing. It posts `file` and the unsigned
`upload_preset` from `getCloudinaryConfig()` to
-`https://api.cloudinary.com/v1_1/<cloudName>/<resourceType>/upload` with `XMLHttpRequest`, so
+`https://api.cloudinary.com/v1_1/<cloudName>/<video|image>/upload` (`video` for `video/*` files, `image` otherwise) with `XMLHttpRequest`, so
upload progress is available, and resolves with the response's `secure_url`.
- `cloudConfig`: the object from `getCloudinaryConfig()` (`cloudName`, `uploadPreset`). Callers check it is set first.
- `onProgress(percent)`: optional; called with a whole number from 0 to 100. The progress listener is only attached when this is passed.
-- `resourceType`: optional; defaults to `video` for `video/*` files and `image` otherwise.
Failures reject with an `Error` that carries a `reason` so each page keeps its own wording:
diff --git a/docs/architecture/frontend/FEATURES.md b/docs/architecture/frontend/FEATURES.md
index c448c58..971cea2 100644
--- a/docs/architecture/frontend/FEATURES.md
+++ b/docs/architecture/frontend/FEATURES.md
@@ -86,7 +86,8 @@ frontend/src/
│ │ ├── useSinglePublish.js, usePublishProgress.js <- Single post publish or schedule run
│ │ ├── useBulkSettings.js, useBulkQueue.js, useBulkPublish.js <- Bulk settings, file queue, upload loop
│ │ ├── useScheduledPosts.js, useScheduledPostEdit.js, useScheduledPostDelete.js
-│ │ ├── igPublishFlow.js <- Cloudinary upload, container polling, single post publish steps
+│ │ ├── containerPolling.js <- Waits for an Instagram container (single post and bulk)
+│ │ ├── singlePostFlow.js <- Single post Cloudinary upload, then publish or schedule
│ │ ├── publishConstants.js <- Content types, statuses, intervals, caption limit
│ │ └── publishHelpers.js, scheduleHelpers.js, queueHelpers.js <- Pure helpers with *.test.js
│ ├── settings/
diff --git a/frontend/src/features/publishing/containerPolling.js b/frontend/src/features/publishing/containerPolling.js
new file mode 100644
index 0000000..d38fd3b
--- /dev/null
+++ b/frontend/src/features/publishing/containerPolling.js
@@ -0,0 +1,55 @@
+import { api } from '../../services/api';
+
+const POLL_ATTEMPTS = 30;
+const POLL_INTERVAL_MS = 3000;
+const IMAGE_SETTLE_MS = 2000;
+
+export function wait(ms) {
+ return new Promise(resolve => setTimeout(resolve, ms));
+}
+
+function containerFinished(status, failedMessage) {
+ if (status.statusCode === 'FINISHED' || status.statusCode === 'PUBLISHED') return true;
+ if (status.statusCode === 'ERROR') throw new Error(failedMessage);
+ if (status.statusCode === 'EXPIRED') throw new Error('Container expired');
+ return false;
+}
+
+async function pollTolerant(containerId, accountId, onStatus) {
+ try {
+ const status = await api.getIGContainerStatus(containerId, accountId);
+ onStatus(`Instagram status: ${status.statusCode || 'IN_PROGRESS'}`);
+ return containerFinished(status, 'Container processing failed: ' + (status.status || 'Unknown'));
+ } catch (err) {
+ if (err.message.includes('processing failed') || err.message.includes('expired')) throw err;
+ return false;
+ }
+}
+
+async function pollStrict(containerId, accountId) {
+ const status = await api.getIGContainerStatus(containerId, accountId);
+ return containerFinished(status, 'Processing failed');
+}
+
+// single posts ride out failed checks and fail on timeout; bulk stops quietly after the last try
+export async function waitForContainer(containerId, accountId, { tolerant = false, onStatus } = {}) {
+ for (let i = 0; i < POLL_ATTEMPTS; i++) {
+ await wait(POLL_INTERVAL_MS);
+ const finished = tolerant
+ ? await pollTolerant(containerId, accountId, onStatus)
+ : await pollStrict(containerId, accountId);
+ if (finished) return true;
+ }
+ if (tolerant) throw new Error('Instagram took too long to process the media. Try again in a few minutes.');
+ return false;
+}
+
+// photos need no processing, Instagram only needs a moment before they can be published
+export async function waitUntilReady(contentType, containerId, accountId, { onProcessing, ...pollOptions }) {
+ if (contentType !== 'REELS' && contentType !== 'STORIES') {
+ await wait(IMAGE_SETTLE_MS);
+ return;
+ }
+ onProcessing();
+ await waitForContainer(containerId, accountId, pollOptions);
+}
diff --git a/frontend/src/features/publishing/igPublishFlow.js b/frontend/src/features/publishing/singlePostFlow.js
similarity index 55%
rename from frontend/src/features/publishing/igPublishFlow.js
rename to frontend/src/features/publishing/singlePostFlow.js
index cf6c172..c6f2944 100644
--- a/frontend/src/features/publishing/igPublishFlow.js
+++ b/frontend/src/features/publishing/singlePostFlow.js
@@ -1,59 +1,6 @@
import { api } from '../../services/api';
import { buildContainerPayload, buildScheduledPostPayload, singleUploadMessage } from './publishHelpers.js';
-
-const POLL_ATTEMPTS = 30;
-const POLL_INTERVAL_MS = 3000;
-const IMAGE_SETTLE_MS = 2000;
-
-export function wait(ms) {
- return new Promise(resolve => setTimeout(resolve, ms));
-}
-
-function containerFinished(status, failedMessage) {
- if (status.statusCode === 'FINISHED' || status.statusCode === 'PUBLISHED') return true;
- if (status.statusCode === 'ERROR') throw new Error(failedMessage);
- if (status.statusCode === 'EXPIRED') throw new Error('Container expired');
- return false;
-}
-
-async function pollTolerant(containerId, accountId, onStatus) {
- try {
- const status = await api.getIGContainerStatus(containerId, accountId);
- onStatus(`Instagram status: ${status.statusCode || 'IN_PROGRESS'}`);
- return containerFinished(status, 'Container processing failed: ' + (status.status || 'Unknown'));
- } catch (err) {
- if (err.message.includes('processing failed') || err.message.includes('expired')) throw err;
- return false;
- }
-}
-
-async function pollStrict(containerId, accountId) {
- const status = await api.getIGContainerStatus(containerId, accountId);
- return containerFinished(status, 'Processing failed');
-}
-
-// single posts ride out failed checks and fail on timeout; bulk stops quietly after the last try
-export async function waitForContainer(containerId, accountId, { tolerant = false, onStatus } = {}) {
- for (let i = 0; i < POLL_ATTEMPTS; i++) {
- await wait(POLL_INTERVAL_MS);
- const finished = tolerant
- ? await pollTolerant(containerId, accountId, onStatus)
- : await pollStrict(containerId, accountId);
- if (finished) return true;
- }
- if (tolerant) throw new Error('Instagram took too long to process the media. Try again in a few minutes.');
- return false;
-}
-
-// photos need no processing, Instagram only needs a moment before they can be published
-export async function waitUntilReady(contentType, containerId, accountId, { onProcessing, ...pollOptions }) {
- if (contentType !== 'REELS' && contentType !== 'STORIES') {
- await wait(IMAGE_SETTLE_MS);
- return;
- }
- onProcessing();
- await waitForContainer(containerId, accountId, pollOptions);
-}
+import { waitUntilReady } from './containerPolling.js';
export async function uploadSingleMedia(file, cloudConfig, onProgress) {
if (!cloudConfig) throw new Error('Cloudinary is not set up on the server. Check Settings.');
diff --git a/frontend/src/features/publishing/useBulkPublish.js b/frontend/src/features/publishing/useBulkPublish.js
index 7fd4ee0..9f86ffe 100644
--- a/frontend/src/features/publishing/useBulkPublish.js
+++ b/frontend/src/features/publishing/useBulkPublish.js
@@ -1,5 +1,5 @@
import { api } from '../../services/api';
-import { waitUntilReady, wait } from './igPublishFlow.js';
+import { waitUntilReady, wait } from './containerPolling.js';
import {
buildContainerPayload, buildScheduledPostPayload, bulkBaseTime, bulkPostDetails, bulkScheduledTime, bulkStartError,
} from './publishHelpers.js';
diff --git a/frontend/src/features/publishing/useSinglePublish.js b/frontend/src/features/publishing/useSinglePublish.js
index 7fd57df..13c7b7d 100644
--- a/frontend/src/features/publishing/useSinglePublish.js
+++ b/frontend/src/features/publishing/useSinglePublish.js
@@ -1,6 +1,6 @@
import { useState } from 'react';
import { usePublishProgress } from './usePublishProgress';
-import { publishSinglePost, scheduleSinglePost } from './igPublishFlow.js';
+import { publishSinglePost, scheduleSinglePost } from './singlePostFlow.js';
import { singlePostError } from './publishHelpers.js';
function beginRun(run) {
diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js
index f8a7ff8..3a5a291 100644
--- a/frontend/src/services/api.js
+++ b/frontend/src/services/api.js
@@ -39,11 +39,11 @@ function readUploadError(responseText) {
}
// the browser posts the file straight to Cloudinary so the Worker never handles binary uploads
-function uploadToCloudinary(file, cloudConfig, { onProgress, resourceType } = {}) {
+function uploadToCloudinary(file, cloudConfig, { onProgress } = {}) {
const formData = new FormData();
formData.append('file', file);
formData.append('upload_preset', cloudConfig.uploadPreset);
- const type = resourceType || (file.type.startsWith('video/') ? 'video' : 'image');
+ const type = file.type.startsWith('video/') ? 'video' : 'image';
const xhr = new XMLHttpRequest();
return new Promise((resolve, reject) => {
if (onProgress) {