246 lines
11 KiB
JavaScript
246 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.SendFavorite(post.id, post.favourited);
|
|
} else if (website == "Bluesky") {
|
|
BlueskyAPI.SendLike(localStorage.getItem(Variables.BlueskyDID), post.post.uri, post.post.cid);
|
|
}
|
|
SetFavorite();
|
|
}
|
|
|
|
Boost.onclick = (event) => {
|
|
if (website == "Mastodon") {
|
|
MastodonAPI.SendReblog(post.id, post.reblogged);
|
|
} else if (website == "Bluesky") {
|
|
BlueskyAPI.SendRepost(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 CreateMedia(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 CreateMedia(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;
|
|
if (AnotherPost.reblog != null) {
|
|
document.getElementsByClassName("PostText Parent")[0].innerHTML = AnotherPost.reblog.content;
|
|
if (AnotherPost.reblog.media_attachments.length != 0) {
|
|
for (let i of AnotherPost.reblog.media_attachments) {
|
|
await CreateMedia(i, document.getElementsByClassName("Images Parent")[0]);
|
|
}
|
|
}
|
|
} else {
|
|
document.getElementsByClassName("PostText Parent")[0].innerHTML = AnotherPost.content;
|
|
if (AnotherPost.media_attachments.length != 0) {
|
|
for (let i of AnotherPost.media_attachments) {
|
|
await CreateMedia(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;
|
|
if (AnotherAnotherPost.reblog != null) {
|
|
document.getElementsByClassName("PostText GrandParent")[0].innerHTML = AnotherAnotherPost.reblog.content;
|
|
if (AnotherAnotherPost.reblog.media_attachments.length != 0) {
|
|
for (let i of AnotherAnotherPost.reblog.media_attachments) {
|
|
await CreateMedia(i, document.getElementsByClassName("Images GrandParent")[0]);
|
|
}
|
|
}
|
|
} else {
|
|
document.getElementsByClassName("PostText GrandParent")[0].innerHTML = AnotherAnotherPost.content;
|
|
if (AnotherAnotherPost.media_attachments.length != 0) {
|
|
for (let i of AnotherAnotherPost.media_attachments) {
|
|
await CreateMedia(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 = post.post.record.text;
|
|
// Check for facets. Facets are things that change what the text does or looks like.
|
|
if (post.post.record.hasOwnProperty("facets")) {
|
|
for (let i of post.post.record.facets) {
|
|
if (i.features[0].$type == "app.bsky.richtext.facet#link") {
|
|
var EmojiRegex = /\p{Emoji_Modifier_Base}\p{Emoji_Modifier}?|\p{Emoji_Presentation}|\p{Emoji}\uFE0F/gu;
|
|
var EmojiObjects = Text.match(EmojiRegex);
|
|
var SubtractNumber = 0;
|
|
if (EmojiObjects != null) {
|
|
SubtractNumber = EmojiObjects.length * 2;
|
|
}
|
|
Text = Text.substring(0, i.index.byteStart - SubtractNumber) + "<a href='" + i.features[0].uri + "'>" + Text.substring(i.index.byteStart - SubtractNumber, i.index.byteEnd - SubtractNumber) + "</a>" + Text.substring(i.index.byteEnd - SubtractNumber, Text.length - 1);
|
|
}
|
|
}
|
|
}
|
|
// 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 CreateMedia(post.post.record, 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 = AnotherPost.value.text;
|
|
Text = Text.replace(/\r?\n|\r/g, "<br/>");
|
|
document.getElementsByClassName("PostText Parent")[0].innerHTML = Text;
|
|
if (AnotherPost.value.hasOwnProperty("embed")) {
|
|
await CreateMedia(AnotherPost.value, 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 = AnotherAnotherPost.value.text;
|
|
Text = Text.replace(/\r?\n|\r/g, "<br/>");
|
|
document.getElementsByClassName("PostText GrandParent")[0].innerHTML = Text;
|
|
if (AnotherAnotherPost.value.hasOwnProperty("embed")) {
|
|
await CreateMedia(AnotherAnotherPost.value, document.getElementsByClassName("Images GrandParent")[0], post.reply.grandparentAuthor.did);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
document.getElementsByClassName("PostText")[0].innerHTML = "Nothing to load.";
|
|
}
|
|
}
|
|
|
|
async function CreateMedia(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='" + Media.description + "'/>";
|
|
} else {
|
|
Element.innerHTML += "<img src=" + Media.remote_url + " alt='" + Media.description + "'/>";
|
|
}
|
|
} else if (Media.type == "video") {
|
|
if (Media.remote_url == null) {
|
|
Element.innerHTML += "<video controls src=" + Media.url + " alt='" + Media.description + "'></video>";
|
|
} else {
|
|
Element.innerHTML += "<video controls src=" + Media.remote_url + " alt='" + Media.description + "'></video>";
|
|
}
|
|
}
|
|
} else if (website == "Bluesky") {
|
|
if (Media.embed.$type == "app.bsky.embed.record") {
|
|
var Texty = await BlueskyAPI.GetPosts([Media.embed.record.uri]);
|
|
console.log(Texty);
|
|
Element.innerHTML += "<p class='Embed'>" + Texty.posts[0].record.text + "</p><br/>";
|
|
if (Texty.posts[0].record.embed.$type == "app.bsky.embed.images") {
|
|
for (let i of Texty.posts[0].record.embed.images) {
|
|
var Blobby = await BlueskyAPI.GetBlob(Texty.posts[0].author.did, i.image.ref.$link);
|
|
var ObjectURL = URL.createObjectURL(Blobby);
|
|
Element.innerHTML += "<img class='Embed' src=" + ObjectURL + " alt='" + i.alt + "'/>";
|
|
}
|
|
} else if (Texty.posts[0].record.embed.$type == "app.bsky.embed.video") {
|
|
var Blobby = await BlueskyAPI.GetBlob(Texty.posts[0].author.did, Texty.posts[0].record.embed.video.ref.$link);
|
|
var ObjectURL = URL.createObjectURL(Blobby);
|
|
Element.innerHTML += "<video class='Embed' controls src=" + ObjectURL + "></video>";
|
|
}
|
|
// It's not an embed, continue...
|
|
} else if (Media.embed.$type == "app.bsky.embed.images") {
|
|
for (let i of Media.embed.images) {
|
|
var Blobby = await BlueskyAPI.GetBlob(Author, i.image.ref.$link);
|
|
var ObjectURL = URL.createObjectURL(Blobby);
|
|
Element.innerHTML += "<img src=" + ObjectURL + " alt='" + i.alt + "'/>";
|
|
}
|
|
} else if (Media.embed.$type == "app.bsky.embed.video") {
|
|
var Blobby = await BlueskyAPI.GetBlob(Author, Media.embed.video.ref.$link);
|
|
var ObjectURL = URL.createObjectURL(Blobby);
|
|
Element.innerHTML += "<video controls src=" + ObjectURL + "></video>";
|
|
}
|
|
}
|
|
}
|
|
|
|
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...";
|
|
}
|
|
}
|