ChatApiHandler.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* eslint-disable
  2. camelcase,
  3. node/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 destroyProject(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}`,
  26. method: 'DELETE',
  27. },
  28. callback
  29. )
  30. }
  31. module.exports = ChatApiHandler = {
  32. _apiRequest(opts, callback) {
  33. if (callback == null) {
  34. callback = function () {}
  35. }
  36. return request(opts, function (error, response, data) {
  37. if (error != null) {
  38. return callback(error)
  39. }
  40. if (response.statusCode >= 200 && response.statusCode < 300) {
  41. return callback(null, data)
  42. } else {
  43. error = new OError(
  44. `chat api returned non-success code: ${response.statusCode}`,
  45. opts
  46. )
  47. error.statusCode = response.statusCode
  48. return callback(error)
  49. }
  50. })
  51. },
  52. sendGlobalMessage(project_id, user_id, content, callback) {
  53. return ChatApiHandler._apiRequest(
  54. {
  55. url: `${settings.apis.chat.internal_url}/project/${project_id}/messages`,
  56. method: 'POST',
  57. json: { user_id, content },
  58. },
  59. callback
  60. )
  61. },
  62. getGlobalMessages(project_id, limit, before, callback) {
  63. const qs = {}
  64. if (limit != null) {
  65. qs.limit = limit
  66. }
  67. if (before != null) {
  68. qs.before = before
  69. }
  70. return ChatApiHandler._apiRequest(
  71. {
  72. url: `${settings.apis.chat.internal_url}/project/${project_id}/messages`,
  73. method: 'GET',
  74. qs,
  75. json: true,
  76. },
  77. callback
  78. )
  79. },
  80. sendComment(project_id, thread_id, user_id, content, callback) {
  81. if (callback == null) {
  82. callback = function () {}
  83. }
  84. return ChatApiHandler._apiRequest(
  85. {
  86. url: `${settings.apis.chat.internal_url}/project/${project_id}/thread/${thread_id}/messages`,
  87. method: 'POST',
  88. json: { user_id, content },
  89. },
  90. callback
  91. )
  92. },
  93. getThreads(project_id, callback) {
  94. if (callback == null) {
  95. callback = function () {}
  96. }
  97. return ChatApiHandler._apiRequest(
  98. {
  99. url: `${settings.apis.chat.internal_url}/project/${project_id}/threads`,
  100. method: 'GET',
  101. json: true,
  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. destroyProject,
  172. promises: {
  173. destroyProject: promisify(destroyProject),
  174. },
  175. }