Fedi.CrowdedGames.Group/JS/MastodonAPI.js
CatAClock 89e2c54d3e Bug fixes (#10)
Another somewhat overhaul & fixing a couple of stupid bugs that just so happen to crop up.

Reviewed-on: #10
2025-05-03 22:22:40 +00:00

112 lines
5 KiB
JavaScript

import * as Cookie from "./Cookies.js";
export const Scopes = "read write follow push";
// Gets the public timeline.
export async function GetPublicTimeline(Local = false, Remote = false, Website) {
// Cookies can be found in `setting.js`
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() {
let Favorites;
// Check for a token.
if (Cookie.IsCookieReal(Cookie.MastodonAccessToken)) {
let Website = Cookie.MastodonWebsite;
// Get the varaibles that are stored in cookies.
Favorites = await fetch(Website + "/api/v1/favourites", {method: "GET", headers: {"Authorization": Cookie.GetCookie(Cookie.MastodonTokenType) + " " + Cookie.GetCookie(Cookie.MastodonAccessToken)}})
.then((response) => response.json());
}
return Favorites;
}
// Gets the bookmarks of a user that you have access to.
export async function GetBookmarks() {
let Bookmarks;
// Check for a token.
if (Cookie.IsCookieReal(Cookie.MastodonAccessToken)) {
let Website = Cookie.GetCookie(Cookie.MastodonWebsite);
// Get the varaibles that are stored in cookies.
Bookmarks = await fetch(Website + "/api/v1/bookmarks", {method: "GET", headers: {"Authorization": Cookie.GetCookie(Cookie.MastodonTokenType) + " " + Cookie.GetCookie(Cookie.MastodonAccessToken)}})
.then((response) => response.json());
}
return Bookmarks;
}
// Gets the notifications of a user that you have access to.
export async function GetNotifications() {
let Notifications;
// Check for a token.
if (Cookie.IsCookieReal(Cookie.MastodonAccessToken)) {
let Website = Cookie.GetCookie(Cookie.MastodonWebsite);
// Get the varaibles that are stored in cookies and then input it.
Notifications = await fetch(Website + "/api/v1/notifications", {method: "GET", headers: {"Authorization": Cookie.GetCookie(Cookie.MastodonTokenType) + " " + Cookie.GetCookie(Cookie.MastodonAccessToken)}})
.then((response) => response.json());
}
return Notifications;
}
// Make a status
export async function CreateStatus(Text) {
// Check for a token
if (Cookie.IsCookieReal(Cookie.MastodonAccessToken)) {
let Website = Cookie.MastodonWebsiteCookie;
return await fetch(Website + "/api/v1/statuses?status=" + Text , {method: "POST", headers: {"Authorization": Cookie.GetCookie(Cookie.MastodonTokenType) + " " + Cookie.GetCookie(Cookie.MastodonAccessToken)}})
.then((response) => response.json());
}
}
// The first step to using the app.
export async function HandleAuthentication(Website) {
// See if the user is smart enough to put https.
let InstanceData = "";
// 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;
}
// Save the website
Cookie.InputCookie(Cookie.MastodonWebsite, Website);
// Registering the app.
InstanceData = await fetch(Website + "/api/v1/apps?client_name=Channel Viewer&redirect_uris=" + document.location.href + "&scopes=" + Scopes, {method: "POST"})
.then((response) => response.json());
// Save the client stuff as cookies.
Cookie.InputCookie(Cookie.MastodonClientID, InstanceData.client_id);
Cookie.InputCookie(Cookie.MastodonClientSecret, InstanceData.client_secret);
// Now authenticate the app.
document.location.href = Website + "/oauth/authorize?client_id=" + InstanceData.client_id + "&redirect_uri=" + document.location.href + "&response_type=code&scope=" + Scopes;
}
// This specific functino goes after HandleAuthentication for when login happens.
export async function GainToken() {
// check if you both have a code and have a current authentication.
if (document.location.href.split("code=").length > 1 && Cookie.IsCookieReal(Cookie.MastodonClientID) && !(Cookie.IsCookieReal(Cookie.MastodonAccessToken))) {
// Get some vars.
let code = document.location.href.split("code=")[1];
let Website = Cookie.GetCookie(Cookie.MastodonWebsite);
// Authenticate.
let AuthenticationToken = await fetch(Website + "/oauth/token?client_id=" + Cookie.GetCookie(Cookie.MastodonClientID) + "&client_secret=" + Cookie.GetCookie(Cookie.MastodonClientSecret) + "&redirect_uri=" + document.location.href + "&grant_type=authorization_code&code=" + code, {method: "POST"})
.then((response) => response.json());
// Cookify These.
Cookie.InputCookie(Cookie.MastodonAccessToken, AuthenticationToken.access_token);
Cookie.InputCookie(Cookie.MastodonTokenType, AuthenticationToken.token_type);
}
}