Changeset 387 in code for trunk/logger.go


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.