sidechat.js 2.2.0 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +8 -3
  2. package/dist/classes/SidechatAPIClient.d.ts +11 -1
  3. package/dist/classes/SidechatAPIClient.d.ts.map +1 -1
  4. package/dist/index.cjs +39 -1
  5. package/dist/index.mjs +39 -1
  6. package/docs/sidechat.js/2.2.0/index.html +1 -1
  7. package/docs/sidechat.js/2.2.1/SidechatAPIClient.html +3 -0
  8. package/docs/sidechat.js/2.2.1/SidechatAPIError.html +3 -0
  9. package/docs/sidechat.js/2.2.1/classes_SidechatAPIClient.js.html +688 -0
  10. package/docs/sidechat.js/2.2.1/classes_SidechatAPIError.js.html +16 -0
  11. package/docs/sidechat.js/2.2.1/data/search.json +1 -0
  12. package/docs/sidechat.js/2.2.1/fonts/Inconsolata-Regular.ttf +0 -0
  13. package/docs/sidechat.js/2.2.1/fonts/OpenSans-Regular.ttf +0 -0
  14. package/docs/sidechat.js/2.2.1/fonts/WorkSans-Bold.ttf +0 -0
  15. package/docs/sidechat.js/2.2.1/global.html +3 -0
  16. package/docs/sidechat.js/2.2.1/index.html +4 -0
  17. package/docs/sidechat.js/2.2.1/scripts/core.js +702 -0
  18. package/docs/sidechat.js/2.2.1/scripts/core.min.js +23 -0
  19. package/docs/sidechat.js/2.2.1/scripts/resize.js +90 -0
  20. package/docs/sidechat.js/2.2.1/scripts/search.js +265 -0
  21. package/docs/sidechat.js/2.2.1/scripts/search.min.js +6 -0
  22. package/docs/sidechat.js/2.2.1/scripts/third-party/Apache-License-2.0.txt +202 -0
  23. package/docs/sidechat.js/2.2.1/scripts/third-party/fuse.js +9 -0
  24. package/docs/sidechat.js/2.2.1/scripts/third-party/hljs-line-num-original.js +369 -0
  25. package/docs/sidechat.js/2.2.1/scripts/third-party/hljs-line-num.js +1 -0
  26. package/docs/sidechat.js/2.2.1/scripts/third-party/hljs-original.js +5171 -0
  27. package/docs/sidechat.js/2.2.1/scripts/third-party/hljs.js +1 -0
  28. package/docs/sidechat.js/2.2.1/scripts/third-party/popper.js +5 -0
  29. package/docs/sidechat.js/2.2.1/scripts/third-party/tippy.js +1 -0
  30. package/docs/sidechat.js/2.2.1/scripts/third-party/tocbot.js +672 -0
  31. package/docs/sidechat.js/2.2.1/scripts/third-party/tocbot.min.js +1 -0
  32. package/docs/sidechat.js/2.2.1/styles/clean-jsdoc-theme-base.css +975 -0
  33. package/docs/sidechat.js/2.2.1/styles/clean-jsdoc-theme-dark.css +407 -0
  34. package/docs/sidechat.js/2.2.1/styles/clean-jsdoc-theme-light.css +388 -0
  35. package/docs/sidechat.js/2.2.1/styles/clean-jsdoc-theme.min.css +1 -0
  36. package/docs/sidechat.js/2.2.1/tutorial-Authentication.html +14 -0
  37. package/docs/sidechat.js/2.2.1/tutorial-Email Registration.html +6 -0
  38. package/docs/sidechat.js/2.2.1/types_SidechatTypes.js.html +134 -0
  39. package/docs/sidechat.js/2.2.3/SidechatAPIClient.html +3 -0
  40. package/docs/sidechat.js/2.2.3/SidechatAPIError.html +3 -0
  41. package/docs/sidechat.js/2.2.3/classes_SidechatAPIClient.js.html +689 -0
  42. package/docs/sidechat.js/2.2.3/classes_SidechatAPIError.js.html +16 -0
  43. package/docs/sidechat.js/2.2.3/data/search.json +1 -0
  44. package/docs/sidechat.js/2.2.3/fonts/Inconsolata-Regular.ttf +0 -0
  45. package/docs/sidechat.js/2.2.3/fonts/OpenSans-Regular.ttf +0 -0
  46. package/docs/sidechat.js/2.2.3/fonts/WorkSans-Bold.ttf +0 -0
  47. package/docs/sidechat.js/2.2.3/global.html +3 -0
  48. package/docs/sidechat.js/2.2.3/index.html +4 -0
  49. package/docs/sidechat.js/2.2.3/scripts/core.js +702 -0
  50. package/docs/sidechat.js/2.2.3/scripts/core.min.js +23 -0
  51. package/docs/sidechat.js/2.2.3/scripts/resize.js +90 -0
  52. package/docs/sidechat.js/2.2.3/scripts/search.js +265 -0
  53. package/docs/sidechat.js/2.2.3/scripts/search.min.js +6 -0
  54. package/docs/sidechat.js/2.2.3/scripts/third-party/Apache-License-2.0.txt +202 -0
  55. package/docs/sidechat.js/2.2.3/scripts/third-party/fuse.js +9 -0
  56. package/docs/sidechat.js/2.2.3/scripts/third-party/hljs-line-num-original.js +369 -0
  57. package/docs/sidechat.js/2.2.3/scripts/third-party/hljs-line-num.js +1 -0
  58. package/docs/sidechat.js/2.2.3/scripts/third-party/hljs-original.js +5171 -0
  59. package/docs/sidechat.js/2.2.3/scripts/third-party/hljs.js +1 -0
  60. package/docs/sidechat.js/2.2.3/scripts/third-party/popper.js +5 -0
  61. package/docs/sidechat.js/2.2.3/scripts/third-party/tippy.js +1 -0
  62. package/docs/sidechat.js/2.2.3/scripts/third-party/tocbot.js +672 -0
  63. package/docs/sidechat.js/2.2.3/scripts/third-party/tocbot.min.js +1 -0
  64. package/docs/sidechat.js/2.2.3/styles/clean-jsdoc-theme-base.css +975 -0
  65. package/docs/sidechat.js/2.2.3/styles/clean-jsdoc-theme-dark.css +407 -0
  66. package/docs/sidechat.js/2.2.3/styles/clean-jsdoc-theme-light.css +388 -0
  67. package/docs/sidechat.js/2.2.3/styles/clean-jsdoc-theme.min.css +1 -0
  68. package/docs/sidechat.js/2.2.3/tutorial-Authentication.html +14 -0
  69. package/docs/sidechat.js/2.2.3/tutorial-Email Registration.html +6 -0
  70. package/docs/sidechat.js/2.2.3/types_SidechatTypes.js.html +134 -0
  71. package/package.json +1 -1
  72. package/src/classes/SidechatAPIClient.js +39 -1
  73. package/src/tests/colorTest.js +42 -0
  74. package/src/tests/logInTest.js +1 -0
