CommentContainer.js 4.2 KB

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