CommentContainer.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { Container } from 'unstated';
  2. import loggerFactory from '~/utils/logger';
  3. import { apiGet, apiPost, apiPostForm } 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. };
  25. this.retrieveComments = this.retrieveComments.bind(this);
  26. this.checkAndUpdateImageOfCommentAuthers = this.checkAndUpdateImageOfCommentAuthers.bind(this);
  27. }
  28. /**
  29. * Workaround for the mangling in production build to break constructor.name
  30. */
  31. static getClassName() {
  32. return 'CommentContainer';
  33. }
  34. getPageContainer() {
  35. return this.appContainer.getContainer('PageContainer');
  36. }
  37. findAndSplice(comment) {
  38. const comments = this.state.comments;
  39. const index = comments.indexOf(comment);
  40. if (index < 0) {
  41. return;
  42. }
  43. comments.splice(index, 1);
  44. this.setState({ comments });
  45. }
  46. /**
  47. * Load data of comments and store them in state
  48. */
  49. async retrieveComments() {
  50. const { pageId } = this.getPageContainer().state;
  51. // get data (desc order array)
  52. const res = await apiGet('/comments.get', { page_id: pageId });
  53. if (res.ok) {
  54. const comments = res.comments;
  55. this.setState({ comments });
  56. this.checkAndUpdateImageOfCommentAuthers(comments);
  57. }
  58. }
  59. async checkAndUpdateImageOfCommentAuthers(comments) {
  60. const noImageCacheUserIds = comments.filter((comment) => {
  61. const { creator } = comment;
  62. return creator != null && creator.imageUrlCached == null;
  63. }).map((comment) => {
  64. return comment.creator._id;
  65. });
  66. if (noImageCacheUserIds.length === 0) {
  67. return;
  68. }
  69. try {
  70. await 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 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 apiPost('/comments.update', {
  107. commentForm: {
  108. comment,
  109. is_markdown: isMarkdown,
  110. revision_id: revisionId,
  111. comment_id: commentId,
  112. },
  113. })
  114. .then((res) => {
  115. if (res.ok) {
  116. return this.retrieveComments();
  117. }
  118. });
  119. }
  120. deleteComment(comment) {
  121. return apiPost('/comments.remove', { comment_id: comment._id })
  122. .then((res) => {
  123. if (res.ok) {
  124. this.findAndSplice(comment);
  125. }
  126. });
  127. }
  128. uploadAttachment(file) {
  129. const { pageId, pagePath } = this.getPageContainer().state;
  130. const endpoint = '/attachments.add';
  131. const formData = new FormData();
  132. formData.append('file', file);
  133. formData.append('path', pagePath);
  134. formData.append('page_id', pageId);
  135. return apiPostForm(endpoint, formData);
  136. }
  137. }