Extract history loading into functions
These will get re-used for sending history to clients that don't support the chathistory extension.
This commit is contained in:
parent
3d316fe01c
commit
745b3f67a0
@ -1598,82 +1598,12 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
|||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
|
||||||
batchRef := "history"
|
var history []*irc.Message
|
||||||
maxTries := 100
|
|
||||||
switch subcommand {
|
switch subcommand {
|
||||||
case "BEFORE":
|
case "BEFORE":
|
||||||
dc.SendMessage(&irc.Message{
|
history, err = loadHistoryBeforeTime(uc.network, entity, timestamp, limit)
|
||||||
Prefix: dc.srv.prefix(),
|
|
||||||
Command: "BATCH",
|
|
||||||
Params: []string{"+" + batchRef, "chathistory", target},
|
|
||||||
})
|
|
||||||
|
|
||||||
history := make([]*irc.Message, limit)
|
|
||||||
remaining := limit
|
|
||||||
|
|
||||||
tries := 0
|
|
||||||
for remaining > 0 && tries < maxTries {
|
|
||||||
buf, err := parseMessagesBefore(uc.network, entity, timestamp, remaining)
|
|
||||||
if err != nil {
|
|
||||||
dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
|
|
||||||
return newChatHistoryError(subcommand, target)
|
|
||||||
}
|
|
||||||
if len(buf) == 0 {
|
|
||||||
tries++
|
|
||||||
} else {
|
|
||||||
tries = 0
|
|
||||||
}
|
|
||||||
copy(history[remaining-len(buf):], buf)
|
|
||||||
remaining -= len(buf)
|
|
||||||
year, month, day := timestamp.Date()
|
|
||||||
timestamp = time.Date(year, month, day, 0, 0, 0, 0, timestamp.Location()).Add(-1)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, msg := range history[remaining:] {
|
|
||||||
msg.Tags["batch"] = irc.TagValue(batchRef)
|
|
||||||
dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
|
||||||
}
|
|
||||||
|
|
||||||
dc.SendMessage(&irc.Message{
|
|
||||||
Prefix: dc.srv.prefix(),
|
|
||||||
Command: "BATCH",
|
|
||||||
Params: []string{"-" + batchRef},
|
|
||||||
})
|
|
||||||
case "AFTER":
|
case "AFTER":
|
||||||
dc.SendMessage(&irc.Message{
|
history, err = loadHistoryAfterTime(uc.network, entity, timestamp, limit)
|
||||||
Prefix: dc.srv.prefix(),
|
|
||||||
Command: "BATCH",
|
|
||||||
Params: []string{"+" + batchRef, "chathistory", target},
|
|
||||||
})
|
|
||||||
|
|
||||||
remaining := limit
|
|
||||||
tries := 0
|
|
||||||
now := time.Now()
|
|
||||||
for remaining > 0 && tries < maxTries && timestamp.Before(now) {
|
|
||||||
buf, err := parseMessagesAfter(uc.network, entity, timestamp, remaining)
|
|
||||||
if err != nil {
|
|
||||||
dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
|
|
||||||
return newChatHistoryError(subcommand, target)
|
|
||||||
}
|
|
||||||
if len(buf) == 0 {
|
|
||||||
tries++
|
|
||||||
} else {
|
|
||||||
tries = 0
|
|
||||||
}
|
|
||||||
for _, msg := range buf {
|
|
||||||
msg.Tags["batch"] = irc.TagValue(batchRef)
|
|
||||||
dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
|
||||||
}
|
|
||||||
remaining -= len(buf)
|
|
||||||
year, month, day := timestamp.Date()
|
|
||||||
timestamp = time.Date(year, month, day+1, 0, 0, 0, 0, timestamp.Location())
|
|
||||||
}
|
|
||||||
|
|
||||||
dc.SendMessage(&irc.Message{
|
|
||||||
Prefix: dc.srv.prefix(),
|
|
||||||
Command: "BATCH",
|
|
||||||
Params: []string{"-" + batchRef},
|
|
||||||
})
|
|
||||||
default:
|
default:
|
||||||
// TODO: support LATEST, BETWEEN
|
// TODO: support LATEST, BETWEEN
|
||||||
return ircError{&irc.Message{
|
return ircError{&irc.Message{
|
||||||
@ -1681,6 +1611,28 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
|
|||||||
Params: []string{"CHATHISTORY", "UNKNOWN_COMMAND", subcommand, "Unknown command"},
|
Params: []string{"CHATHISTORY", "UNKNOWN_COMMAND", subcommand, "Unknown command"},
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
|
dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
|
||||||
|
return newChatHistoryError(subcommand, target)
|
||||||
|
}
|
||||||
|
|
||||||
|
batchRef := "history"
|
||||||
|
dc.SendMessage(&irc.Message{
|
||||||
|
Prefix: dc.srv.prefix(),
|
||||||
|
Command: "BATCH",
|
||||||
|
Params: []string{"+" + batchRef, "chathistory", target},
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, msg := range history {
|
||||||
|
msg.Tags["batch"] = irc.TagValue(batchRef)
|
||||||
|
dc.SendMessage(dc.marshalMessage(msg, uc.network))
|
||||||
|
}
|
||||||
|
|
||||||
|
dc.SendMessage(&irc.Message{
|
||||||
|
Prefix: dc.srv.prefix(),
|
||||||
|
Command: "BATCH",
|
||||||
|
Params: []string{"-" + batchRef},
|
||||||
|
})
|
||||||
default:
|
default:
|
||||||
dc.logger.Printf("unhandled message: %v", msg)
|
dc.logger.Printf("unhandled message: %v", msg)
|
||||||
return newUnknownCommandError(msg.Command)
|
return newUnknownCommandError(msg.Command)
|
||||||
|
48
logger.go
48
logger.go
@ -11,6 +11,8 @@ import (
|
|||||||
"gopkg.in/irc.v3"
|
"gopkg.in/irc.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const messageLoggerMaxTries = 100
|
||||||
|
|
||||||
type messageLogger struct {
|
type messageLogger struct {
|
||||||
network *network
|
network *network
|
||||||
entity string
|
entity string
|
||||||
@ -244,3 +246,49 @@ func parseMessagesAfter(network *network, entity string, ref time.Time, limit in
|
|||||||
|
|
||||||
return history, nil
|
return history, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadHistoryBeforeTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error) {
|
||||||
|
history := make([]*irc.Message, limit)
|
||||||
|
remaining := limit
|
||||||
|
tries := 0
|
||||||
|
for remaining > 0 && tries < messageLoggerMaxTries {
|
||||||
|
buf, err := parseMessagesBefore(network, entity, t, remaining)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(buf) == 0 {
|
||||||
|
tries++
|
||||||
|
} else {
|
||||||
|
tries = 0
|
||||||
|
}
|
||||||
|
copy(history[remaining-len(buf):], buf)
|
||||||
|
remaining -= len(buf)
|
||||||
|
year, month, day := t.Date()
|
||||||
|
t = time.Date(year, month, day, 0, 0, 0, 0, t.Location()).Add(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return history[remaining:], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadHistoryAfterTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error) {
|
||||||
|
var history []*irc.Message
|
||||||
|
remaining := limit
|
||||||
|
tries := 0
|
||||||
|
now := time.Now()
|
||||||
|
for remaining > 0 && tries < messageLoggerMaxTries && t.Before(now) {
|
||||||
|
buf, err := parseMessagesAfter(network, entity, t, remaining)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(buf) == 0 {
|
||||||
|
tries++
|
||||||
|
} else {
|
||||||
|
tries = 0
|
||||||
|
}
|
||||||
|
history = append(history, buf...)
|
||||||
|
remaining -= len(buf)
|
||||||
|
year, month, day := t.Date()
|
||||||
|
t = time.Date(year, month, day+1, 0, 0, 0, 0, t.Location())
|
||||||
|
}
|
||||||
|
return history, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user