Skip to content

Commit f9a6892

Browse files
committed
create and retrieve posts with poll attachments
1 parent 707f54a commit f9a6892

5 files changed

Lines changed: 146 additions & 2 deletions

File tree

public/scripts/form.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,59 @@ function onAsyncRequestEnded() {
2323
}
2424
}
2525

26+
function isPollInputValid(data) {
27+
const pollOptionA = data.get('pollOptionA');
28+
const pollOptionB = data.get('pollOptionB');
29+
const pollOptionC = data.get('pollOptionC');
30+
const pollOptionD = data.get('pollOptionD');
31+
const pollAttached = pollOptionA || pollOptionB || pollOptionC || pollOptionD;
32+
33+
if (pollAttached && (!pollOptionA || !pollOptionB)) {
34+
alert('Options A and B are required for polls.');
35+
return false;
36+
}
37+
38+
if (pollOptionD && !pollOptionC) {
39+
alert('Option D cannot be used without option C.');
40+
return false;
41+
}
42+
43+
return true;
44+
}
45+
46+
function isAttachmentsInputValid(data) {
47+
const pollAttached =
48+
data.get('pollOptionA') ||
49+
data.get('pollOptionB') ||
50+
data.get('pollOptionC') ||
51+
data.get('pollOptionD');
52+
const linkAttached = data.get('linkAttachment');
53+
const hasMediaAttachment = data.has('attachmentType[]');
54+
55+
if (pollAttached && linkAttached) {
56+
alert('Link attachments and poll attachments cannot be used together.');
57+
return false;
58+
}
59+
60+
if (hasMediaAttachment && linkAttached) {
61+
alert('Link attachments can only be used with text posts.')
62+
}
63+
64+
if (hasMediaAttachment && pollAttached) {
65+
alert('Poll attachments can only be used with text posts.')
66+
}
67+
68+
return true;
69+
}
70+
71+
function isFormDataValid(data) {
72+
if (!isPollInputValid(data) || !isAttachmentsInputValid(data)) {
73+
return false;
74+
}
75+
76+
return true;
77+
}
78+
2679
async function processFormAsync(urlGenerator) {
2780
const form = document.getElementById('form');
2881
form.addEventListener('submit', async (e) => {
@@ -31,6 +84,10 @@ async function processFormAsync(urlGenerator) {
3184
const button = document.getElementById('submit');
3285
const formData = new FormData(e.target, button);
3386

87+
if (!isFormDataValid(formData)) {
88+
return;
89+
}
90+
3491
onAsyncRequestStarting();
3592

3693
let id;

public/scripts/upload.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,17 @@ document.addEventListener('DOMContentLoaded', async () => {
5757
});
5858

5959
await updateMediaType(0, null);
60+
61+
const attachPollButton = document.getElementById('poll-attachment-button');
62+
attachPollButton.addEventListener('click', async (e) => {
63+
e.preventDefault();
64+
65+
const pollAttachmentOptions = document.getElementById('poll-attachment-options');
66+
if (pollAttachmentOptions.style.display === 'none') {
67+
pollAttachmentOptions.style.display = 'block';
68+
} else {
69+
pollAttachmentOptions.style.display = 'none';
70+
}
71+
72+
});
6073
});

src/index.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const FIELD__LINK_ATTACHMENT_URL = 'link_attachment_url';
3131
const FIELD__MEDIA_TYPE = 'media_type';
3232
const FIELD__MEDIA_URL = 'media_url';
3333
const FIELD__PERMALINK = 'permalink';
34+
const FIELD__POLL_ATTACHMENT = 'poll_attachment';
3435
const FIELD__REPLIES = 'replies';
3536
const FIELD__REPOSTS = 'reposts';
3637
const FIELD__QUOTES = 'quotes';
@@ -56,6 +57,7 @@ const PARAMS__FIELDS = 'fields';
5657
const PARAMS__HIDE = 'hide';
5758
const PARAMS__LINK_ATTACHMENT = 'link_attachment';
5859
const PARAMS__METRIC = 'metric';
60+
const PARAMS__POLL_ATTACHMENT = 'poll_attachment';
5961
const PARAMS__Q = 'q';
6062
const PARAMS__QUOTA_USAGE = 'quota_usage';
6163
const PARAMS__QUOTE_POST_ID = 'quote_post_id';
@@ -333,14 +335,38 @@ app.post('/repost', upload.array(), async (req, res) => {
333335
});
334336

335337
app.post('/upload', upload.array(), async (req, res) => {
336-
const { text, attachmentType, attachmentUrl, attachmentAltText, replyControl, replyToId, linkAttachment, quotePostId } = req.body;
338+
const {
339+
text,
340+
attachmentType,
341+
attachmentUrl,
342+
attachmentAltText,
343+
replyControl,
344+
replyToId,
345+
linkAttachment,
346+
pollOptionA,
347+
pollOptionB,
348+
pollOptionC,
349+
pollOptionD,
350+
quotePostId
351+
} = req.body;
352+
337353
const params = {
338354
[PARAMS__TEXT]: text,
339355
[PARAMS__REPLY_CONTROL]: replyControl,
340356
[PARAMS__REPLY_TO_ID]: replyToId,
341357
[PARAMS__LINK_ATTACHMENT]: linkAttachment,
342358
};
343359

360+
if (pollOptionA && pollOptionB) {
361+
const pollAttachment = JSON.stringify({
362+
option_a: pollOptionA,
363+
option_b: pollOptionB,
364+
option_c: pollOptionC,
365+
option_d: pollOptionD,
366+
});
367+
params[PARAMS__POLL_ATTACHMENT] = pollAttachment;
368+
}
369+
344370
if (quotePostId) {
345371
params[PARAMS__QUOTE_POST_ID] = quotePostId;
346372
}
@@ -473,12 +499,21 @@ app.get('/threads/:threadId', loggedInUserChecker, async (req, res) => {
473499
FIELD__REPLY_AUDIENCE,
474500
FIELD__ALT_TEXT,
475501
FIELD__LINK_ATTACHMENT_URL,
502+
FIELD__POLL_ATTACHMENT,
476503
].join(','),
477504
}, req.session.access_token);
478505

479506
try {
480507
const queryResponse = await axios.get(queryThreadUrl, { httpsAgent: agent });
481-
data = queryResponse.data;
508+
const { poll_attachment, ...rest } = queryResponse.data;
509+
data = rest;
510+
511+
if (poll_attachment) {
512+
data = {
513+
...data,
514+
...poll_attachment
515+
};
516+
}
482517
} catch (e) {
483518
console.error(e?.response?.data?.error?.message ?? e.message);
484519
}

views/thread.pug

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ block content
2626
tr
2727
td Link Attachment URL
2828
td #{link_attachment_url}
29+
tr
30+
td Poll
31+
td
32+
if option_a && option_b
33+
table
34+
tr
35+
td Option A
36+
td #{option_a}
37+
tr
38+
td Option B
39+
td #{option_b}
40+
tr
41+
td Option C
42+
td #{option_c}
43+
tr
44+
td Option D
45+
td #{option_d}
2946
tr
3047
td Permalink
3148
td

views/upload.pug

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,28 @@ block content
7575
| Link Attachment
7676
input#link-attachment(type='text' name='linkAttachment' value='')
7777

78+
button#poll-attachment-button(type='button' name='pollAttachment') Attach Poll 🗳️
79+
div#poll-attachment-options(style='display:none')
80+
div.poll-attachment-option
81+
label(for='pollOptionA')
82+
| Option A   
83+
input(type='text' name='pollOptionA' autocomplete='off')
84+
85+
div.poll-attachment-option
86+
label(for='pollOptionB')
87+
| Option B   
88+
input(type='text' name='pollOptionB' autocomplete='off')
89+
90+
div.poll-attachment-option
91+
label(for='pollOptionC')
92+
| Option C   
93+
input(type='text' name='pollOptionC' autocomplete='off')
94+
95+
div.poll-attachment-option
96+
label(for='pollOptionD')
97+
| Option D   
98+
input(type='text' name='pollOptionD' autocomplete='off')
99+
78100
if quotePostId
79101
a(href=`/threads/${quotePostId}`) Quoting #{quotePostId}
80102
input#quote-post-id(type='hidden' name='quotePostId' value=quotePostId)

0 commit comments

Comments
 (0)