From 75e0388903e185b5c33b0e17ba020688dcc11b71 Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Tue, 21 Nov 2023 13:27:17 -0500 Subject: [PATCH] Factor out restrictChildren and fix author view --- CHANGELOG.md | 1 + client/author.coffee | 6 +++++- client/search.coffee | 23 +++++------------------ client/since.coffee | 20 +++----------------- lib/messages.coffee | 17 +++++++++++++++++ 5 files changed, 31 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8482167..97d4d98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ instead of version numbers. * Fix escaping search queries with backslash: `\:`, `\"`, `\|`, `\(`, `\)`, `\\` * Newly allow escaping of `*` with `\*` +* Author view no longer lists messages multiple times ## 2023-11-17 diff --git a/client/author.coffee b/client/author.coffee index 147b244..8c411b4 100644 --- a/client/author.coffee +++ b/client/author.coffee @@ -1,10 +1,14 @@ +import {restrictChildren} from '/lib/messages' + Template.author.onCreated -> @autorun -> setTitle "Author #{Template.currentData()?.author}" Template.author.helpers messages: -> - messagesBy @group, @author, true # include @mentions + msgs = messagesBy @group, @author, true # include @mentions + .fetch() + restrictChildren msgs messageCountText: -> pluralize messagesBy(@group, @author).count(), 'message' wildLink: -> diff --git a/client/search.coffee b/client/search.coffee index 098d450..5d12fe6 100644 --- a/client/search.coffee +++ b/client/search.coffee @@ -1,5 +1,5 @@ import {groupDefaultSort} from '/lib/groups' -import {findMessageRoot, messagesSortedBy} from '/lib/messages' +import {findMessageRoot, messagesSortedBy, restrictChildren} from '/lib/messages' import {formatSearch, parseSearch} from '/lib/search' Template.search.onCreated -> @@ -28,27 +28,14 @@ topMessagesSearch = (group, search) -> msgs = messagesSortedBy msgs, groupDefaultSort(group), findMessageRoot ## Mostest significant: sort by group name msgs = _.sortBy msgs, 'group' - ## Form a set of all message IDs in match - byId = {} - for msg in msgs - byId[msg._id] = msg - ## Restrict children pointers to within match - for msg in msgs - msg.readChildren = (byId[child] for child in msg.children when child of byId) - ## Return the messages that are not children within the set - for msg in msgs - for child in msg.readChildren - delete byId[child._id] + ## Restrict children pointers to within match and avoid duplicates + msgs = restrictChildren msgs + ## Set `newGroup` for group headers lastGroup = null for msg in msgs - continue unless msg._id of byId if lastGroup != msg.group msg.newGroup = lastGroup = msg.group - msg - #groups = _.groupBy msgs, (msg) -> - #pairs = _.pairs groups - #pairs.sort() - #for pair in pairs + msgs Template.search.helpers messages: -> diff --git a/client/since.coffee b/client/since.coffee index 27b1030..26dc35a 100644 --- a/client/since.coffee +++ b/client/since.coffee @@ -1,6 +1,6 @@ import {formatDate} from './lib/date' import {groupDefaultSort} from '/lib/groups' -import {findMessageRoot, messagesSortedBy, parseSince} from '/lib/messages' +import {findMessageRoot, messagesSortedBy, parseSince, restrictChildren} from '/lib/messages' Template.since.onCreated -> @autorun -> @@ -26,22 +26,8 @@ topMessagesSince = (group, since) -> ## Most significant: sort by group's default sort order -- ## applying sort to message root, not the message msgs = messagesSortedBy msgs, groupDefaultSort(group), findMessageRoot - ## Form a set of all message IDs in match - byId = {} - for msg in msgs - byId[msg._id] = msg - ## Restrict children pointers to within match - for msg in msgs - msg.readChildren = (byId[child] for child in msg.children when child of byId) - ## Return the messages that are not children within the set - for msg in msgs - for child in msg.readChildren - delete byId[child._id] - msg for msg in msgs when msg._id of byId - #groups = _.groupBy msgs, (msg) -> - #pairs = _.pairs groups - #pairs.sort() - #for pair in pairs + ## Restrict children pointers to within match and avoid duplicates + msgs = restrictChildren msgs Template.since.helpers messages: -> diff --git a/lib/messages.coffee b/lib/messages.coffee index 8c53f17..bcb1e4c 100644 --- a/lib/messages.coffee +++ b/lib/messages.coffee @@ -358,6 +358,23 @@ if Meteor.isServer mentions.push match[1] mentions +## Sets `readChildren` on each message to only include `children` that are +## within the set of matching children. Returns top-level messages +## that aren't such children, avoiding duplication in display. +export restrictChildren = (msgs) -> + ## Form a set of all message IDs in match + byId = {} + for msg in msgs + byId[msg._id] = msg + ## Restrict children pointers to within match + for msg in msgs + msg.readChildren = (byId[child] for child in msg.children when child of byId) + ## Return the messages that are not children within the set + for msg in msgs + for child in msg.readChildren + delete byId[child._id] + msg for msg in msgs when msg._id of byId + if Meteor.isServer Meteor.publish 'messages.author', (group, author) -> check group, String