ChatClient.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import Request from 'request'
  2. const request = Request.defaults({
  3. baseUrl: 'http://127.0.0.1:3010',
  4. })
  5. async function asyncRequest(options) {
  6. return await new Promise((resolve, reject) => {
  7. request(options, (err, response, body) => {
  8. if (err) {
  9. reject(err)
  10. } else {
  11. resolve({ response, body })
  12. }
  13. })
  14. })
  15. }
  16. export async function sendGlobalMessage(projectId, userId, content) {
  17. return await asyncRequest({
  18. method: 'post',
  19. url: `/project/${projectId}/messages`,
  20. json: {
  21. user_id: userId,
  22. content,
  23. },
  24. })
  25. }
  26. export async function getGlobalMessages(projectId) {
  27. return await asyncRequest({
  28. method: 'get',
  29. url: `/project/${projectId}/messages`,
  30. json: true,
  31. })
  32. }
  33. export async function sendMessage(projectId, threadId, userId, content) {
  34. return await asyncRequest({
  35. method: 'post',
  36. url: `/project/${projectId}/thread/${threadId}/messages`,
  37. json: {
  38. user_id: userId,
  39. content,
  40. },
  41. })
  42. }
  43. export async function getThread(projectId, threadId) {
  44. return await asyncRequest({
  45. method: 'get',
  46. url: `/project/${projectId}/thread/${threadId}`,
  47. json: true,
  48. })
  49. }
  50. export async function getThreads(projectId) {
  51. return await asyncRequest({
  52. method: 'get',
  53. url: `/project/${projectId}/threads`,
  54. json: true,
  55. })
  56. }
  57. export async function resolveThread(projectId, threadId, userId) {
  58. return await asyncRequest({
  59. method: 'post',
  60. url: `/project/${projectId}/thread/${threadId}/resolve`,
  61. json: {
  62. user_id: userId,
  63. },
  64. })
  65. }
  66. export async function getResolvedThreadIds(projectId) {
  67. return await asyncRequest({
  68. method: 'get',
  69. url: `/project/${projectId}/resolved-thread-ids`,
  70. json: true,
  71. })
  72. }
  73. export async function editMessage(projectId, threadId, messageId, content) {
  74. return await asyncRequest({
  75. method: 'post',
  76. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  77. json: {
  78. content,
  79. },
  80. })
  81. }
  82. export async function editMessageWithUser(
  83. projectId,
  84. threadId,
  85. messageId,
  86. userId,
  87. content
  88. ) {
  89. return await asyncRequest({
  90. method: 'post',
  91. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  92. json: {
  93. content,
  94. userId,
  95. },
  96. })
  97. }
  98. export async function checkStatus() {
  99. return await asyncRequest({
  100. method: 'get',
  101. url: `/status`,
  102. json: true,
  103. })
  104. }
  105. export async function getMetric(matcher) {
  106. const { body } = await asyncRequest({
  107. method: 'get',
  108. url: `/metrics`,
  109. })
  110. const found = body.split('\n').find(matcher)
  111. if (!found) return 0
  112. return parseInt(found.split(' ')[1], 0)
  113. }
  114. export async function reopenThread(projectId, threadId) {
  115. return await asyncRequest({
  116. method: 'post',
  117. url: `/project/${projectId}/thread/${threadId}/reopen`,
  118. })
  119. }
  120. export async function deleteThread(projectId, threadId) {
  121. return await asyncRequest({
  122. method: 'delete',
  123. url: `/project/${projectId}/thread/${threadId}`,
  124. })
  125. }
  126. export async function deleteMessage(projectId, threadId, messageId) {
  127. return await asyncRequest({
  128. method: 'delete',
  129. url: `/project/${projectId}/thread/${threadId}/messages/${messageId}`,
  130. })
  131. }
  132. export async function destroyProject(projectId) {
  133. return await asyncRequest({
  134. method: 'delete',
  135. url: `/project/${projectId}`,
  136. })
  137. }
  138. export async function duplicateCommentThreads(projectId, threads) {
  139. return await asyncRequest({
  140. method: 'post',
  141. url: `/project/${projectId}/duplicate-comment-threads`,
  142. json: {
  143. threads,
  144. },
  145. })
  146. }
  147. export async function generateThreadData(projectId, threads) {
  148. return await asyncRequest({
  149. method: 'post',
  150. url: `/project/${projectId}/generate-thread-data`,
  151. json: {
  152. threads,
  153. },
  154. })
  155. }