2020-04-03 16:59:17 +00:00
|
|
|
package soju
|
|
|
|
|
|
|
|
import (
|
2021-01-04 15:26:30 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-04-03 16:59:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gopkg.in/irc.v3"
|
|
|
|
)
|
|
|
|
|
2020-10-25 10:13:51 +00:00
|
|
|
// messageStore is a per-user store for IRC messages.
|
2021-01-04 13:24:00 +00:00
|
|
|
type messageStore interface {
|
|
|
|
Close() error
|
|
|
|
// LastMsgID queries the last message ID for the given network, entity and
|
|
|
|
// date. The message ID returned may not refer to a valid message, but can be
|
|
|
|
// used in history queries.
|
|
|
|
LastMsgID(network *network, entity string, t time.Time) (string, error)
|
|
|
|
LoadLatestID(network *network, entity, id string, limit int) ([]*irc.Message, error)
|
|
|
|
Append(network *network, entity string, msg *irc.Message) (id string, err error)
|
2020-08-19 11:17:32 +00:00
|
|
|
}
|
2021-01-04 15:26:30 +00:00
|
|
|
|
2021-01-04 16:17:35 +00:00
|
|
|
// chatHistoryMessageStore is a message store that supports chat history
|
|
|
|
// operations.
|
|
|
|
type chatHistoryMessageStore interface {
|
|
|
|
messageStore
|
|
|
|
|
|
|
|
LoadBeforeTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error)
|
|
|
|
LoadAfterTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error)
|
|
|
|
}
|
|
|
|
|
2021-01-04 15:26:30 +00:00
|
|
|
func formatMsgID(netID int64, entity, extra string) string {
|
|
|
|
return fmt.Sprintf("%v %v %v", netID, entity, extra)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMsgID(s string) (netID int64, entity, extra string, err error) {
|
|
|
|
l := strings.SplitN(s, " ", 3)
|
|
|
|
if len(l) != 3 {
|
|
|
|
return 0, "", "", fmt.Errorf("invalid message ID %q: expected 3 fields", s)
|
|
|
|
}
|
|
|
|
netID, err = strconv.ParseInt(l[0], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, "", "", fmt.Errorf("invalid message ID %q: %v", s, err)
|
|
|
|
}
|
|
|
|
return netID, l[1], l[2], nil
|
|
|
|
}
|