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.MastodonAccessTokenCookie)) { let Website = Cookie.MastodonWebsiteCookie; // Get the varaibles that are stored in cookies. Favorites = await fetch(Website + "/api/v1/favourites", {method: "GET", headers: {"Authorization": Cookie.MastodonTokenTypeCookie + " " + Cookie.MastodonAccessTokenCookie}}) .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.MastodonAccessTokenCookie)) { let Website = Cookie.MastodonWebsiteCookie; // Get the varaibles that are stored in cookies. Bookmarks = await fetch(Website + "/api/v1/bookmarks", {method: "GET", headers: {"Authorization": Cookie.MastodonTokenTypeCookie + " " + Cookie.MastodonAccessTokenCookie}}) .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.MastodonAccessTokenCookie)) { let Website = Cookie.MastodonWebsiteCookie; // Get the varaibles that are stored in cookies and then input it. Notifications = await fetch(Website + "/api/v1/notifications", {method: "GET", headers: {"Authorization": Cookie.MastodonTokenTypeCookie + " " + Cookie.MastodonAccessTokenCookie}}) .then((response) => response.json()); } return Notifications; } // Make a status export async function CreateStatus(Text) { // Check for a token if (Cookie.IsCookieReal(Cookie.MastodonAccessTokenCookie)) { let Website = Cookie.MastodonWebsiteCookie; return await fetch(Website + "/api/v1/statuses?status=" + Text , {method: "POST", headers: {"Authorization": Cookie.MastodonTokenTypeCookie + " " + Cookie.MastodonAccessTokenCookie}}) .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.MastodonWebsiteName, 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.MastodonClientIDName, InstanceData.client_id); Cookie.InputCookie(Cookie.MastodonClientSecretName, 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.MastodonClientIDCookie) && !(Cookie.IsCookieReal(Cookie.MastodonAccessTokenCookie))) { // Get some vars. let code = document.location.href.split("code=")[1]; let Website = Cookie.MastodonWebsiteCookie; // Authenticate. let AuthenticationToken = await fetch(Website + "/oauth/token?client_id=" + Cookie.MastodonClientIDCookie + "&client_secret=" + Cookie.MastodonClientSecretCookie + "&redirect_uri=" + document.location.href + "&grant_type=authorization_code&code=" + code, {method: "POST"}) .then((response) => response.json()); // Cookify These. Cookie.InputCookie(Cookie.MastodonAccessTokenName, AuthenticationToken.access_token); Cookie.InputCookie(Cookie.MastodonTokenTypeName, AuthenticationToken.token_type); } }