Skip to content

Commit 44df04d

Browse files
authored
feat: Add support to create Ghost Posts (#67)
* feat: Add support to create Ghost Posts * add image, code comments
1 parent 65019e8 commit 44df04d

File tree

5 files changed

+121
-26
lines changed

5 files changed

+121
-26
lines changed

public/img/ghost_post.png

8.48 KB
Loading

public/scripts/upload.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@
88
async function updateMediaType(attachmentsCount, attachmentListElem) {
99
const mediaTypeElem = document.getElementById('media-type');
1010
const spoilerMediaControl = document.querySelector('.spoiler-media-control');
11+
const ghostPostMediaControl = document.querySelector('.ghost-post-media-control');
1112

1213
let mediaTypeDesc;
1314
if (attachmentsCount === 0) {
1415
mediaTypeDesc = 'Text 📝';
1516
spoilerMediaControl.style.display = 'none';
17+
ghostPostMediaControl.style.display = 'block';
1618
} else if (attachmentsCount === 1) {
1719
const singleAttachmentType =
1820
attachmentListElem.querySelector('select').value;
1921
if (singleAttachmentType === 'Image') mediaTypeDesc = 'Image 🖼️';
2022
else mediaTypeDesc = 'Video 🎬';
2123
spoilerMediaControl.style.display = 'block';
24+
ghostPostMediaControl.style.display = 'none';
2225
} else {
2326
mediaTypeDesc = 'Carousel 🎠';
2427
spoilerMediaControl.style.display = 'block';
28+
ghostPostMediaControl.style.display = 'none';
2529
}
2630

2731
mediaTypeElem.innerText = mediaTypeDesc;
@@ -72,10 +76,56 @@ document.addEventListener('DOMContentLoaded', async () => {
7276
const pollAttachmentOptions = document.getElementById(
7377
'poll-attachment-options'
7478
);
79+
const ghostPostMediaControl = document.querySelector('.ghost-post-media-control');
7580
if (pollAttachmentOptions.style.display === 'none') {
7681
pollAttachmentOptions.style.display = 'block';
82+
ghostPostMediaControl.style.display = 'none';
7783
} else {
7884
pollAttachmentOptions.style.display = 'none';
85+
ghostPostMediaControl.style.display = 'block';
7986
}
8087
});
88+
89+
const ghostPostCheckbox = document.querySelector('input[name="ghostPostMedia"]');
90+
const topicTagMediaControl = document.querySelector('.topic-tag-media-control');
91+
const linkAttachmentMediaControl = document.querySelector('.link-attachment-media-control');
92+
const attachImageMediaControl = document.querySelector('.attach-image-media-control');
93+
const replyOptionsMediaControl = document.querySelector('.reply-options-media-control');
94+
95+
const topicTagInput = document.getElementById('topic-tag');
96+
const linkAttachmentInput = document.getElementById('link-attachment');
97+
const replyControlSelect = document.getElementById('reply-control');
98+
const attachmentsList = document.getElementById('attachments-list');
99+
const pollAttachmentOptions = document.getElementById('poll-attachment-options');
100+
101+
if (ghostPostCheckbox) {
102+
ghostPostCheckbox.addEventListener('change', async () => {
103+
// When ghost post is checked, disable fields that are not relevant to ghost posts
104+
if (ghostPostCheckbox.checked) {
105+
topicTagMediaControl.style.display = 'none';
106+
linkAttachmentMediaControl.style.display = 'none';
107+
attachPollButton.style.display = 'none';
108+
attachImageMediaControl.style.display = 'none';
109+
replyOptionsMediaControl.style.display = 'none';
110+
111+
if (topicTagInput) topicTagInput.value = '';
112+
if (linkAttachmentInput) linkAttachmentInput.value = '';
113+
if (replyControlSelect) replyControlSelect.value = '';
114+
if (attachmentsList) attachmentsList.innerHTML = '';
115+
if (pollAttachmentOptions) {
116+
pollAttachmentOptions.style.display = 'none';
117+
const pollInputs = pollAttachmentOptions.querySelectorAll('input[type="text"]');
118+
pollInputs.forEach(input => input.value = '');
119+
}
120+
121+
await updateMediaType(0, null);
122+
} else { // When the checkbox is unchecked, enable all fields
123+
topicTagMediaControl.style.display = 'block';
124+
linkAttachmentMediaControl.style.display = 'block';
125+
attachPollButton.style.display = 'block';
126+
attachImageMediaControl.style.display = 'block';
127+
replyOptionsMediaControl.style.display = 'block';
128+
}
129+
});
130+
}
81131
});