@@ -0,0 +1,689 @@
1
+ <!DOCTYPE html><html lang="en" style="font-size:16px"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Source: classes/SidechatAPIClient.js</title><!--[if lt IE 9]>
2
+ <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
3
+ <![endif]--><script src="scripts/third-party/hljs.js" defer="defer"></script><script src="scripts/third-party/hljs-line-num.js" defer="defer"></script><script src="scripts/third-party/popper.js" defer="defer"></script><script src="scripts/third-party/tippy.js" defer="defer"></script><script src="scripts/third-party/tocbot.min.js"></script><script>var baseURL="/",locationPathname="";baseURL=(baseURL=(baseURL="https://micahlindley.com/sidechat.js/").replace(/https?:\/\//i,"")).substr(baseURL.indexOf("/"))</script><link rel="stylesheet" href="styles/clean-jsdoc-theme.min.css"><svg aria-hidden="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none"><defs><symbol id="copy-icon" viewbox="0 0 488.3 488.3"><g><path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/><path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/></g></symbol><symbol id="search-icon" viewBox="0 0 512 512"><g><g><path d="M225.474,0C101.151,0,0,101.151,0,225.474c0,124.33,101.151,225.474,225.474,225.474 c124.33,0,225.474-101.144,225.474-225.474C450.948,101.151,349.804,0,225.474,0z M225.474,409.323 c-101.373,0-183.848-82.475-183.848-183.848S124.101,41.626,225.474,41.626s183.848,82.475,183.848,183.848 S326.847,409.323,225.474,409.323z"/></g></g><g><g><path d="M505.902,476.472L386.574,357.144c-8.131-8.131-21.299-8.131-29.43,0c-8.131,8.124-8.131,21.306,0,29.43l119.328,119.328 c4.065,4.065,9.387,6.098,14.715,6.098c5.321,0,10.649-2.033,14.715-6.098C514.033,497.778,514.033,484.596,505.902,476.472z"/></g></g></symbol><symbol id="font-size-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11.246 15H4.754l-2 5H.6L7 4h2l6.4 16h-2.154l-2-5zm-.8-2L8 6.885 5.554 13h4.892zM21 12.535V12h2v8h-2v-.535a4 4 0 1 1 0-6.93zM19 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"/></symbol><symbol id="add-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 11V5h2v6h6v2h-6v6h-2v-6H5v-2z"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M5 11h14v2H5z"/></symbol><symbol id="dark-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></symbol><symbol id="light-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol><symbol id="reset-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M18.537 19.567A9.961 9.961 0 0 1 12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10c0 2.136-.67 4.116-1.81 5.74L17 12h3a8 8 0 1 0-2.46 5.772l.997 1.795z"/></symbol><symbol id="down-icon" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></symbol><symbol id="codepen-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M16.5 13.202L13 15.535v3.596L19.197 15 16.5 13.202zM14.697 12L12 10.202 9.303 12 12 13.798 14.697 12zM20 10.869L18.303 12 20 13.131V10.87zM19.197 9L13 4.869v3.596l3.5 2.333L19.197 9zM7.5 10.798L11 8.465V4.869L4.803 9 7.5 10.798zM4.803 15L11 19.131v-3.596l-3.5-2.333L4.803 15zM4 13.131L5.697 12 4 10.869v2.262zM2 9a1 1 0 0 1 .445-.832l9-6a1 1 0 0 1 1.11 0l9 6A1 1 0 0 1 22 9v6a1 1 0 0 1-.445.832l-9 6a1 1 0 0 1-1.11 0l-9-6A1 1 0 0 1 2 15V9z"/></symbol><symbol id="close-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"/></symbol><symbol id="menu-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"/></symbol></defs></svg></head><body data-theme="dark"><div class="sidebar-container"><div class="sidebar" id="sidebar"><a href="/" class="sidebar-title sidebar-title-anchor">Home</a><div class="sidebar-items-container"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-tutorials"><div>Tutorials</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="tutorial-Authentication.html">Authentication</a></div><div class="sidebar-section-children"><a href="tutorial-Email Registration.html">Email Registration</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="SidechatAPIClient.html">SidechatAPIClient</a></div><div class="sidebar-section-children"><a href="SidechatAPIError.html">SidechatAPIError</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#SidechatAssetMetadata">SidechatAssetMetadata</a></div><div class="sidebar-section-children"><a href="global.html#SidechatAuthToken">SidechatAuthToken</a></div><div class="sidebar-section-children"><a href="global.html#SidechatCurrentUser">SidechatCurrentUser</a></div><div class="sidebar-section-children"><a href="global.html#SidechatCursorString">SidechatCursorString</a></div><div class="sidebar-section-children"><a href="global.html#SidechatGroup">SidechatGroup</a></div><div class="sidebar-section-children"><a href="global.html#SidechatIdentity">SidechatIdentity</a></div><div class="sidebar-section-children"><a href="global.html#SidechatLibraryAsset">SidechatLibraryAsset</a></div><div class="sidebar-section-children"><a href="global.html#SidechatMembership">SidechatMembership</a></div><div class="sidebar-section-children"><a href="global.html#SidechatPostOrComment">SidechatPostOrComment</a></div><div class="sidebar-section-children"><a href="global.html#SidechatPostsAndCursor">SidechatPostsAndCursor</a></div><div class="sidebar-section-children"><a href="global.html#SidechatPrivateAssetURL">SidechatPrivateAssetURL</a></div><div class="sidebar-section-children"><a href="global.html#SidechatSimpleAsset">SidechatSimpleAsset</a></div><div class="sidebar-section-children"><a href="global.html#SidechatVoteString">SidechatVoteString</a></div></div></div></div></div><div class="navbar-container" id="VuAckcnZhf"><nav class="navbar"><div class="navbar-left-items"><div class="navbar-item"><a id="" href="https://github.com/micahlt/offsides#README.md" target="_blank">Offsides (Sidechat for Android)</a></div></div><div class="navbar-right-items"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#light-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div><nav></nav></nav></div><div class="toc-container"><div class="toc-content"><span class="bold">On this page</span><div id="eed4d2a0bfd64539bb9df78095dec881"></div></div></div><div class="body-wrapper"><div class="main-content"><div class="main-wrapper"><section id="source-page" class="source-page"><header><h1 id="title" class="has-anchor">classes_SidechatAPIClient.js</h1></header><article><pre class="prettyprint source lang-js"><code>import "../types/SidechatTypes.js";
4
+ import SidechatAPIError from "../classes/SidechatAPIError.js";
5
+
6
+ /**
7
+ * API client class for making requests to Sidechat's private API. You'll need to [authenticate]{@tutorial Authentication} before using most of the methods.
8
+ * @class
9
+ * @since 2.0.0-alpha.0
10
+ */
11
+ class SidechatAPIClient {
12
+ /**
13
+ * User bearer token
14
+ * @type {SidechatAuthToken}
15
+ * */
16
+ userToken;
17
+ /**
18
+ * Default headers for every API request
19
+ * @type {Object}
20
+ * @static
21
+ * @constant
22
+ */
23
+ defaultHeaders = {
24
+ Accept: "application/json",
25
+ "Content-Type": "application/json",
26
+ "App-Version": "10.0.0",
27
+ };
28
+
29
+ /**
30
+ * Create a new instance of the API client
31
+ * @param {SidechatAuthToken} [token] - user bearer token
32
+ */
33
+ constructor(token = "") {
34
+ if (token) {
35
+ this.userToken = token;
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Manually set the currently signed in user's token. Generally try to avoid this and instead either pass a token to the constructor or login automatically through the auth functions
41
+ * @method
42
+ * @param {SidechatAuthToken} token - user bearer token
43
+ */
44
+ setToken = (token) => {
45
+ this.userToken = token;
46
+ };
47
+
48
+ /**
49
+ * Initiate the login process with a phone number. Should be followed up with verifySMSCode().
50
+ * @method
51
+ * @since 1.0.0
52
+ * @param {Number} phoneNumber - US phone number (WITHOUT +1) to send verification code to
53
+ */
54
+ loginViaSMS = async (phoneNumber) => {
55
+ try {
56
+ const res = await fetch(`https://api.sidechat.lol/v1/login_register`, {
57
+ method: "POST",
58
+ headers: this.defaultHeaders,
59
+ body: JSON.stringify({
60
+ phone_number: `+1${phoneNumber}`,
61
+ version: 3,
62
+ }),
63
+ });
64
+ const json = await res.json();
65
+ return json;
66
+ } catch (err) {
67
+ console.error(err);
68
+ throw new SidechatAPIError("Failed to request SMS verification.");
69
+ }
70
+ };
71
+
72
+ /**
73
+ * Verify the code sent via SMS with loginViaSMS(). If this function succeeds, the user will be authenticated for future requests.
74
+ * @method
75
+ * @since 1.0.0
76
+ * @param {Number} phoneNumber - US phone number (WITHOUT +1) that verification code was sent to
77
+ * @param {String} code - the verification code
78
+ */
79
+ verifySMSCode = async (phoneNumber, code) => {
80
+ try {
81
+ const res = await fetch(
82
+ `https://api.sidechat.lol/v1/verify_phone_number`,
83
+ {
84
+ method: "POST",
85
+ headers: this.defaultHeaders,
86
+ body: JSON.stringify({
87
+ phone_number: `+1${phoneNumber}`,
88
+ code: code.toUpperCase(),
89
+ }),
90
+ }
91
+ );
92
+ const json = await res.json();
93
+ if (json?.logged_in_user?.token) {
94
+ this.userToken = json.logged_in_user.token;
95
+ }
96
+ return json;
97
+ } catch (err) {
98
+ console.error(err);
99
+ throw new SidechatAPIError("Failed verify this code.");
100
+ }
101
+ };
102
+
103
+ /**
104
+ * Set the user's age. If this function succeeds, the user will be authenticated for future requests.
105
+ * @method
106
+ * @since 1.0.0
107
+ * @param {Number} age - user's age in years
108
+ * @param {String} registrationID - the registration ID generated by verifySMSCode()
109
+ */
110
+ setAge = async (age, registrationID) => {
111
+ if (age &lt; 13) {
112
+ throw new SidechatAPIError("You're too young to use Offsides.");
113
+ }
114
+ try {
115
+ const res = await fetch(
116
+ `https://api.sidechat.lol/v1/complete_registration`,
117
+ {
118
+ method: "POST",
119
+ headers: this.defaultHeaders,
120
+ body: JSON.stringify({
121
+ age: Number(age),
122
+ registration_id: registrationID,
123
+ }),
124
+ }
125
+ );
126
+ const json = await res.json();
127
+ if (json.token) {
128
+ this.userToken = json.token;
129
+ }
130
+ return json;
131
+ } catch (err) {
132
+ console.error(err);
133
+ throw new SidechatAPIError("Failed verify this code.");
134
+ }
135
+ };
136
+
137
+ /**
138
+ * Initiate the email setup process. Should be followed up with checkEmailVerification().
139
+ * @method
140
+ * @since 1.0.0
141
+ * @param {String} email - school email address to send verification code to
142
+ * @tutorial Email Registration
143
+ */
144
+ registerEmail = async (email) => {
145
+ if (!this.userToken) {
146
+ throw new SidechatAPIError("User is not authenticated.");
147
+ }
148
+ try {
149
+ const res = await fetch(
150
+ `https://api.sidechat.lol/v2/users/register_email`,
151
+ {
152
+ method: "POST",
153
+ headers: {
154
+ ...this.defaultHeaders,
155
+ Authorization: `Bearer ${this.userToken}`,
156
+ },
157
+ body: JSON.stringify({
158
+ email: email,
159
+ }),
160
+ }
161
+ );
162
+ const json = await res.json();
163
+ if (json.message) {
164
+ throw new SidechatAPIError(json.message);
165
+ }
166
+ return json;
167
+ } catch (err) {
168
+ console.error(error);
169
+ throw new SidechatAPIError("Failed to request email verification.");
170
+ }
171
+ };
172
+
173
+ /**
174
+ * Check is the user's email is verified.
175
+ * @method
176
+ * @since 1.0.0
177
+ */
178
+ checkEmailVerification = async () => {
179
+ if (!this.userToken) {
180
+ throw new SidechatAPIError("User is not authenticated.");
181
+ }
182
+ try {
183
+ const res = await fetch(
184
+ `https://api.sidechat.lol/v1/users/check_email_verified`,
185
+ {
186
+ method: "GET",
187
+ headers: {
188
+ ...this.defaultHeaders,
189
+ Authorization: `Bearer ${this.userToken}`,
190
+ },
191
+ }
192
+ );
193
+ const json = await res.json();
194
+ if (json.verified_email_updates_response) {
195
+ return json.verified_email_updates_response;
196
+ } else {
197
+ throw new SidechatAPIError(json?.message || "Email is not verified.");
198
+ }
199
+ } catch (err) {
200
+ console.error(error);
201
+ throw new SidechatAPIError("Email is not verified.");
202
+ }
203
+ };
204
+
205
+ /**
206
+ * Set the device ID of the current user
207
+ * @method
208
+ * @since 1.0.0
209
+ * @param {String} deviceId - the device ID to set
210
+ */
211
+ setDeviceID = async (deviceID) => {
212
+ if (!this.userToken) {
213
+ throw new SidechatAPIError("User is not authenticated.");
214
+ }
215
+ try {
216
+ const res = await fetch(
217
+ `https://api.sidechat.lol/v1/register_device_token`,
218
+ {
219
+ method: "POST",
220
+ headers: {
221
+ ...this.defaultHeaders,
222
+ Authorization: `Bearer ${this.userToken}`,
223
+ },
224
+ body: JSON.stringify({
225
+ build_type: "release",
226
+ bundle_id: "com.flowerave.sidechat",
227
+ device_token: deviceID,
228
+ }),
229
+ }
230
+ );
231
+ const json = await res.json();
232
+ await AsyncStorage.setItem("deviceID", deviceID);
233
+ return json;
234
+ } catch (err) {
235
+ console.error(err);
236
+ throw new SidechatAPIError("Failed verify this code.");
237
+ }
238
+ };
239
+
240
+ /**
241
+ * Get updated status for user and group
242
+ * @method
243
+ * @since 1.0.0
244
+ * @deprecated since 2.1.0, will be removed by 3.0.0. Please use `getUpdates` instead!
245
+ * @param {String} [groupID] - ID of a specific group to retrieve info from. Falls back to user's primary group.
246
+ */
247
+ getUserAndGroup = async (groupID = "") => {
248
+ const json = await this.getUpdates(groupID);
249
+ return json;
250
+ };
251
+
252
+ /**
253
+ * Get updated status for user and group
254
+ * @method
255
+ * @since 2.1.0
256
+ * @param {String} [groupID] - ID of a specific group to retrieve info from. Falls back to user's primary group.
257
+ */
258
+ getUpdates = async (groupID = "") => {
259
+ if (!this.userToken) {
260
+ throw new SidechatAPIError("User is not authenticated.");
261
+ }
262
+ try {
263
+ const res = await fetch(
264
+ `https://api.sidechat.lol/v1/updates?group_id=${groupID}`,
265
+ {
266
+ method: "GET",
267
+ headers: {
268
+ ...this.defaultHeaders,
269
+ Authorization: `Bearer ${this.userToken}`,
270
+ },
271
+ }
272
+ );
273
+ const json = await res.json();
274
+ return json;
275
+ } catch (err) {
276
+ console.error(err);
277
+ throw new SidechatAPIError(`Failed to get posts from group.`);
278
+ }
279
+ };
280
+
281
+ /**
282
+ * Fetches posts from the specified category in a group
283
+ * @method
284
+ * @since 1.0.0
285
+ * @param {String} groupID - group ID
286
+ * @param {"hot"|"recent"|"top"} category - category to filter posts
287
+ * @param {SidechatCursorString} [cursor] - cursor string
288
+ * @returns {SidechatPostsAndCursor} List of posts and cursor
289
+ */
290
+ getGroupPosts = async (groupID, category = "hot", cursor) => {
291
+ if (!this.userToken) {
292
+ throw new SidechatAPIError("User is not authenticated.");
293
+ }
294
+ try {
295
+ const res = await fetch(
296
+ `https://api.sidechat.lol/v1/posts?group_id=${groupID}&amp;type=${category}${
297
+ cursor ? "&amp;cursor=" + cursor : ""
298
+ }`,
299
+ {
300
+ method: "GET",
301
+ headers: {
302
+ ...this.defaultHeaders,
303
+ Authorization: `Bearer ${this.userToken}`,
304
+ },
305
+ }
306
+ );
307
+ const json = await res.json();
308
+ return json;
309
+ } catch (err) {
310
+ console.error(err);
311
+ throw new SidechatAPIError(`Failed to get posts from group.`);
312
+ }
313
+ };
314
+
315
+ /**
316
+ * Upvote or downvote, or unvote a post
317
+ * @method
318
+ * @since 2.0.0-alpha.0
319
+ * @param {String} postID - post ID to vote on
320
+ * @param {SidechatVoteString} action - whether to upvote, downvote, or reset vote
321
+ */
322
+ setVote = async (postID, action) => {
323
+ if (!this.userToken) {
324
+ throw new SidechatAPIError("User is not authenticated.");
325
+ }
326
+ try {
327
+ const res = await fetch(`https://api.sidechat.lol/v1/posts/set_vote`, {
328
+ method: "POST",
329
+ headers: {
330
+ ...this.defaultHeaders,
331
+ Authorization: `Bearer ${this.userToken}`,
332
+ },
333
+ body: JSON.stringify({
334
+ post_id: postID,
335
+ vote_status: action,
336
+ }),
337
+ });
338
+ const json = await res.json();
339
+ return json;
340
+ } catch (err) {
341
+ console.error(err);
342
+ throw new SidechatAPIError(`Failed to change the vote on post.`);
343
+ }
344
+ };
345
+
346
+ /**
347
+ * Get all the commments on a post
348
+ * @method
349
+ * @since 2.0.0-alpha.0
350
+ * @param {String} postID - post ID to get comments for
351
+ * @returns {SidechatPostOrComment[]} list of comments
352
+ */
353
+ getPostComments = async (postID) => {
354
+ if (!this.userToken) {
355
+ throw new SidechatAPIError("User is not authenticated.");
356
+ }
357
+ try {
358
+ const res = await fetch(
359
+ `https://api.sidechat.lol/v1/posts/comments/?post_id=${postID}`,
360
+ {
361
+ method: "GET",
362
+ headers: {
363
+ ...this.defaultHeaders,
364
+ Authorization: `Bearer ${this.userToken}`,
365
+ },
366
+ }
367
+ );
368
+ const json = await res.json();
369
+ // Function to preprocess the comments and organize them into a nested structure
370
+ function preprocessComments(apiComments) {
371
+ // Map to store comments by their IDs for efficient lookup
372
+ const commentMap = new Map();
373
+ // List to store top-level comments
374
+ const topLevelComments = [];
375
+
376
+ // Iterate through the API comments
377
+ apiComments.forEach((comment) => {
378
+ // Store the comment in the map with its ID as the key
379
+ commentMap.set(comment.id, comment);
380
+ // Get the parent comment using the reply_post_id
381
+ const parentComment = commentMap.get(comment.reply_post_id);
382
+ // Check if the comment is a top-level comment
383
+ if (
384
+ !parentComment ||
385
+ comment.reply_post_id === comment.parent_post_id
386
+ ) {
387
+ // If it's a top-level comment, push it to the topLevelComments array
388
+ topLevelComments.push(comment);
389
+ } else {
390
+ // If it's a reply, add it to the parent comment's replies array
391
+ if (!parentComment.replies) parentComment.replies = [];
392
+ parentComment.replies.push(comment);
393
+ }
394
+ });
395
+
396
+ // Flatten the nested structure and return a single list of comments
397
+ return flattenComments(topLevelComments);
398
+ }
399
+
400
+ // Function to flatten nested comments into a single list
401
+ function flattenComments(comments) {
402
+ // Use reduce to flatten the nested comments array into a single list
403
+ return comments.reduce((flatComments, comment) => {
404
+ // Push the current comment to the flatComments array
405
+ flatComments.push(comment);
406
+ // If the current comment has replies, recursively flatten them and push to the flatComments array
407
+ if (comment.replies)
408
+ flatComments.push(...flattenComments(comment.replies));
409
+ // Return the flatComments array
410
+ return flatComments;
411
+ }, []);
412
+ }
413
+
414
+ const sortedComments = preprocessComments(json.posts);
415
+ return sortedComments;
416
+ } catch (err) {
417
+ console.error(err);
418
+ throw new SidechatAPIError(`Failed to get comments on post.`);
419
+ }
420
+ };
421
+
422
+ /**
423
+ * Gets groups to be displayed on the "Explore Groups" page
424
+ * @method
425
+ * @since 2.0.0-alpha.0
426
+ * @returns {SidechatGroup[]}
427
+ */
428
+ getAvailableGroups = async () => {
429
+ if (!this.userToken) {
430
+ throw new SidechatAPIError("User is not authenticated.");
431
+ }
432
+ try {
433
+ const res = await fetch(`https://api.sidechat.lol/v1/groups/explore`, {
434
+ method: "GET",
435
+ headers: {
436
+ ...this.defaultHeaders,
437
+ Authorization: `Bearer ${this.userToken}`,
438
+ },
439
+ });
440
+ const json = await res.json();
441
+ return await json.groups;
442
+ } catch (err) {
443
+ console.error(err);
444
+ throw new SidechatAPIError(`Failed to get groups from explore.`);
445
+ }
446
+ };
447
+
448
+ /**
449
+ * Retrieves the entire accessible asset library. Be warned that as of the time of this documentation, it's a 1.5MB JSON download and this request is very expensive.
450
+ * @method
451
+ * @since 2.0.6
452
+ * @returns {SidechatLibraryAsset[]}
453
+ */
454
+ getAssetLibrary = async () => {
455
+ if (!this.userToken) {
456
+ throw new SidechatAPIError("User is not authenticated.");
457
+ }
458
+ try {
459
+ const res = await fetch(`https://api.sidechat.lol/v1/assets/library`, {
460
+ method: "GET",
461
+ headers: {
462
+ ...this.defaultHeaders,
463
+ Authorization: `Bearer ${this.userToken}`,
464
+ },
465
+ });
466
+ const json = await res.json();
467
+ return await json.items;
468
+ } catch (err) {
469
+ console.error(err);
470
+ throw new SidechatAPIError(`Failed to get asset library.`);
471
+ }
472
+ };
473
+
474
+ /**
475
+ * Gets the current authenticated user and a list of the groups of which they are members.
476
+ * @method
477
+ * @since 2.1.0
478
+ * @returns {SidechatCurrentUser}
479
+ */
480
+ getCurrentUser = async () => {
481
+ if (!this.userToken) {
482
+ throw new SidechatAPIError("User is not authenticated.");
483
+ }
484
+ try {
485
+ const res = await fetch(`https://api.sidechat.lol/v1/users/me`, {
486
+ method: "GET",
487
+ headers: {
488
+ ...this.defaultHeaders,
489
+ Authorization: `Bearer ${this.userToken}`,
490
+ },
491
+ });
492
+ const json = await res.json();
493
+ return await json;
494
+ } catch (err) {
495
+ console.error(err);
496
+ throw new SidechatAPIError(`Failed to get asset library.`);
497
+ }
498
+ };
499
+
500
+ /**
501
+ * Gets the metadata of a group from its ID
502
+ * @method
503
+ * @since 2.1.0
504
+ * @param {String} [groupID] - alphanumeric ID of a group to get. Falls back to user's primary group.
505
+ * @returns {SidechatGroup}
506
+ */
507
+ getGroupMetadata = async (groupID = "") => {
508
+ if (!this.userToken) {
509
+ throw new SidechatAPIError("User is not authenticated.");
510
+ }
511
+ try {
512
+ const res = await fetch(`https://api.sidechat.lol/v1/groups/${groupID}`, {
513
+ method: "GET",
514
+ headers: {
515
+ ...this.defaultHeaders,
516
+ Authorization: `Bearer ${this.userToken}`,
517
+ },
518
+ });
519
+ const json = await res.json();
520
+ return await json.group;
521
+ } catch (err) {
522
+ console.error(err);
523
+ throw new SidechatAPIError(`Failed to get group metadata.`);
524
+ }
525
+ };
526
+
527
+ /**
528
+ * Creates a comment on a post
529
+ * @method
530
+ * @since 2.2.0
531
+ * @param {String} parentPostID - alphanumeric ID of post on which this comment is made
532
+ * @param {String} text - text content of comment
533
+ * @param {String} groupID - alphanumeric ID of group in which the parent post resides
534
+ * @param {String} [replyCommentID] - alphanumeric ID of comment to reply to. Falls back to parentPostID
535
+ * @param {SidechatSimpleAsset[]} [assetList] - list of assets to attach
536
+ * @param {Boolean} [disableDMs] - prevent direct messages being sent to comment's author
537
+ * @returns {SidechatPostOrComment} created comment
538
+ */
539
+ createComment = async (
540
+ parentPostID,
541
+ text,
542
+ groupID,
543
+ replyCommentID = parentPostID,
544
+ assetList = [],
545
+ disableDMs = false
546
+ ) => {
547
+ if (!this.userToken) {
548
+ throw new SidechatAPIError("User is not authenticated.");
549
+ }
550
+ try {
551
+ const res = await fetch(`https://api.sidechat.lol/v1/posts`, {
552
+ method: "POST",
553
+ headers: {
554
+ ...this.defaultHeaders,
555
+ Authorization: `Bearer ${this.userToken}`,
556
+ },
557
+ body: JSON.stringify({
558
+ type: "comment",
559
+ assets: assetList,
560
+ group_ids: [groupID],
561
+ text: text,
562
+ reply_post_id: replyCommentID,
563
+ parent_post_id: parentPostID,
564
+ dms_disabled: disableDMs,
565
+ }),
566
+ });
567
+ const json = await res.json();
568
+ return await json.comment;
569
+ } catch (err) {
570
+ console.error(err);
571
+ throw new SidechatAPIError(`Failed to post comment.`);
572
+ }
573
+ };
574
+
575
+ /**
576
+ * Creates a new post in the specified group
577
+ * @method
578
+ * @since 2.2.0
579
+ * @param {String} text - text content of comment
580
+ * @param {String} groupID - alphanumeric ID of group in which the parent post resides
581
+ * @param {SidechatSimpleAsset[]} [assetList] - list of assets to attach.
582
+ * @param {Boolean} [disableDMs] - prevent direct messages from being sent to post's author
583
+ * @param {Boolean} [disableComments] - whether or not comments should be disabled on post
584
+ * @returns {SidechatPostOrComment} the created post
585
+ */
586
+ createPost = async (
587
+ text,
588
+ groupID,
589
+ assetList = [],
590
+ disableDMs = false,
591
+ disableComments = false
592
+ ) => {
593
+ if (!this.userToken) {
594
+ throw new SidechatAPIError("User is not authenticated.");
595
+ }
596
+ try {
597
+ const res = await fetch(`https://api.sidechat.lol/v1/posts`, {
598
+ method: "POST",
599
+ headers: {
600
+ ...this.defaultHeaders,
601
+ Authorization: `Bearer ${this.userToken}`,
602
+ },
603
+ body: JSON.stringify({
604
+ type: "post",
605
+ assets: assetList,
606
+ group_ids: [groupID],
607
+ text: text,
608
+ attachments: [],
609
+ dms_disabled: disableDMs,
610
+ comments_disabled: disableComments,
611
+ }),
612
+ });
613
+ const json = await res.json();
614
+ return await json.posts[0];
615
+ } catch (err) {
616
+ console.error(err);
617
+ throw new SidechatAPIError(`Failed to make post.`);
618
+ }
619
+ };
620
+
621
+ /**
622
+ * Deletes a post or comment that the user created
623
+ * @method
624
+ * @since 2.2.0
625
+ * @param {String} postOrCommentID - alphanumeric ID of post to delete
626
+ */
627
+ deletePostOrComment = async (postOrCommentID) => {
628
+ if (!this.userToken) {
629
+ throw new SidechatAPIError("User is not authenticated.");
630
+ }
631
+ try {
632
+ const res = await fetch(`https://api.sidechat.lol/v1/posts/delete`, {
633
+ method: "POST",
634
+ headers: {
635
+ ...this.defaultHeaders,
636
+ Authorization: `Bearer ${this.userToken}`,
637
+ },
638
+ body: JSON.stringify({
639
+ post_id: postOrCommentID,
640
+ }),
641
+ });
642
+ const json = await res.json();
643
+ return await json;
644
+ } catch (err) {
645
+ console.error(err);
646
+ throw new SidechatAPIError(`Failed to delete post.`);
647
+ }
648
+ };
649
+
650
+ /**
651
+ * Sets the conversation icon of the current user
652
+ * @method
653
+ * @since 2.2.1
654
+ * @param {String} userID - alphanumeric ID of the post to delete
655
+ * @param {String} emoji - emoji to set as icon
656
+ * @param {String} primaryColor - hex string (including #) of primary color
657
+ * @param {String} secondaryColor - hex string (including #) of secondary color
658
+ */
659
+ setUserIcon = async (userID, emoji, primaryColor, secondaryColor) => {
660
+ if (!this.userToken) {
661
+ throw new SidechatAPIError("User is not authenticated.");
662
+ }
663
+ try {
664
+ const res = await fetch(`https://api.sidechat.lol/v1/users/${userID}`, {
665
+ method: "PATCH",
666
+ headers: {
667
+ ...this.defaultHeaders,
668
+ Authorization: `Bearer ${this.userToken}`,
669
+ },
670
+ body: JSON.stringify({
671
+ conversation_icon: {
672
+ emoji: emoji,
673
+ secondary_color: secondaryColor,
674
+ is_migrated: true,
675
+ color: primaryColor,
676
+ },
677
+ }),
678
+ });
679
+ const json = await res.json();
680
+ return await json;
681
+ } catch (err) {
682
+ console.error(err);
683
+ throw new SidechatAPIError(`Failed to set icon.`);
684
+ }
685
+ };
686
+ }
687
+
688
+ export default SidechatAPIClient;
689
+ </code></pre></article></section></div></div></div><div class="search-container" id="PkfLWpAbet" style="display:none"><div class="wrapper" id="iCxFxjkHbP"><button class="icon-button search-close-button" id="VjLlGakifb" aria-label="close search"><svg><use xlink:href="#close-icon"></use></svg></button><div class="search-box-c"><svg><use xlink:href="#search-icon"></use></svg> <input type="text" id="vpcKVYIppa" class="search-input" placeholder="Search..." autofocus></div><div class="search-result-c" id="fWwVHRuDuN"><span class="search-result-c-text">Type anything to view search result</span></div></div></div><div class="mobile-menu-icon-container"><button class="icon-button" id="mobile-menu" data-isopen="false" aria-label="menu"><svg><use xlink:href="#menu-icon"></use></svg></button></div><div id="mobile-sidebar" class="mobile-sidebar-container"><div class="mobile-sidebar-wrapper"><a href="/" class="sidebar-title sidebar-title-anchor">Home</a><div class="mobile-nav-links"><div class="navbar-item"><a id="" href="https://github.com/micahlt/offsides#README.md" target="_blank">Offsides (Sidechat for Android)</a></div></div><div class="mobile-sidebar-items-c"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-tutorials"><div>Tutorials</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="tutorial-Authentication.html">Authentication</a></div><div class="sidebar-section-children"><a href="tutorial-Email Registration.html">Email Registration</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="SidechatAPIClient.html">SidechatAPIClient</a></div><div class="sidebar-section-children"><a href="SidechatAPIError.html">SidechatAPIError</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#SidechatAssetMetadata">SidechatAssetMetadata</a></div><div class="sidebar-section-children"><a href="global.html#SidechatAuthToken">SidechatAuthToken</a></div><div class="sidebar-section-children"><a href="global.html#SidechatCurrentUser">SidechatCurrentUser</a></div><div class="sidebar-section-children"><a href="global.html#SidechatCursorString">SidechatCursorString</a></div><div class="sidebar-section-children"><a href="global.html#SidechatGroup">SidechatGroup</a></div><div class="sidebar-section-children"><a href="global.html#SidechatIdentity">SidechatIdentity</a></div><div class="sidebar-section-children"><a href="global.html#SidechatLibraryAsset">SidechatLibraryAsset</a></div><div class="sidebar-section-children"><a href="global.html#SidechatMembership">SidechatMembership</a></div><div class="sidebar-section-children"><a href="global.html#SidechatPostOrComment">SidechatPostOrComment</a></div><div class="sidebar-section-children"><a href="global.html#SidechatPostsAndCursor">SidechatPostsAndCursor</a></div><div class="sidebar-section-children"><a href="global.html#SidechatPrivateAssetURL">SidechatPrivateAssetURL</a></div><div class="sidebar-section-children"><a href="global.html#SidechatSimpleAsset">SidechatSimpleAsset</a></div><div class="sidebar-section-children"><a href="global.html#SidechatVoteString">SidechatVoteString</a></div></div></div><div class="mobile-navbar-actions"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#light-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div></div></div><script type="text/javascript" src="scripts/core.min.js"></script><script src="scripts/search.min.js" defer="defer"></script><script src="scripts/third-party/fuse.js" defer="defer"></script><script type="text/javascript">var tocbotInstance=tocbot.init({tocSelector:"#eed4d2a0bfd64539bb9df78095dec881",contentSelector:".main-content",headingSelector:"h1, h2, h3",hasInnerContainers:!0,scrollContainer:".main-content",headingsOffset:130,onClick:bringLinkToView})</script></body></html>