Fedi.CrowdedGames.Group/JS/expanded.js
2025-05-23 13:23:53 -07:00

240 lines
11 KiB
JavaScript

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"));
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;
}
// 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) {
await 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) {
await ApplyMedia(i, document.getElementsByClassName("Images Regular")[0]);
}
}
}
// Set the texts. It's opposite because "setting" causes it to switch.
FavoriteFlipper = !(post.favourite);
BoostFlipper = !(post.reblogged);
SetFavorite();
SetBoost();
// Now time to see if there are any parents
if (post.in_reply_to_id != null) {
var AnotherPost = await MastodonAPI.GetStatus(post.in_reply_to_id);
document.getElementsByClassName("Origin Parent")[0].innerHTML = website;
document.getElementsByClassName("Handle Parent")[0].innerHTML = AnotherPost.account.username;
document.getElementsByClassName("PostText Parent")[0].innerHTML = AnotherPost.content;
if (AnotherPost.media_attachments.length != 0) {
for (let i of AnotherPost.media_attachments) {
await ApplyMedia(i, document.getElementsByClassName("Images Parent")[0]);
}
}
// Now time to see if there are any grandparents
if (AnotherPost.in_reply_to_id != null) {
var AnotherAnotherPost = await MastodonAPI.GetStatus(AnotherPost.in_reply_to_id);
document.getElementsByClassName("Origin GrandParent")[0].innerHTML = website;
document.getElementsByClassName("Handle GrandParent")[0].innerHTML = AnotherAnotherPost.account.username;
document.getElementsByClassName("PostText GrandParent")[0].innerHTML = AnotherAnotherPost.content;
if (AnotherAnotherPost.media_attachments.length != 0) {
for (let i of AnotherAnotherPost.media_attachments) {
await 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, "<br/>");
document.getElementsByClassName("PostText Regular")[0].innerHTML = Text;
// Show the image if it exists.
if (post.post.record.hasOwnProperty("embed")) {
await 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")) {
var AnotherPost = 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(AnotherPost.value, AnotherPost.value.text);
Text = Text.replace(/\r?\n|\r/g, "<br/>");
document.getElementsByClassName("PostText Parent")[0].innerHTML = Text;
if (AnotherPost.value.hasOwnProperty("embed")) {
await ApplyMedia(AnotherPost.value.embed, document.getElementsByClassName("Images Parent")[0], post.reply.parent.author.did);
}
// Now time to see if there are any grandparents.
if (AnotherPost.value.hasOwnProperty("reply")) {
var AnotherAnotherPost = await BlueskyAPI.GetRecord(AnotherPost.value.reply.parent.uri.split("/")[2], AnotherPost.value.reply.parent.uri.split("/")[3], AnotherPost.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(AnotherAnotherPost.value, AnotherAnotherPost.value.text);
Text = Text.replace(/\r?\n|\r/g, "<br/>");
document.getElementsByClassName("PostText GrandParent")[0].innerHTML = Text;
if (AnotherAnotherPost.value.hasOwnProperty("embed")) {
await ApplyMedia(AnotherAnotherPost.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 += "<img src=" + Media.url + " alt='" + EscapeRegExp(Media.description) + "'/>";
} else {
Element.innerHTML += "<img src=" + Media.remote_url + " alt='" + EscapeRegExp(Media.description) + "'/>";
}
} else if (Media.type == "video") {
if (Media.remote_url == null) {
Element.innerHTML += "<video controls src=" + Media.url + " alt='" + EscapeRegExp(Media.description) + "'></video>";
} else {
Element.innerHTML += "<video controls src=" + Media.remote_url + " alt='" + EscapeRegExp(Media.description) + "'></video>";
}
}
} 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, "<br/>");
Element.innerHTML += "<p class='Embed'>" + Text + "</p><br/>";
// 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, Author);
}
// 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, "<br/>");
Element.innerHTML += "<p class='Embed'>" + Text + "</p><br/>";
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, Author);
}
// 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 += "<img src=" + ObjectURL + " alt='" + EscapeRegExp(i.alt) + "'/>";
}
// 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 += "<video controls src=" + ObjectURL + "></video>";
}
}
// 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, '\$&');
}