src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const FIELD__USERNAME = 'username';
6262
const FIELD__VIEWS = 'views';
6363
const FIELD_IS_SPOILER_MEDIA = 'is_spoiler_media';
6464
const FIELD_TEXT_ENTITES = 'text_entities';
65+
const FIELD_GHOST_POST_STATUS = 'ghost_post_status';
6566

6667
const MEDIA_TYPE__CAROUSEL = 'CAROUSEL';
6768
const MEDIA_TYPE__IMAGE = 'IMAGE';
@@ -100,6 +101,7 @@ const PARAMS__TOPIC_TAG = 'topic_tag';
100101
const PARAMS__USERNAME = 'username';
101102
const PARAMS_IS_SPOILER_MEDIA = 'is_spoiler_media';
102103
const PARAMS_TEXT_ENTITES = 'text_entities';
104+
const PARAMS_IS_GHOST_POST = 'is_ghost_post';
103105

104106
// Read variables from environment
105107
require('dotenv').config();
@@ -449,6 +451,7 @@ app.post('/upload', upload.array(), async (req, res) => {
449451
pollOptionD,
450452
quotePostId,
451453
spoilerMedia,
454+
ghostPostMedia,
452455
} = req.body;
453456

454457
const params = {
@@ -479,6 +482,10 @@ app.post('/upload', upload.array(), async (req, res) => {
479482
params[PARAMS_IS_SPOILER_MEDIA] = true;
480483
}
481484

485+
if (ghostPostMedia) {
486+
params[PARAMS_IS_GHOST_POST] = true;
487+
}
488+
482489
if (pollOptionA && pollOptionB) {
483490
const pollAttachment = JSON.stringify({
484491
option_a: pollOptionA,
@@ -679,6 +686,7 @@ app.get('/threads/:threadId', loggedInUserChecker, async (req, res) => {
679686
FIELD__REPOSTED_POST,
680687
FIELD_IS_SPOILER_MEDIA,
681688
FIELD_TEXT_ENTITES,
689+
FIELD_GHOST_POST_STATUS,
682690
].join(','),
683691
},
684692
req.session.access_token
@@ -688,7 +696,7 @@ app.get('/threads/:threadId', loggedInUserChecker, async (req, res) => {
688696
const queryResponse = await axios.get(queryThreadUrl, {
689697
httpsAgent: agent,
690698
});
691-
const { poll_attachment, ...rest } = queryResponse.data;
699+
const { poll_attachment, ghost_post_status, ...rest } = queryResponse.data;
692700
data = rest;
693701

694702
if (poll_attachment) {
@@ -697,6 +705,12 @@ app.get('/threads/:threadId', loggedInUserChecker, async (req, res) => {
697705
...poll_attachment,
698706
};
699707
}
708+
709+
const is_ghost_post = ghost_post_status === 'ACTIVE';
710+
data = {
711+
...data,
712+
is_ghost_post,
713+
};
700714
} catch (e) {
701715
console.error(e?.response?.data?.error?.message ?? e.message);
702716
}

views/thread.pug

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ block content
3939
tr
4040
td Is Spoiler Media Post
4141
td #{is_spoiler_media}
42+
tr
43+
td Is Ghost Post
44+
td #{is_ghost_post}
4245
tr
4346
td GIF URL
4447
td

views/upload.pug

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ block content
5858
.spoiler-media-control {
5959
display: none;
6060
}
61+
.ghost-post-media-control {
62+
display: flex;
63+
flex-direction: row;
64+
align-items: center;
65+
}
66+
#ghost-post-img{
67+
width: 25px;
68+
height: 25px;
69+
margin-bottom: 0;
70+
}
6171

6272
if (replyToId !== undefined)
6373
p(style='color: gray') Replying to #{replyToId}
@@ -67,37 +77,55 @@ block content
6777
| Text:
6878
textarea(placeholder='Start a thread...' id='text' name='text' autocomplete='off')
6979
br
70-
| To attach an image or video, click the image below
71-
div.attachments-area
72-
img#attachments-button(src='/img/attachment.png')
73-
div.attachment-controls
74-
div#attachments-list.attachments
75-
div.spoiler-media-control
76-
label(for='spoilerMedia')
77-
| Mark Media as Spoiler   
78-
input(type='checkbox' name='spoilerMedia' value='true')
79-
br
80-
81-
label(for="reply-control") Who Can Reply
82-
select#reply-control(name='replyControl' hint="Reply Control")
83-
option(value="" selected)
84-
option(value="everyone") Everyone
85-
option(value="accounts_you_follow") Accounts You Follow
86-
option(value="mentioned_only") Mentioned Only
87-
option(value="parent_post_author_only") Parent Post Author Only
88-
option(value="followers_only") Followers Only
80+
div.attach-image-media-control
81+
| To attach an image or video, click the image below
82+
div.attachments-area
83+
img#attachments-button(src='/img/attachment.png')
84+
div.attachment-controls
85+
div#attachments-list.attachments
86+
div.spoiler-media-control
87+
label(for='spoilerMedia')
88+
| Mark Media as Spoiler   
89+
input(type='checkbox' name='spoilerMedia' value='true')
90+
br
8991

90-
label(for='topicTag')
91-
| Topic Tag
92-
input#topic-tag(type='text' name='topicTag' value='' maxlength='50')
92+
div.reply-options-media-control
93+
label(for="reply-control") Who Can Reply
94+
br
95+
select#reply-control(name='replyControl' hint="Reply Control")
96+
option(value="" selected)
97+
option(value="everyone") Everyone
98+
option(value="accounts_you_follow") Accounts You Follow
99+
option(value="mentioned_only") Mentioned Only
100+
option(value="parent_post_author_only") Parent Post Author Only
101+
option(value="followers_only") Followers Only
102+
103+
div.topic-tag-media-control
104+
label(for='topicTag')
105+
| Topic Tag
106+
br
107+
input#topic-tag(type='text' name='topicTag' value='' maxlength='50')
108+
br
93109

94-
label(for='linkAttachment')
95-
| Link Attachment
96-
input#link-attachment(type='text' name='linkAttachment' value='')
110+
div.link-attachment-media-control
111+
label(for='linkAttachment')
112+
| Link Attachment
113+
br
114+
input#link-attachment(type='text' name='linkAttachment' value='')
115+
br
97116

98117
label(for="autoPublishText")
99118
input(type="checkbox" name="autoPublishText" id="autoPublishText")
100119
| Auto-Publish (only for text posts)
120+
br
121+
122+
br
123+
div.ghost-post-media-control
124+
input(type='checkbox' name='ghostPostMedia' value='true')
125+
label(for='ghostPostMedia')
126+
| Publish as Ghost Post  
127+
img#ghost-post-img(src='/img/ghost_post.png')
128+
br
101129

102130
button#poll-attachment-button(type='button' name='pollAttachment') Attach Poll 🗳️
103131
div#poll-attachment-options(style='display:none')

0 commit comments

Comments
 (0)