Skip to content

Commit 7ca8a07

Browse files
authored
Merge pull request #51 from fbsamples/auto-publish
Add Option for Automatically Publishing Text Posts
2 parents 26a1d20 + f49d22d commit 7ca8a07

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

public/scripts/form.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,18 @@ function isAttachmentsInputValid(data) {
5151
data.get('pollOptionD');
5252
const linkAttached = data.get('linkAttachment');
5353
const hasMediaAttachment = data.has('attachmentType[]');
54+
const autoPublishText = data.get('autoPublishText');
5455

5556
if (pollAttached && linkAttached) {
5657
alert('Link attachments and poll attachments cannot be used together.');
5758
return false;
5859
}
5960

61+
if (hasMediaAttachment && autoPublishText) {
62+
alert('Media attachments cannot be automatically published.')
63+
return false;
64+
}
65+
6066
if (hasMediaAttachment && linkAttached) {
6167
alert('Link attachments can only be used with text posts.')
6268
}
@@ -91,6 +97,7 @@ async function processFormAsync(urlGenerator) {
9197
onAsyncRequestStarting();
9298

9399
let id;
100+
let redirectUrl;
94101
try {
95102
let response = await fetch(e.target.getAttribute('action'), {
96103
method: 'POST',
@@ -100,6 +107,7 @@ async function processFormAsync(urlGenerator) {
100107
if(response.ok) {
101108
let jsonResponse = await response.json();
102109
id = jsonResponse.id;
110+
redirectUrl = jsonResponse.redirectUrl;
103111
} else {
104112
resultElem.textContent = 'Error submitting form';
105113
}
@@ -112,6 +120,8 @@ async function processFormAsync(urlGenerator) {
112120

113121
if (id) {
114122
window.location.href = urlGenerator(id);
123+
} else if (redirectUrl) {
124+
window.location.href = redirectUrl;
115125
}
116126
});
117127
}

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)