123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- // mastodon javascript lib
- // by @kirschn@pleasehug.me 2017
- // no fucking copyright
- // do whatever you want with it
- // but please don't hurt it (and keep this header)
- var MastodonAPI = function(config) {
- var apiBase = config.instance + "/api/v1/";
- return {
- setConfig: function (key, value) {
- config[key] = value;
- },
- getConfig: function(key) {
- return config[key];
- },
- get: function (endpoint) {
- var queryData,callback,failback,queryStringAppend = "?";
- if (typeof arguments[1] === "function") {
- queryData = {};
- callback = arguments[1];
- if(arguments[2]) failback = arguments[2];
- } else {
- queryData = arguments[1];
- callback = arguments[2];
- if(arguments[3]) failback = arguments[3];
- }
- if(typeof queryData == "string") {
- queryStringAppend = queryData;
- }
- else {
- for (var i in queryData) {
- if (queryData.hasOwnProperty(i)) {
- if (typeof queryData[i] === "string") {
- queryStringAppend += queryData[i] + "&";
- } else if (typeof queryData[i] === "object") {
- queryStringAppend += queryData[i].name + "="+ queryData[i].data + "&";
- }
- }
- }
- }
- var xquerydata = queryData;
- $.ajax({
- url: apiBase + endpoint + queryStringAppend,
- type: "GET",
- headers: {"Authorization": "Bearer " + config.api_user_token},
- success: function(data, textStatus, xhr) {
- console.log("Successful GET API request to " +apiBase+endpoint);
- responce_headers = xhr.getAllResponseHeaders();
- callback(data,textStatus);
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.get(endpoint,queryStringAppend,callback);
- }
- else if(typeof failback == "function") failback();
- else {
- if(xhr.responseText.length > 0) {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- }
- });
- },
- getArray: function (endpoint) {
- var queryData, callback,
- queryStringAppend = "?";
- if (typeof arguments[1] === "function") {
- queryData = {};
- callback = arguments[1];
- } else {
- queryData = arguments[1];
- callback = arguments[2];
- }
- for (var i in queryData) {
- if (queryData.hasOwnProperty(i)) {
- if (typeof queryData[i] === "string") {
- queryStringAppend += queryData[i] + "&";
- } else if (typeof queryData[i] === "object") {
- for ( var j in queryData[i].data ){
- queryStringAppend += queryData[i].name + "[]="+ queryData[i].data[j] + "&";
- }
- }
- }
- }
- $.ajax({
- url: apiBase + endpoint + queryStringAppend,
- type: "GET",
- headers: {"Authorization": "Bearer " + config.api_user_token},
- success: function(data, textStatus, xhr) {
- console.log("Successful GET API request to " +apiBase+endpoint);
- responce_headers = xhr.getAllResponseHeaders();
- callback(data,textStatus);
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.getArray(endpoint,queryData,callback);
- }
- else {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- });
- },
- getOther: function (domainAndEndpoint) {
- var queryData, callback,
- queryStringAppend = "?";
- if (typeof arguments[1] === "function") {
- queryData = {};
- callback = arguments[1];
- } else {
- queryData = arguments[1];
- callback = arguments[2];
- }
- for (var i in queryData) {
- if (queryData.hasOwnProperty(i)) {
- if (typeof queryData[i] === "string") {
- queryStringAppend += queryData[i] + "&";
- } else if (typeof queryData[i] === "object") {
- queryStringAppend += queryData[i].name + "="+ queryData[i].data + "&";
- }
- }
- }
- $.ajax({
- url: domainAndEndpoint + queryStringAppend,
- type: "GET",
- success: function(data, textStatus, xhr) {
- console.log("Successful GET API request to " +domainAndEndpoint);
- responce_headers = xhr.getAllResponseHeaders();
- callback(data,textStatus);
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.getOther(endpoint,queryData,callback);
- }
- else {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- });
- },
- post: function (endpoint) {
- var postData, callback;
- if (typeof arguments[1] === "function") {
- postData = {};
- callback = arguments[1];
- } else {
- postData = arguments[1];
- callback = arguments[2];
- }
- var requestHeaders = {"Authorization":"Bearer "+config.api_user_token};
- if(endpoint == "statuses") {
- if(arguments.length == 4) {
- var idempotencykey = arguments[3];
- }
- else {
- var idempotencykey = getRandom();
- }
- requestHeaders["Idempotency-Key"] = idempotencykey;
- }
- else {
- if(arguments.length == 4) {
- var errorback = arguments[3];
- }
- }
- $.ajax({
- url: apiBase + endpoint,
- type: "POST",
- data: postData,
- headers: requestHeaders,
- success: function(data, textStatus) {
- if(endpoint == "statuses" && data.visibility != "direct") {
- $(".js_current_toots_count").html(++localStorage.current_statuses_count);
- }
- else if(endpoint.indexOf("/follow") != -1) {
- $(".js_current_following_count").html(++localStorage.current_following_count);
- }
- else if(endpoint.indexOf("/unfollow") != -1) {
- $(".js_current_following_count").html(--localStorage.current_following_count);
- }
- console.log("Successful POST API request to " +apiBase+endpoint);
- callback(data,textStatus)
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.post(endpoint,postData,callback,idempotencykey);
- }
- else {
- if(xhr.status === 401) {
- location.href = "/logout";
- }
- else {
- if(errorback) {
- errorback(xhr,textStatus,errorThrown);
- }
- else {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- }
- }
- }
- }
- });
- },
- postMedia: function (endpoint) {
- var postData, callback;
- if (typeof arguments[1] === "function") {
- postData = {};
- callback = arguments[1];
- } else {
- postData = arguments[1];
- callback = arguments[2];
- }
- $.ajax({
- url: config.instance + "/api/v2/" + endpoint,
- type: "POST",
- data: postData,
- contentType: false,
- processData: false,
- headers: {"Authorization": "Bearer " + config.api_user_token},
- success: function(data, textStatus) {
- console.log("Successful POST API request to " +config.instance + "/api/v2/"+endpoint);
- callback(data,textStatus)
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.postMedia(endpoint,postData,callback);
- }
- else {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- });
- },
- delete: function (endpoint, callback) {
- $.ajax({
- url: apiBase + endpoint,
- type: "DELETE",
- headers: {"Authorization": "Bearer " + config.api_user_token},
- success: function(data, textStatus) {
- console.log("Successful DELETE API request to " +apiBase+endpoint);
- callback(data,textStatus)
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.delete(endpoint,callback);
- }
- else {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- });
- },
- patch: function (endpoint) {
- var postData, callback;
- if (typeof arguments[1] === "function") {
- postData = {};
- callback = arguments[1];
- } else {
- postData = arguments[1];
- callback = arguments[2];
- }
- var requestHeaders = {"Authorization":"Bearer "+config.api_user_token};
- $.ajax({
- url: apiBase + endpoint,
- type: "PATCH",
- data: postData,
- headers: requestHeaders,
- contentType: false,
- processData: false,
- success: function(data, textStatus) {
- console.log("Successful PATCH API request to " +apiBase+endpoint);
- callback(data,textStatus)
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.patch(endpoint,postData,callback);
- }
- else {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- });
- },
- put: function (endpoint) {
- var postData, callback;
- if (typeof arguments[1] === "function") {
- postData = {};
- callback = arguments[1];
- } else {
- postData = arguments[1];
- callback = arguments[2];
- }
- var requestHeaders = {"Authorization":"Bearer "+config.api_user_token};
- $.ajax({
- url: apiBase + endpoint,
- type: "PUT",
- data: postData,
- headers: requestHeaders,
- success: function(data, textStatus) {
- console.log("Successful PUT API request to " +apiBase+endpoint);
- callback(data,textStatus)
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.put(endpoint,postData,callback);
- }
- else {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- });
- },
- search: function (queryString) {
- var queryData,callback,failback,queryStringAppend = "?";
- if (typeof arguments[1] === "function") {
- queryData = {};
- callback = arguments[1];
- if(arguments[2]) failback = arguments[2];
- } else {
- queryData = arguments[1];
- callback = arguments[2];
- if(arguments[3]) failback = arguments[3];
- }
- if(typeof queryData == "string") {
- queryStringAppend = queryData;
- }
- else {
- for (var i in queryData) {
- if (queryData.hasOwnProperty(i)) {
- if (typeof queryData[i] === "string") {
- queryStringAppend += queryData[i] + "&";
- } else if (typeof queryData[i] === "object") {
- queryStringAppend += queryData[i].name + "="+ queryData[i].data + "&";
- }
- }
- }
- }
- var xquerydata = queryData;
- $.ajax({
- url: config.instance + "/api/v2/search?" + queryString + queryStringAppend,
- type: "GET",
- headers: {"Authorization": "Bearer " + config.api_user_token},
- success: function(data, textStatus, xhr) {
- console.log("Successful GET API request to " +config.instance + "/api/v2/search");
- responce_headers = xhr.getAllResponseHeaders();
- callback(data,textStatus);
- },
- error: function(xhr, textStatus, errorThrown) {
- if(xhr.readyState == 0) {
- api.search(queryString,queryStringAppend,callback);
- }
- else if(typeof failback == "function") failback();
- else {
- if(xhr.responseText.length > 0) {
- putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
- if ( xhr.status === 401 ) {
- location.href = "/logout";
- }
- }
- }
- }
- });
- },
- stream: function (streamType, onData) {
- var es = new WebSocket("wss://" + apiBase.substr(8) + "streaming/?access_token=" + config.api_user_token + "&stream=" + streamType);
- var listener = function(event) {
- console.log("Got Data from Stream " + streamType);
- if(event.data.length != 0) {
- event = JSON.parse(event.data);
- if(!Number.isInteger(JSON.parse(event.payload))) {
- event.payload = JSON.parse(event.payload);
- }
- onData(event);
- }
- };
- es.onmessage = listener;
- es.onclose = function(event) {
- if(event.target.readyState == 0) {
- api.stream(streamType,onData);
- }
- };
- }
- };
- };
|