Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sender header insertion with respect to membership changes #158

Merged
merged 4 commits into from
Jan 14, 2024

Conversation

Stebalien
Copy link
Contributor

@Stebalien Stebalien commented May 25, 2023

Only insert a sender header if the previous message is from a different sender, and there is no sender header between the current message and the previous message.

While the previous version of this function explicitly skipped the read marker, this version searches for known events (messages and sender headers), skipping everything it doesn't understand. This should make it robust to adding new events, widgets, etc.

fixes #157

@Stebalien Stebalien changed the title Fix username header insertion with respect to membership changes Fix sender header insertion with respect to membership changes May 26, 2023
@alphapapa alphapapa self-assigned this May 27, 2023
@alphapapa alphapapa added the bug Something isn't working label May 27, 2023
@alphapapa
Copy link
Owner

Thanks, I'll want to review and test this carefully before merging, so it might be a little while. I do appreciate your working on this, since I don't use the "Elemental" style very often.

@alphapapa alphapapa added this to the 0.11 milestone May 27, 2023
@Stebalien
Copy link
Contributor Author

Stebalien commented May 27, 2023 via email

@Stebalien
Copy link
Contributor Author

Hm. Actually, I've found one bug. The first sender header in the buffer stays the first sender header, even after loading new events.

I believe ement-room--insert-sender-headers needs to widen its search by:

  1. Scanning back from start-node to the previous message.
  2. Scanning forward from end-node to the next message.

Then it needs to "fix" all headers in this range. As you suggested, simply deleting the sender headers first then re-inserting them is likely the best approach.

@Stebalien
Copy link
Contributor Author

I'll tackle this tomorrow.

@alphapapa
Copy link
Owner

I believe ement-room--insert-sender-headers needs to widen its search by:

1. Scanning back from  `start-node` to the previous message.

2. Scanning forward from `end-node` to the next message.

IIRC it already does that--or maybe I did that for the timestamp headers. I don't touch this code often.

Then it needs to "fix" all headers in this range. As you suggested, simply deleting the sender headers first then re-inserting them is likely the best approach.

My hesitation with that approach is that it will, of course, take longer to do so with every incoming message. Sometimes, e.g. I look way back in a room's history, loading a few thousand old events, which Ement handles just fine; but if this approach were used, it would likely perform poorly in that case. So I'd prefer to avoid that if possible.

Something I haven't done is dig into the Matrix React SDK to see how it does it; but since it uses React, I don't know if any of its algorithms would be relevant here.

Maybe something relatively simple would help us ensure that we cover a large enough range while avoiding scanning the whole buffer, like scanning back to the previous full-date timestamp, or something like that. Even that could be a performance issue in very busy rooms like #emacs:libera.chat or #matrix:matrix.org, but since the "Elemental" style isn't the default, maybe it would be acceptable.

@Stebalien
Copy link
Contributor Author

My hesitation with that approach is that it will, of course, take longer to do so with every incoming message.

It shouldn't. I'm talking about fixing just the range of messages that were modified, not the entire buffer.

Maybe something relatively simple would help us ensure that we cover a large enough range while avoiding scanning the whole buffer, like scanning back to the previous full-date timestamp, or something like that

Ah, that's why I suggested scanning back/forward to the previous/next message (which we don't currently do for sender headers).

