1 | package soju
|
---|
2 |
|
---|
3 | import (
|
---|
4 | "fmt"
|
---|
5 | "strconv"
|
---|
6 | "strings"
|
---|
7 | "time"
|
---|
8 |
|
---|
9 | "gopkg.in/irc.v3"
|
---|
10 | )
|
---|
11 |
|
---|
12 | // messageStore is a per-user store for IRC messages.
|
---|
13 | type messageStore interface {
|
---|
14 | Close() error
|
---|
15 | // LastMsgID queries the last message ID for the given network, entity and
|
---|
16 | // date. The message ID returned may not refer to a valid message, but can be
|
---|
17 | // used in history queries.
|
---|
18 | LastMsgID(network *network, entity string, t time.Time) (string, error)
|
---|
19 | LoadLatestID(network *network, entity, id string, limit int) ([]*irc.Message, error)
|
---|
20 | Append(network *network, entity string, msg *irc.Message) (id string, err error)
|
---|
21 | }
|
---|
22 |
|
---|
23 | // chatHistoryMessageStore is a message store that supports chat history
|
---|
24 | // operations.
|
---|
25 | type chatHistoryMessageStore interface {
|
---|
26 | messageStore
|
---|
27 |
|
---|
28 | LoadBeforeTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error)
|
---|
29 | LoadAfterTime(network *network, entity string, t time.Time, limit int) ([]*irc.Message, error)
|
---|
30 | }
|
---|
31 |
|
---|
32 | func formatMsgID(netID int64, entity, extra string) string {
|
---|
33 | return fmt.Sprintf("%v %v %v", netID, entity, extra)
|
---|
34 | }
|
---|
35 |
|
---|
36 | func parseMsgID(s string) (netID int64, entity, extra string, err error) {
|
---|
37 | l := strings.SplitN(s, " ", 3)
|
---|
38 | if len(l) != 3 {
|
---|
39 | return 0, "", "", fmt.Errorf("invalid message ID %q: expected 3 fields", s)
|
---|
40 | }
|
---|
41 | netID, err = strconv.ParseInt(l[0], 10, 64)
|
---|
42 | if err != nil {
|
---|
43 | return 0, "", "", fmt.Errorf("invalid message ID %q: %v", s, err)
|
---|
44 | }
|
---|
45 | return netID, l[1], l[2], nil
|
---|
46 | }
|
---|