feature: forum posts working
This commit is contained in:
parent
7e4ab72fe6
commit
778d1b3aef
13 changed files with 352 additions and 77 deletions
|
|
@ -1,8 +1,128 @@
|
|||
<style>
|
||||
.forum-container {
|
||||
padding: 2rem;
|
||||
#forum-posts.container {
|
||||
padding: 2rem 0.5rem;
|
||||
}
|
||||
|
||||
.post-container {
|
||||
position: relative;
|
||||
display: grid;
|
||||
grid-template-rows: auto auto 1fr;
|
||||
grid-template-columns: auto auto 1fr;
|
||||
grid-template-areas:
|
||||
"expander preview info"
|
||||
"expander preview subject"
|
||||
"expander preview content";
|
||||
max-height: 6rem;
|
||||
padding: 1rem;
|
||||
border: 1px solid var(--border-subtle);
|
||||
overflow-y: hidden;
|
||||
transition: all ease-in-out 0.33s;
|
||||
margin-bottom: 2rem;
|
||||
}
|
||||
|
||||
.post-container:has(input[name="expanded"]:checked) {
|
||||
max-height: 40rem;
|
||||
overflow-y: scroll;
|
||||
}
|
||||
|
||||
.expand-toggle-container input[name="expanded"] {
|
||||
display: none;
|
||||
visibility: hidden;
|
||||
opacity: 0;
|
||||
}
|
||||
.post-container:has(input[name="expanded"]) .icon.minus,
|
||||
.post-container:has(input[name="expanded"]:checked) .icon.plus {
|
||||
display: block;
|
||||
opacity: 1;
|
||||
visibility: visible;
|
||||
}
|
||||
|
||||
.post-container:has(input[name="expanded"]) .icon.plus,
|
||||
.post-container:has(input[name="expanded"]:checked) .icon.minus {
|
||||
display: none;
|
||||
opacity: 0;
|
||||
visibility: hidden;
|
||||
}
|
||||
|
||||
.post-container .expand-toggle-container {
|
||||
grid-area: expander;
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
|
||||
.post-container .expand-toggle-container label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
width: 22px;
|
||||
height: 22px;
|
||||
}
|
||||
|
||||
.post-container .media-preview-container {
|
||||
grid-area: preview;
|
||||
width: 6rem;
|
||||
height: 4rem;
|
||||
margin-right: 1rem;
|
||||
}
|
||||
|
||||
.post-container .media-preview-container img {
|
||||
max-width: 100%;
|
||||
max-height: 100%;
|
||||
}
|
||||
|
||||
.post-container .info-container {
|
||||
grid-area: info;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.post-container .info-container .avatar-container {
|
||||
display: block;
|
||||
margin: 0.5rem;
|
||||
width: 3rem;
|
||||
height: 3rem;
|
||||
border-radius: 16%;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.post-container .info-container .avatar-container img {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.post-container .info-container .username-container {
|
||||
display: block;
|
||||
margin: 0 0.5rem;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.post-container .info-container .datetime-container {
|
||||
display: block;
|
||||
margin: 0 0.5rem;
|
||||
}
|
||||
|
||||
.post-container .info-container .datetime-container .long {
|
||||
font-size: x-small;
|
||||
text-transform: uppercase;
|
||||
visibility: hidden;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.post-container .info-container .datetime-container .short {
|
||||
font-size: xx-small;
|
||||
}
|
||||
|
||||
.post-container .subject-container {
|
||||
grid-area: subject;
|
||||
padding-left: 5rem;
|
||||
margin-top: -2.5rem;
|
||||
font-size: larger;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.post-container .content-container {
|
||||
grid-area: content;
|
||||
padding-left: 5rem;
|
||||
margin-top: 2rem;
|
||||
}
|
||||
</style>
|
||||
|
||||
<div id="forum" class="tab">
|
||||
<input
|
||||
type="radio"
|
||||
|
|
@ -11,77 +131,186 @@
|
|||
class="tab-switch"
|
||||
data-view="forum"
|
||||
/>
|
||||
<label for="forum-tab-input" class="tab-label mockup"
|
||||
<label for="forum-tab-input" class="tab-label"
|
||||
><div class="icon forum"></div>
|
||||
<div class="label">Forum</div></label
|
||||
>
|
||||
<div class="tab-content forum-container">
|
||||
<div id="forum-thread-list"></div>
|
||||
<div id="forum-posts" class="container">
|
||||
<div id="forum-posts-list"></div>
|
||||
<script>
|
||||
const forum_posts_list = document.getElementById("forum-posts-list");
|
||||
|
||||
<div id="thread-creation-container" data-requires-permission="topics.threads.write">
|
||||
async function render_post(post) {
|
||||
const creator = await USERS.get(post.creator_id);
|
||||
const existing_element =
|
||||
document.getElementById(`post-${post.id.substring(0, 49)}`) ??
|
||||
document.getElementById(`post-${post.meta.temp_id}`);
|
||||
const post_datetime = datetime_to_local(post.timestamps.created);
|
||||
|
||||
const html_content = `<div class="post-container" data-creator_id="${creator.id}">
|
||||
<div class="expand-toggle-container">
|
||||
<label>
|
||||
<input type="checkbox" name="expanded"/><i class="icon plus"></i><i class="icon minus"></i>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="media-preview-container">
|
||||
<img src="/images/placeholders/${String(post_datetime.ms % 10).padStart(2, "0")}.svg" />
|
||||
</div>
|
||||
|
||||
<div class="info-container">
|
||||
<div class="avatar-container">
|
||||
<img src="${creator.meta?.avatar ?? "/images/default_avatar.gif"}" alt="user avatar" />
|
||||
</div>
|
||||
<div class="username-container">
|
||||
<span class="username">${creator.username}</span>
|
||||
</div>
|
||||
<div class="datetime-container">
|
||||
<span class="long">${post_datetime.long}</span>
|
||||
<span class="short">${post_datetime.short}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="subject-container">${post.data.subject}</div>
|
||||
<div class="content-container">${htmlify(post.data.content)}</div>
|
||||
</div>`;
|
||||
|
||||
if (existing_element) {
|
||||
const template = document.createElement("template");
|
||||
template.innerHTML = html_content;
|
||||
existing_element.replaceWith(template.content.firstChild);
|
||||
} else {
|
||||
forum_posts_list.insertAdjacentHTML("beforeend", html_content);
|
||||
}
|
||||
}
|
||||
|
||||
async function append_posts(posts) {
|
||||
let last_post_id = forum_posts_list.dataset.last_post_id ?? "";
|
||||
for await (const post of posts) {
|
||||
// if the last post is undefined, it becomes this event, otherwise, if this event's id is newer,
|
||||
// it becomes the latest post
|
||||
last_post_id =
|
||||
post.id > last_post_id && post.id.indexOf("TEMP") !== 0
|
||||
? post.id
|
||||
: last_post_id;
|
||||
|
||||
// if the last post has been updated, update the content's dataset to reflect that
|
||||
if (last_post_id !== forum_posts_list.dataset.last_post_id) {
|
||||
forum_posts_list.dataset.last_post_id = last_post_id;
|
||||
}
|
||||
|
||||
await render_post(post);
|
||||
}
|
||||
|
||||
forum_posts_list.scrollTop = 0;
|
||||
}
|
||||
|
||||
// TODO: we need some abortcontroller handling here or something
|
||||
// similar for when we change topics, this is the most basic
|
||||
// first pass outline
|
||||
let post_polling_abort_controller = null;
|
||||
async function poll_for_new_posts() {
|
||||
const forum_posts_list = document.getElementById("forum-posts-list");
|
||||
const topic_id = document.body.dataset.topic;
|
||||
const last_post_id = forum_posts_list.dataset.last_post_id;
|
||||
|
||||
if (!topic_id) {
|
||||
return;
|
||||
}
|
||||
|
||||
const message_polling_url = `/api/topics/${topic_id}/events?type=post&limit=100&sort=newest&wait=true${last_post_id ? `&after_id=${last_post_id}` : ""}`;
|
||||
|
||||
post_polling_abort_controller =
|
||||
post_polling_abort_controller || new AbortController();
|
||||
|
||||
api.fetch(message_polling_url, {
|
||||
signal: post_polling_abort_controller.signal,
|
||||
})
|
||||
.then(async (new_events_response) => {
|
||||
const new_events = (await new_events_response.json()) ?? [];
|
||||
await append_posts(new_events);
|
||||
poll_for_new_posts(topic_id);
|
||||
})
|
||||
.catch((error) => {
|
||||
// TODO: poll again? back off?
|
||||
console.error(error);
|
||||
});
|
||||
}
|
||||
|
||||
async function load_active_topic_for_forum() {
|
||||
const topic_id = document.body.dataset.topic;
|
||||
if (!topic_id) return;
|
||||
|
||||
const user = document.body.dataset.user
|
||||
? JSON.parse(document.body.dataset.user)
|
||||
: null;
|
||||
if (!user) return;
|
||||
|
||||
const forum_posts_list = document.getElementById("forum-posts-list");
|
||||
|
||||
if (post_polling_abort_controller) {
|
||||
post_polling_abort_controller.abort();
|
||||
post_polling_abort_controller = null;
|
||||
delete forum_posts_list.dataset.last_post_id;
|
||||
}
|
||||
|
||||
const topic_response = await api.fetch(`/api/topics/${topic_id}`);
|
||||
if (!topic_response.ok) {
|
||||
const error = await topic_response.json();
|
||||
alert(error.message ?? JSON.stringify(error));
|
||||
return;
|
||||
}
|
||||
|
||||
const topic = await topic_response.json();
|
||||
|
||||
forum_posts_list.innerHTML = "";
|
||||
|
||||
const events_response = await api.fetch(
|
||||
`/api/topics/${topic_id}/events?type=post&limit=100&sort=newest`,
|
||||
);
|
||||
if (!events_response.ok) {
|
||||
const error = await events_response.json();
|
||||
alert(error.message ?? JSON.stringify(error));
|
||||
return;
|
||||
}
|
||||
|
||||
const events = await events_response.json();
|
||||
|
||||
await append_posts(events);
|
||||
poll_for_new_posts();
|
||||
}
|
||||
document.addEventListener("topic_changed", load_active_topic_for_forum);
|
||||
document.addEventListener("user_logged_in", load_active_topic_for_forum);
|
||||
</script>
|
||||
</div>
|
||||
|
||||
<div id="post-creation-container" data-requires-permission="topics.posts.create">
|
||||
<button
|
||||
onclick="(() => { document.querySelector( '#thread-creation' ).classList.toggle( 'collapsed' ); })()"
|
||||
class="mockup"
|
||||
onclick="(() => { document.querySelector( '#post-creation' ).classList.toggle( 'collapsed' ); })()"
|
||||
>
|
||||
<i class="icon plus" style="display: inline-block; margin-right: 1rem"></i>New
|
||||
Thread
|
||||
<i class="icon plus" style="display: inline-block; margin-right: 1rem"></i>New Post
|
||||
</button>
|
||||
<form
|
||||
id="thread-creation"
|
||||
id="post-creation"
|
||||
data-smart="true"
|
||||
action="/api/topics"
|
||||
method="POST"
|
||||
class="collapsed mockup"
|
||||
class="collapsed"
|
||||
style="
|
||||
margin-top: 1rem;
|
||||
margin-top: 1rem
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
overflow: hidden;
|
||||
transition: all 0.5s;
|
||||
"
|
||||
>
|
||||
<input
|
||||
id="new-thread-subject"
|
||||
type="text"
|
||||
name="data.subject"
|
||||
value=""
|
||||
placeholder="Thread subject..."
|
||||
style="margin-bottom: 1rem"
|
||||
class="mockup"
|
||||
/>
|
||||
|
||||
<input
|
||||
id="file-upload-and-share-input-for-thread"
|
||||
aria-label="Upload and share file"
|
||||
type="file"
|
||||
multiple
|
||||
/>
|
||||
<label for="file-upload-and-share-input-for-thread" class="mockup">
|
||||
<div class="icon attachment"></div>
|
||||
</label>
|
||||
|
||||
<textarea
|
||||
id="new-thread-content"
|
||||
type="text"
|
||||
name="data.content"
|
||||
value=""
|
||||
placeholder=" ... "
|
||||
class="mockup"
|
||||
></textarea>
|
||||
|
||||
<input type="submit" hidden />
|
||||
<script>
|
||||
{
|
||||
const form = document.currentScript.closest("form");
|
||||
const file_input = document.querySelector('input[type="file"]');
|
||||
const subject_input = document.getElementById('input[name="subject"]');
|
||||
const content_input = document.getElementById('input[name="content"]');
|
||||
const form = document.currentScript.closest("form");
|
||||
|
||||
const parent_id_input = document.getElementById("parent-id");
|
||||
|
||||
const forum_thread_list = document.getElementById("forum-thread-list");
|
||||
|
||||
let threads_in_flight = {};
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
const file_input = form.querySelector('input[type="file"]');
|
||||
const subject_input = form.querySelector('[name="data.subject"]');
|
||||
const content_input = form.querySelector('[name="data.content"]');
|
||||
const parent_id_input = form.querySelector('[name="parent_id"]');
|
||||
|
||||
form.on_submit = async (event) => {
|
||||
const user = JSON.parse(document.body.dataset.user);
|
||||
|
|
@ -120,8 +349,10 @@
|
|||
return false;
|
||||
}
|
||||
|
||||
const message = chat_input.value.trim();
|
||||
if (form.uploaded_urls.length === 0 && message.length === 0) {
|
||||
const subject = subject_input.value.trim();
|
||||
const content = content_input.value.trim();
|
||||
|
||||
if (subject.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -133,7 +364,7 @@
|
|||
|
||||
const temp_id = `TEMP-${now}`;
|
||||
json.id = temp_id;
|
||||
json.type = "chat";
|
||||
json.type = "post";
|
||||
json.meta = {
|
||||
temp_id,
|
||||
};
|
||||
|
|
@ -141,9 +372,9 @@
|
|||
created: now,
|
||||
updated: now,
|
||||
};
|
||||
json.data = json.data ?? {};
|
||||
|
||||
if (form.uploaded_urls.length) {
|
||||
json.data = json.data ?? {};
|
||||
json.data.content =
|
||||
(typeof json.data.content === "string" &&
|
||||
json.data.content.trim().length
|
||||
|
|
@ -151,29 +382,58 @@
|
|||
: "") + form.uploaded_urls.join("\n");
|
||||
}
|
||||
|
||||
const user = JSON.parse(document.body.dataset.user);
|
||||
render_text_event(topic_chat_content, json, user);
|
||||
document.getElementById(`chat-${temp_id}`)?.classList.add("sending");
|
||||
render_post(json);
|
||||
document.getElementById(`post-${temp_id}`)?.classList.add("sending");
|
||||
};
|
||||
|
||||
form.on_error = (error) => {
|
||||
// TODO: mark the temporary message element with the failed class?
|
||||
alert(error);
|
||||
chat_input.focus();
|
||||
content_input.focus();
|
||||
};
|
||||
|
||||
form.on_reply = (sent_message) => {
|
||||
document
|
||||
.getElementById(`chat-${sent_message.meta?.temp_id ?? ""}`)
|
||||
?.classList.remove("sending");
|
||||
form.on_reply = (post) => {
|
||||
const post_id = `post-${post.meta?.temp_id ?? ""}`;
|
||||
document.getElementById(post_id)?.classList.remove("sending");
|
||||
|
||||
append_topic_events([sent_message]);
|
||||
append_posts([post]);
|
||||
parent_id_input.value = "";
|
||||
chat_input.value = "";
|
||||
chat_input.focus();
|
||||
subject_input.value = "";
|
||||
content_input.value = "";
|
||||
};
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<input type="hidden" name="parent_id" value="" />
|
||||
|
||||
<input
|
||||
id="new-thread-subject"
|
||||
type="text"
|
||||
name="data.subject"
|
||||
value=""
|
||||
placeholder="Thread subject..."
|
||||
style="margin-bottom: 1rem"
|
||||
/>
|
||||
|
||||
<input
|
||||
id="file-upload-and-share-input-for-thread"
|
||||
aria-label="Upload and share file"
|
||||
type="file"
|
||||
multiple
|
||||
/>
|
||||
<label for="file-upload-and-share-input-for-thread">
|
||||
<div class="icon attachment"></div>
|
||||
</label>
|
||||
|
||||
<textarea
|
||||
id="new-thread-content"
|
||||
type="text"
|
||||
name="data.content"
|
||||
value=""
|
||||
placeholder=" ... "
|
||||
></textarea>
|
||||
|
||||
<input type="submit" value="Post" />
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue