import * as MastodonAPI from "./MastodonAPI.js"; import * as BlueskyAPI from "./BlueskyAPI.js"; import * as TumblrAPI from "./TumblrAPI.js"; import * as Variables from "./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; // Variables let website = document.location.href.split("website=")[1]; let post = JSON.parse(localStorage.getItem("post")); let Parentpost; let GrandParentpost; document.getElementsByClassName("Origin Regular")[0].innerHTML = website; GetPost(); // Button stuff Favorite.onclick = (event) => { if (website == "Mastodon") { MastodonAPI.CreateFavorite(post.id, post.favourited); } else if (website == "Bluesky") { BlueskyAPI.CreateLike(localStorage.getItem(Variables.BlueskyDID), post.post.uri, post.post.cid); } SetFavorite(); } Boost.onclick = (event) => { if (website == "Mastodon") { MastodonAPI.CreateReblog(post.id, post.reblogged); } else if (website == "Bluesky") { BlueskyAPI.CreateRepost(localStorage.getItem(Variables.BlueskyDID), post.post.uri, post.post.cid); } SetBoost(); } Reply.onclick = (event) => { window.location.href = "../../HTML/post.html?website=" + website; } // Other post stuff. for (let i of document.getElementsByClassName("Regular")) { i.onclick = (event) => { if (i.classList.contains("Handle")) { window.location.href = "../../HTML/account.html?website=" + website; } } } // Selecting other posts. for (let i of document.getElementsByClassName("Parent")) { i.onclick = (event) => { SetThreadPost(Parentpost); if (i.classList.contains("Handle")) { window.location.href = "../../HTML/account.html?website=" + website; } else { document.location.href = "./expanded.html?website=" + website; } } } for (let i of document.getElementsByClassName("GrandParent")) { i.onclick = (event) => { SetThreadPost(GrandParentpost); if (i.classList.contains("Handle")) { window.location.href = "../../HTML/account.html?website=" + website; } else { document.location.href = "./expanded.html?website=" + website; } } } async function SetThreadPost(element) { 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)); } } // Functions and things. async function GetPost() { if (website == "Mastodon") { // Check for a reblog. if (post.reblog != null) { document.getElementsByClassName("PostText Regular")[0].innerHTML = post.reblog.content; document.getElementsByClassName("Handle Regular")[0].innerHTML = post.reblog.account.username + " ( R: " + post.account.username + " )"; if (post.reblog.media_attachments.length != 0) { for (let i of post.reblog.media_attachments) { ApplyMedia(i, document.getElementsByClassName("Images Regular")[0]); } } } else { document.getElementsByClassName("PostText Regular")[0].innerHTML = post.content; document.getElementsByClassName("Handle Regular")[0].innerHTML = post.account.username; // Show the image if it exists. if (post.media_attachments.length != 0) { for (let i of post.media_attachments) { ApplyMedia(i, document.getElementsByClassName("Images Regular")[0]); } } } // 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(); // Now time to see if there are any parents if (post.in_reply_to_id != null) { Parentpost = await MastodonAPI.GetStatus(post.in_reply_to_id); document.getElementsByClassName("Origin Parent")[0].innerHTML = website; document.getElementsByClassName("Handle Parent")[0].innerHTML = Parentpost.account.username; if (Parentpost.spoiler_text != "") { document.getElementsByClassName("PostText Parent")[0].innerHTML = "WARNING: " + Parentpost.spoiler_text; } else { document.getElementsByClassName("PostText Parent")[0].innerHTML = Parentpost.content; } if (Parentpost.media_attachments.length != 0) { for (let i of Parentpost.media_attachments) { ApplyMedia(i, document.getElementsByClassName("Images Parent")[0]); } } // Now time to see if there are any grandparents if (Parentpost.in_reply_to_id != null) { GrandParentpost = await MastodonAPI.GetStatus(Parentpost.in_reply_to_id); document.getElementsByClassName("Origin GrandParent")[0].innerHTML = website; document.getElementsByClassName("Handle GrandParent")[0].innerHTML = GrandParentpost.account.username; if (GrandParentpost.spoiler_text != "") { document.getElementsByClassName("PostText GrandParent")[0].innerHTML = "WARNING: " + GrandParentpost.spoiler_text; } else { document.getElementsByClassName("PostText GrandParent")[0].innerHTML = GrandParentpost.content; } if (GrandParentpost.media_attachments.length != 0) { for (let i of GrandParentpost.media_attachments) { ApplyMedia(i, document.getElementsByClassName("Images GrandParent")[0]); } } } } } else if (website == "Bluesky") { // Check for a reblog. if (post.hasOwnProperty("reason") && post.reason.$type == "app.bsky.feed.defs#reasonRepost") { document.getElementsByClassName("Handle Regular")[0].innerHTML = post.post.author.handle + " ( R: " + post.reason.by.handle + " )"; } else { document.getElementsByClassName("Handle Regular")[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 Regular")[0].innerHTML = Text; // Show the image if it exists. if (post.post.record.hasOwnProperty("embed")) { ApplyMedia(post.post.record.embed, document.getElementsByClassName("Images Regular")[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. if (post.hasOwnProperty("reply")) { Parentpost = await BlueskyAPI.GetRecord(post.reply.parent.uri.split("/")[2], post.reply.parent.uri.split("/")[3], post.reply.parent.uri.split("/")[4]); document.getElementsByClassName("Origin Parent")[0].innerHTML = website; document.getElementsByClassName("Handle Parent")[0].innerHTML = post.reply.parent.author.handle; Text = BlueskyAPI.ApplyFacets(Parentpost.value, Parentpost.value.text); Text = Text.replace(/\r?\n|\r/g, "
"); // Sensitive topic :3 if (Parentpost.value.hasOwnProperty("labels") && Parentpost.value.labels.length != 0) { document.getElementsByClassName("PostText Parent")[0].innerHTML = "LABELS APPLIED: "; console.log(Parentpost.value.labels); for (let lab of Parentpost.value.labels.values) { document.getElementsByClassName("PostText Parent")[0].innerHTML += lab.val + " "; } } else { document.getElementsByClassName("PostText Parent")[0].innerHTML = Text; if (Parentpost.value.hasOwnProperty("embed")) { ApplyMedia(Parentpost.value.embed, document.getElementsByClassName("Images Parent")[0], post.reply.parent.author.did); } } // Now time to see if there are any grandparents. if (Parentpost.value.hasOwnProperty("reply")) { GrandParentpost = await BlueskyAPI.GetRecord(Parentpost.value.reply.parent.uri.split("/")[2], Parentpost.value.reply.parent.uri.split("/")[3], Parentpost.value.reply.parent.uri.split("/")[4]); document.getElementsByClassName("Origin GrandParent")[0].innerHTML = website; document.getElementsByClassName("Handle GrandParent")[0].innerHTML = post.reply.grandparentAuthor.handle; Text = BlueskyAPI.ApplyFacets(GrandParentpost.value, GrandParentpost.value.text); Text = Text.replace(/\r?\n|\r/g, "
"); // Sensitive topic :3 if (GrandParentpost.value.hasOwnProperty("labels") && GrandParentpost.value.labels.length != 0) { document.getElementsByClassName("PostText GrandParent")[0].innerHTML = "LABELS APPLIED: "; for (let lab of GrandParentpost.value.labels.values) { document.getElementsByClassName("PostText GrandParent")[0].innerHTML += lab.val + " "; } } else { document.getElementsByClassName("PostText GrandParent")[0].innerHTML = Text; if (GrandParentpost.value.hasOwnProperty("embed")) { ApplyMedia(GrandParentpost.value.embed, document.getElementsByClassName("Images GrandParent")[0], post.reply.grandparentAuthor.did); } } } } } else { document.getElementsByClassName("PostText")[0].innerHTML = "Nothing to load."; } } // Applyers. Essentially applies a thing to an operation. // Finds any associated media and applies it to the post. async function ApplyMedia(Media, Element, Author = undefined) { // Check to see if the image is on the same server. if (website == "Mastodon") { if (Media.type == "image") { if (Media.remote_url == null) { Element.innerHTML += "\"""; } else { Element.innerHTML += "\"""; } } else if (Media.type == "video" || Media.type == "gifv") { if (Media.remote_url == null) { Element.innerHTML += ""; } else { Element.innerHTML += ""; } } } else if (website == "Bluesky") { // Record and media embed; They are from seperate posts. if (Media.$type == "app.bsky.embed.recordWithMedia") { await BlueskyBlobEmbed(Media.media, Element, Author); var Texty = await BlueskyAPI.GetPosts([Media.record.record.uri]); var Text = BlueskyAPI.ApplyFacets(Texty.posts[0].record, Texty.posts[0].record.text); Text = Text.replace(/\r?\n|\r/g, "
"); Element.innerHTML += "

" + Text + "


"; // To stop confusion: this is for the RECORD EMBED! It gets an image from the record embed and puts it there. if (Texty.posts[0].record.embed.$type == "app.bsky.embed.images" || Texty.posts[0].record.embed.$type == "app.bsky.embed.video") { await BlueskyBlobEmbed(Texty.posts[0].record.embed, Element, Texty.posts[0].author.did); } // Just a record on it's own. } else if (Media.$type == "app.bsky.embed.record") { var Texty = await BlueskyAPI.GetPosts([Media.record.uri]); var Text = BlueskyAPI.ApplyFacets(Texty.posts[0].record, Texty.posts[0].record.text); Text = Text.replace(/\r?\n|\r/g, "
"); Element.innerHTML += "

" + Text + "


"; if (Texty.posts[0].record.embed.$type == "app.bsky.embed.images" || Texty.posts[0].record.embed.$type == "app.bsky.embed.video") { await BlueskyBlobEmbed(Texty.posts[0].record.embed, Element, Texty.posts[0].author.did); } // Image on it's own } else if (Media.$type == "app.bsky.embed.images" || Media.$type == "app.bsky.embed.video") { await BlueskyBlobEmbed(Media, Element, Author); } } } // Fucking hell. async function BlueskyBlobEmbed(Blob, Element, Author) { if (Blob.$type == "app.bsky.embed.images") { for (let i of Blob.images) { var Blobby = await BlueskyAPI.GetBlob(Author, i.image.ref.$link); var ObjectURL = URL.createObjectURL(Blobby); Element.innerHTML += "\"""; } // Video embed. } else if (Blob.$type == "app.bsky.embed.video") { var Blobby = await BlueskyAPI.GetBlob(Author, Blob.video.ref.$link); var ObjectURL = URL.createObjectURL(Blobby); Element.innerHTML += ""; } } // Setters function SetFavorite() { FavoriteFlipper = !(FavoriteFlipper); if (FavoriteFlipper == false) { Favorite.innerHTML = "Favorite!"; } else { Favorite.innerHTML = "Unfavorite..."; } } function SetBoost() { BoostFlipper = !(BoostFlipper); if (BoostFlipper == false) { Boost.innerHTML = "Boost!"; } else { Boost.innerHTML = "Unboost..."; } } // Functions stolen form elsewhere function EscapeRegExp(text) { if (text == null) { return null; } return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\$&'); }