~emersion/soju

26473ed60d4fb859a67dcf0c822df01c82965c49 — Simon Ser 22 days ago 7e39f6d
Introduce downstreamConn.sendTargetBacklog
1 files changed, 43 insertions(+), 36 deletions(-)

M downstream.go
M downstream.go => downstream.go +43 -36
@@ 979,53 979,60 @@ func (dc *downstreamConn) messageSupportsHistory(msg *irc.Message) bool {
}

func (dc *downstreamConn) sendNetworkBacklog(net *network) {
	for target := range net.delivered {
		dc.sendTargetBacklog(net, target)
	}
}

func (dc *downstreamConn) sendTargetBacklog(net *network, target string) {
	if dc.caps["draft/chathistory"] || dc.user.msgStore == nil {
		return
	}
	for target, delivered := range net.delivered {
		if ch, ok := net.channels[target]; ok && ch.Detached {
			continue
		}
	if ch, ok := net.channels[target]; ok && ch.Detached {
		return
	}
	delivered, ok := net.delivered[target]
	if !ok {
		return
	}
	lastDelivered, ok := delivered[dc.clientName]
	if !ok {
		return
	}

		lastDelivered, ok := delivered[dc.clientName]
		if !ok {
			continue
		}
	limit := 4000
	history, err := dc.user.msgStore.LoadLatestID(net, target, lastDelivered, limit)
	if err != nil {
		dc.logger.Printf("failed to send implicit history for %q: %v", target, err)
		return
	}

		limit := 4000
		history, err := dc.user.msgStore.LoadLatestID(net, target, lastDelivered, limit)
		if err != nil {
			dc.logger.Printf("failed to send implicit history for %q: %v", target, err)
	batchRef := "history"
	if dc.caps["batch"] {
		dc.SendMessage(&irc.Message{
			Prefix:  dc.srv.prefix(),
			Command: "BATCH",
			Params:  []string{"+" + batchRef, "chathistory", dc.marshalEntity(net, target)},
		})
	}

	for _, msg := range history {
		if !dc.messageSupportsHistory(msg) {
			continue
		}

		batchRef := "history"
		if dc.caps["batch"] {
			dc.SendMessage(&irc.Message{
				Prefix:  dc.srv.prefix(),
				Command: "BATCH",
				Params:  []string{"+" + batchRef, "chathistory", dc.marshalEntity(net, target)},
			})
		}

		for _, msg := range history {
			if !dc.messageSupportsHistory(msg) {
				continue
			}

			if dc.caps["batch"] {
				msg.Tags["batch"] = irc.TagValue(batchRef)
			}
			dc.SendMessage(dc.marshalMessage(msg, net))
			msg.Tags["batch"] = irc.TagValue(batchRef)
		}
		dc.SendMessage(dc.marshalMessage(msg, net))
	}

		if dc.caps["batch"] {
			dc.SendMessage(&irc.Message{
				Prefix:  dc.srv.prefix(),
				Command: "BATCH",
				Params:  []string{"-" + batchRef},
			})
		}
	if dc.caps["batch"] {
		dc.SendMessage(&irc.Message{
			Prefix:  dc.srv.prefix(),
			Command: "BATCH",
			Params:  []string{"-" + batchRef},
		})
	}
}