Skip to content

Commit

Permalink
Factor out restrictChildren and fix author view
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Nov 21, 2023
1 parent 4998711 commit 75e0388
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion client/author.coffee
Original file line number Diff line number Diff line change
@@ -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: ->
Expand Down
23 changes: 5 additions & 18 deletions client/search.coffee
Original file line number Diff line number Diff line change
@@ -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 ->
Expand Down Expand Up @@ -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: ->
Expand Down
20 changes: 3 additions & 17 deletions client/since.coffee
Original file line number Diff line number Diff line change
@@ -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 ->
Expand All @@ -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: ->
Expand Down
17 changes: 17 additions & 0 deletions lib/messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 75e0388

Please sign in to comment.