CommentContainer.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. return comment.creator.imageUrlCached == null;
  63. }).map((comment) => {
  64. return comment.creator._id;
  65. });
  66. if (noImageCacheUserIds.length === 0) {
  67. return;
  68. }
  69. try {
  70. await this.appContainer.apiv3Put('/users/update.imageUrlCache', { userIds: noImageCacheUserIds });
  71. }
  72. catch (err) {
  73. // Error alert doesn't apear, because user don't need to notice this error.
  74. logger.error(err);
  75. }
  76. }
  77. /**
  78. * Load data of comments and rerender <PageComments />
  79. */
  80. postComment(comment, isMarkdown, replyTo, isSlackEnabled, slackChannels) {
  81. const { pageId, revisionId } = this.getPageContainer().state;
  82. return this.appContainer.apiPost('/comments.add', {
  83. commentForm: {
  84. comment,
  85. page_id: pageId,
  86. revision_id: revisionId,
  87. is_markdown: isMarkdown,
  88. replyTo,
  89. },
  90. slackNotificationForm: {
  91. isSlackEnabled,
  92. slackChannels,
  93. },
  94. })
  95. .then((res) => {
  96. if (res.ok) {
  97. return this.retrieveComments();
  98. }
  99. });
  100. }
  101. /**
  102. * Load data of comments and rerender <PageComments />
  103. */
  104. putComment(comment, isMarkdown, commentId, author) {
  105. const { pageId, revisionId } = this.getPageContainer().state;
  106. return this.appContainer.apiPost('/comments.update', {
  107. commentForm: {
  108. comment,
  109. page_id: pageId,
  110. revision_id: revisionId,
  111. is_markdown: isMarkdown,
  112. comment_id: commentId,
  113. author,
  114. },
  115. })
  116. .then((res) => {
  117. if (res.ok) {
  118. return this.retrieveComments();
  119. }
  120. });
  121. }
  122. deleteComment(comment) {
  123. return this.appContainer.apiPost('/comments.remove', { comment_id: comment._id })
  124. .then((res) => {
  125. if (res.ok) {
  126. this.findAndSplice(comment);
  127. }
  128. });
  129. }
  130. uploadAttachment(file) {
  131. const { pageId, pagePath } = this.getPageContainer().state;
  132. const endpoint = '/attachments.add';
  133. const formData = new FormData();
  134. formData.append('_csrf', this.appContainer.csrfToken);
  135. formData.append('file', file);
  136. formData.append('path', pagePath);
  137. formData.append('page_id', pageId);
  138. return this.appContainer.apiPost(endpoint, formData);
  139. }
  140. }