Fedi.CrowdedGames.Group/JS/MastodonAPI.js

101 lines
5.4 KiB
JavaScript

export const Scopes = "read write follow push";
// Gets the public timeline.
export async function GetPublicTimeline(Local = false, Remote = false, Website) {
let Timeline;
if (Local == true && Remote == true) {
console.error("Don't set both Local and Remote timelines to true.");
return Timeline;
}
if (Local == true) {
Timeline = await fetch(Website + "/api/v1/timelines/public?limit=12&local=true")
.then((response) => response.json());
} else if (Remote == true) {
Timeline = await fetch(Website + "/api/v1/timelines/public?limit=12&remote=true")
.then((response) => response.json());
} else {
Timeline = await fetch(Website + "/api/v1/timelines/public?limit=12")
.then((response) => response.json());
}
return Timeline;
}
// Gets the favorites of a user that you have access to.
export async function GetFavorites(Website, MastodonAccessToken, MastodonTokenType) {
let Favorites;
// Check for a token.
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1) {
// Get the varaibles that are stored in cookies.
let AccessToken = document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=")[1];
let TokenType = document.cookie.split("; ").find((row) => row.startsWith(MastodonTokenType + "="))?.split("=")[1];
Favorites = await fetch(Website + "/api/v1/favourites", {method: "GET", headers: {"Authorization": TokenType + " " + AccessToken}})
.then((response) => response.json());
}
return Favorites;
}
// Gets the bookmarks of a user that you have access to.
export async function GetBookmarks(Website, MastodonAccessToken, MastodonTokenType) {
let Bookmarks;
// Check for a token.
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1) {
// Get the varaibles that are stored in cookies.
let AccessToken = document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=")[1];
let TokenType = document.cookie.split("; ").find((row) => row.startsWith(MastodonTokenType + "="))?.split("=")[1];
Bookmarks = await fetch(Website + "/api/v1/bookmarks", {method: "GET", headers: {"Authorization": TokenType + " " + AccessToken}})
.then((response) => response.json());
}
return Bookmarks;
}
// Gets the notifications of a user that you have access to.
export async function GetNotifications(Website, MastodonAccessToken, MastodonTokenType) {
let Notifications;
// Check for a token.
if (document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=").length > 1) {
// Get the varaibles that are stored in cookies.
let AccessToken = document.cookie.split("; ").find((row) => row.startsWith(MastodonAccessToken + "="))?.split("=")[1];
let TokenType = document.cookie.split("; ").find((row) => row.startsWith(MastodonTokenType + "="))?.split("=")[1];
Notifications = await fetch(Website + "/api/v1/notifications", {method: "GET", headers: {"Authorization": TokenType + " " + AccessToken}})
.then((response) => response.json());
}
return Notifications;
}
// The first step to using the app.
export async function HandleAuthentication(Website, CookieClientID, CookieClientSecret) {
// See if the user is smart enough to put https.
let InstanceData;
if (!Website.toLowerCase().split("://")[0].includes("https")) {
// Quickly check to see if it has something before :// so it doesn't screw the link.
if (Website.toLowerCase().split("://").length > 1) {
Website = "https://" + Website.split("://")[1];
} else {
Website = "https://" + Website;
}
InstanceData = await fetch(Website + "/api/v1/apps?client_name=Channel Viewer&redirect_uris=" + Origin + "&scopes=" + ActivityPub.Scopes, {method: "POST"})
.then((response) => response.json());
}
// Save the client stuff as cookies.
document.cookie = CookieClientID + "=" + InstanceData.client_id + ";samesite=strict;path=/;expires=9999-01-01;";
document.cookie = CookieClientSecret + "=" + InstanceData.client_secret + ";samesite=strict;path=/;expires=9999-01-01;";
// Now authenticate the app.
document.location.href = Website + "/oauth/authorize?client_id=" + InstanceData.client_id + "&redirect_uri=" + Origin + "&response_type=code&scope=" + ActivityPub.Scopes;
}
// This specific functino goes after HandleAuthentication for when login happens.
export async function GainToken(Website, CookieClientID, CookieClientSecret, CookieAccessToken, CookieTokenType) {
// check if you both have a code and have a current authentication.
if (document.location.href.split("code=").length > 1 && document.cookie.split("; ").find((row) => row.startsWith(CookieClientID + "="))?.split("=") > 1) {
let code = document.location.href.split("code=")[1];
let ClientID = document.cookie.split("; ").find((row) => row.startsWith(CookieClientID + "="))?.split("=")[1];
let ClientSecret = document.cookie.split("; ").find((row) => row.startsWith(CookieClientSecret + "="))?.split("=")[1];
let AuthenticationToken = await fetch(Website + "/oauth/token?client_id=" + ClientID + "&client_secret=" + ClientSecret + "&redirect_uri=" + Origin + "&grant_type=authorization_code&code=" + code, {method: "POST"})
.then((response) => response.json());
// Cookify These
document.cookie = CookieAccessToken + "=" + AuthenticationToken.access_token + ";samesite=strict;path=/;expires=9999-01-01;";
document.cookie = CookieTokenType + "=" + AuthenticationToken.token_type + ";samesite=strict;path=/;expires=9999-01-01;";
}
}