@@ -3034,7 +3020,7 @@ Search starts from node START and moves by NEXT."
;; HACK: Insert after any read markers.
(cl-loop for node-after-node-before = (ewoc-next ewoc event-node-before)
while node-after-node-before
while (not (ement-event-p (ewoc-data node-after-node-before)))
while (read-marker-p (ewoc-data node-after-node-before))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is optional, but it helps keep sender headers attached to their messages.

(setf end-node (ement-room--ewoc-next-matching ewoc end-node #'message-event-p)))

(let* ((event-node start-node) prev-node)
(while (and event-node (not (eq event-node end-node)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eq should be sufficient now, as far as I can tell.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I've tested this with simple printf debugging and it appears to work)

ement-room.el Outdated
Comment on lines 2943 to 3047
;; We have a previous sender, but it's wrong. Fix it.
((ement-user-p (ewoc-data prev-node))
(unless (equal sender (ewoc-data prev-node))
(ewoc-set-data prev-node sender)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can happen if we insert an event after a header but before another message. The read-marker fix below should prevent this from happening (in most cases), but we might as well handle it.

@Stebalien Stebalien force-pushed the fix/157 branch 2 times, most recently from b09619a to 1f22358 Compare May 27, 2023 20:39
@Stebalien
Copy link
Contributor Author

Ok, I think this should actually be good now.

@alphapapa alphapapa modified the milestones: 0.11, 0.12 Jul 9, 2023
@alphapapa
Copy link
Owner

Deferring this to 0.12 so 0.11 can be released sooner.

@Stebalien
Copy link
Contributor Author

No problem, I'm the one asking for your time. If there's anything I can do to make the review easier (or any general feedback), please let me know.

@alphapapa
Copy link
Owner

Thanks. I was hoping to have time for things like this last weekend, but I didn't. Probably I just need to start running this PR's code on my own system to test it "in the background." FWIW, have you been using it regularly yourself?

@Stebalien
Copy link
Contributor Author

Not every day, but I use this patch whenever I use ement.el and haven't seen any issues.

@Stebalien
Copy link
Contributor Author

🥺

@alphapapa
Copy link
Owner

🥺

Geez, you make me DDG that emoji to see what it's supposed to mean. :P

You'll have to forgive me, as I have had a lot going on outside of my various Emacs packages. And each one of them has a list of issues and PRs that are waiting. In the past couple of days I just merged a couple that were 3 years old.

In the meantime, I would still be grateful for more feedback from having tested this patch.

But I'll go ahead and rebase it and install the PR branch to test it myself...

@alphapapa
Copy link
Owner

I've installed the PR's branch. Seems to work well so far, but I'll have to remember to use the "Elemental" message format in order to see this code in action.

If no problems are found, I'll plan to merge this for v0.14.

@Stebalien
Copy link
Contributor Author

Thank you ❤️. And I understand, I've merged some very old PRs.

In terms of testing, I've been using this more frequently and still haven't run into any issues.

@alphapapa alphapapa force-pushed the fix/157 branch 2 times, most recently from 9f608b9 to 83beebc Compare January 9, 2024 08:19
@alphapapa
Copy link
Owner

@Stebalien I think I'm ready to merge this.

AFAIK you already signed the FSF CA, because I see some non-exempt commits from you in emacs.git, and of course you've contributed several here already. But I can't find where I've asked you for confirmation already, so allow me to ask for confirmation now, just to be sure. :) Have you signed it already?

Thanks.

@Stebalien
Copy link
Contributor Author

Yep, the FSF officially owns all my Emacs work.

@alphapapa
Copy link
Owner

Yep, the FSF officially owns all my Emacs work.

Thanks.

BTW, I encountered an error which I've pushed an attempted fix for here, as well as adding a small failsafe: b520bc6 Please let me know what you think about that change.

@alphapapa
Copy link
Owner

Note as well that I discovered that the failsafe is unnecessary (which I should have realized from reading your comments in the code), so I removed it. Seems to be working correctly now, but I still think it needs more testing, because this PR's code isn't the first time I've found a bug in this kind of code that only manifested after a few days (e.g. re-syncing after a long interruption).

@Stebalien
Copy link
Contributor Author

Hm. That shouldn't be possible. prev-node can be either nil, a user, or a message given the prior (setf prev-node ....

@alphapapa
Copy link
Owner

Hm. That shouldn't be possible. prev-node can be either nil, a user, or a message given the prior (setf prev-node ....

You're right, the bug was caused by my "Tidy..." commit that incorrectly consolidated one of the clauses. I'll take care of it. Thanks.

alphapapa added a commit to Stebalien/ement.el that referenced this pull request Jan 14, 2024
@alphapapa
Copy link
Owner

I'm going to go ahead and merge this to master now. Hopefully enough users use the "Elemental" format for it to get some testing. :)

alphapapa added a commit to Stebalien/ement.el that referenced this pull request Jan 14, 2024
Fix: (ement-room--insert-sender-headers)

After suspending, resuming, and syncing again, an error happened,
failing here because PREV-NODE's data was an EMENT-USER rather than an
EMENT-EVENT, so EMENT-EVENT-SENDER failed on it.

Now we test PREV-NODE's data's type to ensure it's an event before
testing the sender.

Also, as a failsafe, if the PREV-NODE is something else (could be
membership events, e.g.), we insert the sender header, to be sure a
message event has one visible.

Fix: Remove unneeded failsafe

This was duplicating sender headers; Stebalien's design makes this
unnecessary.

Fix: (ement-room--insert-sender-headers)

Bug was introduced in a8c8b08.

See <alphapapa#158 (comment)>.
Stebalien and others added 4 commits January 14, 2024 04:10
Only insert a sender header if the previous message is from a different
sender, and there is no sender header between the current message and
the previous message.

While the previous version of this function explicitly skipped the read
marker, this version searches for known events (messages and sender
headers), skipping everything it doesn't understand. This should make it
robust to adding new events, widgets, etc.

fixes alphapapa#157

Fix new event insertion with respect to sender headers

Only skip over the read marker, don't skip over sender headers.

Make sender headers robust

1. Widen the "fixup" range to the next message after the end-node.
2. Simplify the loop logic.
3. Fixup incorrect sender headers.
For Emacs 29.
Fix: (ement-room--insert-sender-headers)

After suspending, resuming, and syncing again, an error happened,
failing here because PREV-NODE's data was an EMENT-USER rather than an
EMENT-EVENT, so EMENT-EVENT-SENDER failed on it.

Now we test PREV-NODE's data's type to ensure it's an event before
testing the sender.

Also, as a failsafe, if the PREV-NODE is something else (could be
membership events, e.g.), we insert the sender header, to be sure a
message event has one visible.

Fix: Remove unneeded failsafe

This was duplicating sender headers; Stebalien's design makes this
unnecessary.

Fix: (ement-room--insert-sender-headers)

Bug was introduced in a8c8b08.

See <alphapapa#158 (comment)>.
@alphapapa alphapapa merged commit 4a25cc0 into alphapapa:master Jan 14, 2024
0 of 4 checks passed
@alphapapa
Copy link
Owner

@Stebalien Thanks for your work on this and your patience.

@Stebalien
Copy link
Contributor Author

No, thank you! I realize you don't use this feature so I'm just glad I don't have to maintain it myself.

@alphapapa
Copy link
Owner

No, thank you! I realize you don't use this feature so I'm just glad I don't have to maintain it myself.

I mean...if this breaks, I'll probably be pinging you to help fix it. ;)

@Stebalien
Copy link
Contributor Author

Stebalien commented Jan 14, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sender headers may need updating after receiving new/old events
2 participants