sidechat.js 2.6.4 → 2.6.6

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 (248) hide show
  1. package/.github/workflows/static.yml +0 -0
  2. package/README.md +0 -0
  3. package/dist/classes/SidechatAPIClient.d.ts +50 -47
  4. package/dist/classes/SidechatAPIClient.d.ts.map +1 -1
  5. package/dist/classes/SidechatAPIError.d.ts +0 -0
  6. package/dist/classes/SidechatAPIError.d.ts.map +0 -0
  7. package/dist/classes/SidechatColorList.d.ts +0 -0
  8. package/dist/classes/SidechatColorList.d.ts.map +0 -0
  9. package/dist/index.cjs +1439 -1408
  10. package/dist/index.d.mts +0 -0
  11. package/dist/index.d.ts +0 -0
  12. package/dist/index.d.ts.map +0 -0
  13. package/dist/index.mjs +1439 -1408
  14. package/dist/types/SidechatTypes.d.ts +0 -0
  15. package/dist/types/SidechatTypes.d.ts.map +0 -0
  16. package/docs/sidechat.js/2.5.6/SidechatAPIClient.html +0 -0
  17. package/docs/sidechat.js/2.5.6/SidechatAPIError.html +0 -0
  18. package/docs/sidechat.js/2.5.6/SidechatColorList.html +0 -0
  19. package/docs/sidechat.js/2.5.6/classes_SidechatAPIClient.js.html +0 -0
  20. package/docs/sidechat.js/2.5.6/classes_SidechatAPIError.js.html +0 -0
  21. package/docs/sidechat.js/2.5.6/classes_SidechatColorList.js.html +0 -0
  22. package/docs/sidechat.js/2.5.6/data/search.json +0 -0
  23. package/docs/sidechat.js/2.5.6/fonts/Inconsolata-Regular.ttf +0 -0
  24. package/docs/sidechat.js/2.5.6/fonts/OpenSans-Regular.ttf +0 -0
  25. package/docs/sidechat.js/2.5.6/fonts/WorkSans-Bold.ttf +0 -0
  26. package/docs/sidechat.js/2.5.6/global.html +0 -0
  27. package/docs/sidechat.js/2.5.6/index.html +0 -0
  28. package/docs/sidechat.js/2.5.6/scripts/core.js +0 -0
  29. package/docs/sidechat.js/2.5.6/scripts/core.min.js +0 -0
  30. package/docs/sidechat.js/2.5.6/scripts/resize.js +0 -0
  31. package/docs/sidechat.js/2.5.6/scripts/search.js +0 -0
  32. package/docs/sidechat.js/2.5.6/scripts/search.min.js +0 -0
  33. package/docs/sidechat.js/2.5.6/scripts/third-party/Apache-License-2.0.txt +0 -0
  34. package/docs/sidechat.js/2.5.6/scripts/third-party/fuse.js +0 -0
  35. package/docs/sidechat.js/2.5.6/scripts/third-party/hljs-line-num-original.js +0 -0
  36. package/docs/sidechat.js/2.5.6/scripts/third-party/hljs-line-num.js +0 -0
  37. package/docs/sidechat.js/2.5.6/scripts/third-party/hljs-original.js +0 -0
  38. package/docs/sidechat.js/2.5.6/scripts/third-party/hljs.js +0 -0
  39. package/docs/sidechat.js/2.5.6/scripts/third-party/popper.js +0 -0
  40. package/docs/sidechat.js/2.5.6/scripts/third-party/tippy.js +0 -0
  41. package/docs/sidechat.js/2.5.6/scripts/third-party/tocbot.js +0 -0
  42. package/docs/sidechat.js/2.5.6/scripts/third-party/tocbot.min.js +0 -0
  43. package/docs/sidechat.js/2.5.6/styles/clean-jsdoc-theme-base.css +0 -0
  44. package/docs/sidechat.js/2.5.6/styles/clean-jsdoc-theme-dark.css +0 -0
  45. package/docs/sidechat.js/2.5.6/styles/clean-jsdoc-theme-light.css +0 -0
  46. package/docs/sidechat.js/2.5.6/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  47. package/docs/sidechat.js/2.5.6/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  48. package/docs/sidechat.js/2.5.6/styles/clean-jsdoc-theme.min.css +0 -0
  49. package/docs/sidechat.js/2.5.6/tutorial-Authentication.html +0 -0
  50. package/docs/sidechat.js/2.5.6/tutorial-Email Registration.html +0 -0
  51. package/docs/sidechat.js/2.5.6/types_SidechatTypes.js.html +0 -0
  52. package/docs/sidechat.js/2.6.0/SidechatAPIClient.html +0 -0
  53. package/docs/sidechat.js/2.6.0/SidechatAPIError.html +0 -0
  54. package/docs/sidechat.js/2.6.0/SidechatColorList.html +0 -0
  55. package/docs/sidechat.js/2.6.0/classes_SidechatAPIClient.js.html +0 -0
  56. package/docs/sidechat.js/2.6.0/classes_SidechatAPIError.js.html +0 -0
  57. package/docs/sidechat.js/2.6.0/classes_SidechatColorList.js.html +0 -0
  58. package/docs/sidechat.js/2.6.0/data/search.json +0 -0
  59. package/docs/sidechat.js/2.6.0/fonts/Inconsolata-Regular.ttf +0 -0
  60. package/docs/sidechat.js/2.6.0/fonts/OpenSans-Regular.ttf +0 -0
  61. package/docs/sidechat.js/2.6.0/fonts/WorkSans-Bold.ttf +0 -0
  62. package/docs/sidechat.js/2.6.0/global.html +0 -0
  63. package/docs/sidechat.js/2.6.0/index.html +0 -0
  64. package/docs/sidechat.js/2.6.0/scripts/core.js +0 -0
  65. package/docs/sidechat.js/2.6.0/scripts/core.min.js +0 -0
  66. package/docs/sidechat.js/2.6.0/scripts/resize.js +0 -0
  67. package/docs/sidechat.js/2.6.0/scripts/search.js +0 -0
  68. package/docs/sidechat.js/2.6.0/scripts/search.min.js +0 -0
  69. package/docs/sidechat.js/2.6.0/scripts/third-party/Apache-License-2.0.txt +0 -0
  70. package/docs/sidechat.js/2.6.0/scripts/third-party/fuse.js +0 -0
  71. package/docs/sidechat.js/2.6.0/scripts/third-party/hljs-line-num-original.js +0 -0
  72. package/docs/sidechat.js/2.6.0/scripts/third-party/hljs-line-num.js +0 -0
  73. package/docs/sidechat.js/2.6.0/scripts/third-party/hljs-original.js +0 -0
  74. package/docs/sidechat.js/2.6.0/scripts/third-party/hljs.js +0 -0
  75. package/docs/sidechat.js/2.6.0/scripts/third-party/popper.js +0 -0
  76. package/docs/sidechat.js/2.6.0/scripts/third-party/tippy.js +0 -0
  77. package/docs/sidechat.js/2.6.0/scripts/third-party/tocbot.js +0 -0
  78. package/docs/sidechat.js/2.6.0/scripts/third-party/tocbot.min.js +0 -0
  79. package/docs/sidechat.js/2.6.0/styles/clean-jsdoc-theme-base.css +0 -0
  80. package/docs/sidechat.js/2.6.0/styles/clean-jsdoc-theme-dark.css +0 -0
  81. package/docs/sidechat.js/2.6.0/styles/clean-jsdoc-theme-light.css +0 -0
  82. package/docs/sidechat.js/2.6.0/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  83. package/docs/sidechat.js/2.6.0/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  84. package/docs/sidechat.js/2.6.0/styles/clean-jsdoc-theme.min.css +0 -0
  85. package/docs/sidechat.js/2.6.0/tutorial-Authentication.html +0 -0
  86. package/docs/sidechat.js/2.6.0/tutorial-Email Registration.html +0 -0
  87. package/docs/sidechat.js/2.6.0/types_SidechatTypes.js.html +0 -0
  88. package/docs/sidechat.js/2.6.1/SidechatAPIClient.html +0 -0
  89. package/docs/sidechat.js/2.6.1/SidechatAPIError.html +0 -0
  90. package/docs/sidechat.js/2.6.1/SidechatColorList.html +0 -0
  91. package/docs/sidechat.js/2.6.1/classes_SidechatAPIClient.js.html +0 -0
  92. package/docs/sidechat.js/2.6.1/classes_SidechatAPIError.js.html +0 -0
  93. package/docs/sidechat.js/2.6.1/classes_SidechatColorList.js.html +0 -0
  94. package/docs/sidechat.js/2.6.1/data/search.json +0 -0
  95. package/docs/sidechat.js/2.6.1/fonts/Inconsolata-Regular.ttf +0 -0
  96. package/docs/sidechat.js/2.6.1/fonts/OpenSans-Regular.ttf +0 -0
  97. package/docs/sidechat.js/2.6.1/fonts/WorkSans-Bold.ttf +0 -0
  98. package/docs/sidechat.js/2.6.1/global.html +0 -0
  99. package/docs/sidechat.js/2.6.1/index.html +0 -0
  100. package/docs/sidechat.js/2.6.1/scripts/core.js +0 -0
  101. package/docs/sidechat.js/2.6.1/scripts/core.min.js +0 -0
  102. package/docs/sidechat.js/2.6.1/scripts/resize.js +0 -0
  103. package/docs/sidechat.js/2.6.1/scripts/search.js +0 -0
  104. package/docs/sidechat.js/2.6.1/scripts/search.min.js +0 -0
  105. package/docs/sidechat.js/2.6.1/scripts/third-party/Apache-License-2.0.txt +0 -0
  106. package/docs/sidechat.js/2.6.1/scripts/third-party/fuse.js +0 -0
  107. package/docs/sidechat.js/2.6.1/scripts/third-party/hljs-line-num-original.js +0 -0
  108. package/docs/sidechat.js/2.6.1/scripts/third-party/hljs-line-num.js +0 -0
  109. package/docs/sidechat.js/2.6.1/scripts/third-party/hljs-original.js +0 -0
  110. package/docs/sidechat.js/2.6.1/scripts/third-party/hljs.js +0 -0
  111. package/docs/sidechat.js/2.6.1/scripts/third-party/popper.js +0 -0
  112. package/docs/sidechat.js/2.6.1/scripts/third-party/tippy.js +0 -0
  113. package/docs/sidechat.js/2.6.1/scripts/third-party/tocbot.js +0 -0
  114. package/docs/sidechat.js/2.6.1/scripts/third-party/tocbot.min.js +0 -0
  115. package/docs/sidechat.js/2.6.1/styles/clean-jsdoc-theme-base.css +0 -0
  116. package/docs/sidechat.js/2.6.1/styles/clean-jsdoc-theme-dark.css +0 -0
  117. package/docs/sidechat.js/2.6.1/styles/clean-jsdoc-theme-light.css +0 -0
  118. package/docs/sidechat.js/2.6.1/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  119. package/docs/sidechat.js/2.6.1/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  120. package/docs/sidechat.js/2.6.1/styles/clean-jsdoc-theme.min.css +0 -0
  121. package/docs/sidechat.js/2.6.1/tutorial-Authentication.html +0 -0
  122. package/docs/sidechat.js/2.6.1/tutorial-Email Registration.html +0 -0
  123. package/docs/sidechat.js/2.6.1/types_SidechatTypes.js.html +0 -0
  124. package/docs/sidechat.js/2.6.4/SidechatAPIClient.html +0 -0
  125. package/docs/sidechat.js/2.6.4/SidechatAPIError.html +0 -0
  126. package/docs/sidechat.js/2.6.4/SidechatColorList.html +0 -0
  127. package/docs/sidechat.js/2.6.4/classes_SidechatAPIClient.js.html +0 -0
  128. package/docs/sidechat.js/2.6.4/classes_SidechatAPIError.js.html +0 -0
  129. package/docs/sidechat.js/2.6.4/classes_SidechatColorList.js.html +0 -0
  130. package/docs/sidechat.js/2.6.4/data/search.json +0 -0
  131. package/docs/sidechat.js/2.6.4/fonts/Inconsolata-Regular.ttf +0 -0
  132. package/docs/sidechat.js/2.6.4/fonts/OpenSans-Regular.ttf +0 -0
  133. package/docs/sidechat.js/2.6.4/fonts/WorkSans-Bold.ttf +0 -0
  134. package/docs/sidechat.js/2.6.4/global.html +0 -0
  135. package/docs/sidechat.js/2.6.4/index.html +0 -0
  136. package/docs/sidechat.js/2.6.4/scripts/core.js +0 -0
  137. package/docs/sidechat.js/2.6.4/scripts/core.min.js +0 -0
  138. package/docs/sidechat.js/2.6.4/scripts/resize.js +0 -0
  139. package/docs/sidechat.js/2.6.4/scripts/search.js +0 -0
  140. package/docs/sidechat.js/2.6.4/scripts/search.min.js +0 -0
  141. package/docs/sidechat.js/2.6.4/scripts/third-party/Apache-License-2.0.txt +0 -0
  142. package/docs/sidechat.js/2.6.4/scripts/third-party/fuse.js +0 -0
  143. package/docs/sidechat.js/2.6.4/scripts/third-party/hljs-line-num-original.js +0 -0
  144. package/docs/sidechat.js/2.6.4/scripts/third-party/hljs-line-num.js +0 -0
  145. package/docs/sidechat.js/2.6.4/scripts/third-party/hljs-original.js +0 -0
  146. package/docs/sidechat.js/2.6.4/scripts/third-party/hljs.js +0 -0
  147. package/docs/sidechat.js/2.6.4/scripts/third-party/popper.js +0 -0
  148. package/docs/sidechat.js/2.6.4/scripts/third-party/tippy.js +0 -0
  149. package/docs/sidechat.js/2.6.4/scripts/third-party/tocbot.js +0 -0
  150. package/docs/sidechat.js/2.6.4/scripts/third-party/tocbot.min.js +0 -0
  151. package/docs/sidechat.js/2.6.4/styles/clean-jsdoc-theme-base.css +0 -0
  152. package/docs/sidechat.js/2.6.4/styles/clean-jsdoc-theme-dark.css +0 -0
  153. package/docs/sidechat.js/2.6.4/styles/clean-jsdoc-theme-light.css +0 -0
  154. package/docs/sidechat.js/2.6.4/styles/clean-jsdoc-theme-scrollbar.css +0 -0
  155. package/docs/sidechat.js/2.6.4/styles/clean-jsdoc-theme-without-scrollbar.min.css +0 -0
  156. package/docs/sidechat.js/2.6.4/styles/clean-jsdoc-theme.min.css +0 -0
  157. package/docs/sidechat.js/2.6.4/tutorial-Authentication.html +0 -0
  158. package/docs/sidechat.js/2.6.4/tutorial-Email Registration.html +0 -0
  159. package/docs/sidechat.js/2.6.4/types_SidechatTypes.js.html +0 -0
  160. package/docs/sidechat.js/2.6.5/SidechatAPIClient.html +3 -0
  161. package/docs/sidechat.js/2.6.5/SidechatAPIError.html +3 -0
  162. package/docs/sidechat.js/2.6.5/SidechatColorList.html +3 -0
  163. package/docs/sidechat.js/2.6.5/classes_SidechatAPIClient.js.html +1429 -0
  164. package/docs/sidechat.js/2.6.5/classes_SidechatAPIError.js.html +16 -0
  165. package/docs/sidechat.js/2.6.5/classes_SidechatColorList.js.html +79 -0
  166. package/docs/sidechat.js/2.6.5/data/search.json +1 -0
  167. package/docs/sidechat.js/2.6.5/fonts/Inconsolata-Regular.ttf +0 -0
  168. package/docs/sidechat.js/2.6.5/fonts/OpenSans-Regular.ttf +0 -0
  169. package/docs/sidechat.js/2.6.5/fonts/WorkSans-Bold.ttf +0 -0
  170. package/docs/sidechat.js/2.6.5/global.html +3 -0
  171. package/docs/sidechat.js/2.6.5/index.html +4 -0
  172. package/docs/sidechat.js/2.6.5/scripts/core.js +726 -0
  173. package/docs/sidechat.js/2.6.5/scripts/core.min.js +23 -0
  174. package/docs/sidechat.js/2.6.5/scripts/resize.js +90 -0
  175. package/docs/sidechat.js/2.6.5/scripts/search.js +265 -0
  176. package/docs/sidechat.js/2.6.5/scripts/search.min.js +6 -0
  177. package/docs/sidechat.js/2.6.5/scripts/third-party/Apache-License-2.0.txt +202 -0
  178. package/docs/sidechat.js/2.6.5/scripts/third-party/fuse.js +9 -0
  179. package/docs/sidechat.js/2.6.5/scripts/third-party/hljs-line-num-original.js +369 -0
  180. package/docs/sidechat.js/2.6.5/scripts/third-party/hljs-line-num.js +1 -0
  181. package/docs/sidechat.js/2.6.5/scripts/third-party/hljs-original.js +5171 -0
  182. package/docs/sidechat.js/2.6.5/scripts/third-party/hljs.js +1 -0
  183. package/docs/sidechat.js/2.6.5/scripts/third-party/popper.js +5 -0
  184. package/docs/sidechat.js/2.6.5/scripts/third-party/tippy.js +1 -0
  185. package/docs/sidechat.js/2.6.5/scripts/third-party/tocbot.js +672 -0
  186. package/docs/sidechat.js/2.6.5/scripts/third-party/tocbot.min.js +1 -0
  187. package/docs/sidechat.js/2.6.5/styles/clean-jsdoc-theme-base.css +1159 -0
  188. package/docs/sidechat.js/2.6.5/styles/clean-jsdoc-theme-dark.css +412 -0
  189. package/docs/sidechat.js/2.6.5/styles/clean-jsdoc-theme-light.css +482 -0
  190. package/docs/sidechat.js/2.6.5/styles/clean-jsdoc-theme-scrollbar.css +30 -0
  191. package/docs/sidechat.js/2.6.5/styles/clean-jsdoc-theme-without-scrollbar.min.css +1 -0
  192. package/docs/sidechat.js/2.6.5/styles/clean-jsdoc-theme.min.css +1 -0
  193. package/docs/sidechat.js/2.6.5/tutorial-Authentication.html +14 -0
  194. package/docs/sidechat.js/2.6.5/tutorial-Email Registration.html +6 -0
  195. package/docs/sidechat.js/2.6.5/types_SidechatTypes.js.html +204 -0
  196. package/docs/sidechat.js/2.6.6/SidechatAPIClient.html +3 -0
  197. package/docs/sidechat.js/2.6.6/SidechatAPIError.html +3 -0
  198. package/docs/sidechat.js/2.6.6/SidechatColorList.html +3 -0
  199. package/docs/sidechat.js/2.6.6/classes_SidechatAPIClient.js.html +1447 -0
  200. package/docs/sidechat.js/2.6.6/classes_SidechatAPIError.js.html +16 -0
  201. package/docs/sidechat.js/2.6.6/classes_SidechatColorList.js.html +79 -0
  202. package/docs/sidechat.js/2.6.6/data/search.json +1 -0
  203. package/docs/sidechat.js/2.6.6/fonts/Inconsolata-Regular.ttf +0 -0
  204. package/docs/sidechat.js/2.6.6/fonts/OpenSans-Regular.ttf +0 -0
  205. package/docs/sidechat.js/2.6.6/fonts/WorkSans-Bold.ttf +0 -0
  206. package/docs/sidechat.js/2.6.6/global.html +3 -0
  207. package/docs/sidechat.js/2.6.6/index.html +4 -0
  208. package/docs/sidechat.js/2.6.6/scripts/core.js +726 -0
  209. package/docs/sidechat.js/2.6.6/scripts/core.min.js +23 -0
  210. package/docs/sidechat.js/2.6.6/scripts/resize.js +90 -0
  211. package/docs/sidechat.js/2.6.6/scripts/search.js +265 -0
  212. package/docs/sidechat.js/2.6.6/scripts/search.min.js +6 -0
  213. package/docs/sidechat.js/2.6.6/scripts/third-party/Apache-License-2.0.txt +202 -0
  214. package/docs/sidechat.js/2.6.6/scripts/third-party/fuse.js +9 -0
  215. package/docs/sidechat.js/2.6.6/scripts/third-party/hljs-line-num-original.js +369 -0
  216. package/docs/sidechat.js/2.6.6/scripts/third-party/hljs-line-num.js +1 -0
  217. package/docs/sidechat.js/2.6.6/scripts/third-party/hljs-original.js +5171 -0
  218. package/docs/sidechat.js/2.6.6/scripts/third-party/hljs.js +1 -0
  219. package/docs/sidechat.js/2.6.6/scripts/third-party/popper.js +5 -0
  220. package/docs/sidechat.js/2.6.6/scripts/third-party/tippy.js +1 -0
  221. package/docs/sidechat.js/2.6.6/scripts/third-party/tocbot.js +672 -0
  222. package/docs/sidechat.js/2.6.6/scripts/third-party/tocbot.min.js +1 -0
  223. package/docs/sidechat.js/2.6.6/styles/clean-jsdoc-theme-base.css +1159 -0
  224. package/docs/sidechat.js/2.6.6/styles/clean-jsdoc-theme-dark.css +412 -0
  225. package/docs/sidechat.js/2.6.6/styles/clean-jsdoc-theme-light.css +482 -0
  226. package/docs/sidechat.js/2.6.6/styles/clean-jsdoc-theme-scrollbar.css +30 -0
  227. package/docs/sidechat.js/2.6.6/styles/clean-jsdoc-theme-without-scrollbar.min.css +1 -0
  228. package/docs/sidechat.js/2.6.6/styles/clean-jsdoc-theme.min.css +1 -0
  229. package/docs/sidechat.js/2.6.6/tutorial-Authentication.html +14 -0
  230. package/docs/sidechat.js/2.6.6/tutorial-Email Registration.html +6 -0
  231. package/docs/sidechat.js/2.6.6/types_SidechatTypes.js.html +204 -0
  232. package/docs/tutorials/Authentication.md +0 -0
  233. package/docs/tutorials/Email Registration.md +0 -0
  234. package/jsdoc.json +0 -0
  235. package/package.json +1 -1
  236. package/rollup.config.mjs +0 -0
  237. package/src/classes/SidechatAPIClient.js +1444 -1414
  238. package/src/classes/SidechatAPIError.js +0 -0
  239. package/src/classes/SidechatColorList.js +0 -0
  240. package/src/index.js +0 -0
  241. package/src/tests/colorTest.js +0 -0
  242. package/src/tests/commentTest.js +0 -0
  243. package/src/tests/directMessageTest.js +0 -0
  244. package/src/tests/logInTest.js +0 -0
  245. package/src/tests/postTest.js +0 -0
  246. package/src/tests/updatesTest.js +0 -0
  247. package/src/types/SidechatTypes.js +0 -0
  248. package/tsconfig.json +0 -0
