feat: use reply endpoint

This commit is contained in:
ktyl 2024-08-04 19:33:00 +01:00
parent 56c315c301
commit c6bd7d80d6
1 changed files with 33 additions and 18 deletions

43
main.js
View File

@ -258,40 +258,55 @@ function getCurrentUser() {
}; };
} }
function writePost(postCallback) { function getPostRequest(body) {
const request = { return {
method: "POST", method: "POST",
mode: "cors", mode: "cors",
body: JSON.stringify(getCurrentUser()), body: JSON.stringify(body),
headers: { headers: {
"Content-type": "application/json; charset=UTF-8" "Content-type": "application/json; charset=UTF-8"
} }
}; };
}
function writePost() {
if (localMode) { if (localMode) {
const post = new Post({ const post = new Post({
id: "1234", id: "1234",
associatedUser: getCurrentUser().user, associatedUser: getCurrentUser().user,
body: "local mode post (local mode post)" body: "local mode post (local mode post)"
}); });
postCallback(post); blockContainer.insertBefore(post.getElement(), getTopPost());
} else { return;
fetch("https://api.wayfarer.games/singularity/generate-posts.php", request) }
fetch("https://api.wayfarer.games/singularity/generate-posts.php", getPostRequest(getCurrentUser()))
.then(response => response.json()) .then(response => response.json())
.then(makePostFromJson) .then(makePostFromJson)
.then(postCallback); .then(post => blockContainer.insertBefore(post.getElement(), getTopPost()));
}
}
function writeNewPost() {
writePost(post => blockContainer.insertBefore(post.getElement(), getTopPost()));
} }
function writeReply(post) { function writeReply(post) {
// find the correct element // find the correct element
const elem = document.getElementById(post.id); const elem = document.getElementById(post.id);
const user = getCurrentUser();
writePost(reply => { if (localMode) {
console.error("TODO: implement local replies");
return;
}
const replyBody = {
postId: post.id,
interests: user.interests,
user: user.user,
posting_style: user.posting_style
};
const request = getPostRequest(replyBody);
fetch("https://api.wayfarer.games/singularity/generate-reply.php", getPostRequest(replyBody))
.then(response => response.json())
.then(makePostFromJson)
.then(reply => {
post.addReply(reply); post.addReply(reply);
elem.append(reply.getElement()); elem.append(reply.getElement());
}); });
@ -300,7 +315,7 @@ function writeReply(post) {
function addWritePostBlock() { function addWritePostBlock() {
const blockElem = document.createElement("div"); const blockElem = document.createElement("div");
blockElem.addEventListener("click", writeNewPost); blockElem.addEventListener("click", writePost);
blockElem.className = "block write-post"; blockElem.className = "block write-post";
const spanElem = document.createElement("h2"); const spanElem = document.createElement("h2");