ChatApiHandler.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* eslint-disable
  2. n/handle-callback-err,
  3. max-len,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let ChatApiHandler
  14. const OError = require('@overleaf/o-error')
  15. const request = require('request')
  16. const settings = require('@overleaf/settings')
  17. const { promisify } = require('util')
  18. function getThreads(projectId, callback) {
  19. if (callback == null) {
  20. callback = function () {}
  21. }
  22. return ChatApiHandler._apiRequest(
  23. {
  24. url: `${settings.apis.chat.internal_url}/project/${projectId}/threads`,
  25. method: 'GET',
  26. json: true,
  27. },
  28. callback
  29. )
  30. }
  31. function destroyProject(projectId, callback) {
  32. if (callback == null) {
  33. callback = function () {}
  34. }
  35. return ChatApiHandler._apiRequest(
  36. {
  37. url: `${settings.apis.chat.internal_url}/project/${projectId}`,
  38. method: 'DELETE',
  39. },
  40. callback
  41. )
  42. }
  43. module.exports = ChatApiHandler = {
  44. _apiRequest(opts, callback) {
  45. if (callback == null) {
  46. callback = function () {}
  47. }
  48. return request(opts, function (error, response, data) {
  49. if (error != null) {
  50. return callback(error)
  51. }
  52. if (response.statusCode >= 200 && response.statusCode < 300) {
  53. return callback(null, data)
  54. } else {
  55. error = new OError(
  56. `chat api returned non-success code: ${response.statusCode}`,
  57. opts
  58. )
  59. error.statusCode = response.statusCode
  60. return callback(error)
  61. }
  62. })
  63. },
  64. sendGlobalMessage(projectId, userId, content, callback) {
  65. return ChatApiHandler._apiRequest(
  66. {
  67. url: `${settings.apis.chat.internal_url}/project/${projectId}/messages`,
  68. method: 'POST',
  69. json: { user_id: userId, content },
  70. },
  71. callback
  72. )
  73. },
  74. getGlobalMessages(projectId, limit, before, callback) {
  75. const qs = {}
  76. if (limit != null) {
  77. qs.limit = limit
  78. }
  79. if (before != null) {
  80. qs.before = before
  81. }
  82. return ChatApiHandler._apiRequest(
  83. {
  84. url: `${settings.apis.chat.internal_url}/project/${projectId}/messages`,
  85. method: 'GET',
  86. qs,
  87. json: true,
  88. },
  89. callback
  90. )
  91. },
  92. sendComment(projectId, threadId, userId, content, callback) {
  93. if (callback == null) {
  94. callback = function () {}
  95. }
  96. return ChatApiHandler._apiRequest(
  97. {
  98. url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/messages`,
  99. method: 'POST',
  100. json: { user_id: userId, content },
  101. },
  102. callback
  103. )
  104. },
  105. resolveThread(projectId, threadId, userId, callback) {
  106. if (callback == null) {
  107. callback = function () {}
  108. }
  109. return ChatApiHandler._apiRequest(
  110. {
  111. url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/resolve`,
  112. method: 'POST',
  113. json: { user_id: userId },
  114. },
  115. callback
  116. )
  117. },
  118. reopenThread(projectId, threadId, callback) {
  119. if (callback == null) {
  120. callback = function () {}
  121. }
  122. return ChatApiHandler._apiRequest(
  123. {
  124. url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/reopen`,
  125. method: 'POST',
  126. },
  127. callback
  128. )
  129. },
  130. deleteThread(projectId, threadId, callback) {
  131. if (callback == null) {
  132. callback = function () {}
  133. }
  134. return ChatApiHandler._apiRequest(
  135. {
  136. url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}`,
  137. method: 'DELETE',
  138. },
  139. callback
  140. )
  141. },
  142. editMessage(projectId, threadId, messageId, userId, content, callback) {
  143. if (callback == null) {
  144. callback = function () {}
  145. }
  146. return ChatApiHandler._apiRequest(
  147. {
  148. url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/messages/${messageId}/edit`,
  149. method: 'POST',
  150. json: {
  151. content,
  152. userId,
  153. },
  154. },
  155. callback
  156. )
  157. },
  158. deleteMessage(projectId, threadId, messageId, callback) {
  159. if (callback == null) {
  160. callback = function () {}
  161. }
  162. return ChatApiHandler._apiRequest(
  163. {
  164. url: `${settings.apis.chat.internal_url}/project/${projectId}/thread/${threadId}/messages/${messageId}`,
  165. method: 'DELETE',
  166. },
  167. callback
  168. )
  169. },
  170. getThreads,
  171. destroyProject,
  172. promises: {
  173. getThreads: promisify(getThreads),
  174. destroyProject: promisify(destroyProject),
  175. },
  176. }