Skip to content

Commit f341102

Browse files
committed
Add Option for Automatically Publishing Text Posts
1 parent 26a1d20 commit f341102

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

public/scripts/form.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ async function processFormAsync(urlGenerator) {
9191
onAsyncRequestStarting();
9292

9393
let id;
94+
let redirectUrl;
9495
try {
9596
let response = await fetch(e.target.getAttribute('action'), {
9697
method: 'POST',
@@ -100,6 +101,7 @@ async function processFormAsync(urlGenerator) {
100101
if(response.ok) {
101102
let jsonResponse = await response.json();
102103
id = jsonResponse.id;
104+
redirectUrl = jsonResponse.redirectUrl;
103105
} else {
104106
resultElem.textContent = 'Error submitting form';
105107
}
@@ -112,6 +114,8 @@ async function processFormAsync(urlGenerator) {
112114

113115
if (id) {
114116
window.location.href = urlGenerator(id);
117+
} else if (redirectUrl) {
118+
window.location.href = redirectUrl;
115119
}
116120
});
117121
}

src/index.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const MEDIA_TYPE__TEXT = 'TEXT';
5555
const MEDIA_TYPE__VIDEO = 'VIDEO';
5656

5757
const PARAMS__ACCESS_TOKEN = 'access_token';
58+
const PARAMS__AUTO_PUBLISH_TEXT = 'auto_publish_text';
5859
const PARAMS__ALT_TEXT = 'alt_text';
5960
const PARAMS__CLIENT_ID = 'client_id';
6061
const PARAMS__CONFIG = 'config';
@@ -370,6 +371,7 @@ app.post('/upload', upload.array(), async (req, res) => {
370371
replyControl,
371372
replyToId,
372373
linkAttachment,
374+
autoPublishText,
373375
pollOptionA,
374376
pollOptionB,
375377
pollOptionC,
@@ -419,6 +421,10 @@ app.post('/upload', upload.array(), async (req, res) => {
419421
});
420422
}
421423

424+
if (autoPublishText === 'on' && params.media_type === MEDIA_TYPE__TEXT) {
425+
params[PARAMS__AUTO_PUBLISH_TEXT] = true;
426+
}
427+
422428
if (params.media_type === MEDIA_TYPE__CAROUSEL) {
423429
const createChildPromises = params.children.map(child => (
424430
axios.post(
@@ -446,10 +452,18 @@ app.post('/upload', upload.array(), async (req, res) => {
446452
const postThreadsUrl = buildGraphAPIURL(`me/threads`, params, req.session.access_token);
447453
try {
448454
const postResponse = await axios.post(postThreadsUrl, {}, { httpsAgent: agent });
449-
const containerId = postResponse.data.id;
450-
res.json({
451-
id: containerId,
452-
});
455+
const id = postResponse.data.id;
456+
if (autoPublishText === 'on' && params.media_type === MEDIA_TYPE__TEXT) {
457+
// If auto_publish_text is on, the returned ID is the published Threads ID.
458+
return res.json({
459+
redirectUrl: `/threads/${id}`
460+
});
461+
} else {
462+
// Otherwise, the returned ID is the container ID.
463+
return res.json({
464+
id: id,
465+
});
466+
}
453467
}
454468
catch (e) {
455469
console.error(e.message);

views/upload.pug

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ block content
7777
| Link Attachment
7878
input#link-attachment(type='text' name='linkAttachment' value='')
7979

80+
label(for="autoPublishText")
81+
input(type="checkbox" name="autoPublishText" id="autoPublishText")
82+
| Auto-Publish (only for text posts)
83+
8084
button#poll-attachment-button(type='button' name='pollAttachment') Attach Poll 🗳️
8185
div#poll-attachment-options(style='display:none')
8286
div.poll-attachment-option

0 commit comments

Comments
 (0)