activities.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import DOMPurify from 'dompurify';
  2. const activitiesPerPage = 500;
  3. BlazeComponent.extendComponent({
  4. onCreated() {
  5. // XXX Should we use ReactiveNumber?
  6. this.page = new ReactiveVar(1);
  7. this.loadNextPageLocked = false;
  8. // TODO is sidebar always available? E.g. on small screens/mobile devices
  9. const sidebar = Sidebar;
  10. sidebar && sidebar.callFirstWith(null, 'resetNextPeak');
  11. this.autorun(() => {
  12. let mode = this.data().mode;
  13. const capitalizedMode = Utils.capitalize(mode);
  14. let thisId, searchId;
  15. if (mode === 'linkedcard' || mode === 'linkedboard') {
  16. thisId = Session.get('currentCard');
  17. searchId = Cards.findOne({ _id: thisId }).linkedId;
  18. mode = mode.replace('linked', '');
  19. } else {
  20. thisId = Session.get(`current${capitalizedMode}`);
  21. searchId = thisId;
  22. }
  23. const limit = this.page.get() * activitiesPerPage;
  24. const user = Meteor.user();
  25. const hideSystem = user ? user.hasHiddenSystemMessages() : false;
  26. if (searchId === null) return;
  27. this.subscribe('activities', mode, searchId, limit, hideSystem, () => {
  28. this.loadNextPageLocked = false;
  29. // TODO the guard can be removed as soon as the TODO above is resolved
  30. if (!sidebar) return;
  31. // If the sibear peak hasn't increased, that mean that there are no more
  32. // activities, and we can stop calling new subscriptions.
  33. // XXX This is hacky! We need to know excatly and reactively how many
  34. // activities there are, we probably want to denormalize this number
  35. // dirrectly into card and board documents.
  36. const nextPeakBefore = sidebar.callFirstWith(null, 'getNextPeak');
  37. sidebar.calculateNextPeak();
  38. const nextPeakAfter = sidebar.callFirstWith(null, 'getNextPeak');
  39. if (nextPeakBefore === nextPeakAfter) {
  40. sidebar.callFirstWith(null, 'resetNextPeak');
  41. }
  42. });
  43. });
  44. },
  45. loadNextPage() {
  46. if (this.loadNextPageLocked === false) {
  47. this.page.set(this.page.get() + 1);
  48. this.loadNextPageLocked = true;
  49. }
  50. },
  51. }).register('activities');
  52. BlazeComponent.extendComponent({
  53. checkItem() {
  54. const checkItemId = this.currentData().activity.checklistItemId;
  55. const checkItem = ChecklistItems.findOne({ _id: checkItemId });
  56. return checkItem && checkItem.title;
  57. },
  58. boardLabelLink() {
  59. const data = this.currentData();
  60. if (data.mode !== 'board') {
  61. return createBoardLink(data.activity.board(), data.activity.listName);
  62. }
  63. return TAPi18n.__('this-board');
  64. },
  65. cardLabelLink() {
  66. const data = this.currentData();
  67. if (data.mode !== 'card') {
  68. return createCardLink(data.activity.card());
  69. }
  70. return TAPi18n.__('this-card');
  71. },
  72. cardLink() {
  73. return createCardLink(this.currentData().activity.card());
  74. },
  75. receivedDate() {
  76. const receivedDate = this.currentData().activity.card();
  77. if (!receivedDate) return null;
  78. return receivedDate.receivedAt;
  79. },
  80. startDate() {
  81. const startDate = this.currentData().activity.card();
  82. if (!startDate) return null;
  83. return startDate.startAt;
  84. },
  85. dueDate() {
  86. const dueDate = this.currentData().activity.card();
  87. if (!dueDate) return null;
  88. return dueDate.dueAt;
  89. },
  90. endDate() {
  91. const endDate = this.currentData().activity.card();
  92. if (!endDate) return null;
  93. return endDate.endAt;
  94. },
  95. lastLabel() {
  96. const lastLabelId = this.currentData().activity.labelId;
  97. if (!lastLabelId) return null;
  98. const lastLabel = Boards.findOne(
  99. this.currentData().activity.boardId,
  100. ).getLabelById(lastLabelId);
  101. if (lastLabel && (lastLabel.name === undefined || lastLabel.name === '')) {
  102. return lastLabel.color;
  103. } else if (lastLabel.name !== undefined && lastLabel.name !== '') {
  104. return lastLabel.name;
  105. } else {
  106. return null;
  107. }
  108. },
  109. lastCustomField() {
  110. const lastCustomField = CustomFields.findOne(
  111. this.currentData().activity.customFieldId,
  112. );
  113. if (!lastCustomField) return null;
  114. return lastCustomField.name;
  115. },
  116. lastCustomFieldValue() {
  117. const lastCustomField = CustomFields.findOne(
  118. this.currentData().activity.customFieldId,
  119. );
  120. if (!lastCustomField) return null;
  121. const value = this.currentData().activity.value;
  122. if (
  123. lastCustomField.settings.dropdownItems &&
  124. lastCustomField.settings.dropdownItems.length > 0
  125. ) {
  126. const dropDownValue = _.find(
  127. lastCustomField.settings.dropdownItems,
  128. item => {
  129. return item._id === value;
  130. },
  131. );
  132. if (dropDownValue) return dropDownValue.name;
  133. }
  134. return value;
  135. },
  136. listLabel() {
  137. const activity = this.currentData().activity;
  138. const list = activity.list();
  139. return (list && list.title) || activity.title;
  140. },
  141. sourceLink() {
  142. const source = this.currentData().activity.source;
  143. if (source) {
  144. if (source.url) {
  145. return Blaze.toHTML(
  146. HTML.A(
  147. {
  148. href: source.url,
  149. },
  150. DOMPurify.sanitize(source.system, {
  151. ALLOW_UNKNOWN_PROTOCOLS: true,
  152. }),
  153. ),
  154. );
  155. } else {
  156. return DOMPurify.sanitize(source.system, {
  157. ALLOW_UNKNOWN_PROTOCOLS: true,
  158. });
  159. }
  160. }
  161. return null;
  162. },
  163. memberLink() {
  164. return Blaze.toHTMLWithData(Template.memberName, {
  165. user: this.currentData().activity.member(),
  166. });
  167. },
  168. attachmentLink() {
  169. const attachment = this.currentData().activity.attachment();
  170. // trying to display url before file is stored generates js errors
  171. return (
  172. (attachment &&
  173. attachment.url({ download: true }) &&
  174. Blaze.toHTML(
  175. HTML.A(
  176. {
  177. href: attachment.url({ download: true }),
  178. target: '_blank',
  179. },
  180. DOMPurify.sanitize(attachment.name()),
  181. ),
  182. )) ||
  183. DOMPurify.sanitize(this.currentData().activity.attachmentName)
  184. );
  185. },
  186. customField() {
  187. const customField = this.currentData().activity.customField();
  188. if (!customField) return null;
  189. return customField.name;
  190. },
  191. events() {
  192. return [
  193. {
  194. // XXX We should use Popup.afterConfirmation here
  195. 'click .js-delete-comment'() {
  196. const commentId = this.currentData().activity.commentId;
  197. CardComments.remove(commentId);
  198. },
  199. 'submit .js-edit-comment'(evt) {
  200. evt.preventDefault();
  201. const commentText = this.currentComponent()
  202. .getValue()
  203. .trim();
  204. const commentId = Template.parentData().activity.commentId;
  205. if (commentText) {
  206. CardComments.update(commentId, {
  207. $set: {
  208. text: commentText,
  209. },
  210. });
  211. }
  212. },
  213. },
  214. ];
  215. },
  216. }).register('activity');
  217. Template.activity.helpers({
  218. sanitize(value) {
  219. return DOMPurify.sanitize(value, { ALLOW_UNKNOWN_PROTOCOLS: true });
  220. },
  221. });
  222. Template.commentReactions.events({
  223. 'click .reaction'(event) {
  224. if (Meteor.user().isBoardMember()) {
  225. const codepoint = event.currentTarget.dataset['codepoint'];
  226. const commentId = Template.instance().data.commentId;
  227. const cardComment = CardComments.findOne({_id: commentId});
  228. cardComment.toggleReaction(codepoint);
  229. }
  230. },
  231. 'click .open-comment-reaction-popup': Popup.open('addReaction'),
  232. })
  233. Template.addReactionPopup.events({
  234. 'click .add-comment-reaction'(event) {
  235. if (Meteor.user().isBoardMember()) {
  236. const codepoint = event.currentTarget.dataset['codepoint'];
  237. const commentId = Template.instance().data.commentId;
  238. const cardComment = CardComments.findOne({_id: commentId});
  239. cardComment.toggleReaction(codepoint);
  240. }
  241. Popup.close();
  242. },
  243. })
  244. Template.addReactionPopup.helpers({
  245. codepoints() {
  246. // Starting set of unicode codepoints as comment reactions
  247. return [
  248. '👍',
  249. '👎',
  250. '👀',
  251. '✅',
  252. '❌',
  253. '🙏',
  254. '👏',
  255. '🎉',
  256. '🚀',
  257. '😊',
  258. '🤔',
  259. '😔'];
  260. }
  261. })
  262. Template.commentReactions.helpers({
  263. isSelected(userIds) {
  264. return userIds.includes(Meteor.user()._id);
  265. },
  266. userNames(userIds) {
  267. return Users.find({_id: {$in: userIds}})
  268. .map(user => user.profile.fullname)
  269. .join(', ');
  270. }
  271. })
  272. function createCardLink(card) {
  273. if (!card) return '';
  274. return (
  275. card &&
  276. Blaze.toHTML(
  277. HTML.A(
  278. {
  279. href: card.originRelativeUrl(),
  280. class: 'action-card',
  281. },
  282. DOMPurify.sanitize(card.title, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  283. ),
  284. )
  285. );
  286. }
  287. function createBoardLink(board, list) {
  288. let text = board.title;
  289. if (list) text += `: ${list}`;
  290. return (
  291. board &&
  292. Blaze.toHTML(
  293. HTML.A(
  294. {
  295. href: board.originRelativeUrl(),
  296. class: 'action-board',
  297. },
  298. DOMPurify.sanitize(text, { ALLOW_UNKNOWN_PROTOCOLS: true }),
  299. ),
  300. )
  301. );
  302. }