Fedi.CrowdedGames.Group/JS/post.js
CatAClock f03e73132a MoreWork Merge (#40)
- Animation improvements on `Index.html`.
- Reduced the amount of code (and improved readability) of the APIs.
- Optimizations as well, but not that much.
- Accounts are now viewable. You can also follow and block them to your heart's content (I don't know what happens when you both block and follow).
- Many, many bug fixes that made it through.

Reviewed-on: #40
2025-05-26 02:37:03 +00:00

130 lines
4.8 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";
// Elements.
let PostButton = document.getElementsByClassName("Button")[0];
let VisibilityDropdown = document.getElementsByClassName("PostVisibility")[0];
let InputArea = document.getElementsByClassName("Text")[0];
let YoutubePoser = document.getElementsByClassName("PostYoutube")[0];
let WarningInputArea = document.getElementsByClassName("Warning")[0];
// Clicking the beeg POST button.
PostButton.onclick = (event) => {
Post();
}
async function Post() {
let website;
if (document.location.href.split("website=").length > 1) {
website = document.location.href.split("website=")[1];
} else {
website = "All";
}
// Check to see if you are replying.
let Text = InputArea.value;
let Visible = VisibilityDropdown.value;
// don't do anything if there is no value
if (Text == "") {
return;
}
// Mastodon posting.
if (localStorage.getItem(Variables.MastodonAccessToken) != null) {
let TempVisible;
switch(Visible) {
case "Public":
TempVisible = "public";
break;
case "Quiet":
TempVisible = "unlisted";
break;
case "Friend":
TempVisible = "private";
break;
case "Private":
TempVisible = "direct";
break;
}
if (website == "Mastodon") {
if (WarningInputArea.value == "") {
await MastodonAPI.CreateReplyStatus(Text, undefined, TempVisible, JSON.parse(localStorage.getItem("post")).id);
} else {
await MastodonAPI.CreateReplyStatus(Text, WarningInputArea.value, TempVisible, JSON.parse(localStorage.getItem("post")).id);
}
InputArea.value = "";
WarningInputArea.value = "";
return;
} else if (website == "All") {
if (WarningInputArea.value == "") {
await MastodonAPI.CreateStatus(Text, undefined, TempVisible);
} else {
await MastodonAPI.CreateStatus(Text, WarningInputArea.value, TempVisible);
}
}
}
// Bluesky posting.
if (localStorage.getItem(Variables.BlueskyAccessToken) != null) {
let TempVisible;
switch(Visible) {
case "Public":
TempVisible = undefined;
break;
case "Quiet":
TempVisible = undefined;
break;
case "Friend":
TempVisible = [{"$type": "app.bsky.feed.threadgate#followingRule"}, {"$type": "app.bsky.feed.threadgate#followerRule"}];
break;
case "Private":
TempVisible = [];
break;
}
if (website == "Bluesky") {
let Post;
if (JSON.parse(localStorage.getItem("post")).hasOwnProperty("reply")) {
if (WarningInputArea.value == "") {
Post = await BlueskyAPI.CreateReplyPost(localStorage.getItem(Variables.BlueskyDID), Text, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).reply.root);
} else {
Post = await BlueskyAPI.CreateReplyPost(localStorage.getItem(Variables.BlueskyDID), Text, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).reply.root, WarningInputArea.value);
}
await BlueskyAPI.CreateThreadGate(localStorage.getItem(Variables.BlueskyDID), Post.uri, TempVisible);
} else {
if (WarningInputArea.value == "") {
Post = await BlueskyAPI.CreateReplyPost(localStorage.getItem(Variables.BlueskyDID), Text, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).post);
} else {
Post = await BlueskyAPI.CreateReplyPost(localStorage.getItem(Variables.BlueskyDID), Text, JSON.parse(localStorage.getItem("post")).post, JSON.parse(localStorage.getItem("post")).post, WarningInputArea.value);
}
await BlueskyAPI.CreateThreadGate(localStorage.getItem(Variables.BlueskyDID), Post.uri, TempVisible);
}
InputArea.value = "";
WarningInputArea.value = "";
return;
} else if (website == "All") {
let Post;
if (WarningInputArea.value == "") {
Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text);
} else {
Post = await BlueskyAPI.CreatePost(localStorage.getItem(Variables.BlueskyDID), Text, WarningInputArea.value);
}
await BlueskyAPI.CreateThreadGate(localStorage.getItem(Variables.BlueskyDID), Post.uri, TempVisible);
}
}
// Youtube posting.
if (YoutubePoser.checked == true) {
navigator.clipboard.writeText(Text);
window.open("https://www.youtube.com/channel/" + localStorage.getItem(Variables.YoutubeID) + "/posts");
}
// The input being cleared means that the posting happened.
InputArea.value = "";
WarningInputArea.value = "";
}
// Check if you can interact with the textbox
if (localStorage.getItem(Variables.MastodonAccessToken) == null && localStorage.getItem(Variables.BlueskyAccessToken) == null && localStorage.getItem(Variables.YoutubeID) == null) {
InputArea.disabled = true;
}
// Check if you can post on Youtube
if (localStorage.getItem(Variables.YoutubeID) == null) {
YoutubePoser.disabled = true;
}