Changeset 387 in code for trunk


Ignore:
Timestamp:
Aug 11, 2020, 1:58:50 PM (5 years ago)
Author:
contact
Message:

Extract history loading into functions

These will get re-used for sending history to clients that don't support
the chathistory extension.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/downstream.go

    r362 r387  
    15991599                }
    16001600
    1601                 batchRef := "history"
    1602                 maxTries := 100
     1601                var history []*irc.Message
    16031602                switch subcommand {
    16041603                case "BEFORE":
    1605                         dc.SendMessage(&irc.Message{
    1606                                 Prefix:  dc.srv.prefix(),
    1607                                 Command: "BATCH",
    1608                                 Params:  []string{"+" + batchRef, "chathistory", target},
    1609                         })
    1610 
    1611                         history := make([]*irc.Message, limit)
    1612                         remaining := limit
    1613 
    1614                         tries := 0
    1615                         for remaining > 0 && tries < maxTries {
    1616                                 buf, err := parseMessagesBefore(uc.network, entity, timestamp, remaining)
    1617                                 if err != nil {
    1618                                         dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
    1619                                         return newChatHistoryError(subcommand, target)
    1620                                 }
    1621                                 if len(buf) == 0 {
    1622                                         tries++
    1623                                 } else {
    1624                                         tries = 0
    1625                                 }
    1626                                 copy(history[remaining-len(buf):], buf)
    1627                                 remaining -= len(buf)
    1628                                 year, month, day := timestamp.Date()
    1629                                 timestamp = time.Date(year, month, day, 0, 0, 0, 0, timestamp.Location()).Add(-1)
    1630                         }
    1631 
    1632                         for _, msg := range history[remaining:] {
    1633                                 msg.Tags["batch"] = irc.TagValue(batchRef)
    1634                                 dc.SendMessage(dc.marshalMessage(msg, uc.network))
    1635                         }
    1636 
    1637                         dc.SendMessage(&irc.Message{
    1638                                 Prefix:  dc.srv.prefix(),
    1639                                 Command: "BATCH",
    1640                                 Params:  []string{"-" + batchRef},
    1641                         })
     1604                        history, err = loadHistoryBeforeTime(uc.network, entity, timestamp, limit)
    16421605                case "AFTER":
    1643                         dc.SendMessage(&irc.Message{
    1644                                 Prefix:  dc.srv.prefix(),
    1645                                 Command: "BATCH",
    1646                                 Params:  []string{"+" + batchRef, "chathistory", target},
    1647                         })
    1648 
    1649                         remaining := limit
    1650                         tries := 0
    1651                         now := time.Now()
    1652                         for remaining > 0 && tries < maxTries && timestamp.Before(now) {
    1653                                 buf, err := parseMessagesAfter(uc.network, entity, timestamp, remaining)
    1654                                 if err != nil {
    1655                                         dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
    1656                                         return newChatHistoryError(subcommand, target)
    1657                                 }
    1658                                 if len(buf) == 0 {
    1659                                         tries++
    1660                                 } else {
    1661                                         tries = 0
    1662                                 }
    1663                                 for _, msg := range buf {
    1664                                         msg.Tags["batch"] = irc.TagValue(batchRef)
    1665                                         dc.SendMessage(dc.marshalMessage(msg, uc.network))
    1666                                 }
    1667                                 remaining -= len(buf)
    1668                                 year, month, day := timestamp.Date()
    1669                                 timestamp = time.Date(year, month, day+1, 0, 0, 0, 0, timestamp.Location())
    1670                         }
    1671 
    1672                         dc.SendMessage(&irc.Message{
    1673                                 Prefix:  dc.srv.prefix(),
    1674                                 Command: "BATCH",
    1675                                 Params:  []string{"-" + batchRef},
    1676                         })
     1606                        history, err = loadHistoryAfterTime(uc.network, entity, timestamp, limit)
    16771607                default:
    16781608                        // TODO: support LATEST, BETWEEN
     
    16821612                        }}
    16831613                }
     1614                if err != nil {
     1615                        dc.logger.Printf("failed parsing log messages for chathistory: %v", err)
     1616                        return newChatHistoryError(subcommand, target)
     1617                }
     1618
     1619                batchRef := "history"
     1620                dc.SendMessage(&irc.Message{
     1621                        Prefix:  dc.srv.prefix(),
     1622                        Command: "BATCH",
     1623                        Params:  []string{"+" + batchRef, "chathistory", target},
     1624                })
     1625
     1626                for _, msg := range history {
     1627                        msg.Tags["batch"] = irc.TagValue(batchRef)
     1628                        dc.SendMessage(dc.marshalMessage(msg, uc.network))
     1629                }
     1630
     1631                dc.SendMessage(&irc.Message{
     1632                        Prefix:  dc.srv.prefix(),
     1633                        Command: "BATCH",
     1634                        Params:  []string{"-" + batchRef},
     1635                })
    16841636        default:
    16851637                dc.logger.Printf("unhandled message: %v", msg)
  • trunk/logger.go

    r362 r387  
    1111        "gopkg.in/irc.v3"
    1212)
     13
     14const messageLoggerMaxTries = 100
    1315
    1416type messageLogger struct {
     
    245247        return history, nil
    246248}
     249
     250func loadHistoryBeforeTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error) {
     251        history := make([]*irc.Message, limit)
     252        remaining := limit
     253        tries := 0
     254        for remaining > 0 && tries < messageLoggerMaxTries {
     255                buf, err := parseMessagesBefore(network, entity, t, remaining)
     256                if err != nil {
     257                        return nil, err
     258                }
     259                if len(buf) == 0 {
     260                        tries++
     261                } else {
     262                        tries = 0
     263                }
     264                copy(history[remaining-len(buf):], buf)
     265                remaining -= len(buf)
     266                year, month, day := t.Date()
     267                t = time.Date(year, month, day, 0, 0, 0, 0, t.Location()).Add(-1)
     268        }
     269
     270        return history[remaining:], nil
     271}
     272
     273func loadHistoryAfterTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error) {
     274        var history []*irc.Message
     275        remaining := limit
     276        tries := 0
     277        now := time.Now()
     278        for remaining > 0 && tries < messageLoggerMaxTries && t.Before(now) {
     279                buf, err := parseMessagesAfter(network, entity, t, remaining)
     280                if err != nil {
     281                        return nil, err
     282                }
     283                if len(buf) == 0 {
     284                        tries++
     285                } else {
     286                        tries = 0
     287                }
     288                history = append(history, buf...)
     289                remaining -= len(buf)
     290                year, month, day := t.Date()
     291                t = time.Date(year, month, day+1, 0, 0, 0, 0, t.Location())
     292        }
     293        return history, nil
     294}
Note: See TracChangeset for help on using the changeset viewer.