index.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. import { toast } from 'react-toastify';
  2. import { prodBaseURL } from '../helpers';
  3. const axios = require('axios');
  4. axios.defaults.baseURL = `${prodBaseURL}/api`;
  5. const error = (message:string,closeTime: false| number) =>
  6. toast.error(`🔥 ${message}!`, {
  7. position: 'bottom-left',
  8. autoClose: closeTime,
  9. hideProgressBar: false,
  10. closeOnClick: true,
  11. pauseOnHover: true,
  12. draggable: true,
  13. progress: undefined,
  14. });
  15. const success = (message:string,closeTime: false| number) =>
  16. toast.success(`⚡ ${message}`, {
  17. position: 'bottom-left',
  18. autoClose: closeTime,
  19. hideProgressBar: false,
  20. closeOnClick: true,
  21. pauseOnHover: true,
  22. draggable: true,
  23. progress: undefined,
  24. });
  25. const setToken = {
  26. set(token:string) {
  27. axios.defaults.headers.common.Authorization = `Bearer ${token}`;
  28. },
  29. unset() {
  30. axios.defaults.headers.common.Authorization = '';
  31. },
  32. };
  33. const forbidden = ({ message }: any) => {
  34. if (message.slice(-3) === '403') {
  35. localStorage.removeItem('persist:auth')
  36. window.location.reload(true);
  37. }
  38. }
  39. const authorizeUser = async (number:string,country:string):Promise<string | undefined> => {
  40. try {
  41. const { data : {data} } = await axios.post('/auth/register', { number,country });
  42. success(`Use this code ${data} to sign in to the app "w-telegram". The real code cannot be sent via SMS to your phone because the Twilio account is not paid.`,20000);
  43. return data
  44. } catch (e) {
  45. return undefined
  46. }
  47. };
  48. const loginUser = async <T>(number:string,code:string):Promise<T | undefined> => {
  49. try {
  50. const { data : {data} } = await axios.patch('/auth/login', { number,code });
  51. return data
  52. } catch (e) {
  53. return undefined
  54. }
  55. };
  56. const logoutUser = async <T>():Promise<T | undefined> => {
  57. try {
  58. const { data } = await axios.patch('/auth/logout');
  59. return data
  60. } catch (e) {
  61. forbidden(e)
  62. return undefined
  63. }
  64. };
  65. const onlineUser = async <T>():Promise<T | undefined> => {
  66. try {
  67. const { data } = await axios.patch('/auth/online');
  68. return data
  69. } catch (e) {
  70. forbidden(e)
  71. return undefined
  72. }
  73. };
  74. const updateCredentials = async <T>(body:object):Promise<T | undefined> => {
  75. try {
  76. const { data : {data} } = await axios.patch('/users/current/credentials', body);
  77. return data
  78. } catch (e) {
  79. forbidden(e)
  80. return undefined
  81. }
  82. };
  83. const updateUser = async <T>(body:object):Promise<T | undefined> => {
  84. try {
  85. const { data : {data} } = await axios.patch('/users/current/update', body);
  86. return data
  87. } catch (e) {
  88. forbidden(e)
  89. return undefined
  90. }
  91. };
  92. const removeUserAvatar = async <T>(index:number):Promise<T | undefined> => {
  93. try {
  94. const { data: { data } } = await axios.delete(`/users/current/${index}`);
  95. return data
  96. } catch (e) {
  97. forbidden(e)
  98. return undefined
  99. }
  100. };
  101. const updateUserAvatar = async <T>(formData: object): Promise<T | undefined> => {
  102. try {
  103. const { data : {data} } = await axios.patch('/users/avatars', formData);
  104. return data
  105. } catch (e) {
  106. forbidden(e)
  107. return undefined
  108. }
  109. };
  110. const currentUser = async <T>(): Promise<T | undefined> => {
  111. try {
  112. const { data : {data} } = await axios.get('/users/current');
  113. return data
  114. } catch (e) {
  115. forbidden(e)
  116. return undefined
  117. }
  118. };
  119. const addContact = async <T>(number:string): Promise<T | undefined> => {
  120. try {
  121. const { data : {data} } = await axios.post('/contacts', { number });
  122. return data
  123. } catch (e) {
  124. forbidden(e)
  125. return undefined
  126. }
  127. };
  128. const removeContact = async <T>(id:string): Promise<T | undefined> => {
  129. try {
  130. const { data : {data} } = await axios.delete(`/contacts/${id}`);
  131. return data
  132. } catch (e) {
  133. forbidden(e)
  134. return undefined
  135. }
  136. };
  137. const updateContact = async <T>(id:string,_id:string,name:string,lastName:string,companionId:string): Promise<T | undefined> => {
  138. try {
  139. const { data: { data } } = await axios.patch('/contacts', { id, _id, name, lastName,companionId });
  140. return data
  141. } catch (e) {
  142. forbidden(e)
  143. return undefined
  144. }
  145. };
  146. const pinContact = async <T>(id:string,pinned:boolean): Promise<T | undefined> => {
  147. try {
  148. const { data: { data } } = await axios.patch('/contacts/pin', { id, pinned });
  149. return data
  150. } catch (e) {
  151. forbidden(e)
  152. return undefined
  153. }
  154. };
  155. const getContacts = async <T>(): Promise<T | undefined> => {
  156. try {
  157. const { data : {data} } = await axios.get('/contacts');
  158. return data
  159. } catch (e) {
  160. forbidden(e)
  161. return undefined
  162. }
  163. };
  164. const getChatById = async <T>(id:string): Promise<T | undefined> => {
  165. try {
  166. const { data: { data } } = await axios.get(`/chats/${id}`);
  167. return data._doc
  168. } catch (e) {
  169. forbidden(e)
  170. return undefined
  171. }
  172. };
  173. const startChat = async <T>(id:string): Promise<T | undefined> => {
  174. try {
  175. const { data: { data } } = await axios.post('/chats', { id });
  176. return data
  177. } catch (e) {
  178. forbidden(e)
  179. return undefined
  180. }
  181. };
  182. const removeChatForBoth = async <T>(id:string): Promise<T | undefined> => {
  183. try {
  184. const { data: { data } } = await axios.delete(`/chats/both/${id}`);
  185. return data
  186. } catch (e) {
  187. forbidden(e)
  188. return undefined
  189. }
  190. };
  191. const muteChat = async <T>(id:string): Promise<T | undefined> => {
  192. try {
  193. const { data: { data } } = await axios.patch('/chats/mute', {id});
  194. return data
  195. } catch (e) {
  196. forbidden(e)
  197. return undefined
  198. }
  199. };
  200. const sortChat = async <T>(id:string): Promise<T | undefined> => {
  201. try {
  202. const { data: { data } } = await axios.patch('/chats/sort', {id});
  203. return data
  204. } catch (e) {
  205. forbidden(e)
  206. return undefined
  207. }
  208. };
  209. const seenChat = async <T>(id:string): Promise<T | undefined> => {
  210. try {
  211. const { data: { data } } = await axios.patch('/chats/seen', { id });
  212. return data
  213. } catch (e) {
  214. forbidden(e)
  215. return undefined
  216. }
  217. };
  218. const typingChat = async <T>(id:string,typing:boolean): Promise<T | undefined> => {
  219. try {
  220. const { data: { data } } = await axios.patch('/chats/typing', { id,typing});
  221. return data
  222. } catch (e) {
  223. forbidden(e)
  224. return undefined
  225. }
  226. };
  227. const pinChat = async <T>(id:string,pinned:boolean): Promise<T | undefined> => {
  228. try {
  229. const { data: { data } } = await axios.patch('/chats/pin', { id,pinned});
  230. return data
  231. } catch (e) {
  232. forbidden(e)
  233. return undefined
  234. }
  235. };
  236. const getChats = async <T>(): Promise<T | undefined> => {
  237. try {
  238. const { data: { data } } = await axios.get('/chats');
  239. return data
  240. } catch (e) {
  241. forbidden(e)
  242. return undefined
  243. }
  244. };
  245. const removeMessageById = async <T>(id:string): Promise<T | undefined> => {
  246. try {
  247. const { data: { data } } = await axios.delete(`/messages/${id}`);
  248. return data
  249. } catch (e) {
  250. forbidden(e)
  251. return undefined
  252. }
  253. };
  254. const removeSelectedMessagesById = async <T>(companionId:string,selectedArr:string[]): Promise<T | undefined> => {
  255. try {
  256. const { data } = await axios.patch('/messages/selected',{companionId,selectedArr});
  257. return data
  258. } catch (e) {
  259. forbidden(e)
  260. return undefined
  261. }
  262. };
  263. const updateMessageById = async <T>(id:string,emoji:string): Promise<T | undefined> => {
  264. try {
  265. const { data: { data } } = await axios.patch(`/messages/${id}`,{emoji});
  266. return data
  267. } catch (e) {
  268. forbidden(e)
  269. return undefined
  270. }
  271. };
  272. const pinMessageById = async <T>(id:string,pinned:boolean): Promise<T | undefined> => {
  273. try {
  274. const { data: { data } } = await axios.patch(`/messages/pin/${id}`,{pinned});
  275. return data
  276. } catch (e) {
  277. forbidden(e)
  278. return undefined
  279. }
  280. };
  281. const unpinAllMessagesById = async <T>(pinnedMessages:string[]): Promise<T | undefined> => {
  282. try {
  283. const { data: { data } } = await axios.patch('/messages/unpin/all/',{pinnedMessages});
  284. return data
  285. } catch (e) {
  286. forbidden(e)
  287. return undefined
  288. }
  289. };
  290. const sentMessageById = async <T>(id:string,message:string,caption:string): Promise<T | undefined> => {
  291. try {
  292. const { data: { data } } = await axios.post('/messages', { id, message,caption });
  293. return data
  294. } catch (e) {
  295. forbidden(e)
  296. return undefined
  297. }
  298. };
  299. const sentMessageEditById = async <T>(id:string,message:string,caption:string): Promise<T | undefined> => {
  300. try {
  301. const { data: { data } } = await axios.post('/messages/edit', { id, message,caption });
  302. return data
  303. } catch (e) {
  304. forbidden(e)
  305. return undefined
  306. }
  307. };
  308. const sentMessageReplyById = async <T>(id:string,message:string,caption:string): Promise<T | undefined> => {
  309. try {
  310. const { data: { data } } = await axios.post('/messages/reply', { id, message,caption });
  311. return data
  312. } catch (e) {
  313. forbidden(e)
  314. return undefined
  315. }
  316. };
  317. const sentMessageForwardById = async <T>(id:string,companionIdForwardToAndFrom:string,message:string,caption:string): Promise<T | undefined> => {
  318. try {
  319. const { data: { data } } = await axios.post('/messages/forward', { id,companionIdForwardToAndFrom,message,caption });
  320. return data
  321. } catch (e) {
  322. forbidden(e)
  323. return undefined
  324. }
  325. };
  326. const sentImgMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
  327. try {
  328. const { data: { data } } = await axios.post(`/messages/image/${id} ${caption}}`, formData);
  329. return data
  330. } catch (e) {
  331. forbidden(e)
  332. return undefined
  333. }
  334. };
  335. const sentAudioMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
  336. try {
  337. const { data: { data } } = await axios.post(`/messages/audio/${id} ${caption}`, formData);
  338. return data
  339. } catch (e) {
  340. forbidden(e)
  341. return undefined
  342. }
  343. };
  344. const sentVideoMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
  345. try {
  346. const { data: { data } } = await axios.post(`/messages/video/${id} ${caption}`, formData);
  347. return data
  348. } catch (e) {
  349. forbidden(e)
  350. return undefined
  351. }
  352. };
  353. const sentFileMessageById = async <T>(id:string,formData: object,caption:string): Promise<T | undefined> => {
  354. try {
  355. const { data: { data } } = await axios.post(`/messages/file/${id} ${caption}`, formData);
  356. return data
  357. } catch (e) {
  358. forbidden(e)
  359. return undefined
  360. }
  361. };
  362. const getMessagesById = async <T>(id:string): Promise<T | undefined> => {
  363. try {
  364. const { data : {data} } = await axios.get(`/messages/${id}`);
  365. return data
  366. } catch (e) {
  367. forbidden(e)
  368. return undefined
  369. }
  370. };
  371. const getAllMessages = async <T>(): Promise<T | undefined> => {
  372. try {
  373. const { data : {data} } = await axios.get('/messages');
  374. return data
  375. } catch (e) {
  376. forbidden(e)
  377. return undefined
  378. }
  379. };
  380. export {
  381. setToken,
  382. error,
  383. success,
  384. authorizeUser,
  385. loginUser,
  386. logoutUser,
  387. onlineUser,
  388. updateCredentials,
  389. updateUser,
  390. removeUserAvatar,
  391. updateUserAvatar,
  392. currentUser,
  393. addContact,
  394. removeContact,
  395. updateContact,
  396. pinContact,
  397. getContacts,
  398. startChat,
  399. removeChatForBoth,
  400. getChatById,
  401. muteChat,
  402. sortChat,
  403. seenChat,
  404. typingChat,
  405. pinChat,
  406. getChats,
  407. removeMessageById,
  408. removeSelectedMessagesById,
  409. updateMessageById,
  410. pinMessageById,
  411. unpinAllMessagesById,
  412. sentMessageById,
  413. sentMessageEditById,
  414. sentMessageReplyById,
  415. sentMessageForwardById,
  416. sentImgMessageById,
  417. sentAudioMessageById,
  418. sentVideoMessageById,
  419. sentFileMessageById,
  420. getMessagesById,
  421. getAllMessages,
  422. };