141 lines
5.7 KiB
JavaScript
141 lines
5.7 KiB
JavaScript
import * as MastodonAPI from "./MastodonAPI.js";
|
|
import * as BlueskyAPI from "./BlueskyAPI.js";
|
|
import * as TumblrAPI from "./TumblrAPI.js";
|
|
|
|
let MastodonLoginButton = document.getElementsByClassName("Login Mastodon")[0];
|
|
let MastodonWebInput = document.getElementsByClassName("WebInput Mastodon")[0];
|
|
let MastodonLogoutButton = document.getElementsByClassName("Logout Mastodon")[0];
|
|
let BlueskyLoginButton = document.getElementsByClassName("Login Bluesky")[0];
|
|
let BlueskyWebInput = document.getElementsByClassName("WebInput Bluesky")[0];
|
|
let BlueskyLogoutButton = document.getElementsByClassName("Logout Bluesky")[0];
|
|
|
|
// Mastodon
|
|
let MastodonWebsite = "https://wetdry.world";
|
|
let MastodonClientID = "mastodon_client_id";
|
|
let MastodonClientSecret = "mastodon_client_secret";
|
|
let MastodonAccessToken = "mastodon_access_token";
|
|
let MastodonTokenType = "mastodon_token_type";
|
|
|
|
// Bluesky (TODO: use these variables).
|
|
let BlueskyApp = "https://bsky.app";
|
|
let BlueskyPDS = "https://bsky.social";
|
|
|
|
// Tumblr
|
|
let TumblrWebsite = "https://www.tumblr.com";
|
|
|
|
MastodonLoginButton.onclick = (event) => {
|
|
if (MastodonWebInput != "") {
|
|
MastodonAPI.HandleAuthentication(MastodonWebsite, MastodonClientID, MastodonClientSecret);
|
|
}
|
|
}
|
|
|
|
MastodonLogoutButton.onclick = (event) => {
|
|
document.cookie = MastodonClientID + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
|
|
document.cookie = MastodonClientSecret + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
|
|
document.cookie = MastodonAccessToken + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
|
|
document.cookie = MastodonTokenType + "=nothing;" + ";samesite=strict;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT;";
|
|
document.location.href = document.location.href;
|
|
}
|
|
|
|
BlueskyLoginButton.onclick = (event) => {
|
|
if (BlueskyWebInput != "") {
|
|
BlueskyTestingAuthorization();
|
|
}
|
|
}
|
|
|
|
BlueskyLogoutButton.onclick = (event) => {
|
|
// Nothing at the moment
|
|
}
|
|
|
|
// if an access token is found, login.
|
|
function CheckLogin() {
|
|
// Check for a mastodon token.
|
|
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1 && !document.location.href.split("code=").length) {
|
|
// Swap the buttons
|
|
MastodonLoginButton.remove();
|
|
MastodonWebInput.remove();
|
|
MastodonLogoutButton.setAttribute("style", "");
|
|
}
|
|
// Check for a bluesky token.
|
|
}
|
|
|
|
// Below is the thing it populates if you login.
|
|
async function PopulateFavorites() {
|
|
let Favorites = await MastodonAPI.GetFavorites(MastodonWebsite, MastodonAccessToken, MastodonTokenType);
|
|
|
|
let FavoritesArea = document.getElementsByClassName("Favorites")[0];
|
|
// Populate the favorites area.
|
|
for (let i in Favorites) {
|
|
FavoritesArea.innerHTML += "<article style='top:" + getRandomArbitrary(0, 84) + "%; left: " + getRandomArbitrary(0, 84) + "%;' class='Favorite'></article>";
|
|
FavoritesArea.getElementsByClassName("Favorite")[i].innerHTML = Favorites[i].content;
|
|
}
|
|
}
|
|
|
|
async function PopulateBookmarks() {
|
|
let Bookmarks = await MastodonAPI.GetBookmarks(MastodonWebsite, MastodonAccessToken, MastodonTokenType);
|
|
|
|
let BookmarksArea = document.getElementsByClassName("Bookmarks")[0];
|
|
// Populate the Bookmarks area.
|
|
for (let i in Bookmarks) {
|
|
BookmarksArea.innerHTML += "<article style='top:" + getRandomArbitrary(0, 84) + "%; left: " + getRandomArbitrary(0, 84) + "%;' class='Bookmark'></article>";
|
|
BookmarksArea.getElementsByClassName("Bookmark")[i].innerHTML = Bookmarks[i].content;
|
|
}
|
|
}
|
|
|
|
async function PopulateNotifications() {
|
|
let Notifications = await MastodonAPI.GetNotifications(MastodonWebsite, MastodonAccessToken, MastodonTokenType);
|
|
|
|
let NotificationsArea = document.getElementsByClassName("Notifications")[0];
|
|
// Populate the Conversations area.
|
|
for (let i in Notifications) {
|
|
NotificationsArea.innerHTML += "<article style='top:" + getRandomArbitrary(0, 84) + "%; left: " + getRandomArbitrary(0, 84) + "%;' class='Notification'></article>";
|
|
NotificationsArea.getElementsByClassName("Notification")[i].innerHTML = Notifications[i].status.content;
|
|
}
|
|
}
|
|
|
|
await MastodonAPI.GainToken(MastodonWebsite, MastodonClientID, MastodonClientSecret, MastodonAccessToken, MastodonTokenType);
|
|
|
|
// Runs on website start.
|
|
// Remove traces of "login".
|
|
CheckLogin();
|
|
|
|
// Populate the areas.
|
|
PopulateFavorites();
|
|
PopulateBookmarks();
|
|
PopulateNotifications();
|
|
|
|
// Functions stolen elsewhere
|
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
|
|
function getRandomArbitrary(min, max) {
|
|
return Math.random() * (max - min) + min;
|
|
}
|
|
|
|
// The next section is dedicated to testing.
|
|
// WARNING: I don't know what I am doing.
|
|
|
|
async function BlueskyTestingAuthorization() {
|
|
// Declare Variables
|
|
let WellKnown = await BlueskyAPI.GetPDSWellKnown();
|
|
let PAREndpoint = WellKnown.pushed_authorization_request_endpoint;
|
|
|
|
let State = generateToken(64);
|
|
|
|
let PKCEverifier = await BlueskyAPI.CreatePKCECodeVerifier();
|
|
let PKCEchallenge = await BlueskyAPI.CreatePKCECodeChallenge(PKCEverifier);
|
|
// PAR request (beginning)
|
|
let PAR = await BlueskyAPI.PARrequest(WellKnown.pushed_authorization_request_endpoint, State, PKCEchallenge);
|
|
console.log(PAR);
|
|
// Now we need to authenticate. Make sure the State stays the same throughout this whole process :]
|
|
document.location.href = "https://bsky.social/oauth/authorize?client_id=https://fedi.crowdedgames.group/oauth/client-metadata.json&request_uri=" + PAR.request_uri;
|
|
}
|
|
|
|
// Stolen from Search
|
|
// TODO: implement my own function.
|
|
function generateToken(length) {
|
|
var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
|
var token = '';
|
|
for(var i = 0; i < length; i++) {
|
|
token += chars[Math.floor(Math.random() * chars.length)];
|
|
}
|
|
return token;
|
|
}
|