ChatApiHandler.js 4.7 KB

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