ChatClient.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const request = require('request').defaults({
  2. baseUrl: 'http://localhost:3010',
  3. })
  4. async function asyncRequest(options) {
  5. return new Promise((resolve, reject) => {
  6. request(options, (err, response, body) => {
  7. if (err) {
  8. reject(err)
  9. } else {
  10. resolve({ response, body })
  11. }
  12. })
  13. })
  14. }
  15. async function sendGlobalMessage(projectId, userId, content) {
  16. return asyncRequest({
  17. method: 'post',
  18. url: `/project/${projectId}/messages`,
  19. json: {
  20. user_id: userId,
  21. content,
  22. },
  23. })
  24. }
  25. async function getGlobalMessages(projectId) {
  26. return asyncRequest({
  27. method: 'get',
  28. url: `/project/${projectId}/messages`,
  29. json: true,
  30. })
  31. }
  32. async function sendMessage(projectId, threadId, userId, content) {
  33. return asyncRequest({
  34. method: 'post',
  35. url: `/project/${projectId}/thread/${threadId}/messages`,
  36. json: {
  37. user_id: userId,
  38. content,
  39. },
  40. })
  41. }
  42. async function getThreads(projectId) {
  43. return asyncRequest({
  44. method: 'get',
  45. url: `/project/${projectId}/threads`,
  46. json: true,
  47. })
  48. }
  49. async function resolveThread(projectId, threadId, userId) {
  50. return asyncRequest({
  51. method: 'post',
  52. url: `/project/${projectId}/thread/${threadId}/resolve`,
  53. json: {
  54. user_id: userId,
  55. },
  56. })
  57. }
  58. async function reopenThread(projectId, threadId) {
  59. return asyncRequest({
  60. method: 'post',
  61. url: `/project/${projectId}/thread/${threadId}/reopen`,
  62. })
  63. }
  64. async function deleteThread(projectId, threadId) {
  65. return asyncRequest({
  66. method: 'delete',
  67. url: `/project/${projectId}/thread/${threadId}`,
  68. })
  69. }
  70. async function editMessage(projectId, threadId, messageId, content) {
  71. return asyncRequest({
  72. method: 'post',
  73. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  74. json: {
  75. content,
  76. },
  77. })
  78. }
  79. async function editMessageWithUser(
  80. projectId,
  81. threadId,
  82. messageId,
  83. userId,
  84. content
  85. ) {
  86. return asyncRequest({
  87. method: 'post',
  88. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  89. json: {
  90. content,
  91. userId,
  92. },
  93. })
  94. }
  95. async function deleteMessage(projectId, threadId, messageId) {
  96. return asyncRequest({
  97. method: 'delete',
  98. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
  99. })
  100. }
  101. async function destroyProject(projectId) {
  102. return asyncRequest({
  103. method: 'delete',
  104. url: `/project/${projectId}`,
  105. })
  106. }
  107. module.exports = {
  108. sendGlobalMessage,
  109. getGlobalMessages,
  110. sendMessage,
  111. getThreads,
  112. resolveThread,
  113. reopenThread,
  114. deleteThread,
  115. editMessage,
  116. editMessageWithUser,
  117. deleteMessage,
  118. destroyProject,
  119. }