mastodon.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // mastodon javascript lib
  2. // by @kirschn@pleasehug.me 2017
  3. // no fucking copyright
  4. // do whatever you want with it
  5. // but please don't hurt it (and keep this header)
  6. var MastodonAPI = function(config) {
  7. var apiBase = config.instance + "/api/v1/";
  8. return {
  9. setConfig: function (key, value) {
  10. config[key] = value;
  11. },
  12. getConfig: function(key) {
  13. return config[key];
  14. },
  15. get: function (endpoint) {
  16. var queryData,callback,failback,queryStringAppend = "?";
  17. if (typeof arguments[1] === "function") {
  18. queryData = {};
  19. callback = arguments[1];
  20. if(arguments[2]) failback = arguments[2];
  21. } else {
  22. queryData = arguments[1];
  23. callback = arguments[2];
  24. if(arguments[3]) failback = arguments[3];
  25. }
  26. if(typeof queryData == "string") {
  27. queryStringAppend = queryData;
  28. }
  29. else {
  30. for (var i in queryData) {
  31. if (queryData.hasOwnProperty(i)) {
  32. if (typeof queryData[i] === "string") {
  33. queryStringAppend += queryData[i] + "&";
  34. } else if (typeof queryData[i] === "object") {
  35. queryStringAppend += queryData[i].name + "="+ queryData[i].data + "&";
  36. }
  37. }
  38. }
  39. }
  40. var xquerydata = queryData;
  41. $.ajax({
  42. url: apiBase + endpoint + queryStringAppend,
  43. type: "GET",
  44. headers: {"Authorization": "Bearer " + config.api_user_token},
  45. success: function(data, textStatus, xhr) {
  46. console.log("Successful GET API request to " +apiBase+endpoint);
  47. responce_headers = xhr.getAllResponseHeaders();
  48. callback(data,textStatus);
  49. },
  50. error: function(xhr, textStatus, errorThrown) {
  51. if(xhr.readyState == 0) {
  52. api.get(endpoint,queryStringAppend,callback);
  53. }
  54. else if(typeof failback == "function") failback();
  55. else {
  56. if(xhr.responseText.length > 0) {
  57. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  58. if ( xhr.status === 401 ) {
  59. location.href = "/logout";
  60. }
  61. }
  62. }
  63. }
  64. });
  65. },
  66. getArray: function (endpoint) {
  67. var queryData, callback,
  68. queryStringAppend = "?";
  69. if (typeof arguments[1] === "function") {
  70. queryData = {};
  71. callback = arguments[1];
  72. } else {
  73. queryData = arguments[1];
  74. callback = arguments[2];
  75. }
  76. for (var i in queryData) {
  77. if (queryData.hasOwnProperty(i)) {
  78. if (typeof queryData[i] === "string") {
  79. queryStringAppend += queryData[i] + "&";
  80. } else if (typeof queryData[i] === "object") {
  81. for ( var j in queryData[i].data ){
  82. queryStringAppend += queryData[i].name + "[]="+ queryData[i].data[j] + "&";
  83. }
  84. }
  85. }
  86. }
  87. $.ajax({
  88. url: apiBase + endpoint + queryStringAppend,
  89. type: "GET",
  90. headers: {"Authorization": "Bearer " + config.api_user_token},
  91. success: function(data, textStatus, xhr) {
  92. console.log("Successful GET API request to " +apiBase+endpoint);
  93. responce_headers = xhr.getAllResponseHeaders();
  94. callback(data,textStatus);
  95. },
  96. error: function(xhr, textStatus, errorThrown) {
  97. if(xhr.readyState == 0) {
  98. api.getArray(endpoint,queryData,callback);
  99. }
  100. else {
  101. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  102. if ( xhr.status === 401 ) {
  103. location.href = "/logout";
  104. }
  105. }
  106. }
  107. });
  108. },
  109. getOther: function (domainAndEndpoint) {
  110. var queryData, callback,
  111. queryStringAppend = "?";
  112. if (typeof arguments[1] === "function") {
  113. queryData = {};
  114. callback = arguments[1];
  115. } else {
  116. queryData = arguments[1];
  117. callback = arguments[2];
  118. }
  119. for (var i in queryData) {
  120. if (queryData.hasOwnProperty(i)) {
  121. if (typeof queryData[i] === "string") {
  122. queryStringAppend += queryData[i] + "&";
  123. } else if (typeof queryData[i] === "object") {
  124. queryStringAppend += queryData[i].name + "="+ queryData[i].data + "&";
  125. }
  126. }
  127. }
  128. $.ajax({
  129. url: domainAndEndpoint + queryStringAppend,
  130. type: "GET",
  131. success: function(data, textStatus, xhr) {
  132. console.log("Successful GET API request to " +domainAndEndpoint);
  133. responce_headers = xhr.getAllResponseHeaders();
  134. callback(data,textStatus);
  135. },
  136. error: function(xhr, textStatus, errorThrown) {
  137. if(xhr.readyState == 0) {
  138. api.getOther(endpoint,queryData,callback);
  139. }
  140. else {
  141. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  142. if ( xhr.status === 401 ) {
  143. location.href = "/logout";
  144. }
  145. }
  146. }
  147. });
  148. },
  149. post: function (endpoint) {
  150. var postData, callback;
  151. if (typeof arguments[1] === "function") {
  152. postData = {};
  153. callback = arguments[1];
  154. } else {
  155. postData = arguments[1];
  156. callback = arguments[2];
  157. }
  158. var requestHeaders = {"Authorization":"Bearer "+config.api_user_token};
  159. if(endpoint == "statuses") {
  160. if(arguments.length == 4) {
  161. var idempotencykey = arguments[3];
  162. }
  163. else {
  164. var idempotencykey = getRandom();
  165. }
  166. requestHeaders["Idempotency-Key"] = idempotencykey;
  167. }
  168. else {
  169. if(arguments.length == 4) {
  170. var errorback = arguments[3];
  171. }
  172. }
  173. $.ajax({
  174. url: apiBase + endpoint,
  175. type: "POST",
  176. data: postData,
  177. headers: requestHeaders,
  178. success: function(data, textStatus) {
  179. if(endpoint == "statuses" && data.visibility != "direct") {
  180. $(".js_current_toots_count").html(++localStorage.current_statuses_count);
  181. }
  182. else if(endpoint.indexOf("/follow") != -1) {
  183. $(".js_current_following_count").html(++localStorage.current_following_count);
  184. }
  185. else if(endpoint.indexOf("/unfollow") != -1) {
  186. $(".js_current_following_count").html(--localStorage.current_following_count);
  187. }
  188. console.log("Successful POST API request to " +apiBase+endpoint);
  189. callback(data,textStatus)
  190. },
  191. error: function(xhr, textStatus, errorThrown) {
  192. if(xhr.readyState == 0) {
  193. api.post(endpoint,postData,callback,idempotencykey);
  194. }
  195. else {
  196. if(xhr.status === 401) {
  197. location.href = "/logout";
  198. }
  199. else {
  200. if(errorback) {
  201. errorback(xhr,textStatus,errorThrown);
  202. }
  203. else {
  204. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  205. }
  206. }
  207. }
  208. }
  209. });
  210. },
  211. postMedia: function (endpoint) {
  212. var postData, callback;
  213. if (typeof arguments[1] === "function") {
  214. postData = {};
  215. callback = arguments[1];
  216. } else {
  217. postData = arguments[1];
  218. callback = arguments[2];
  219. }
  220. $.ajax({
  221. url: config.instance + "/api/v2/" + endpoint,
  222. type: "POST",
  223. data: postData,
  224. contentType: false,
  225. processData: false,
  226. headers: {"Authorization": "Bearer " + config.api_user_token},
  227. success: function(data, textStatus) {
  228. console.log("Successful POST API request to " +config.instance + "/api/v2/"+endpoint);
  229. callback(data,textStatus)
  230. },
  231. error: function(xhr, textStatus, errorThrown) {
  232. if(xhr.readyState == 0) {
  233. api.postMedia(endpoint,postData,callback);
  234. }
  235. else {
  236. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  237. if ( xhr.status === 401 ) {
  238. location.href = "/logout";
  239. }
  240. }
  241. }
  242. });
  243. },
  244. delete: function (endpoint, callback) {
  245. $.ajax({
  246. url: apiBase + endpoint,
  247. type: "DELETE",
  248. headers: {"Authorization": "Bearer " + config.api_user_token},
  249. success: function(data, textStatus) {
  250. console.log("Successful DELETE API request to " +apiBase+endpoint);
  251. callback(data,textStatus)
  252. },
  253. error: function(xhr, textStatus, errorThrown) {
  254. if(xhr.readyState == 0) {
  255. api.delete(endpoint,callback);
  256. }
  257. else {
  258. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  259. if ( xhr.status === 401 ) {
  260. location.href = "/logout";
  261. }
  262. }
  263. }
  264. });
  265. },
  266. patch: function (endpoint) {
  267. var postData, callback;
  268. if (typeof arguments[1] === "function") {
  269. postData = {};
  270. callback = arguments[1];
  271. } else {
  272. postData = arguments[1];
  273. callback = arguments[2];
  274. }
  275. var requestHeaders = {"Authorization":"Bearer "+config.api_user_token};
  276. $.ajax({
  277. url: apiBase + endpoint,
  278. type: "PATCH",
  279. data: postData,
  280. headers: requestHeaders,
  281. contentType: false,
  282. processData: false,
  283. success: function(data, textStatus) {
  284. console.log("Successful PATCH API request to " +apiBase+endpoint);
  285. callback(data,textStatus)
  286. },
  287. error: function(xhr, textStatus, errorThrown) {
  288. if(xhr.readyState == 0) {
  289. api.patch(endpoint,postData,callback);
  290. }
  291. else {
  292. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  293. if ( xhr.status === 401 ) {
  294. location.href = "/logout";
  295. }
  296. }
  297. }
  298. });
  299. },
  300. put: function (endpoint) {
  301. var postData, callback;
  302. if (typeof arguments[1] === "function") {
  303. postData = {};
  304. callback = arguments[1];
  305. } else {
  306. postData = arguments[1];
  307. callback = arguments[2];
  308. }
  309. var requestHeaders = {"Authorization":"Bearer "+config.api_user_token};
  310. $.ajax({
  311. url: apiBase + endpoint,
  312. type: "PUT",
  313. data: postData,
  314. headers: requestHeaders,
  315. success: function(data, textStatus) {
  316. console.log("Successful PUT API request to " +apiBase+endpoint);
  317. callback(data,textStatus)
  318. },
  319. error: function(xhr, textStatus, errorThrown) {
  320. if(xhr.readyState == 0) {
  321. api.put(endpoint,postData,callback);
  322. }
  323. else {
  324. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  325. if ( xhr.status === 401 ) {
  326. location.href = "/logout";
  327. }
  328. }
  329. }
  330. });
  331. },
  332. search: function (queryString) {
  333. var queryData,callback,failback,queryStringAppend = "?";
  334. if (typeof arguments[1] === "function") {
  335. queryData = {};
  336. callback = arguments[1];
  337. if(arguments[2]) failback = arguments[2];
  338. } else {
  339. queryData = arguments[1];
  340. callback = arguments[2];
  341. if(arguments[3]) failback = arguments[3];
  342. }
  343. if(typeof queryData == "string") {
  344. queryStringAppend = queryData;
  345. }
  346. else {
  347. for (var i in queryData) {
  348. if (queryData.hasOwnProperty(i)) {
  349. if (typeof queryData[i] === "string") {
  350. queryStringAppend += queryData[i] + "&";
  351. } else if (typeof queryData[i] === "object") {
  352. queryStringAppend += queryData[i].name + "="+ queryData[i].data + "&";
  353. }
  354. }
  355. }
  356. }
  357. var xquerydata = queryData;
  358. $.ajax({
  359. url: config.instance + "/api/v2/search?" + queryString + queryStringAppend,
  360. type: "GET",
  361. headers: {"Authorization": "Bearer " + config.api_user_token},
  362. success: function(data, textStatus, xhr) {
  363. console.log("Successful GET API request to " +config.instance + "/api/v2/search");
  364. responce_headers = xhr.getAllResponseHeaders();
  365. callback(data,textStatus);
  366. },
  367. error: function(xhr, textStatus, errorThrown) {
  368. if(xhr.readyState == 0) {
  369. api.search(queryString,queryStringAppend,callback);
  370. }
  371. else if(typeof failback == "function") failback();
  372. else {
  373. if(xhr.responseText.length > 0) {
  374. putMessage(`[${xhr.status}] ${xhr.responseJSON['error']}`);
  375. if ( xhr.status === 401 ) {
  376. location.href = "/logout";
  377. }
  378. }
  379. }
  380. }
  381. });
  382. },
  383. stream: function (streamType, onData) {
  384. var es = new WebSocket("wss://" + apiBase.substr(8) + "streaming/?access_token=" + config.api_user_token + "&stream=" + streamType);
  385. var listener = function(event) {
  386. console.log("Got Data from Stream " + streamType);
  387. if(event.data.length != 0) {
  388. event = JSON.parse(event.data);
  389. if(!Number.isInteger(JSON.parse(event.payload))) {
  390. event.payload = JSON.parse(event.payload);
  391. }
  392. onData(event);
  393. }
  394. };
  395. es.onmessage = listener;
  396. es.onclose = function(event) {
  397. if(event.target.readyState == 0) {
  398. api.stream(streamType,onData);
  399. }
  400. };
  401. }
  402. };
  403. };