Skip to content

Support poll attachments #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions public/scripts/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,59 @@ function onAsyncRequestEnded() {
}
}

function isPollInputValid(data) {
const pollOptionA = data.get('pollOptionA');
const pollOptionB = data.get('pollOptionB');
const pollOptionC = data.get('pollOptionC');
const pollOptionD = data.get('pollOptionD');
const pollAttached = pollOptionA || pollOptionB || pollOptionC || pollOptionD;

if (pollAttached && (!pollOptionA || !pollOptionB)) {
alert('Options A and B are required for polls.');
return false;
}

if (pollOptionD && !pollOptionC) {
alert('Option D cannot be used without option C.');
return false;
}

return true;
}

function isAttachmentsInputValid(data) {
const pollAttached =
data.get('pollOptionA') ||
data.get('pollOptionB') ||
data.get('pollOptionC') ||
data.get('pollOptionD');
const linkAttached = data.get('linkAttachment');
const hasMediaAttachment = data.has('attachmentType[]');

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

if (hasMediaAttachment && linkAttached) {
alert('Link attachments can only be used with text posts.')
}

if (hasMediaAttachment && pollAttached) {
alert('Poll attachments can only be used with text posts.')
}

return true;
}

function isFormDataValid(data) {
if (!isPollInputValid(data) || !isAttachmentsInputValid(data)) {
return false;
}

return true;
}

async function processFormAsync(urlGenerator) {
const form = document.getElementById('form');
form.addEventListener('submit', async (e) => {
Expand All @@ -31,6 +84,10 @@ async function processFormAsync(urlGenerator) {
const button = document.getElementById('submit');
const formData = new FormData(e.target, button);

if (!isFormDataValid(formData)) {
return;
}

onAsyncRequestStarting();

let id;
Expand Down
13 changes: 13 additions & 0 deletions public/scripts/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ document.addEventListener('DOMContentLoaded', async () => {
});

await updateMediaType(0, null);

const attachPollButton = document.getElementById('poll-attachment-button');
attachPollButton.addEventListener('click', async (e) => {
e.preventDefault();

const pollAttachmentOptions = document.getElementById('poll-attachment-options');
if (pollAttachmentOptions.style.display === 'none') {
pollAttachmentOptions.style.display = 'block';
} else {
pollAttachmentOptions.style.display = 'none';
}

});
});
39 changes: 37 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const FIELD__LINK_ATTACHMENT_URL = 'link_attachment_url';
const FIELD__MEDIA_TYPE = 'media_type';
const FIELD__MEDIA_URL = 'media_url';
const FIELD__PERMALINK = 'permalink';
const FIELD__POLL_ATTACHMENT = 'poll_attachment';
const FIELD__REPLIES = 'replies';
const FIELD__REPOSTS = 'reposts';
const FIELD__QUOTES = 'quotes';
Expand All @@ -56,6 +57,7 @@ const PARAMS__FIELDS = 'fields';
const PARAMS__HIDE = 'hide';
const PARAMS__LINK_ATTACHMENT = 'link_attachment';
const PARAMS__METRIC = 'metric';
const PARAMS__POLL_ATTACHMENT = 'poll_attachment';
const PARAMS__Q = 'q';
const PARAMS__QUOTA_USAGE = 'quota_usage';
const PARAMS__QUOTE_POST_ID = 'quote_post_id';
Expand Down Expand Up @@ -333,14 +335,38 @@ app.post('/repost', upload.array(), async (req, res) => {
});

app.post('/upload', upload.array(), async (req, res) => {
const { text, attachmentType, attachmentUrl, attachmentAltText, replyControl, replyToId, linkAttachment, quotePostId } = req.body;
const {
text,
attachmentType,
attachmentUrl,
attachmentAltText,
replyControl,
replyToId,
linkAttachment,
pollOptionA,
pollOptionB,
pollOptionC,
pollOptionD,
quotePostId
} = req.body;

const params = {
[PARAMS__TEXT]: text,
[PARAMS__REPLY_CONTROL]: replyControl,
[PARAMS__REPLY_TO_ID]: replyToId,
[PARAMS__LINK_ATTACHMENT]: linkAttachment,
};

if (pollOptionA && pollOptionB) {
const pollAttachment = JSON.stringify({
option_a: pollOptionA,
option_b: pollOptionB,
option_c: pollOptionC,
option_d: pollOptionD,
});
params[PARAMS__POLL_ATTACHMENT] = pollAttachment;
}

if (quotePostId) {
params[PARAMS__QUOTE_POST_ID] = quotePostId;
}
Expand Down Expand Up @@ -473,12 +499,21 @@ app.get('/threads/:threadId', loggedInUserChecker, async (req, res) => {
FIELD__REPLY_AUDIENCE,
FIELD__ALT_TEXT,
FIELD__LINK_ATTACHMENT_URL,
FIELD__POLL_ATTACHMENT,
].join(','),
}, req.session.access_token);

try {
const queryResponse = await axios.get(queryThreadUrl, { httpsAgent: agent });
data = queryResponse.data;
const { poll_attachment, ...rest } = queryResponse.data;
data = rest;

if (poll_attachment) {
data = {
...data,
...poll_attachment
};
}
} catch (e) {
console.error(e?.response?.data?.error?.message ?? e.message);
}
Expand Down
17 changes: 17 additions & 0 deletions views/thread.pug
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ block content
tr
td Link Attachment URL
td #{link_attachment_url}
tr
td Poll
td
if option_a && option_b
table
tr
td Option A
td #{option_a}
tr
td Option B
td #{option_b}
tr
td Option C
td #{option_c}
tr
td Option D
td #{option_d}
tr
td Permalink
td
Expand Down
22 changes: 22 additions & 0 deletions views/upload.pug
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ block content
| Link Attachment
input#link-attachment(type='text' name='linkAttachment' value='')

button#poll-attachment-button(type='button' name='pollAttachment') Attach Poll 🗳️
div#poll-attachment-options(style='display:none')
div.poll-attachment-option
label(for='pollOptionA')
| Option A   
input(type='text' name='pollOptionA' autocomplete='off')

div.poll-attachment-option
label(for='pollOptionB')
| Option B   
input(type='text' name='pollOptionB' autocomplete='off')

div.poll-attachment-option
label(for='pollOptionC')
| Option C   
input(type='text' name='pollOptionC' autocomplete='off')

div.poll-attachment-option
label(for='pollOptionD')
| Option D   
input(type='text' name='pollOptionD' autocomplete='off')

if quotePostId
a(href=`/threads/${quotePostId}`) Quoting #{quotePostId}
input#quote-post-id(type='hidden' name='quotePostId' value=quotePostId)
Expand Down