Skip to content

Commit

Permalink
Sort Since view by root message status, not child message status (fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Oct 12, 2023
1 parent 49a21c8 commit 4f1ed31
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ To see every change with descriptions aimed at developers, see
As a continuously updated web app, Coauthor uses dates
instead of version numbers.

## 2023-10-12

* Fix bug in Since sorting
[[#634](https://github.com/edemaine/coauthor/issues/634)]

## 2023-10-11

* Embedded images, videos, and PDFs now all generate warnings if the embedded
Expand Down
3 changes: 1 addition & 2 deletions client/since.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ topMessagesSince = (group, since) ->
key: 'root'
reversed: true
## ...applying sort to message root, not the message
msgs = messagesSortedBy msgs, defaultSort, (keyOf) -> (msg) ->
keyOf findMessageRoot msg
msgs = messagesSortedBy msgs, defaultSort, (msg) -> findMessageRoot msg
## Form a set of all message IDs in match
byId = {}
for msg in msgs
Expand Down
16 changes: 9 additions & 7 deletions lib/messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1654,8 +1654,9 @@ export messageNeighbors = (root) ->

## Given already-fetched messages, promotes pinned and unpublished messages,
## and demotes minimized and deleted messages.
sortMessagesByStatus = (msgs) ->
sortMessagesByStatus = (msgs, transform) ->
_.sortBy msgs, (msg) ->
msg = transform msg if transform?
weight = 0
weight += 8 if msg.deleted ## deleted messages go very bottom
weight += 4 if msg.minimized ## minimized messages go bottom
Expand All @@ -1675,7 +1676,7 @@ export messagesSortedBy = (msgs, sorts, transform) ->
break
## If only tag sorts, then message status is least significant.
if leadingTags == sorts.length
msgs = sortMessagesByStatus msgs
msgs = sortMessagesByStatus msgs, transform
## Apply sorts in reverse order, stably (radix sort).
for sort, i in sorts[..].reverse()
switch sort.key
Expand Down Expand Up @@ -1709,10 +1710,11 @@ export messagesSortedBy = (msgs, sorts, transform) ->
else
throw new Error "Invalid sort key: '#{sort.key}'"
if transform? # transform key getter
if typeof key == 'string' # convert string to actual getter
do (keyString = key) ->
key = (msg) -> msg?[keyString]
key = transform key
do (origKey = key) ->
if typeof key == 'string' # convert string to actual getter
key = (msg) -> transform(msg)?[origKey]
else
key = (msg) -> origKey transform msg
## To reverse sort, we reverse before and after the sort,
## so that less-significant sorts already done aren't affected.
msgs.reverse() if sort.reverse
Expand All @@ -1721,7 +1723,7 @@ export messagesSortedBy = (msgs, sorts, transform) ->
## If remaining sorts are by tags, sort by message status now
## (to stay within cluster).
if sorts.length-1 - i == leadingTags
msgs = sortMessagesByStatus msgs
msgs = sortMessagesByStatus msgs, transform
msgs

export mongoSort = (key) ->
Expand Down

0 comments on commit 4f1ed31

Please sign in to comment.