optimized the Mastodon API
This commit is contained in:
parent
a42c8be258
commit
128539d426
1 changed files with 58 additions and 67 deletions
|
@ -35,30 +35,27 @@ export async function GetPublicTimeline(Local = false, Remote = false, Website,
|
|||
}
|
||||
|
||||
// Get the personal timeline.
|
||||
export async function GetTimeline(Cursor) {
|
||||
export async function GetTimeline(Cursor = "") {
|
||||
if (localStorage.getItem(Variables.MastodonAccessToken) == null) {
|
||||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
// Get the varaibles that are stored in localStorage.
|
||||
if (Cursor == "") {
|
||||
return await fetch(Website + "/api/v1/timelines/home", {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
} else {
|
||||
return await fetch(Website + "/api/v1/timelines/home?max_id=" + Cursor, {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
// If there is a timestamp, GET with the timestamp.
|
||||
let request = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/timelines/home";
|
||||
if (Cursor != "") {
|
||||
request += "?max_id=" + Cursor;
|
||||
}
|
||||
return await fetch(request, {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
// Get the relationship. The token houses the person you are getting a relationship for.
|
||||
export async function GetRelationship(ID) {
|
||||
if (localStorage.getItem(Variables.MastodonAccessToken) == null) {
|
||||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
// Get the varaibles that are stored in localStorage.
|
||||
return await fetch(Website + "/api/v1/accounts/relationships?id[]=" + ID, {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
return await fetch(localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/accounts/relationships?id[]=" + ID, {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
|
@ -68,9 +65,7 @@ export async function GetFavorites() {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
// Get the varaibles that are stored in localStorage.
|
||||
return await fetch(Website + "/api/v1/favourites", {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
return await fetch(localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/favourites", {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
|
@ -80,9 +75,7 @@ export async function GetBookmarks() {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
// Get the varaibles that are stored in Variables.
|
||||
return await fetch(Website + "/api/v1/bookmarks", {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
return await fetch(localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/bookmarks", {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
|
@ -92,9 +85,7 @@ export async function GetNotifications() {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
// Get the varaibles that are stored in Variables and then input it.
|
||||
return await fetch(Website + "/api/v1/notifications", {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
return await fetch(localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/notifications", {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
|
@ -104,9 +95,7 @@ export async function GetStatus(ID) {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
// Get the varaibles that are stored in Variables and then input it.
|
||||
return await fetch(Website + "/api/v1/statuses/" + ID, {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
return await fetch(localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/statuses/" + ID, {method: "GET", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
|
@ -117,26 +106,27 @@ export async function CreateStatus(Text, SpoilerText = undefined, Visibility = "
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
// Stolen from StackOverflow
|
||||
// Stolen from StackOverflow.
|
||||
Text = Text.replace(/(?:\r|\n|\r\n)/g, '<br>');
|
||||
// Check for a token
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
let request;
|
||||
if (SpoilerText == undefined) {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
} else {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility + "&spoiler_text=" + SpoilerText, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
// Get the correct fetch body.
|
||||
let FetchThing = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility;
|
||||
if (SpoilerText != undefined) {
|
||||
FetchThing += "&spoiler_text=" + SpoilerText;
|
||||
}
|
||||
// Send the request.
|
||||
let request = fetch(FetchThing, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
let body = await request.then((response) => response.json());
|
||||
let status = await request.then((response) => response.status);
|
||||
// This is in case you went over the characters.
|
||||
if (status == 422) {
|
||||
let matches = body.error.match(/(\d+)/);
|
||||
if (SpoilerText == undefined) {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text.slice(0, matches[0] - 1) + "&visibility=" + Visibility, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
} else {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility + "&spoiler_text=" + SpoilerText, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
// Get the correct fetch body.
|
||||
FetchThing = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/statuses?status=" + Text.slice(0, matches[0] - 1) + "&visibility=" + Visibility
|
||||
if (SpoilerText != undefined) {
|
||||
FetchThing += "&spoiler_text=" + SpoilerText;
|
||||
}
|
||||
// Send the request.
|
||||
request = fetch(FetchThing, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
body = await request.then((response) => response.json());
|
||||
await CreateReplyStatus(Text.slice(matches[0] - 1, SpoilerText, Text.length - 1), Visibility, body.id);
|
||||
}
|
||||
|
@ -150,24 +140,25 @@ export async function CreateReplyStatus(Text, SpoilerText = undefined, Visibilit
|
|||
}
|
||||
// Stolen from StackOverflow
|
||||
Text = Text.replace(/(?:\r|\n|\r\n)/g, '<br>');
|
||||
// Check for a token
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
let request;
|
||||
if (SpoilerText == undefined) {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility + "&in_reply_to_id=" + ReplyID, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
} else {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility + "&in_reply_to_id=" + ReplyID + "&spoiler_text=" + SpoilerText, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
// Get the correct fetch body.
|
||||
let FetchThing = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility + "&in_reply_to_id=" + ReplyID;
|
||||
if (SpoilerText != undefined) {
|
||||
FetchThing += "&spoiler_text=" + SpoilerText;
|
||||
}
|
||||
// Send the request.
|
||||
let request = fetch(FetchThing, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
let body = await request.then((response) => response.json());
|
||||
let status = await request.then((response) => response.status);
|
||||
// This is in case you went over the characters.
|
||||
if (status == 422) {
|
||||
let matches = body.error.match(/(\d+)/);
|
||||
if (SpoilerText == undefined) {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text.slice(0, matches[0] - 1) + "&visibility=" + Visibility + "&in_reply_to_id=" + ReplyID, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
} else {
|
||||
request = fetch(Website + "/api/v1/statuses?status=" + Text + "&visibility=" + Visibility + "&in_reply_to_id=" + ReplyID + "&spoiler_text=" + SpoilerText, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
// Get the correct fetch body.
|
||||
FetchThing = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/statuses?status=" + Text.slice(0, matches[0] - 1) + "&visibility=" + Visibility + "&in_reply_to_id=" + ReplyID;
|
||||
if (SpoilerText != undefined) {
|
||||
FetchThing += "&spoiler_text=" + SpoilerText;
|
||||
}
|
||||
// Send the request.
|
||||
request = fetch(FetchThing, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}});
|
||||
body = await request.then((response) => response.json());
|
||||
await CreateReplyStatus(Text.slice(matches[0] - 1, Text.length - 1), SpoilerText, Visibility, body.id);
|
||||
}
|
||||
|
@ -179,14 +170,14 @@ export async function CreateFavorite(ID, IsFavorited) {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
let request = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/statuses/" + ID;
|
||||
if (IsFavorited == false) {
|
||||
return await fetch(Website + "/api/v1/statuses/" + ID + "/favourite", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/favourite";
|
||||
} else {
|
||||
return await fetch(Website + "/api/v1/statuses/" + ID + "/unfavourite", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/unfavourite";
|
||||
}
|
||||
return await fetch(request, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
export async function CreateReblog(ID, IsReblogged) {
|
||||
|
@ -194,14 +185,14 @@ export async function CreateReblog(ID, IsReblogged) {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
let request = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/statuses/" + ID;
|
||||
if (IsReblogged == false) {
|
||||
return await fetch(Website + "/api/v1/statuses/" + ID + "/reblog", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/reblog";
|
||||
} else {
|
||||
return await fetch(Website + "/api/v1/statuses/" + ID + "/unreblog", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/unreblog";
|
||||
}
|
||||
return await fetch(request, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
export async function CreateFollow(ID, IsFollowed) {
|
||||
|
@ -209,14 +200,14 @@ export async function CreateFollow(ID, IsFollowed) {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
let request = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/accounts/" + ID;
|
||||
if (IsFollowed == false) {
|
||||
return await fetch(Website + "/api/v1/accounts/" + ID + "/follow", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/follow";
|
||||
} else {
|
||||
return await fetch(Website + "/api/v1/accounts/" + ID + "/unfollow", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/unfollow";
|
||||
}
|
||||
return await fetch(request, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
export async function CreateBlock(ID, IsBlocked) {
|
||||
|
@ -224,14 +215,14 @@ export async function CreateBlock(ID, IsBlocked) {
|
|||
console.log("No access token!");
|
||||
return "";
|
||||
}
|
||||
let Website = localStorage.getItem(Variables.MastodonWebsite);
|
||||
let request = localStorage.getItem(Variables.MastodonWebsite) + "/api/v1/accounts/" + ID;
|
||||
if (IsReblogged == false) {
|
||||
return await fetch(Website + "/api/v1/accounts/" + ID + "/block", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/block";
|
||||
} else {
|
||||
return await fetch(Website + "/api/v1/accounts/" + ID + "/unblock", {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
request += "/unblock";
|
||||
}
|
||||
return await fetch(request, {method: "POST", headers: {"Authorization": localStorage.getItem(Variables.MastodonTokenType) + " " + localStorage.getItem(Variables.MastodonAccessToken)}})
|
||||
.then((response) => response.json());
|
||||
}
|
||||
|
||||
// Things to make this work
|
||||
|
|
Loading…
Add table
Reference in a new issue