import * as MastodonAPI from "/JS/MastodonAPI.js";
import * as BlueskyAPI from "/JS/BlueskyAPI.js";
import * as TumblrAPI from "/JS/TumblrAPI.js";
import * as Variables from "/JS/Variables.js";
// Buttons
let Favorite = document.getElementsByClassName("Favorite")[0];
let Boost = document.getElementsByClassName("Boost")[0];
let Reply = document.getElementsByClassName("Reply")[0];
let FavoriteFlipper = false;
let BoostFlipper = false;
let Favoritable = false;
let Boostable = false;
// Variables
let website = document.location.href.split("website=")[1];
let post = JSON.parse(localStorage.getItem("post"));
let ThreadedPost = [];
document.getElementsByClassName("Origin")[0].innerHTML = website;
GetPost();
// Fixes a bug where the interpreter sucks ass.
let EmbedCounter = 0;
// Button stuff
Favorite.onclick = (event) => {
if (Favoritable == false) {
return;
}
Favoritee();
}
async function Favoritee() {
Favoritable = false;
if (website == "Mastodon") {
await MastodonAPI.CreateFavorite(post.id, post.favourited);
} else if (website == "Bluesky") {
await BlueskyAPI.CreateLike(localStorage.getItem(Variables.BlueskyDID), post.post.uri, post.post.cid);
}
SetFavorite();
}
Boost.onclick = (event) => {
if (Boostable == false) {
return;
}
Boostee();
}
async function Boostee() {
Boostable = false;
if (website == "Mastodon") {
await MastodonAPI.CreateReblog(post.id, post.reblogged);
} else if (website == "Bluesky") {
await BlueskyAPI.CreateRepost(localStorage.getItem(Variables.BlueskyDID), post.post.uri, post.post.cid);
}
SetBoost();
}
Reply.onclick = (event) => {
window.location.href = "/post?website=" + website;
}
// Other post stuff.
for (let i of document.getElementsByClassName("Regular")) {
i.onclick = (event) => {
if (i.classList.contains("Handle")) {
window.location.href = "/account?website=" + website;
}
}
}
// Threads are the thing above the post you clicked.
async function SetThreadPost(element, i) {
if (website == "Mastodon") {
localStorage.setItem("post", JSON.stringify(element));
} else if (website == "Bluesky") {
let Temp = await BlueskyAPI.GetPosts(element.uri);
element.post = Temp.posts[0];
localStorage.setItem("post", JSON.stringify(element));
}
localStorage.setItem("post", JSON.stringify(element));
if (i.classList.contains("Handle")) {
window.location.href = "/account?website=" + website;
} else {
window.location.href = "/expanded?website=" + website;
}
}
// Functions and things.
async function GetPost() {
if (website == "Mastodon") {
document.getElementsByClassName("Handle")[0].innerHTML = post.account.acct;
// Check for a reblog.
if (post.reblog != null) {
document.getElementsByClassName("Handle")[0].innerHTML += " ( R: " + post.reblog + " )";
}
document.getElementsByClassName("PostText")[0].innerHTML = post.content;
// Set the texts. It's opposite because "setting" causes it to switch.
post = await MastodonAPI.GetStatus(post.id);
FavoriteFlipper = !(post.favourited);
BoostFlipper = !(post.reblogged);
SetFavorite();
SetBoost();
// Show the image if it exists.
if (post.media_attachments.length != 0) {
for (let i of post.media_attachments) {
ApplyMedia(i, document.getElementsByClassName("Images")[0]);
}
}
// Now time to see if there are any parents.
let NumberOfThreads = 0;
let TemporaryPost = post;
while (TemporaryPost.in_reply_to_id != null) {
TemporaryPost = await MastodonThreadFunction(NumberOfThreads, TemporaryPost.in_reply_to_id);
ThreadedPost.push(TemporaryPost);
// Any posts? Give them the thing :3
for (let i of document.getElementsByClassName(NumberOfThreads)) {
let TempID = NumberOfThreads;
i.onclick = (event) => {
SetThreadPost(ThreadedPost[TempID], i);
}
}
NumberOfThreads += 1;
}
// Replies, anyone?
let Context = await GetContexts(post.id);
for (let i of Context.descendants) {
TemporaryPost = await MastodonReplyFunction(NumberOfThreads, i);
ThreadedPost.push(TemporaryPost);
// Any posts? Give them the thing :3
for (let j of document.getElementsByClassName(NumberOfThreads)) {
let TempID = NumberOfThreads;
j.onclick = (event) => {
SetThreadPost(ThreadedPost[TempID], j);
}
}
NumberOfThreads += 1;
}
} else if (website == "Bluesky") {
// Check for a reblog.
if (post.hasOwnProperty("reason") && post.reason.$type == "app.bsky.feed.defs#reasonRepost") {
document.getElementsByClassName("Handle")[0].innerHTML = post.post.author.handle + " ( R: " + post.reason.by.handle + " )";
} else {
document.getElementsByClassName("Handle")[0].innerHTML = post.post.author.handle;
}
// Text. This will be modified later.
var Text = await BlueskyAPI.ApplyFacets(post.post.record, post.post.record.text);
// Place the text.
Text = Text.replace(/\r?\n|\r/g, "
");
document.getElementsByClassName("PostText")[0].innerHTML = Text;
// Show the image if it exists.
if (post.post.record.hasOwnProperty("embed")) {
ApplyMedia(post.post.record.embed, document.getElementsByClassName("Images")[0], post.post.author.did);
}
// We don't need to update the post with new information. The repos are seperate.
// Set the texts. It's opposite because "setting" causes it to switch.
let GetRepo = await BlueskyAPI.GetRecord(localStorage.getItem(Variables.BlueskyDID), "app.bsky.feed.like", post.post.uri.split("/")[post.post.uri.split("/").length - 1]);
if (GetRepo.hasOwnProperty("error") && GetRepo.error == "RecordNotFound") {
FavoriteFlipper = true;
}
GetRepo = await BlueskyAPI.GetRecord(localStorage.getItem(Variables.BlueskyDID), "app.bsky.feed.repost", post.post.uri.split("/")[post.post.uri.split("/").length - 1]);
if (GetRepo.hasOwnProperty("error") && GetRepo.error == "RecordNotFound") {
BoostFlipper = true;
}
SetFavorite();
SetBoost();
// Now time to see if there are any parents.
let NumberOfThreads = 0;
let TemporaryPost = post;
while (TemporaryPost.hasOwnProperty("reply")) {
TemporaryPost = await BlueskyThreadFunction(NumberOfThreads, TemporaryPost.reply.parent);
ThreadedPost.push(TemporaryPost);
// Any posts? Give them the thing :3
for (let i of document.getElementsByClassName(NumberOfThreads)) {
let TempID = NumberOfThreads;
i.onclick = (event) => {
SetThreadPost(ThreadedPost[TempID], i);
}
}
NumberOfThreads += 1;
}
} else {
document.getElementsByClassName("PostText")[0].innerHTML = "Nothing to load.";
}
}
// Because of repeat code, we can put it into it's own function. It grabs posts "above" it.
async function MastodonThreadFunction(Id, post) {
let OtherPost = await MastodonAPI.GetStatus(post);
let ShitHTML = " " + OtherPost.account.acct + "
" + website + "
" + Text + "
" + Text + "