CommentContainer.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. const logger = loggerFactory('growi:services:CommentContainer');
  4. /**
  5. *
  6. * @author Yuki Takei <yuki@weseek.co.jp>
  7. *
  8. * @extends {Container} unstated Container
  9. */
  10. export default class CommentContainer extends Container {
  11. constructor(appContainer) {
  12. super();
  13. this.appContainer = appContainer;
  14. this.appContainer.registerContainer(this);
  15. const mainContent = document.querySelector('#content-main');
  16. if (mainContent == null) {
  17. logger.debug('#content-main element is not exists');
  18. return;
  19. }
  20. this.state = {
  21. comments: [],
  22. // settings shared among all of CommentEditor
  23. isSlackEnabled: false,
  24. slackChannels: mainContent.getAttribute('data-slack-channels') || '',
  25. };
  26. this.retrieveComments = this.retrieveComments.bind(this);
  27. this.checkAndUpdateImageOfCommentAuthers = this.checkAndUpdateImageOfCommentAuthers.bind(this);
  28. }
  29. /**
  30. * Workaround for the mangling in production build to break constructor.name
  31. */
  32. static getClassName() {
  33. return 'CommentContainer';
  34. }
  35. getPageContainer() {
  36. return this.appContainer.getContainer('PageContainer');
  37. }
  38. findAndSplice(comment) {
  39. const comments = this.state.comments;
  40. const index = comments.indexOf(comment);
  41. if (index < 0) {
  42. return;
  43. }
  44. comments.splice(index, 1);
  45. this.setState({ comments });
  46. }
  47. /**
  48. * Load data of comments and store them in state
  49. */
  50. async retrieveComments() {
  51. const { pageId } = this.getPageContainer().state;
  52. // get data (desc order array)
  53. const res = await this.appContainer.apiGet('/comments.get', { page_id: pageId });
  54. if (res.ok) {
  55. const comments = res.comments;
  56. this.setState({ comments });
  57. this.checkAndUpdateImageOfCommentAuthers(comments);
  58. }
  59. }
  60. async checkAndUpdateImageOfCommentAuthers(comments) {
  61. const noImageCacheUserIds = comments.filter((comment) => {
  62. const { creator } = comment;
  63. return creator != null && creator.imageUrlCached == null;
  64. }).map((comment) => {
  65. return comment.creator._id;
  66. });
  67. if (noImageCacheUserIds.length === 0) {
  68. return;
  69. }
  70. try {
  71. await this.appContainer.apiv3Put('/users/update.imageUrlCache', { userIds: noImageCacheUserIds });
  72. }
  73. catch (err) {
  74. // Error alert doesn't apear, because user don't need to notice this error.
  75. logger.error(err);
  76. }
  77. }
  78. /**
  79. * Load data of comments and rerender <PageComments />
  80. */
  81. postComment(comment, isMarkdown, replyTo, isSlackEnabled, slackChannels) {
  82. const { pageId, revisionId } = this.getPageContainer().state;
  83. return this.appContainer.apiPost('/comments.add', {
  84. commentForm: {
  85. comment,
  86. page_id: pageId,
  87. revision_id: revisionId,
  88. is_markdown: isMarkdown,
  89. replyTo,
  90. },
  91. slackNotificationForm: {
  92. isSlackEnabled,
  93. slackChannels,
  94. },
  95. })
  96. .then((res) => {
  97. if (res.ok) {
  98. return this.retrieveComments();
  99. }
  100. });
  101. }
  102. /**
  103. * Load data of comments and rerender <PageComments />
  104. */
  105. putComment(comment, isMarkdown, commentId, author) {
  106. const { pageId, revisionId } = this.getPageContainer().state;
  107. return this.appContainer.apiPost('/comments.update', {
  108. commentForm: {
  109. comment,
  110. page_id: pageId,
  111. revision_id: revisionId,
  112. is_markdown: isMarkdown,
  113. comment_id: commentId,
  114. author,
  115. },
  116. })
  117. .then((res) => {
  118. if (res.ok) {
  119. return this.retrieveComments();
  120. }
  121. });
  122. }
  123. deleteComment(comment) {
  124. return this.appContainer.apiPost('/comments.remove', { comment_id: comment._id })
  125. .then((res) => {
  126. if (res.ok) {
  127. this.findAndSplice(comment);
  128. }
  129. });
  130. }
  131. uploadAttachment(file) {
  132. const { pageId, pagePath } = this.getPageContainer().state;
  133. const endpoint = '/attachments.add';
  134. const formData = new FormData();
  135. formData.append('_csrf', this.appContainer.csrfToken);
  136. formData.append('file', file);
  137. formData.append('path', pagePath);
  138. formData.append('page_id', pageId);
  139. return this.appContainer.apiPost(endpoint, formData);
  140. }
  141. }