~emersion/soju-dev

Store the last msg ID on welcome and JOIN v2 PROPOSED

Eyal Sawady: 1
 Store the last msg ID on welcome and JOIN

 1 files changed, 8 insertions(+), 4 deletions(-)
> https://lists.sr.ht/~emersion/soju-dev/patches/28130
It looks to me like this tests DMs from new people which happen when a
client is disconnected - this is a separate (though also important)
issue from what I fixed. A better test for this specific issue would be
to have the downstream join a channel then send a message to that
channel once it disconnects.

That being said, I think the right fix for this issue will be
initializing the last msg ID for all clients when a new DM arrives. If
that sounds right to you, I can whip up a patch.
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/soju-dev/patches/27288/mbox | git am -3
Learn more about email & git

[PATCH v2] Store the last msg ID on welcome and JOIN Export this patch

Backlog for a target isn't sent to a client until it's seen at least one
message ID for that target (in order to avoid sending the complete
history of each target every time a new client is used). However, prior
to this commit, message IDs were only stored for a client when a message
was sent to a target /while the client was connected/, which caused
backlog to be missed if messages were only sent while the client was
disconnected.

To fix this, store the last message ID on JOIN (to fix new channels)
and on welcome (to fix previously-used targets on new clients).
---
v1 -> v2: simplify, store a msg ID on JOIN as well, improve commit
message

Hopefully the new commit message should clear things up for you as to
why I think this is the right approach - we're not fast-forwarding the
history, we're marking the client's start of the history so that we can
successfully send backlogs later.
 downstream.go | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/downstream.go b/downstream.go
index 026b04b..1ce6ef3 100644
--- a/downstream.go
+++ b/downstream.go
@@ -1486,12 +1486,10 @@ func (dc *downstreamConn) welcome(ctx context.Context) error {
		if firstClient {
			net.delivered.ForEachTarget(func(target string) {
				lastDelivered := net.delivered.LoadID(target, dc.clientName)
				if lastDelivered == "" {
					return
				if lastDelivered != "" {
					dc.sendTargetBacklog(ctx, net, target, lastDelivered)
				}

				dc.sendTargetBacklog(ctx, net, target, lastDelivered)

				// Fast-forward history to last message
				targetCM := net.casemap(target)
				lastID, err := dc.user.msgStore.LastMsgID(&net.Network, targetCM, time.Now())
@@ -1825,6 +1823,12 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
			if err := dc.srv.db.StoreChannel(ctx, uc.network.ID, ch); err != nil {
				dc.logger.Printf("failed to create or update channel %q: %v", upstreamName, err)
			}
			lastID, err := dc.user.msgStore.LastMsgID(&uc.network.Network, upstreamName, time.Now())
			if err != nil {
				dc.logger.Printf("failed to get last message ID: %v", err)
			} else {
				uc.network.delivered.StoreID(upstreamName, dc.clientName, lastID)
			}
		}
	case "PART":
		var namesStr string
-- 
2.34.1
Hm, I've tried writing a test [1] for this, but it doesn't seem like this patch
fixes it.

[1]: https://lists.sr.ht/~emersion/soju-dev/patches/28130