@@ -1,1414 +1,1444 @@
1
- import "../types/SidechatTypes.js";
2
- import SidechatAPIError from "../classes/SidechatAPIError.js";
3
-
4
- /**
5
- * API client class for making requests to Sidechat's private API. You'll need to [authenticate]{@tutorial Authentication} before using most of the methods.
6
- * @class
7
- * @since 2.0.0-alpha.0
8
- */
9
- class SidechatAPIClient {
10
- /**
11
- * User bearer token
12
- * @type {SidechatAuthToken}
13
- * */
14
- userToken;
15
-
16
- /**
17
- * Default headers for every API request
18
- * @type {Object}
19
- * @static
20
- * @constant
21
- */
22
- defaultHeaders = {
23
- Accept: "application/json",
24
- "Content-Type": "application/json",
25
- "App-Version": "6.0.0",
26
- Dnt: 1,
27
- };
28
-
29
- /**
30
- * Root URL for every API request
31
- * @type {String}
32
- * @default "https://api.sidechat.lol"
33
- */
34
- apiRoot = "https://api.sidechat.lol";
35
-
36
- /**
37
- * Create a new instance of the API client
38
- * @param {SidechatAuthToken} [token] - user bearer token
39
- * @param {String} rootUrl - custom API root URL for mocking or using other server
40
- */
41
- constructor(token = "", rootUrl = "") {
42
- if (token) {
43
- this.userToken = token;
44
- }
45
- if (rootUrl) {
46
- this.apiRoot = rootUrl;
47
- }
48
- }
49
-
50
- /**
51
- * 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
52
- * @method
53
- * @param {SidechatAuthToken} token - user bearer token
54
- */
55
- setToken = (token) => {
56
- this.userToken = token;
57
- };
58
-
59
- /**
60
- * Manually set the root URL for all API requests. This can be used for mocking requests or redirecting them to a different server
61
- * @method
62
- * @param {String} url - new root URL to set
63
- * @since 2.3.9
64
- */
65
- setAPIRoot = (url) => {
66
- this.apiRoot = url;
67
- };
68
-
69
- /**
70
- * Run an arbitrary API request using the current client's authentication
71
- * @method
72
- * @param {String} endpoint - API endpoint to request (e.g. "/v1/posts")
73
- * @param {"GET"|"POST"|"PUT"|"DELETE"|"PATCH"|"OPTIONS"} [method] - HTTP method to use
74
- * @param {Object} [body] - body to send with the request
75
- * @param {Object} [headers] - custom headers to send with the request
76
- * @param {Boolean} [stripHeaders] - remove the default headers from the request
77
- * @since 2.4.9
78
- */
79
- sendRequest = (
80
- endpoint,
81
- method = "GET",
82
- body = undefined,
83
- headers = {},
84
- stripHeaders = false
85
- ) => {
86
- let requestHeaders = this.defaultHeaders;
87
- if (stripHeaders) {
88
- headers = {};
89
- }
90
- requestHeaders = { ...requestHeaders, ...headers };
91
- return fetch(`${this.apiRoot}${endpoint}`, {
92
- headers: { Authorization: `Bearer ${this.userToken}`, ...requestHeaders },
93
- body: body,
94
- method: method,
95
- });
96
- };
97
-
98
- /**
99
- * Initiate the login process with a phone number. Should be followed up with verifySMSCode().
100
- * @method
101
- * @since 1.0.0
102
- * @param {Number} phoneNumber - US phone number (WITHOUT +1) to send verification code to
103
- */
104
- loginViaSMS = async (phoneNumber) => {
105
- try {
106
- const res = await fetch(`${this.apiRoot}/v1/login_register`, {
107
- method: "POST",
108
- headers: this.defaultHeaders,
109
- body: JSON.stringify({
110
- phone_number: `+1${phoneNumber}`,
111
- version: 3,
112
- }),
113
- });
114
- const json = await res.json();
115
- return json;
116
- } catch (err) {
117
- console.error(err);
118
- throw new SidechatAPIError("Failed to request SMS verification.");
119
- }
120
- };
121
-
122
- /**
123
- * Verify the code sent via SMS with loginViaSMS(). If this function succeeds, the user will be authenticated for future requests.
124
- * @method
125
- * @since 1.0.0
126
- * @param {Number} phoneNumber - US phone number (WITHOUT +1) that verification code was sent to
127
- * @param {String} code - the verification code
128
- */
129
- verifySMSCode = async (phoneNumber, code) => {
130
- try {
131
- const res = await fetch(`${this.apiRoot}/v1/verify_phone_number`, {
132
- method: "POST",
133
- headers: this.defaultHeaders,
134
- body: JSON.stringify({
135
- phone_number: `+1${phoneNumber}`,
136
- code: code.toUpperCase(),
137
- }),
138
- });
139
- const json = await res.json();
140
- if (json?.logged_in_user?.token) {
141
- this.userToken = json.logged_in_user.token;
142
- }
143
- return json;
144
- } catch (err) {
145
- console.error(err);
146
- throw new SidechatAPIError("Failed verify this code.");
147
- }
148
- };
149
-
150
- /**
151
- * Set the user's age. If this function succeeds, the user will be authenticated for future requests.
152
- * @method
153
- * @since 1.0.0
154
- * @param {Number} age - user's age in years
155
- * @param {String} registrationID - the registration ID generated by verifySMSCode()
156
- */
157
- setAge = async (age, registrationID) => {
158
- if (age < 13) {
159
- throw new SidechatAPIError("You're too young to use Offsides.");
160
- }
161
- try {
162
- const res = await fetch(`${this.apiRoot}/v1/complete_registration`, {
163
- method: "POST",
164
- headers: this.defaultHeaders,
165
- body: JSON.stringify({
166
- age: Number(age),
167
- registration_id: registrationID,
168
- }),
169
- });
170
- const json = await res.json();
171
- if (json.token) {
172
- this.userToken = json.token;
173
- }
174
- return json;
175
- } catch (err) {
176
- console.error(err);
177
- throw new SidechatAPIError("Failed verify this code.");
178
- }
179
- };
180
-
181
- /**
182
- * Initiate the email setup process. Should be followed up with checkEmailVerification().
183
- * @method
184
- * @since 1.0.0
185
- * @param {String} email - school email address to send verification code to
186
- * @tutorial Email Registration
187
- */
188
- registerEmail = async (email) => {
189
- if (!this.userToken) {
190
- throw new SidechatAPIError("User is not authenticated.");
191
- }
192
- try {
193
- const res = await fetch(`${this.apiRoot}/v2/users/register_email`, {
194
- method: "POST",
195
- headers: {
196
- ...this.defaultHeaders,
197
- Authorization: `Bearer ${this.userToken}`,
198
- },
199
- body: JSON.stringify({
200
- email: email,
201
- }),
202
- });
203
- const json = await res.json();
204
- if (json.message) {
205
- throw new SidechatAPIError(json.message);
206
- }
207
- return json;
208
- } catch (err) {
209
- console.error(err);
210
- throw new SidechatAPIError("Failed to request email verification.");
211
- }
212
- };
213
-
214
- /**
215
- * Check is the user's email is verified.
216
- * @method
217
- * @since 1.0.0
218
- */
219
- checkEmailVerification = async () => {
220
- if (!this.userToken) {
221
- throw new SidechatAPIError("User is not authenticated.");
222
- }
223
- try {
224
- const res = await fetch(`${this.apiRoot}/v1/users/check_email_verified`, {
225
- method: "GET",
226
- headers: {
227
- ...this.defaultHeaders,
228
- Authorization: `Bearer ${this.userToken}`,
229
- },
230
- });
231
- const json = await res.json();
232
- if (json.verified_email_updates_response) {
233
- return json.verified_email_updates_response;
234
- } else if (json.changing_phone_number_verified_email_user) {
235
- return json.changing_phone_number_verified_email_user;
236
- } else {
237
- throw new SidechatAPIError(json?.message || "Email is not verified.");
238
- }
239
- } catch (err) {
240
- console.error(err);
241
- throw new SidechatAPIError("Email is not verified.");
242
- }
243
- };
244
-
245
- /**
246
- * Set the device ID of the current user
247
- * @method
248
- * @since 1.0.0
249
- * @param {String} deviceId - the device ID to set
250
- */
251
- setDeviceID = async (deviceID) => {
252
- if (!this.userToken) {
253
- throw new SidechatAPIError("User is not authenticated.");
254
- }
255
- try {
256
- const res = await fetch(`${this.apiRoot}/v1/register_device_token`, {
257
- method: "POST",
258
- headers: {
259
- ...this.defaultHeaders,
260
- Authorization: `Bearer ${this.userToken}`,
261
- },
262
- body: JSON.stringify({
263
- build_type: "release",
264
- bundle_id: "com.flowerave.sidechat",
265
- device_token: deviceID,
266
- }),
267
- });
268
- const json = await res.json();
269
- return json;
270
- } catch (err) {
271
- console.error(err);
272
- throw new SidechatAPIError("Failed verify this code.");
273
- }
274
- };
275
-
276
- /**
277
- * Get updated status for user and group
278
- * @method
279
- * @since 1.0.0
280
- * @deprecated since 2.1.0, will be removed by 3.0.0. Please use `getUpdates` instead!
281
- * @param {String} [groupID] - ID of a specific group to retrieve info from. Falls back to user's primary group.
282
- */
283
- getUserAndGroup = async (groupID = "") => {
284
- const json = await this.getUpdates(groupID);
285
- return json;
286
- };
287
-
288
- /**
289
- * Get updated status for user and group
290
- * @method
291
- * @since 2.1.0
292
- * @param {String} [groupID] - ID of a specific group to retrieve info from. Falls back to user's primary group.
293
- */
294
- getUpdates = async (groupID = "") => {
295
- if (!this.userToken) {
296
- throw new SidechatAPIError("User is not authenticated.");
297
- }
298
- try {
299
- const res = await fetch(
300
- `${this.apiRoot}/v1/updates?group_id=${groupID}`,
301
- {
302
- method: "GET",
303
- headers: {
304
- ...this.defaultHeaders,
305
- Authorization: `Bearer ${this.userToken}`,
306
- },
307
- }
308
- );
309
- const json = await res.json();
310
- return json;
311
- } catch (err) {
312
- console.error(err);
313
- throw new SidechatAPIError(`Failed to get posts from group.`);
314
- }
315
- };
316
-
317
- /**
318
- * Fetches posts from the specified category in a group
319
- * @method
320
- * @since 1.0.0
321
- * @param {String} groupID - group ID
322
- * @param {"hot"|"recent"|"top"} category - category to filter posts
323
- * @param {SidechatCursorString} [cursor] - cursor string
324
- * @returns {SidechatPostsAndCursor} List of posts and cursor
325
- */
326
- getGroupPosts = async (groupID, category = "hot", cursor) => {
327
- if (!this.userToken) {
328
- throw new SidechatAPIError("User is not authenticated.");
329
- }
330
- try {
331
- const res = await fetch(
332
- `${this.apiRoot}/v1/posts?${cursor ? "cursor=" + encodeURIComponent(cursor) + "&" : ""
333
- }group_id=${groupID}&type=${category}`,
334
- {
335
- method: "GET",
336
- headers: {
337
- ...this.defaultHeaders,
338
- Authorization: `Bearer ${this.userToken}`,
339
- },
340
- }
341
- );
342
- const json = await res.json();
343
- return json;
344
- } catch (err) {
345
- console.error(err);
346
- throw new SidechatAPIError(`Failed to get posts from group.`);
347
- }
348
- };
349
-
350
- /**
351
- * Upvote or downvote, or unvote a post
352
- * @method
353
- * @since 2.0.0-alpha.0
354
- * @param {String} postID - post ID to vote on
355
- * @param {SidechatVoteString} action - whether to upvote, downvote, or reset vote
356
- */
357
- setVote = async (postID, action) => {
358
- if (!this.userToken) {
359
- throw new SidechatAPIError("User is not authenticated.");
360
- }
361
- try {
362
- const res = await fetch(`${this.apiRoot}/v1/posts/set_vote`, {
363
- method: "POST",
364
- headers: {
365
- ...this.defaultHeaders,
366
- Authorization: `Bearer ${this.userToken}`,
367
- },
368
- body: JSON.stringify({
369
- post_id: postID,
370
- vote_status: action,
371
- }),
372
- });
373
- const json = await res.json();
374
- return json;
375
- } catch (err) {
376
- console.error(err);
377
- throw new SidechatAPIError(`Failed to change the vote on post.`);
378
- }
379
- };
380
-
381
- /**
382
- * Fetches a single post with just its ID
383
- * @method
384
- * @since 2.3.0
385
- * @param {String} postID - ID of post to fetch
386
- * @param {Boolean} includeDeleted - undocumented
387
- * @returns {SidechatPostOrComment} post object
388
- */
389
- getPost = async (postID, includeDeleted = false) => {
390
- if (!this.userToken) {
391
- throw new SidechatAPIError("User is not authenticated.");
392
- }
393
- try {
394
- const res = await fetch(
395
- `${this.apiRoot}/v1/posts/get?include_deleted=${includeDeleted}&post_id=${postID}`,
396
- {
397
- method: "GET",
398
- headers: {
399
- ...this.defaultHeaders,
400
- Authorization: `Bearer ${this.userToken}`,
401
- },
402
- }
403
- );
404
- const json = await res.json();
405
- return json.post;
406
- } catch (err) {
407
- console.error(err);
408
- throw new SidechatAPIError(`Failed to get post from ID.`);
409
- }
410
- };
411
-
412
- /**
413
- * Fetches the posts or comments that the user has created
414
- * @method
415
- * @since 2.3.5
416
- * @param {"posts"|"comments"} contentType - type of user content to fetch
417
- * @returns {SidechatPostOrComment[]} post object
418
- */
419
- getUserContent = async (contentType) => {
420
- if (!this.userToken) {
421
- throw new SidechatAPIError("User is not authenticated.");
422
- }
423
- if (contentType == "posts") {
424
- contentType = "my_posts";
425
- } else if (contentType == "comments") {
426
- contentType = "my_comments";
427
- }
428
- try {
429
- const res = await fetch(`${this.apiRoot}/v1/posts&type=${contentType}`, {
430
- method: "GET",
431
- headers: {
432
- ...this.defaultHeaders,
433
- Authorization: `Bearer ${this.userToken}`,
434
- },
435
- });
436
- const json = await res.json();
437
- return json.posts;
438
- } catch (err) {
439
- console.error(err);
440
- throw new SidechatAPIError(`Failed to get content from user.`);
441
- }
442
- };
443
-
444
- /**
445
- * Get all the commments on a post
446
- * @method
447
- * @since 2.0.0-alpha.0
448
- * @param {String} postID - post ID to get comments for
449
- * @returns {SidechatPostOrComment[]} list of comments
450
- */
451
- getPostComments = async (postID) => {
452
- if (!this.userToken) {
453
- throw new SidechatAPIError("User is not authenticated.");
454
- }
455
- try {
456
- const res = await fetch(
457
- `${this.apiRoot}/v1/posts/comments/?post_id=${postID}`,
458
- {
459
- method: "GET",
460
- headers: {
461
- ...this.defaultHeaders,
462
- Authorization: `Bearer ${this.userToken}`,
463
- },
464
- }
465
- );
466
- const json = await res.json();
467
- // Function to preprocess the comments and organize them into a nested structure
468
- function preprocessComments(apiComments) {
469
- // Map to store comments by their IDs for efficient lookup
470
- const commentMap = new Map();
471
- // List to store top-level comments
472
- const topLevelComments = [];
473
-
474
- // Iterate through the API comments
475
- apiComments.forEach((comment) => {
476
- // Store the comment in the map with its ID as the key
477
- commentMap.set(comment.id, comment);
478
- // Get the parent comment using the reply_post_id
479
- const parentComment = commentMap.get(comment.reply_post_id);
480
- // Check if the comment is a top-level comment
481
- if (
482
- !parentComment ||
483
- comment.reply_post_id === comment.parent_post_id
484
- ) {
485
- // If it's a top-level comment, push it to the topLevelComments array
486
- topLevelComments.push(comment);
487
- } else {
488
- // If it's a reply, add it to the parent comment's replies array
489
- if (!parentComment.replies) parentComment.replies = [];
490
- parentComment.replies.push(comment);
491
- }
492
- });
493
-
494
- // Flatten the nested structure and return a single list of comments
495
- return flattenComments(topLevelComments);
496
- }
497
-
498
- // Function to flatten nested comments into a single list
499
- function flattenComments(comments) {
500
- // Use reduce to flatten the nested comments array into a single list
501
- return comments.reduce((flatComments, comment) => {
502
- // Push the current comment to the flatComments array
503
- flatComments.push(comment);
504
- // If the current comment has replies, recursively flatten them and push to the flatComments array
505
- if (comment.replies)
506
- flatComments.push(...flattenComments(comment.replies));
507
- // Return the flatComments array
508
- return flatComments;
509
- }, []);
510
- }
511
-
512
- const sortedComments = preprocessComments(json.posts);
513
- return sortedComments;
514
- } catch (err) {
515
- console.error(err);
516
- throw new SidechatAPIError(`Failed to get comments on post.`);
517
- }
518
- };
519
-
520
- /**
521
- * Gets groups to be displayed on the "Explore Groups" page
522
- * @method
523
- * @since 2.0.0-alpha.0
524
- * @param {Boolean} onePage - whether or not results should be returned as a single page
525
- * @returns {SidechatGroup[]}
526
- */
527
- getAvailableGroups = async (onePage = true) => {
528
- if (!this.userToken) {
529
- throw new SidechatAPIError("User is not authenticated.");
530
- }
531
- try {
532
- const res = await fetch(`${this.apiRoot}/v1/groups/explore`, {
533
- method: "GET",
534
- headers: {
535
- ...this.defaultHeaders,
536
- "App-Version": onePage ? "0" : this.defaultHeaders["App-Version"],
537
- Authorization: `Bearer ${this.userToken}`,
538
- },
539
- });
540
- const json = await res.json();
541
- return await json.groups;
542
- } catch (err) {
543
- console.error(err);
544
- throw new SidechatAPIError(`Failed to get groups from explore.`);
545
- }
546
- };
547
-
548
- /**
549
- * Searches for new groups based on a query keyword
550
- * @method
551
- * @since 2.6.0
552
- * @param {String} query - the string to search for. This will be encoded, so strings with spaces and special characters are okay.
553
- * @returns {SidechatGroup[]}
554
- */
555
- searchAvailableGroups = async (query) => {
556
- if (!this.userToken) {
557
- throw new SidechatAPIError("User is not authenticated.");
558
- }
559
- try {
560
- const res = await fetch(`${this.apiRoot}/v1/groups/explore/search?term=${encodeURIComponent(query)}`, {
561
- method: "GET",
562
- headers: {
563
- ...this.defaultHeaders,
564
- Authorization: `Bearer ${this.userToken}`,
565
- },
566
- });
567
- const json = await res.json();
568
- return await json.results;
569
- } catch (err) {
570
- console.error(err);
571
- throw new SidechatAPIError(`Failed to search groups.`);
572
- }
573
- };
574
-
575
- /**
576
- * 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.
577
- * @method
578
- * @since 2.0.6
579
- * @returns {SidechatLibraryAsset[]}
580
- */
581
- getAssetLibrary = async () => {
582
- if (!this.userToken) {
583
- throw new SidechatAPIError("User is not authenticated.");
584
- }
585
- try {
586
- const res = await fetch(`${this.apiRoot}/v1/assets/library`, {
587
- method: "GET",
588
- headers: {
589
- ...this.defaultHeaders,
590
- Authorization: `Bearer ${this.userToken}`,
591
- },
592
- });
593
- const json = await res.json();
594
- return await json.items;
595
- } catch (err) {
596
- console.error(err);
597
- throw new SidechatAPIError(`Failed to get asset library.`);
598
- }
599
- };
600
-
601
- /**
602
- * Gets the current authenticated user and a list of the groups of which they are members.
603
- * @method
604
- * @since 2.1.0
605
- * @returns {SidechatCurrentUser}
606
- */
607
- getCurrentUser = async () => {
608
- if (!this.userToken) {
609
- throw new SidechatAPIError("User is not authenticated.");
610
- }
611
- try {
612
- const res = await fetch(`${this.apiRoot}/v1/users/me`, {
613
- method: "GET",
614
- headers: {
615
- ...this.defaultHeaders,
616
- Authorization: `Bearer ${this.userToken}`,
617
- },
618
- });
619
- const json = await res.json();
620
- return await json;
621
- } catch (err) {
622
- console.error(err);
623
- throw new SidechatAPIError(`Failed to get asset library.`);
624
- }
625
- };
626
-
627
- /**
628
- * Gets the metadata of a group from its ID
629
- * @method
630
- * @since 2.1.0
631
- * @param {String} [groupID] - alphanumeric ID of a group to get. Falls back to user's primary group.
632
- * @returns {SidechatGroup}
633
- */
634
- getGroupMetadata = async (groupID = "") => {
635
- if (!this.userToken) {
636
- throw new SidechatAPIError("User is not authenticated.");
637
- }
638
- try {
639
- const res = await fetch(`${this.apiRoot}/v1/groups/${groupID}`, {
640
- method: "GET",
641
- headers: {
642
- ...this.defaultHeaders,
643
- Authorization: `Bearer ${this.userToken}`,
644
- },
645
- });
646
- const json = await res.json();
647
- return await json.group;
648
- } catch (err) {
649
- console.error(err);
650
- throw new SidechatAPIError(`Failed to get group metadata.`);
651
- }
652
- };
653
-
654
- /**
655
- * Joins or leaves a group
656
- * @method
657
- * @param {String} groupID - alphanumeric ID of group to join or leave
658
- * @param {Boolean} isMember - whether or not the user should be a member of the group
659
- * @since 2.3.8
660
- */
661
- setGroupMembership = async (groupID, isMember) => {
662
- if (!this.userToken) {
663
- throw new SidechatAPIError("User is not authenticated.");
664
- }
665
- try {
666
- const res = await fetch(
667
- `${this.apiRoot}/v1/groups/${isMember ? "join" : "leave"}`,
668
- {
669
- method: "POST",
670
- headers: {
671
- ...this.defaultHeaders,
672
- Authorization: `Bearer ${this.userToken}`,
673
- },
674
- body: JSON.stringify({
675
- group_id: groupID,
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 modify group membership.`);
684
- }
685
- };
686
-
687
- /**
688
- * Creates a comment on a post
689
- * @method
690
- * @since 2.2.0
691
- * @param {String} parentPostID - alphanumeric ID of post on which this comment is made
692
- * @param {String} text - text content of comment
693
- * @param {String} groupID - alphanumeric ID of group in which the parent post resides
694
- * @param {String} [replyCommentID] - alphanumeric ID of comment to reply to. Falls back to parentPostID
695
- * @param {String} [topLevelReplyID] - alphanumeric ID of the top-level comment to reply to. Used only when replying to replies. Falls back to parentPostID
696
- * @param {SidechatSimpleAsset[]} [assetList] - list of assets to attach
697
- * @param {Boolean} [disableDMs] - prevent direct messages being sent to comment's author
698
- * @param {Boolean} [anonymous] - whether or not to hide user's name and icon on comment
699
- * @returns {SidechatPostOrComment} created comment
700
- */
701
- createComment = async (
702
- parentPostID,
703
- text,
704
- groupID,
705
- replyCommentID,
706
- topLevelReplyID,
707
- assetList = [],
708
- disableDMs = false,
709
- anonymous = false
710
- ) => {
711
- if (!this.userToken) {
712
- throw new SidechatAPIError("User is not authenticated.");
713
- }
714
- try {
715
- const res = await fetch(`${this.apiRoot}/v1/posts`, {
716
- method: "POST",
717
- headers: {
718
- ...this.defaultHeaders,
719
- Authorization: `Bearer ${this.userToken}`,
720
- },
721
- body: JSON.stringify({
722
- type: "comment",
723
- assets: assetList,
724
- group_ids: [groupID],
725
- text: text,
726
- reply_post_id: topLevelReplyID || replyCommentID || parentPostID,
727
- reply_comment_post_id: replyCommentID || parentPostID,
728
- parent_post_id: parentPostID,
729
- dms_disabled: disableDMs,
730
- using_identity: !anonymous,
731
- }),
732
- });
733
- const json = await res.json();
734
- return await json.comment;
735
- } catch (err) {
736
- console.error(err);
737
- throw new SidechatAPIError(`Failed to post comment.`);
738
- }
739
- };
740
-
741
- /**
742
- * Creates a new post in the specified group
743
- * @method
744
- * @since 2.2.0
745
- * @param {String} text - text content of comment
746
- * @param {String} groupID - alphanumeric ID of group in which the parent post resides
747
- * @param {SidechatSimpleAsset[]} [assetList] - list of assets to attach.
748
- * @param {Boolean} [disableDMs] - prevent direct messages from being sent to post's author
749
- * @param {Boolean} [disableComments] - whether or not comments should be disabled on post
750
- * @param {Boolean} [anonymous] - whether or not to hide user's name and icon on post
751
- * @returns {SidechatPostOrComment} the created post
752
- */
753
- createPost = async (
754
- text,
755
- groupID,
756
- assetList = [],
757
- disableDMs = false,
758
- disableComments = false,
759
- anonymous = false,
760
- repostId = undefined
761
- ) => {
762
- if (!this.userToken) {
763
- throw new SidechatAPIError("User is not authenticated.");
764
- }
765
- try {
766
- const res = await fetch(`${this.apiRoot}/v1/posts`, {
767
- method: "POST",
768
- headers: {
769
- ...this.defaultHeaders,
770
- Authorization: `Bearer ${this.userToken}`,
771
- },
772
- body: JSON.stringify({
773
- type: "post",
774
- assets: assetList,
775
- group_ids: [groupID],
776
- text: text,
777
- attachments: [],
778
- dms_disabled: disableDMs,
779
- comments_disabled: disableComments,
780
- using_identity: !anonymous,
781
- quote_post_id: repostId
782
- }),
783
- });
784
- const json = await res.json();
785
- return await json.posts[0];
786
- } catch (err) {
787
- console.error(err);
788
- throw new SidechatAPIError(`Failed to make post.`);
789
- }
790
- };
791
-
792
- /**
793
- * Deletes a post or comment that the user created
794
- * @method
795
- * @since 2.2.0
796
- * @param {String} postOrCommentID - alphanumeric ID of post to delete
797
- */
798
- deletePostOrComment = async (postOrCommentID) => {
799
- if (!this.userToken) {
800
- throw new SidechatAPIError("User is not authenticated.");
801
- }
802
- try {
803
- const res = await fetch(`${this.apiRoot}/v1/posts/delete`, {
804
- method: "POST",
805
- headers: {
806
- ...this.defaultHeaders,
807
- Authorization: `Bearer ${this.userToken}`,
808
- },
809
- body: JSON.stringify({
810
- post_id: postOrCommentID,
811
- }),
812
- });
813
- const json = await res.json();
814
- return await json;
815
- } catch (err) {
816
- console.error(err);
817
- throw new SidechatAPIError(`Failed to delete post.`);
818
- }
819
- };
820
-
821
- /**
822
- * Votes on a poll attached to a post
823
- * @method
824
- * @param {String} pollId - alphanumeric ID of poll to vote on
825
- * @param {Number} choiceIndex - index of the choice to vote on
826
- * @since 2.5.4
827
- */
828
- voteOnPoll = async (pollId, choiceIndex) => {
829
- if (!this.userToken) {
830
- throw new SidechatAPIError("User is not authenticated.");
831
- }
832
- try {
833
- const res = await fetch(`${this.apiRoot}/v1/polls/vote`, {
834
- method: "POST",
835
- headers: {
836
- ...this.defaultHeaders,
837
- Authorization: `Bearer ${this.userToken}`,
838
- },
839
- body: JSON.stringify({
840
- poll_id: pollId,
841
- choice: choiceIndex,
842
- }),
843
- });
844
- const json = await res.json();
845
- return await json;
846
- } catch (err) {
847
- console.error(err);
848
- throw new SidechatAPIError(`Failed to vote on poll`);
849
- }
850
- };
851
-
852
- /**
853
- * Marks that the user has viewed results on a poll. Note that this does not actually return the results of the poll.
854
- * @method
855
- * @param {String} pollId - alphanumeric ID of poll to vote on
856
- * @since 2.5.4
857
- */
858
- viewPollResults = async (pollId) => {
859
- if (!this.userToken) {
860
- throw new SidechatAPIError("User is not authenticated.");
861
- }
862
- try {
863
- const res = await fetch(`${this.apiRoot}/v1/polls/view_results`, {
864
- method: "POST",
865
- headers: {
866
- ...this.defaultHeaders,
867
- Authorization: `Bearer ${this.userToken}`,
868
- },
869
- body: JSON.stringify({
870
- poll_id: pollId,
871
- }),
872
- });
873
- const json = await res.json();
874
- return await json;
875
- } catch (err) {
876
- console.error(err);
877
- throw new SidechatAPIError(`Failed to mark poll results as viewed.`);
878
- }
879
- };
880
-
881
- /**
882
- * Uploads an asset to AWS S3 for use in posts and comments. Currently photos only
883
- * @method
884
- * @param {String} uri - URI of the asset to upload
885
- * @param {String} mimeType - mimetype of the asset (e.g. "image/png")
886
- * @param {String} [name] - filename of the asset
887
- * @returns {String} URL of the uploaded asset
888
- * @since 2.5.1
889
- */
890
- uploadAsset = async (uri, mimeType, name = "") => {
891
- if (!this.userToken) {
892
- throw new SidechatAPIError("User is not authenticated.");
893
- }
894
-
895
- let imageType = mimeType.split("/")[1];
896
- if (!["png", "jpeg", "gif"].includes(imageType)) {
897
- throw new SidechatAPIError("Unsupported image format.");
898
- }
899
-
900
- const data = new FormData();
901
- data.append("image", {
902
- name: name,
903
- type: mimeType,
904
- uri: uri,
905
- });
906
-
907
- const urlReq = await this.sendRequest(`/v1/assets/upload_url?content_type=${imageType}`);
908
- const urlJson = await urlReq.json();
909
-
910
- try {
911
- const uploadReq = await fetch(urlJson.upload_url, {
912
- body: data.getAll("image")[0],
913
- method: "PUT",
914
- headers: {
915
- "Content-Type": mimeType,
916
- },
917
- });
918
- if (uploadReq.status == 200) {
919
- return `${this.apiRoot}/v1/assets/library/${urlJson.asset_id}`;
920
- } else {
921
- throw new SidechatAPIError(
922
- `Couldn't upload image - error ${uploadReq.status}`
923
- );
924
- }
925
- } catch (e) {
926
- throw new SidechatAPIError(e.message);
927
- }
928
- };
929
-
930
- /**
931
- * Sets the conversation icon of a user
932
- * @method
933
- * @since 2.2.1
934
- * @param {String} userID - alphanumeric ID of user
935
- * @param {String} emoji - emoji to set as icon
936
- * @param {String} primaryColor - hex string (including #) of primary color
937
- * @param {String} secondaryColor - hex string (including #) of secondary color
938
- */
939
- setUserIcon = async (userID, emoji, primaryColor, secondaryColor) => {
940
- if (!this.userToken) {
941
- throw new SidechatAPIError("User is not authenticated.");
942
- }
943
- try {
944
- const res = await fetch(`${this.apiRoot}/v1/users/${userID}`, {
945
- method: "PATCH",
946
- headers: {
947
- ...this.defaultHeaders,
948
- "App-Version": "0",
949
- Authorization: `Bearer ${this.userToken}`,
950
- },
951
- body: JSON.stringify({
952
- conversation_icon: {
953
- emoji: emoji,
954
- secondary_color: secondaryColor,
955
- is_migrated: true,
956
- color: primaryColor,
957
- },
958
- }),
959
- });
960
- const json = await res.json();
961
- return await json;
962
- } catch (err) {
963
- console.error(err);
964
- throw new SidechatAPIError(`Failed to set icon.`);
965
- }
966
- };
967
-
968
- /**
969
- * Sets the bio text of a user
970
- * @method
971
- * @since 2.5.6
972
- * @param {String} userID - alphanumeric ID of user
973
- * @param {String} bio - text to set as bio
974
- */
975
- setUserBio = async (userID, bio) => {
976
- if (!this.userToken) {
977
- throw new SidechatAPIError("User is not authenticated.");
978
- }
979
- try {
980
- const res = await fetch(`${this.apiRoot}/v1/users/${userID}`, {
981
- method: "PATCH",
982
- headers: {
983
- ...this.defaultHeaders,
984
- "App-Version": "5.4.22",
985
- Authorization: `Bearer ${this.userToken}`,
986
- },
987
- body: JSON.stringify({
988
- bio: bio
989
- }),
990
- });
991
- const json = await res.json();
992
- return await json;
993
- } catch (err) {
994
- console.error(err);
995
- throw new SidechatAPIError(`Failed to set bio.`);
996
- }
997
- };
998
-
999
- /**
1000
- * Checks if user can set their username to a string
1001
- * @method
1002
- * @since 2.3.6
1003
- * @param {String} username - string to check
1004
- * @returns {Boolean} whether or not username is valid and unused
1005
- */
1006
- checkUsername = async (username) => {
1007
- if (!this.userToken) {
1008
- throw new SidechatAPIError("User is not authenticated.");
1009
- }
1010
- try {
1011
- const res = await fetch(
1012
- `${this.apiRoot}/v1/users/username?username=${username}`,
1013
- {
1014
- method: "GET",
1015
- headers: {
1016
- ...this.defaultHeaders,
1017
- Authorization: `Bearer ${this.userToken}`,
1018
- },
1019
- }
1020
- );
1021
- if (res?.status == 200 || res?.status == 204) {
1022
- return true;
1023
- } else {
1024
- return false;
1025
- }
1026
- } catch (err) {
1027
- console.error(err);
1028
- throw new SidechatAPIError(`Failed to check username.`);
1029
- }
1030
- };
1031
-
1032
- /**
1033
- * Changes the username of the current user
1034
- * @method
1035
- * @since 2.3.6
1036
- * @param {String} userID - alphanumeric ID of user
1037
- * @param {String} username - new username to set
1038
- */
1039
- setUsername = async (userID, username) => {
1040
- if (!this.userToken) {
1041
- throw new SidechatAPIError("User is not authenticated.");
1042
- }
1043
- try {
1044
- const res = await fetch(`${this.apiRoot}/v1/users/${userID}`, {
1045
- method: "PATCH",
1046
- headers: {
1047
- ...this.defaultHeaders,
1048
- Authorization: `Bearer ${this.userToken}`,
1049
- },
1050
- body: JSON.stringify({
1051
- username: username,
1052
- }),
1053
- });
1054
- const json = await res.json();
1055
- return await json.user;
1056
- } catch (err) {
1057
- console.error(err);
1058
- throw new SidechatAPIError(`Failed to set icon.`);
1059
- }
1060
- };
1061
-
1062
- /**
1063
- * Fetches a public user profile
1064
- * @method
1065
- * @since 2.6.0
1066
- * @param {String} username - username of the user to fetch
1067
- * @returns {SidechatProfile}
1068
- */
1069
- getUserProfile = async (username) => {
1070
- if (!this.userToken) {
1071
- throw new SidechatAPIError("User is not authenticated.");
1072
- }
1073
- try {
1074
- const res = await fetch(`${this.apiRoot}/v1/groups/username?username=${username}`, {
1075
- method: "GET",
1076
- headers: {
1077
- ...this.defaultHeaders,
1078
- Authorization: `Bearer ${this.userToken}`,
1079
- }
1080
- });
1081
- const json = await res.json();
1082
- return await json.group;
1083
- } catch (err) {
1084
- console.error(err);
1085
- throw new SidechatAPIError(`Failed to set icon.`);
1086
- }
1087
- };
1088
-
1089
- /**
1090
- * Fetches a public user's posts
1091
- * @method
1092
- * @since 2.6.0
1093
- * @param {String} username - username of the user to fetch
1094
- * @returns {SidechatPostOrComment[]}
1095
- */
1096
- getUserPosts = async (username) => {
1097
- if (!this.userToken) {
1098
- throw new SidechatAPIError("User is not authenticated.");
1099
- }
1100
- try {
1101
- const res = await fetch(`${this.apiRoot}/v1/users/posts?username=${username}`, {
1102
- method: "GET",
1103
- headers: {
1104
- ...this.defaultHeaders,
1105
- Authorization: `Bearer ${this.userToken}`,
1106
- }
1107
- });
1108
- const json = await res.json();
1109
- return await json.posts;
1110
- } catch (err) {
1111
- console.error(err);
1112
- throw new SidechatAPIError(`Failed to set icon.`);
1113
- }
1114
- };
1115
-
1116
- /**
1117
- * Marks an activity item as read
1118
- * @method
1119
- * @since 2.3.2
1120
- * @param {String} activityID - alphanumeric ID of activity object
1121
- */
1122
- readActivity = async (activityID) => {
1123
- if (!this.userToken) {
1124
- throw new SidechatAPIError("User is not authenticated.");
1125
- }
1126
- try {
1127
- const res = await fetch(`${this.apiRoot}/v1/activity/seen`, {
1128
- method: "POST",
1129
- headers: {
1130
- ...this.defaultHeaders,
1131
- Authorization: `Bearer ${this.userToken}`,
1132
- },
1133
- body: JSON.stringify({
1134
- ids: [activityID],
1135
- }),
1136
- });
1137
- const json = await res.json();
1138
- return await json;
1139
- } catch (err) {
1140
- console.error(err);
1141
- throw new SidechatAPIError(`Failed to mark activity as read.`);
1142
- }
1143
- };
1144
-
1145
- /**
1146
- * Retrieves joinable group chats
1147
- * @method
1148
- * @since 2.3.5
1149
- */
1150
- getGroupChats = async () => {
1151
- if (!this.userToken) {
1152
- throw new SidechatAPIError("User is not authenticated.");
1153
- }
1154
- try {
1155
- const res = await fetch(`${this.apiRoot}/v1/chats/explore`, {
1156
- method: "GET",
1157
- headers: {
1158
- ...this.defaultHeaders,
1159
- Authorization: `Bearer ${this.userToken}`,
1160
- },
1161
- });
1162
- const json = await res.json();
1163
- return await json.chats;
1164
- } catch (err) {
1165
- console.error(err);
1166
- throw new SidechatAPIError(`Failed to get groupchats.`);
1167
- }
1168
- };
1169
-
1170
- /**
1171
- * Joins a group chat. To mimic the official client, use the user's display name and icon by default.
1172
- * @method
1173
- * @param {String} groupChatID - alphanumeric ID of group chat to join
1174
- * @param {String} displayName - display name to use in chat
1175
- * @param {String} emoji - emoji to use as icon
1176
- * @param {String} primaryColor - hex string of primary color
1177
- * @param {String} secondaryColor - hex string of secondary color
1178
- * @since 2.3.5
1179
- */
1180
- joinGroupChat = async (
1181
- groupChatID,
1182
- displayName,
1183
- emoji,
1184
- primaryColor,
1185
- secondaryColor
1186
- ) => {
1187
- if (!this.userToken) {
1188
- throw new SidechatAPIError("User is not authenticated.");
1189
- }
1190
- try {
1191
- const res = await fetch(`${this.apiRoot}/v1/chats/groups/join`, {
1192
- method: "POST",
1193
- headers: {
1194
- ...this.defaultHeaders,
1195
- Authorization: `Bearer ${this.userToken}`,
1196
- },
1197
- body: JSON.stringify({
1198
- chat_id: groupChatID,
1199
- identity: {
1200
- display_name: displayName,
1201
- emoji: emoji,
1202
- secondary_color: secondaryColor,
1203
- color: primaryColor,
1204
- },
1205
- }),
1206
- });
1207
- const json = await res.json();
1208
- return await json;
1209
- } catch (err) {
1210
- console.error(err);
1211
- throw new SidechatAPIError(`Failed to join groupchat.`);
1212
- }
1213
- };
1214
-
1215
- /**
1216
- * Gets a list of the user's direct messages
1217
- * @method
1218
- * @returns {SidechatDirectThread[]}
1219
- * @since 2.4.4
1220
- */
1221
- getDMs = async () => {
1222
- if (!this.userToken) {
1223
- throw new SidechatAPIError("User is not authenticated.");
1224
- }
1225
- try {
1226
- const res = await fetch(`${this.apiRoot}/v1/chats`, {
1227
- method: "GET",
1228
- headers: {
1229
- ...this.defaultHeaders,
1230
- Authorization: `Bearer ${this.userToken}`,
1231
- },
1232
- });
1233
- const json = await res.json();
1234
- const list = await json.chats;
1235
- let r = [];
1236
- list.forEach((o) => {
1237
- r.push(o.chat);
1238
- });
1239
- return await r;
1240
- } catch (err) {
1241
- console.error(err);
1242
- throw new SidechatAPIError(`Failed to fetch DMs.`);
1243
- }
1244
- };
1245
-
1246
- /**
1247
- * Gets a single direct message thread
1248
- * @method
1249
- * @param {String} id - alphanumeric ID of the chat to fetch
1250
- * @returns {SidechatDirectThread}
1251
- * @since 2.4.4
1252
- */
1253
- getDMThread = async (id) => {
1254
- if (!this.userToken) {
1255
- throw new SidechatAPIError("User is not authenticated.");
1256
- }
1257
- try {
1258
- const res = await fetch(
1259
- `${this.apiRoot}/v1/chats/messages?chat_id=${id}`,
1260
- {
1261
- method: "GET",
1262
- headers: {
1263
- ...this.defaultHeaders,
1264
- Authorization: `Bearer ${this.userToken}`,
1265
- },
1266
- }
1267
- );
1268
- const json = await res.json();
1269
- return await json.chat;
1270
- } catch (err) {
1271
- console.error(err);
1272
- throw new SidechatAPIError(`Failed to fetch DM thread.`);
1273
- }
1274
- };
1275
-
1276
- /**
1277
- * Sends a message to an existing direct message thread - note that you must first use startDM() to start a thread.
1278
- * @method
1279
- * @param {String} chatID - alphanumeric ID of the chat to send to
1280
- * @param {String} text - text content of message
1281
- * @param {String} clientID - alphanumeric device ID
1282
- * @param {SidechatAsset[]} assets - array of assets to send
1283
- * @param {Boolean} anonymous - whether the DM should be sent anonymously
1284
- * @since 2.4.4
1285
- */
1286
- sendDM = async (chatID, text, clientID, assets = [], anonymous = false) => {
1287
- if (!this.userToken) {
1288
- throw new SidechatAPIError("User is not authenticated.");
1289
- }
1290
- try {
1291
- const res = await fetch(`${this.apiRoot}/v1/chats/send`, {
1292
- method: "POST",
1293
- headers: {
1294
- ...this.defaultHeaders,
1295
- Authorization: `Bearer ${this.userToken}`,
1296
- },
1297
- body: JSON.stringify({
1298
- chat_id: chatID,
1299
- text: text,
1300
- client_id: clientID,
1301
- anonymous: anonymous,
1302
- assets: assets,
1303
- }),
1304
- });
1305
- const json = await res.json();
1306
- return await json;
1307
- } catch (err) {
1308
- console.error(err);
1309
- throw new SidechatAPIError(`Failed to send message.`);
1310
- }
1311
- };
1312
-
1313
- /**
1314
- * Creates a new direct message thread
1315
- * @method
1316
- * @param {String} text - text content of message
1317
- * @param {String} clientID - alphanumeric ID of devide
1318
- * @param {String} postID - alphanumeric ID of post or comment
1319
- * @param {Boolean} anonymous - whether the DM should be sent anonymously
1320
- * @param {"feed"} postContext - context of post (mostly undocumented, defaults to "feed")
1321
- * @since 2.4.4
1322
- */
1323
- startDM = async (
1324
- text,
1325
- clientID,
1326
- postID,
1327
- anonymous = false,
1328
- postContext = "feed"
1329
- ) => {
1330
- if (!this.userToken) {
1331
- throw new SidechatAPIError("User is not authenticated.");
1332
- }
1333
- try {
1334
- const res = await fetch(`${this.apiRoot}/v1/chats/start`, {
1335
- method: "POST",
1336
- headers: {
1337
- ...this.defaultHeaders,
1338
- Authorization: `Bearer ${this.userToken}`,
1339
- },
1340
- body: JSON.stringify({
1341
- text: text,
1342
- client_id: clientID,
1343
- post_id: postID,
1344
- anonymous: anonymous,
1345
- post_context: postContext,
1346
- }),
1347
- });
1348
- const json = await res.json();
1349
- return await json;
1350
- } catch (err) {
1351
- console.error(err);
1352
- throw new SidechatAPIError(`Failed to start DM.`);
1353
- }
1354
- };
1355
-
1356
- /**
1357
- * Hides posts from user
1358
- * @method
1359
- * @param {String} postID - alphanumeric ID of post to hide
1360
- * @since 2.6.2
1361
- */
1362
- hidePostsFromUser = async (postID) => {
1363
- if (!this.userToken) {
1364
- throw new SidechatAPIError("User is not authenticated.");
1365
- }
1366
- try {
1367
- const res = await fetch(`${this.apiRoot}/v1/posts/hide_posts_from_user`, {
1368
- method: "POST",
1369
- headers: {
1370
- ...this.defaultHeaders,
1371
- Authorization: `Bearer ${this.userToken}`,
1372
- },
1373
- body: JSON.stringify({
1374
- post_id: postID,
1375
- post_context: "feed",
1376
- report: false
1377
- }),
1378
- });
1379
- const json = await res.json();
1380
- return await json;
1381
- } catch (err) {
1382
- console.error(err);
1383
- throw new SidechatAPIError(`Failed to hide post.`);
1384
- }
1385
- };
1386
-
1387
- /**
1388
- * Unhides all posts from all users
1389
- * @method
1390
- * @since 2.6.2
1391
- */
1392
- unhidePostsFromAllUsers = async () => {
1393
- if (!this.userToken) {
1394
- throw new SidechatAPIError("User is not authenticated.");
1395
- }
1396
- try {
1397
- const res = await fetch(`${this.apiRoot}/v1/users/unhide_all`, {
1398
- method: "POST",
1399
- headers: {
1400
- ...this.defaultHeaders,
1401
- Authorization: `Bearer ${this.userToken}`,
1402
- },
1403
- body: JSON.stringify({}),
1404
- });
1405
- const json = await res.json();
1406
- return await json;
1407
- } catch (err) {
1408
- console.error(err);
1409
- throw new SidechatAPIError(`Failed to unhide post.`);
1410
- }
1411
- };
1412
- }
1413
-
1414
- export default SidechatAPIClient;
1
+ import SidechatAPIError from "../classes/SidechatAPIError.js";
2
+
3
+ /** @import * as types from "../types/SidechatTypes.js" */
4
+
5
+ /**
6
+ * API client class for making requests to Sidechat's private API. You'll need to [authenticate]{@tutorial Authentication} before using most of the methods.
7
+ * @class
8
+ * @since 2.0.0-alpha.0
9
+ */
10
+ class SidechatAPIClient {
11
+ /**
12
+ * User bearer token
13
+ * @type {types.SidechatAuthToken}
14
+ * */
15
+ userToken;
16
+
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": "6.0.0",
27
+ Dnt: 1,
28
+ };
29
+
30
+ /**
31
+ * Root URL for every API request
32
+ * @type {String}
33
+ * @default "https://api.sidechat.lol"
34
+ */
35
+ apiRoot = "https://api.sidechat.lol";
36
+
37
+ /**
38
+ * Create a new instance of the API client
39
+ * @param {types.SidechatAuthToken} [token] - user bearer token
40
+ * @param {String} rootUrl - custom API root URL for mocking or using other server
41
+ */
42
+ constructor(token = "", rootUrl = "") {
43
+ if (token) {
44
+ this.userToken = token;
45
+ }
46
+ if (rootUrl) {
47
+ this.apiRoot = rootUrl;
48
+ }
49
+ }
50
+
51
+ /**
52
+ * 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
53
+ * @method
54
+ * @param {types.SidechatAuthToken} token - user bearer token
55
+ */
56
+ setToken = (token) => {
57
+ this.userToken = token;
58
+ };
59
+
60
+ /**
61
+ * Manually set the root URL for all API requests. This can be used for mocking requests or redirecting them to a different server
62
+ * @method
63
+ * @param {String} url - new root URL to set
64
+ * @since 2.3.9
65
+ */
66
+ setAPIRoot = (url) => {
67
+ this.apiRoot = url;
68
+ };
69
+
70
+ /**
71
+ * Run an arbitrary API request using the current client's authentication
72
+ * @method
73
+ * @param {String} endpoint - API endpoint to request (e.g. "/v1/posts")
74
+ * @param {"GET"|"POST"|"PUT"|"DELETE"|"PATCH"|"OPTIONS"} [method] - HTTP method to use
75
+ * @param {Object} [body] - body to send with the request
76
+ * @param {Object} [headers] - custom headers to send with the request
77
+ * @param {Boolean} [stripHeaders] - remove the default headers from the request
78
+ * @since 2.4.9
79
+ */
80
+ sendRequest = (
81
+ endpoint,
82
+ method = "GET",
83
+ body = undefined,
84
+ headers = {},
85
+ stripHeaders = false,
86
+ ) => {
87
+ let requestHeaders = this.defaultHeaders;
88
+ if (stripHeaders) {
89
+ headers = {};
90
+ }
91
+ requestHeaders = { ...requestHeaders, ...headers };
92
+ return fetch(`${this.apiRoot}${endpoint}`, {
93
+ headers: { Authorization: `Bearer ${this.userToken}`, ...requestHeaders },
94
+ body: body,
95
+ method: method,
96
+ });
97
+ };
98
+
99
+ /**
100
+ * Initiate the login process with a phone number. Should be followed up with verifySMSCode().
101
+ * @method
102
+ * @since 1.0.0
103
+ * @param {Number} phoneNumber - US phone number (WITHOUT +1) to send verification code to
104
+ */
105
+ loginViaSMS = async (phoneNumber) => {
106
+ try {
107
+ const res = await fetch(`${this.apiRoot}/v1/login_register`, {
108
+ method: "POST",
109
+ headers: this.defaultHeaders,
110
+ body: JSON.stringify({
111
+ phone_number: `+1${phoneNumber}`,
112
+ version: 3,
113
+ }),
114
+ });
115
+ const json = await res.json();
116
+ return json;
117
+ } catch (err) {
118
+ console.error(err);
119
+ throw new SidechatAPIError("Failed to request SMS verification.");
120
+ }
121
+ };
122
+
123
+ /**
124
+ * Verify the code sent via SMS with loginViaSMS(). If this function succeeds, the user will be authenticated for future requests.
125
+ * @method
126
+ * @since 1.0.0
127
+ * @param {Number} phoneNumber - US phone number (WITHOUT +1) that verification code was sent to
128
+ * @param {String} code - the verification code
129
+ */
130
+ verifySMSCode = async (phoneNumber, code) => {
131
+ try {
132
+ const res = await fetch(`${this.apiRoot}/v1/verify_phone_number`, {
133
+ method: "POST",
134
+ headers: this.defaultHeaders,
135
+ body: JSON.stringify({
136
+ phone_number: `+1${phoneNumber}`,
137
+ code: code.toUpperCase(),
138
+ }),
139
+ });
140
+ const json = await res.json();
141
+ if (json?.logged_in_user?.token) {
142
+ this.userToken = json.logged_in_user.token;
143
+ }
144
+ return json;
145
+ } catch (err) {
146
+ console.error(err);
147
+ throw new SidechatAPIError("Failed verify this code.");
148
+ }
149
+ };
150
+
151
+ /**
152
+ * Set the user's age. If this function succeeds, the user will be authenticated for future requests.
153
+ * @method
154
+ * @since 1.0.0
155
+ * @param {Number} age - user's age in years
156
+ * @param {String} registrationID - the registration ID generated by verifySMSCode()
157
+ */
158
+ setAge = async (age, registrationID) => {
159
+ if (age < 13) {
160
+ throw new SidechatAPIError("You're too young to use Offsides.");
161
+ }
162
+ try {
163
+ const res = await fetch(`${this.apiRoot}/v1/complete_registration`, {
164
+ method: "POST",
165
+ headers: this.defaultHeaders,
166
+ body: JSON.stringify({
167
+ age: Number(age),
168
+ registration_id: registrationID,
169
+ }),
170
+ });
171
+ const json = await res.json();
172
+ if (json.token) {
173
+ this.userToken = json.token;
174
+ }
175
+ return json;
176
+ } catch (err) {
177
+ console.error(err);
178
+ throw new SidechatAPIError("Failed verify this code.");
179
+ }
180
+ };
181
+
182
+ /**
183
+ * Initiate the email setup process. Should be followed up with checkEmailVerification().
184
+ * @method
185
+ * @since 1.0.0
186
+ * @param {String} email - school email address to send verification code to
187
+ * @tutorial Email Registration
188
+ */
189
+ registerEmail = async (email) => {
190
+ if (!this.userToken) {
191
+ throw new SidechatAPIError("User is not authenticated.");
192
+ }
193
+ try {
194
+ const res = await fetch(`${this.apiRoot}/v2/users/register_email`, {
195
+ method: "POST",
196
+ headers: {
197
+ ...this.defaultHeaders,
198
+ Authorization: `Bearer ${this.userToken}`,
199
+ },
200
+ body: JSON.stringify({
201
+ email: email,
202
+ }),
203
+ });
204
+ const json = await res.json();
205
+ if (json.message) {
206
+ throw new SidechatAPIError(json.message);
207
+ }
208
+ return json;
209
+ } catch (err) {
210
+ console.error(err);
211
+ throw new SidechatAPIError("Failed to request email verification.");
212
+ }
213
+ };
214
+
215
+ /**
216
+ * Check is the user's email is verified.
217
+ * @method
218
+ * @since 1.0.0
219
+ */
220
+ checkEmailVerification = async () => {
221
+ if (!this.userToken) {
222
+ throw new SidechatAPIError("User is not authenticated.");
223
+ }
224
+ try {
225
+ const res = await fetch(`${this.apiRoot}/v1/users/check_email_verified`, {
226
+ method: "GET",
227
+ headers: {
228
+ ...this.defaultHeaders,
229
+ Authorization: `Bearer ${this.userToken}`,
230
+ },
231
+ });
232
+ const json = await res.json();
233
+ if (json.verified_email_updates_response) {
234
+ return json.verified_email_updates_response;
235
+ } else if (json.changing_phone_number_verified_email_user) {
236
+ return json.changing_phone_number_verified_email_user;
237
+ } else {
238
+ throw new SidechatAPIError(json?.message || "Email is not verified.");
239
+ }
240
+ } catch (err) {
241
+ console.error(err);
242
+ throw new SidechatAPIError("Email is not verified.");
243
+ }
244
+ };
245
+
246
+ /**
247
+ * Set the device ID of the current user
248
+ * @method
249
+ * @since 1.0.0
250
+ * @param {String} deviceId - the device ID to set
251
+ */
252
+ setDeviceID = async (deviceID) => {
253
+ if (!this.userToken) {
254
+ throw new SidechatAPIError("User is not authenticated.");
255
+ }
256
+ try {
257
+ const res = await fetch(`${this.apiRoot}/v1/register_device_token`, {
258
+ method: "POST",
259
+ headers: {
260
+ ...this.defaultHeaders,
261
+ Authorization: `Bearer ${this.userToken}`,
262
+ },
263
+ body: JSON.stringify({
264
+ build_type: "release",
265
+ bundle_id: "com.flowerave.sidechat",
266
+ device_token: deviceID,
267
+ }),
268
+ });
269
+ const json = await res.json();
270
+ return json;
271
+ } catch (err) {
272
+ console.error(err);
273
+ throw new SidechatAPIError("Failed verify this code.");
274
+ }
275
+ };
276
+
277
+ /**
278
+ * Get updated status for user and group
279
+ * @method
280
+ * @since 1.0.0
281
+ * @deprecated since 2.1.0, will be removed by 3.0.0. Please use `getUpdates` instead!
282
+ * @param {String} [groupID] - ID of a specific group to retrieve info from. Falls back to user's primary group.
283
+ */
284
+ getUserAndGroup = async (groupID = "") => {
285
+ const json = await this.getUpdates(groupID);
286
+ return json;
287
+ };
288
+
289
+ /**
290
+ * Get updated status for user and group
291
+ * @method
292
+ * @since 2.1.0
293
+ * @param {String} [groupID] - ID of a specific group to retrieve info from. Falls back to user's primary group.
294
+ */
295
+ getUpdates = async (groupID = "") => {
296
+ if (!this.userToken) {
297
+ throw new SidechatAPIError("User is not authenticated.");
298
+ }
299
+ try {
300
+ const res = await fetch(
301
+ `${this.apiRoot}/v1/updates?group_id=${groupID}`,
302
+ {
303
+ method: "GET",
304
+ headers: {
305
+ ...this.defaultHeaders,
306
+ Authorization: `Bearer ${this.userToken}`,
307
+ },
308
+ },
309
+ );
310
+ const json = await res.json();
311
+ return json;
312
+ } catch (err) {
313
+ console.error(err);
314
+ throw new SidechatAPIError(`Failed to get posts from group.`);
315
+ }
316
+ };
317
+
318
+ /**
319
+ * Fetches posts from the specified category in a group
320
+ * @method
321
+ * @since 1.0.0
322
+ * @param {String} groupID - group ID
323
+ * @param {"hot"|"recent"|"top"} category - category to filter posts
324
+ * @param {types.SidechatCursorString} [cursor] - cursor string
325
+ * @returns {Promise<types.SidechatPostsAndCursor>} List of posts and cursor
326
+ */
327
+ getGroupPosts = async (groupID, category = "hot", cursor) => {
328
+ if (!this.userToken) {
329
+ throw new SidechatAPIError("User is not authenticated.");
330
+ }
331
+ try {
332
+ const res = await fetch(
333
+ `${this.apiRoot}/v1/posts?${
334
+ cursor ? "cursor=" + encodeURIComponent(cursor) + "&" : ""
335
+ }group_id=${groupID}&type=${category}&cacheBust=${Date.now()}`,
336
+ {
337
+ method: "GET",
338
+ headers: {
339
+ ...this.defaultHeaders,
340
+ Authorization: `Bearer ${this.userToken}`,
341
+ },
342
+ },
343
+ );
344
+ const json = await res.json();
345
+ return json;
346
+ } catch (err) {
347
+ console.error(err);
348
+ throw new SidechatAPIError(`Failed to get posts from group.`);
349
+ }
350
+ };
351
+
352
+ /**
353
+ * Upvote or downvote, or unvote a post
354
+ * @method
355
+ * @since 2.0.0-alpha.0
356
+ * @param {String} postID - post ID to vote on
357
+ * @param {types.SidechatVoteString} action - whether to upvote, downvote, or reset vote
358
+ */
359
+ setVote = async (postID, action) => {
360
+ if (!this.userToken) {
361
+ throw new SidechatAPIError("User is not authenticated.");
362
+ }
363
+ try {
364
+ const res = await fetch(`${this.apiRoot}/v1/posts/set_vote`, {
365
+ method: "POST",
366
+ headers: {
367
+ ...this.defaultHeaders,
368
+ Authorization: `Bearer ${this.userToken}`,
369
+ },
370
+ body: JSON.stringify({
371
+ post_id: postID,
372
+ vote_status: action,
373
+ }),
374
+ });
375
+ const json = await res.json();
376
+ return json;
377
+ } catch (err) {
378
+ console.error(err);
379
+ throw new SidechatAPIError(`Failed to change the vote on post.`);
380
+ }
381
+ };
382
+
383
+ /**
384
+ * Fetches a single post with just its ID
385
+ * @method
386
+ * @since 2.3.0
387
+ * @param {String} postID - ID of post to fetch
388
+ * @param {Boolean} includeDeleted - undocumented
389
+ * @returns {Promise<types.SidechatPostOrComment>} post object
390
+ */
391
+ getPost = async (postID, includeDeleted = false) => {
392
+ if (!this.userToken) {
393
+ throw new SidechatAPIError("User is not authenticated.");
394
+ }
395
+ try {
396
+ const res = await fetch(
397
+ `${this.apiRoot}/v1/posts/get?include_deleted=${includeDeleted}&post_id=${postID}&cacheBust=${Date.now()}`,
398
+ {
399
+ method: "GET",
400
+ headers: {
401
+ ...this.defaultHeaders,
402
+ Authorization: `Bearer ${this.userToken}`,
403
+ },
404
+ },
405
+ );
406
+ const json = await res.json();
407
+ return json.post;
408
+ } catch (err) {
409
+ console.error(err);
410
+ throw new SidechatAPIError(`Failed to get post from ID.`);
411
+ }
412
+ };
413
+
414
+ /**
415
+ * Fetches the posts or comments that the user has created
416
+ * @method
417
+ * @since 2.3.5
418
+ * @param {"posts"|"comments"} contentType - type of user content to fetch
419
+ * @returns {Promise<types.SidechatPostOrComment[]>} post object
420
+ */
421
+ getUserContent = async (contentType) => {
422
+ if (!this.userToken) {
423
+ throw new SidechatAPIError("User is not authenticated.");
424
+ }
425
+ if (contentType == "posts") {
426
+ contentType = "my_posts";
427
+ } else if (contentType == "comments") {
428
+ contentType = "my_comments";
429
+ }
430
+ try {
431
+ const res = await fetch(`${this.apiRoot}/v1/posts&type=${contentType}`, {
432
+ method: "GET",
433
+ headers: {
434
+ ...this.defaultHeaders,
435
+ Authorization: `Bearer ${this.userToken}`,
436
+ },
437
+ });
438
+ const json = await res.json();
439
+ return json.posts;
440
+ } catch (err) {
441
+ console.error(err);
442
+ throw new SidechatAPIError(`Failed to get content from user.`);
443
+ }
444
+ };
445
+
446
+ /**
447
+ * Get all the commments on a post
448
+ * @method
449
+ * @since 2.0.0-alpha.0
450
+ * @param {String} postID - post ID to get comments for
451
+ * @returns {Promise<types.SidechatPostOrComment[]>} list of comments
452
+ */
453
+ getPostComments = async (postID) => {
454
+ if (!this.userToken) {
455
+ throw new SidechatAPIError("User is not authenticated.");
456
+ }
457
+ try {
458
+ const res = await fetch(
459
+ `${this.apiRoot}/v1/posts/comments/?post_id=${postID}&cacheBust=${Date.now()}`,
460
+ {
461
+ method: "GET",
462
+ headers: {
463
+ ...this.defaultHeaders,
464
+ Authorization: `Bearer ${this.userToken}`,
465
+ },
466
+ },
467
+ );
468
+ const json = await res.json();
469
+ // Function to preprocess the comments and organize them into a nested structure
470
+ function preprocessComments(apiComments) {
471
+ // Map to store comments by their IDs for efficient lookup
472
+ const commentMap = new Map();
473
+ // List to store top-level comments
474
+ const topLevelComments = [];
475
+
476
+ // Iterate through the API comments
477
+ apiComments.forEach((comment) => {
478
+ // Store the comment in the map with its ID as the key
479
+ commentMap.set(comment.id, comment);
480
+ // Get the parent comment using the reply_post_id
481
+ const parentComment = commentMap.get(comment.reply_post_id);
482
+ // Check if the comment is a top-level comment
483
+ if (
484
+ !parentComment ||
485
+ comment.reply_post_id === comment.parent_post_id
486
+ ) {
487
+ // If it's a top-level comment, push it to the topLevelComments array
488
+ topLevelComments.push(comment);
489
+ } else {
490
+ // If it's a reply, add it to the parent comment's replies array
491
+ if (!parentComment.replies) parentComment.replies = [];
492
+ parentComment.replies.push(comment);
493
+ }
494
+ });
495
+
496
+ // Flatten the nested structure and return a single list of comments
497
+ return flattenComments(topLevelComments);
498
+ }
499
+
500
+ // Function to flatten nested comments into a single list
501
+ function flattenComments(comments) {
502
+ // Use reduce to flatten the nested comments array into a single list
503
+ return comments.reduce((flatComments, comment) => {
504
+ // Push the current comment to the flatComments array
505
+ flatComments.push(comment);
506
+ // If the current comment has replies, recursively flatten them and push to the flatComments array
507
+ if (comment.replies)
508
+ flatComments.push(...flattenComments(comment.replies));
509
+ // Return the flatComments array
510
+ return flatComments;
511
+ }, []);
512
+ }
513
+
514
+ const sortedComments = preprocessComments(json.posts);
515
+ return sortedComments;
516
+ } catch (err) {
517
+ console.error(err);
518
+ throw new SidechatAPIError(`Failed to get comments on post.`);
519
+ }
520
+ };
521
+
522
+ /**
523
+ * Gets groups to be displayed on the "Explore Groups" page
524
+ * @method
525
+ * @since 2.0.0-alpha.0
526
+ * @param {Boolean} onePage - whether or not results should be returned as a single page
527
+ * @returns {Promise<types.SidechatGroup[]>}
528
+ */
529
+ getAvailableGroups = async (onePage = true) => {
530
+ if (!this.userToken) {
531
+ throw new SidechatAPIError("User is not authenticated.");
532
+ }
533
+ try {
534
+ const res = await fetch(`${this.apiRoot}/v1/groups/explore`, {
535
+ method: "GET",
536
+ headers: {
537
+ ...this.defaultHeaders,
538
+ "App-Version": onePage ? "0" : this.defaultHeaders["App-Version"],
539
+ Authorization: `Bearer ${this.userToken}`,
540
+ },
541
+ });
542
+ const json = await res.json();
543
+ return await json.groups;
544
+ } catch (err) {
545
+ console.error(err);
546
+ throw new SidechatAPIError(`Failed to get groups from explore.`);
547
+ }
548
+ };
549
+
550
+ /**
551
+ * Searches for new groups based on a query keyword
552
+ * @method
553
+ * @since 2.6.0
554
+ * @param {String} query - the string to search for. This will be encoded, so strings with spaces and special characters are okay.
555
+ * @returns {Promise<types.SidechatGroup[]>}
556
+ */
557
+ searchAvailableGroups = async (query) => {
558
+ if (!this.userToken) {
559
+ throw new SidechatAPIError("User is not authenticated.");
560
+ }
561
+ try {
562
+ const res = await fetch(
563
+ `${this.apiRoot}/v1/groups/explore/search?term=${encodeURIComponent(query)}`,
564
+ {
565
+ method: "GET",
566
+ headers: {
567
+ ...this.defaultHeaders,
568
+ Authorization: `Bearer ${this.userToken}`,
569
+ },
570
+ },
571
+ );
572
+ const json = await res.json();
573
+ return await json.results;
574
+ } catch (err) {
575
+ console.error(err);
576
+ throw new SidechatAPIError(`Failed to search groups.`);
577
+ }
578
+ };
579
+
580
+ /**
581
+ * 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.
582
+ * @method
583
+ * @since 2.0.6
584
+ * @returns {Promise<types.SidechatLibraryAsset[]>}
585
+ */
586
+ getAssetLibrary = async () => {
587
+ if (!this.userToken) {
588
+ throw new SidechatAPIError("User is not authenticated.");
589
+ }
590
+ try {
591
+ const res = await fetch(`${this.apiRoot}/v1/assets/library`, {
592
+ method: "GET",
593
+ headers: {
594
+ ...this.defaultHeaders,
595
+ Authorization: `Bearer ${this.userToken}`,
596
+ },
597
+ });
598
+ const json = await res.json();
599
+ return await json.items;
600
+ } catch (err) {
601
+ console.error(err);
602
+ throw new SidechatAPIError(`Failed to get asset library.`);
603
+ }
604
+ };
605
+
606
+ /**
607
+ * Gets the current authenticated user and a list of the groups of which they are members.
608
+ * @method
609
+ * @since 2.1.0
610
+ * @returns {Promise<types.SidechatCurrentUser>}
611
+ */
612
+ getCurrentUser = async () => {
613
+ if (!this.userToken) {
614
+ throw new SidechatAPIError("User is not authenticated.");
615
+ }
616
+ try {
617
+ const res = await fetch(`${this.apiRoot}/v1/users/me`, {
618
+ method: "GET",
619
+ headers: {
620
+ ...this.defaultHeaders,
621
+ Authorization: `Bearer ${this.userToken}`,
622
+ },
623
+ });
624
+ const json = await res.json();
625
+ return await json;
626
+ } catch (err) {
627
+ console.error(err);
628
+ throw new SidechatAPIError(`Failed to get asset library.`);
629
+ }
630
+ };
631
+
632
+ /**
633
+ * Gets the metadata of a group from its ID
634
+ * @method
635
+ * @since 2.1.0
636
+ * @param {String} [groupID] - alphanumeric ID of a group to get. Falls back to user's primary group.
637
+ * @returns {Promise<types.SidechatGroup>}
638
+ */
639
+ getGroupMetadata = async (groupID = "") => {
640
+ if (!this.userToken) {
641
+ throw new SidechatAPIError("User is not authenticated.");
642
+ }
643
+ try {
644
+ const res = await fetch(`${this.apiRoot}/v1/groups/${groupID}`, {
645
+ method: "GET",
646
+ headers: {
647
+ ...this.defaultHeaders,
648
+ Authorization: `Bearer ${this.userToken}`,
649
+ },
650
+ });
651
+ const json = await res.json();
652
+ return await json.group;
653
+ } catch (err) {
654
+ console.error(err);
655
+ throw new SidechatAPIError(`Failed to get group metadata.`);
656
+ }
657
+ };
658
+
659
+ /**
660
+ * Joins or leaves a group
661
+ * @method
662
+ * @param {String} groupID - alphanumeric ID of group to join or leave
663
+ * @param {Boolean} isMember - whether or not the user should be a member of the group
664
+ * @since 2.3.8
665
+ */
666
+ setGroupMembership = async (groupID, isMember) => {
667
+ if (!this.userToken) {
668
+ throw new SidechatAPIError("User is not authenticated.");
669
+ }
670
+ try {
671
+ const res = await fetch(
672
+ `${this.apiRoot}/v1/groups/${isMember ? "join" : "leave"}`,
673
+ {
674
+ method: "POST",
675
+ headers: {
676
+ ...this.defaultHeaders,
677
+ Authorization: `Bearer ${this.userToken}`,
678
+ },
679
+ body: JSON.stringify({
680
+ group_id: groupID,
681
+ }),
682
+ },
683
+ );
684
+ const json = await res.json();
685
+ return await json;
686
+ } catch (err) {
687
+ console.error(err);
688
+ throw new SidechatAPIError(`Failed to modify group membership.`);
689
+ }
690
+ };
691
+
692
+ /**
693
+ * Creates a comment on a post
694
+ * @method
695
+ * @since 2.2.0
696
+ * @param {String} parentPostID - alphanumeric ID of post on which this comment is made
697
+ * @param {String} text - text content of comment
698
+ * @param {String} groupID - alphanumeric ID of group in which the parent post resides
699
+ * @param {String} [replyCommentID] - alphanumeric ID of comment to reply to. Falls back to parentPostID
700
+ * @param {String} [topLevelReplyID] - alphanumeric ID of the top-level comment to reply to. Used only when replying to replies. Falls back to parentPostID
701
+ * @param {types.SidechatSimpleAsset[]} [assetList] - list of assets to attach
702
+ * @param {Boolean} [disableDMs] - prevent direct messages being sent to comment's author
703
+ * @param {Boolean} [anonymous] - whether or not to hide user's name and icon on comment
704
+ * @returns {Promise<types.SidechatPostOrComment>} created comment
705
+ */
706
+ createComment = async (
707
+ parentPostID,
708
+ text,
709
+ groupID,
710
+ replyCommentID,
711
+ topLevelReplyID,
712
+ assetList = [],
713
+ disableDMs = false,
714
+ anonymous = false,
715
+ ) => {
716
+ if (!this.userToken) {
717
+ throw new SidechatAPIError("User is not authenticated.");
718
+ }
719
+ try {
720
+ const res = await fetch(`${this.apiRoot}/v1/posts`, {
721
+ method: "POST",
722
+ headers: {
723
+ ...this.defaultHeaders,
724
+ Authorization: `Bearer ${this.userToken}`,
725
+ },
726
+ body: JSON.stringify({
727
+ type: "comment",
728
+ assets: assetList,
729
+ group_ids: [groupID],
730
+ text: text,
731
+ reply_post_id: topLevelReplyID || replyCommentID || parentPostID,
732
+ reply_comment_post_id: replyCommentID || parentPostID,
733
+ parent_post_id: parentPostID,
734
+ dms_disabled: disableDMs,
735
+ using_identity: !anonymous,
736
+ }),
737
+ });
738
+ const json = await res.json();
739
+ return await json.comment;
740
+ } catch (err) {
741
+ console.error(err);
742
+ throw new SidechatAPIError(`Failed to post comment.`);
743
+ }
744
+ };
745
+
746
+ /**
747
+ * Creates a new post in the specified group
748
+ * @method
749
+ * @since 2.2.0
750
+ * @param {String} text - text content of comment
751
+ * @param {String} groupID - alphanumeric ID of group in which the parent post resides
752
+ * @param {types.SidechatSimpleAsset[]} [assetList] - list of assets to attach.
753
+ * @param {Boolean} [disableDMs] - prevent direct messages from being sent to post's author
754
+ * @param {Boolean} [disableComments] - whether or not comments should be disabled on post
755
+ * @param {Boolean} [anonymous] - whether or not to hide user's name and icon on post
756
+ * @param {Array<String>} [pollOptions] - List of poll options. If provided, a poll will be created with these options.
757
+ * @returns {Promise<types.SidechatPostOrComment>} the created post
758
+ */
759
+ createPost = async (
760
+ text,
761
+ groupID,
762
+ assetList = [],
763
+ disableDMs = false,
764
+ disableComments = false,
765
+ anonymous = false,
766
+ repostId = undefined,
767
+ pollOptions = undefined,
768
+ ) => {
769
+ if (!this.userToken) {
770
+ throw new SidechatAPIError("User is not authenticated.");
771
+ }
772
+ try {
773
+ // Build the request body
774
+ const body = {
775
+ type: "post",
776
+ assets: assetList,
777
+ group_ids: [groupID],
778
+ text: text,
779
+ attachments: [],
780
+ dms_disabled: disableDMs,
781
+ comments_disabled: disableComments,
782
+ using_identity: !anonymous,
783
+ quote_post_id: repostId,
784
+ };
785
+ // If pollOptions is provided and is a non-empty array, add poll_request
786
+ if (Array.isArray(pollOptions) && pollOptions.length > 0) {
787
+ body.poll_request = {
788
+ allows_view_results: true,
789
+ choices: pollOptions,
790
+ };
791
+ }
792
+ const res = await fetch(`${this.apiRoot}/v1/posts`, {
793
+ method: "POST",
794
+ headers: {
795
+ ...this.defaultHeaders,
796
+ Authorization: `Bearer ${this.userToken}`,
797
+ },
798
+ body: JSON.stringify(body),
799
+ });
800
+ const json = await res.json();
801
+ return await json.posts[0];
802
+ } catch (err) {
803
+ console.error(err);
804
+ throw new SidechatAPIError(`Failed to make post.`);
805
+ }
806
+ };
807
+
808
+ /**
809
+ * Deletes a post or comment that the user created
810
+ * @method
811
+ * @since 2.2.0
812
+ * @param {String} postOrCommentID - alphanumeric ID of post to delete
813
+ */
814
+ deletePostOrComment = async (postOrCommentID) => {
815
+ if (!this.userToken) {
816
+ throw new SidechatAPIError("User is not authenticated.");
817
+ }
818
+ try {
819
+ const res = await fetch(`${this.apiRoot}/v1/posts/delete`, {
820
+ method: "POST",
821
+ headers: {
822
+ ...this.defaultHeaders,
823
+ Authorization: `Bearer ${this.userToken}`,
824
+ },
825
+ body: JSON.stringify({
826
+ post_id: postOrCommentID,
827
+ }),
828
+ });
829
+ const json = await res.json();
830
+ return await json;
831
+ } catch (err) {
832
+ console.error(err);
833
+ throw new SidechatAPIError(`Failed to delete post.`);
834
+ }
835
+ };
836
+
837
+ /**
838
+ * Votes on a poll attached to a post
839
+ * @method
840
+ * @param {String} pollId - alphanumeric ID of poll to vote on
841
+ * @param {Number} choiceIndex - index of the choice to vote on
842
+ * @since 2.5.4
843
+ */
844
+ voteOnPoll = async (pollId, choiceIndex) => {
845
+ if (!this.userToken) {
846
+ throw new SidechatAPIError("User is not authenticated.");
847
+ }
848
+ try {
849
+ const res = await fetch(`${this.apiRoot}/v1/polls/vote`, {
850
+ method: "POST",
851
+ headers: {
852
+ ...this.defaultHeaders,
853
+ Authorization: `Bearer ${this.userToken}`,
854
+ },
855
+ body: JSON.stringify({
856
+ poll_id: pollId,
857
+ choice: choiceIndex,
858
+ }),
859
+ });
860
+ const json = await res.json();
861
+ return await json;
862
+ } catch (err) {
863
+ console.error(err);
864
+ throw new SidechatAPIError(`Failed to vote on poll`);
865
+ }
866
+ };
867
+
868
+ /**
869
+ * Marks that the user has viewed results on a poll. Note that this does not actually return the results of the poll.
870
+ * @method
871
+ * @param {String} pollId - alphanumeric ID of poll to vote on
872
+ * @since 2.5.4
873
+ */
874
+ viewPollResults = async (pollId) => {
875
+ if (!this.userToken) {
876
+ throw new SidechatAPIError("User is not authenticated.");
877
+ }
878
+ try {
879
+ const res = await fetch(
880
+ `${this.apiRoot}/v1/polls/view_results&cacheBust=${Date.now()}`,
881
+ {
882
+ method: "POST",
883
+ headers: {
884
+ ...this.defaultHeaders,
885
+ Authorization: `Bearer ${this.userToken}`,
886
+ },
887
+ body: JSON.stringify({
888
+ poll_id: pollId,
889
+ }),
890
+ },
891
+ );
892
+ const json = await res.json();
893
+ return await json;
894
+ } catch (err) {
895
+ console.error(err);
896
+ throw new SidechatAPIError(`Failed to mark poll results as viewed.`);
897
+ }
898
+ };
899
+
900
+ /**
901
+ * Uploads an asset to AWS S3 for use in posts and comments. Currently photos only
902
+ * @method
903
+ * @param {String} uri - URI of the asset to upload
904
+ * @param {String} mimeType - mimetype of the asset (e.g. "image/png")
905
+ * @param {String} [name] - filename of the asset
906
+ * @returns {Promise<String>} URL of the uploaded asset
907
+ * @since 2.5.1
908
+ */
909
+ uploadAsset = async (uri, mimeType, name = "") => {
910
+ if (!this.userToken) {
911
+ throw new SidechatAPIError("User is not authenticated.");
912
+ }
913
+
914
+ let imageType = mimeType.split("/")[1];
915
+ if (!["png", "jpeg", "gif"].includes(imageType)) {
916
+ throw new SidechatAPIError("Unsupported image format.");
917
+ }
918
+
919
+ const data = new FormData();
920
+ data.append("image", {
921
+ name: name,
922
+ type: mimeType,
923
+ uri: uri,
924
+ });
925
+
926
+ const urlReq = await this.sendRequest(
927
+ `/v1/assets/upload_url?content_type=${imageType}`,
928
+ );
929
+ const urlJson = await urlReq.json();
930
+
931
+ try {
932
+ const uploadReq = await fetch(urlJson.upload_url, {
933
+ body: data.getAll("image")[0],
934
+ method: "PUT",
935
+ headers: {
936
+ "Content-Type": mimeType,
937
+ },
938
+ });
939
+ if (uploadReq.status == 200) {
940
+ return `${this.apiRoot}/v1/assets/library/${urlJson.asset_id}`;
941
+ } else {
942
+ throw new SidechatAPIError(
943
+ `Couldn't upload image - error ${uploadReq.status}`,
944
+ );
945
+ }
946
+ } catch (e) {
947
+ throw new SidechatAPIError(e.message);
948
+ }
949
+ };
950
+
951
+ /**
952
+ * Sets the conversation icon of a user
953
+ * @method
954
+ * @since 2.2.1
955
+ * @param {String} userID - alphanumeric ID of user
956
+ * @param {String} emoji - emoji to set as icon
957
+ * @param {String} primaryColor - hex string (including #) of primary color
958
+ * @param {String} secondaryColor - hex string (including #) of secondary color
959
+ */
960
+ setUserIcon = async (userID, emoji, primaryColor, secondaryColor) => {
961
+ if (!this.userToken) {
962
+ throw new SidechatAPIError("User is not authenticated.");
963
+ }
964
+ try {
965
+ const res = await fetch(`${this.apiRoot}/v1/users/${userID}`, {
966
+ method: "PATCH",
967
+ headers: {
968
+ ...this.defaultHeaders,
969
+ "App-Version": "0",
970
+ Authorization: `Bearer ${this.userToken}`,
971
+ },
972
+ body: JSON.stringify({
973
+ conversation_icon: {
974
+ emoji: emoji,
975
+ secondary_color: secondaryColor,
976
+ is_migrated: true,
977
+ color: primaryColor,
978
+ },
979
+ }),
980
+ });
981
+ const json = await res.json();
982
+ return await json;
983
+ } catch (err) {
984
+ console.error(err);
985
+ throw new SidechatAPIError(`Failed to set icon.`);
986
+ }
987
+ };
988
+
989
+ /**
990
+ * Sets the bio text of a user
991
+ * @method
992
+ * @since 2.5.6
993
+ * @param {String} userID - alphanumeric ID of user
994
+ * @param {String} bio - text to set as bio
995
+ */
996
+ setUserBio = async (userID, bio) => {
997
+ if (!this.userToken) {
998
+ throw new SidechatAPIError("User is not authenticated.");
999
+ }
1000
+ try {
1001
+ const res = await fetch(`${this.apiRoot}/v1/users/${userID}`, {
1002
+ method: "PATCH",
1003
+ headers: {
1004
+ ...this.defaultHeaders,
1005
+ "App-Version": "5.4.22",
1006
+ Authorization: `Bearer ${this.userToken}`,
1007
+ },
1008
+ body: JSON.stringify({
1009
+ bio: bio,
1010
+ }),
1011
+ });
1012
+ const json = await res.json();
1013
+ return await json;
1014
+ } catch (err) {
1015
+ console.error(err);
1016
+ throw new SidechatAPIError(`Failed to set bio.`);
1017
+ }
1018
+ };
1019
+
1020
+ /**
1021
+ * Checks if user can set their username to a string
1022
+ * @method
1023
+ * @since 2.3.6
1024
+ * @param {String} username - string to check
1025
+ * @returns {Promise<Boolean>} whether or not username is valid and unused
1026
+ */
1027
+ checkUsername = async (username) => {
1028
+ if (!this.userToken) {
1029
+ throw new SidechatAPIError("User is not authenticated.");
1030
+ }
1031
+ try {
1032
+ const res = await fetch(
1033
+ `${this.apiRoot}/v1/users/username?username=${username}`,
1034
+ {
1035
+ method: "GET",
1036
+ headers: {
1037
+ ...this.defaultHeaders,
1038
+ Authorization: `Bearer ${this.userToken}`,
1039
+ },
1040
+ },
1041
+ );
1042
+ if (res?.status == 200 || res?.status == 204) {
1043
+ return true;
1044
+ } else {
1045
+ return false;
1046
+ }
1047
+ } catch (err) {
1048
+ console.error(err);
1049
+ throw new SidechatAPIError(`Failed to check username.`);
1050
+ }
1051
+ };
1052
+
1053
+ /**
1054
+ * Changes the username of the current user
1055
+ * @method
1056
+ * @since 2.3.6
1057
+ * @param {String} userID - alphanumeric ID of user
1058
+ * @param {String} username - new username to set
1059
+ */
1060
+ setUsername = async (userID, username) => {
1061
+ if (!this.userToken) {
1062
+ throw new SidechatAPIError("User is not authenticated.");
1063
+ }
1064
+ try {
1065
+ const res = await fetch(`${this.apiRoot}/v1/users/${userID}`, {
1066
+ method: "PATCH",
1067
+ headers: {
1068
+ ...this.defaultHeaders,
1069
+ Authorization: `Bearer ${this.userToken}`,
1070
+ },
1071
+ body: JSON.stringify({
1072
+ username: username,
1073
+ }),
1074
+ });
1075
+ const json = await res.json();
1076
+ return await json.user;
1077
+ } catch (err) {
1078
+ console.error(err);
1079
+ throw new SidechatAPIError(`Failed to set icon.`);
1080
+ }
1081
+ };
1082
+
1083
+ /**
1084
+ * Fetches a public user profile
1085
+ * @method
1086
+ * @since 2.6.0
1087
+ * @param {String} username - username of the user to fetch
1088
+ * @returns {Promise<types.SidechatProfile>}
1089
+ */
1090
+ getUserProfile = async (username) => {
1091
+ if (!this.userToken) {
1092
+ throw new SidechatAPIError("User is not authenticated.");
1093
+ }
1094
+ try {
1095
+ const res = await fetch(
1096
+ `${this.apiRoot}/v1/groups/username?username=${username}&cacheBust=${Date.now()}`,
1097
+ {
1098
+ method: "GET",
1099
+ headers: {
1100
+ ...this.defaultHeaders,
1101
+ Authorization: `Bearer ${this.userToken}`,
1102
+ },
1103
+ },
1104
+ );
1105
+ const json = await res.json();
1106
+ return await json.group;
1107
+ } catch (err) {
1108
+ console.error(err);
1109
+ throw new SidechatAPIError(`Failed to set icon.`);
1110
+ }
1111
+ };
1112
+
1113
+ /**
1114
+ * Fetches a public user's posts
1115
+ * @method
1116
+ * @since 2.6.0
1117
+ * @param {String} username - username of the user to fetch
1118
+ * @returns {Promise<types.SidechatPostOrComment[]>}
1119
+ */
1120
+ getUserPosts = async (username) => {
1121
+ if (!this.userToken) {
1122
+ throw new SidechatAPIError("User is not authenticated.");
1123
+ }
1124
+ try {
1125
+ const res = await fetch(
1126
+ `${this.apiRoot}/v1/users/posts?username=${username}`,
1127
+ {
1128
+ method: "GET",
1129
+ headers: {
1130
+ ...this.defaultHeaders,
1131
+ Authorization: `Bearer ${this.userToken}`,
1132
+ },
1133
+ },
1134
+ );
1135
+ const json = await res.json();
1136
+ return await json.posts;
1137
+ } catch (err) {
1138
+ console.error(err);
1139
+ throw new SidechatAPIError(`Failed to set icon.`);
1140
+ }
1141
+ };
1142
+
1143
+ /**
1144
+ * Marks an activity item as read
1145
+ * @method
1146
+ * @since 2.3.2
1147
+ * @param {String} activityID - alphanumeric ID of activity object
1148
+ */
1149
+ readActivity = async (activityID) => {
1150
+ if (!this.userToken) {
1151
+ throw new SidechatAPIError("User is not authenticated.");
1152
+ }
1153
+ try {
1154
+ const res = await fetch(`${this.apiRoot}/v1/activity/seen`, {
1155
+ method: "POST",
1156
+ headers: {
1157
+ ...this.defaultHeaders,
1158
+ Authorization: `Bearer ${this.userToken}`,
1159
+ },
1160
+ body: JSON.stringify({
1161
+ ids: [activityID],
1162
+ }),
1163
+ });
1164
+ const json = await res.json();
1165
+ return await json;
1166
+ } catch (err) {
1167
+ console.error(err);
1168
+ throw new SidechatAPIError(`Failed to mark activity as read.`);
1169
+ }
1170
+ };
1171
+
1172
+ /**
1173
+ * Retrieves joinable group chats
1174
+ * @method
1175
+ * @since 2.3.5
1176
+ */
1177
+ getGroupChats = async () => {
1178
+ if (!this.userToken) {
1179
+ throw new SidechatAPIError("User is not authenticated.");
1180
+ }
1181
+ try {
1182
+ const res = await fetch(
1183
+ `${this.apiRoot}/v1/chats/explore&cacheBust=${Date.now()}`,
1184
+ {
1185
+ method: "GET",
1186
+ headers: {
1187
+ ...this.defaultHeaders,
1188
+ Authorization: `Bearer ${this.userToken}`,
1189
+ },
1190
+ },
1191
+ );
1192
+ const json = await res.json();
1193
+ return await json.chats;
1194
+ } catch (err) {
1195
+ console.error(err);
1196
+ throw new SidechatAPIError(`Failed to get groupchats.`);
1197
+ }
1198
+ };
1199
+
1200
+ /**
1201
+ * Joins a group chat. To mimic the official client, use the user's display name and icon by default.
1202
+ * @method
1203
+ * @param {String} groupChatID - alphanumeric ID of group chat to join
1204
+ * @param {String} displayName - display name to use in chat
1205
+ * @param {String} emoji - emoji to use as icon
1206
+ * @param {String} primaryColor - hex string of primary color
1207
+ * @param {String} secondaryColor - hex string of secondary color
1208
+ * @since 2.3.5
1209
+ */
1210
+ joinGroupChat = async (
1211
+ groupChatID,
1212
+ displayName,
1213
+ emoji,
1214
+ primaryColor,
1215
+ secondaryColor,
1216
+ ) => {
1217
+ if (!this.userToken) {
1218
+ throw new SidechatAPIError("User is not authenticated.");
1219
+ }
1220
+ try {
1221
+ const res = await fetch(`${this.apiRoot}/v1/chats/groups/join`, {
1222
+ method: "POST",
1223
+ headers: {
1224
+ ...this.defaultHeaders,
1225
+ Authorization: `Bearer ${this.userToken}`,
1226
+ },
1227
+ body: JSON.stringify({
1228
+ chat_id: groupChatID,
1229
+ identity: {
1230
+ display_name: displayName,
1231
+ emoji: emoji,
1232
+ secondary_color: secondaryColor,
1233
+ color: primaryColor,
1234
+ },
1235
+ }),
1236
+ });
1237
+ const json = await res.json();
1238
+ return await json;
1239
+ } catch (err) {
1240
+ console.error(err);
1241
+ throw new SidechatAPIError(`Failed to join groupchat.`);
1242
+ }
1243
+ };
1244
+
1245
+ /**
1246
+ * Gets a list of the user's direct messages
1247
+ * @method
1248
+ * @returns {Promise<types.SidechatDirectThread[]>}
1249
+ * @since 2.4.4
1250
+ */
1251
+ getDMs = async () => {
1252
+ if (!this.userToken) {
1253
+ throw new SidechatAPIError("User is not authenticated.");
1254
+ }
1255
+ try {
1256
+ const res = await fetch(`${this.apiRoot}/v1/chats`, {
1257
+ method: "GET",
1258
+ headers: {
1259
+ ...this.defaultHeaders,
1260
+ Authorization: `Bearer ${this.userToken}`,
1261
+ },
1262
+ });
1263
+ const json = await res.json();
1264
+ const list = await json.chats;
1265
+ let r = [];
1266
+ list.forEach((o) => {
1267
+ r.push(o.chat);
1268
+ });
1269
+ return await r;
1270
+ } catch (err) {
1271
+ console.error(err);
1272
+ throw new SidechatAPIError(`Failed to fetch DMs.`);
1273
+ }
1274
+ };
1275
+
1276
+ /**
1277
+ * Gets a single direct message thread
1278
+ * @method
1279
+ * @param {String} id - alphanumeric ID of the chat to fetch
1280
+ * @returns {Promise<types.SidechatDirectThread>}
1281
+ * @since 2.4.4
1282
+ */
1283
+ getDMThread = async (id) => {
1284
+ if (!this.userToken) {
1285
+ throw new SidechatAPIError("User is not authenticated.");
1286
+ }
1287
+ try {
1288
+ const res = await fetch(
1289
+ `${this.apiRoot}/v1/chats/messages?chat_id=${id}`,
1290
+ {
1291
+ method: "GET",
1292
+ headers: {
1293
+ ...this.defaultHeaders,
1294
+ Authorization: `Bearer ${this.userToken}`,
1295
+ },
1296
+ },
1297
+ );
1298
+ const json = await res.json();
1299
+ return await json.chat;
1300
+ } catch (err) {
1301
+ console.error(err);
1302
+ throw new SidechatAPIError(`Failed to fetch DM thread.`);
1303
+ }
1304
+ };
1305
+
1306
+ /**
1307
+ * Sends a message to an existing direct message thread - note that you must first use startDM() to start a thread.
1308
+ * @method
1309
+ * @param {String} chatID - alphanumeric ID of the chat to send to
1310
+ * @param {String} text - text content of message
1311
+ * @param {String} clientID - alphanumeric device ID
1312
+ * @param {types.SidechatAsset[]} assets - array of assets to send
1313
+ * @param {Boolean} anonymous - whether the DM should be sent anonymously
1314
+ * @since 2.4.4
1315
+ */
1316
+ sendDM = async (chatID, text, clientID, assets = [], anonymous = false) => {
1317
+ if (!this.userToken) {
1318
+ throw new SidechatAPIError("User is not authenticated.");
1319
+ }
1320
+ try {
1321
+ const res = await fetch(`${this.apiRoot}/v1/chats/send`, {
1322
+ method: "POST",
1323
+ headers: {
1324
+ ...this.defaultHeaders,
1325
+ Authorization: `Bearer ${this.userToken}`,
1326
+ },
1327
+ body: JSON.stringify({
1328
+ chat_id: chatID,
1329
+ text: text,
1330
+ client_id: clientID,
1331
+ anonymous: anonymous,
1332
+ assets: assets,
1333
+ }),
1334
+ });
1335
+ const json = await res.json();
1336
+ return await json;
1337
+ } catch (err) {
1338
+ console.error(err);
1339
+ throw new SidechatAPIError(`Failed to send message.`);
1340
+ }
1341
+ };
1342
+
1343
+ /**
1344
+ * Creates a new direct message thread
1345
+ * @method
1346
+ * @param {String} text - text content of message
1347
+ * @param {String} clientID - alphanumeric ID of devide
1348
+ * @param {String} postID - alphanumeric ID of post or comment
1349
+ * @param {Boolean} anonymous - whether the DM should be sent anonymously
1350
+ * @param {"feed"} postContext - context of post (mostly undocumented, defaults to "feed")
1351
+ * @since 2.4.4
1352
+ */
1353
+ startDM = async (
1354
+ text,
1355
+ clientID,
1356
+ postID,
1357
+ anonymous = false,
1358
+ postContext = "feed",
1359
+ ) => {
1360
+ if (!this.userToken) {
1361
+ throw new SidechatAPIError("User is not authenticated.");
1362
+ }
1363
+ try {
1364
+ const res = await fetch(`${this.apiRoot}/v1/chats/start`, {
1365
+ method: "POST",
1366
+ headers: {
1367
+ ...this.defaultHeaders,
1368
+ Authorization: `Bearer ${this.userToken}`,
1369
+ },
1370
+ body: JSON.stringify({
1371
+ text: text,
1372
+ client_id: clientID,
1373
+ post_id: postID,
1374
+ anonymous: anonymous,
1375
+ post_context: postContext,
1376
+ }),
1377
+ });
1378
+ const json = await res.json();
1379
+ return await json;
1380
+ } catch (err) {
1381
+ console.error(err);
1382
+ throw new SidechatAPIError(`Failed to start DM.`);
1383
+ }
1384
+ };
1385
+
1386
+ /**
1387
+ * Hides posts from user
1388
+ * @method
1389
+ * @param {String} postID - alphanumeric ID of post to hide
1390
+ * @since 2.6.2
1391
+ */
1392
+ hidePostsFromUser = async (postID) => {
1393
+ if (!this.userToken) {
1394
+ throw new SidechatAPIError("User is not authenticated.");
1395
+ }
1396
+ try {
1397
+ const res = await fetch(`${this.apiRoot}/v1/posts/hide_posts_from_user`, {
1398
+ method: "POST",
1399
+ headers: {
1400
+ ...this.defaultHeaders,
1401
+ Authorization: `Bearer ${this.userToken}`,
1402
+ },
1403
+ body: JSON.stringify({
1404
+ post_id: postID,
1405
+ post_context: "feed",
1406
+ report: false,
1407
+ }),
1408
+ });
1409
+ const json = await res.json();
1410
+ return await json;
1411
+ } catch (err) {
1412
+ console.error(err);
1413
+ throw new SidechatAPIError(`Failed to hide post.`);
1414
+ }
1415
+ };
1416
+
1417
+ /**
1418
+ * Unhides all posts from all users
1419
+ * @method
1420
+ * @since 2.6.2
1421
+ */
1422
+ unhidePostsFromAllUsers = async () => {
1423
+ if (!this.userToken) {
1424
+ throw new SidechatAPIError("User is not authenticated.");
1425
+ }
1426
+ try {
1427
+ const res = await fetch(`${this.apiRoot}/v1/users/unhide_all`, {
1428
+ method: "POST",
1429
+ headers: {
1430
+ ...this.defaultHeaders,
1431
+ Authorization: `Bearer ${this.userToken}`,
1432
+ },
1433
+ body: JSON.stringify({}),
1434
+ });
1435
+ const json = await res.json();
1436
+ return await json;
1437
+ } catch (err) {
1438
+ console.error(err);
1439
+ throw new SidechatAPIError(`Failed to unhide post.`);
1440
+ }
1441
+ };
1442
+ }
1443
+
1444
+ export default SidechatAPIClient;