From 65c58adbd9ee9cb6c43b97bfef0f10e2bd961deb Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 13 Apr 2021 17:49:37 +0200 Subject: [PATCH] Take msg ID in sendTargetBacklog --- downstream.go | 44 ++++++++++++++++++-------------------------- user.go | 7 ++++++- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/downstream.go b/downstream.go index 74e69d8..0d84e5c 100644 --- a/downstream.go +++ b/downstream.go @@ -986,26 +986,23 @@ func (dc *downstreamConn) welcome() error { }) if firstClient { net.delivered.ForEachTarget(func(target string) { - dc.sendTargetBacklog(net, target) + lastDelivered := net.delivered.LoadID(target, dc.clientName) + if lastDelivered == "" { + return + } + + dc.sendTargetBacklog(net, target, lastDelivered) + + // Fast-forward history to last message + targetCM := net.casemap(target) + lastID, err := dc.user.msgStore.LastMsgID(net, targetCM, time.Now()) + if err != nil { + dc.logger.Printf("failed to get last message ID: %v", err) + return + } + net.delivered.StoreID(target, dc.clientName, lastID) }) } - - // Fast-forward history to last message - net.delivered.ForEachTarget(func(target string) { - ch := net.channels.Value(target) - if ch != nil && ch.Detached { - return - } - - targetCM := net.casemap(target) - lastID, err := dc.user.msgStore.LastMsgID(net, targetCM, time.Now()) - if err != nil { - dc.logger.Printf("failed to get last message ID: %v", err) - return - } - - net.delivered.StoreID(target, dc.clientName, lastID) - }) }) return nil @@ -1025,7 +1022,7 @@ func (dc *downstreamConn) messageSupportsHistory(msg *irc.Message) bool { return false } -func (dc *downstreamConn) sendTargetBacklog(net *network, target string) { +func (dc *downstreamConn) sendTargetBacklog(net *network, target, msgID string) { if dc.caps["draft/chathistory"] || dc.user.msgStore == nil { return } @@ -1033,16 +1030,11 @@ func (dc *downstreamConn) sendTargetBacklog(net *network, target string) { return } - lastDelivered := net.delivered.LoadID(target, dc.clientName) - if lastDelivered == "" { - return - } - limit := 4000 targetCM := net.casemap(target) - history, err := dc.user.msgStore.LoadLatestID(net, targetCM, lastDelivered, limit) + history, err := dc.user.msgStore.LoadLatestID(net, targetCM, msgID, limit) if err != nil { - dc.logger.Printf("failed to send implicit history for %q: %v", target, err) + dc.logger.Printf("failed to send backlog for %q: %v", target, err) return } diff --git a/user.go b/user.go index da70475..8052234 100644 --- a/user.go +++ b/user.go @@ -276,7 +276,12 @@ func (net *network) attach(ch *Channel) { forwardChannel(dc, uch) } - dc.sendTargetBacklog(net, ch.Name) + lastDelivered := net.delivered.LoadID(ch.Name, dc.clientName) + if lastDelivered == "" { + return + } + + dc.sendTargetBacklog(net, ch.Name, lastDelivered) }) }