msgstore: rename searchMessageOptions, export fields

Preparation for splitting msgstore into a separate package.
This commit is contained in:
Simon Ser 2022-05-09 15:44:41 +02:00
parent f508d36c38
commit 89412187d4
3 changed files with 27 additions and 27 deletions

View File

@ -2994,8 +2994,8 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
var uc *upstreamConn var uc *upstreamConn
const searchMaxLimit = 100 const searchMaxLimit = 100
opts := searchOptions{ opts := searchMessageOptions{
limit: searchMaxLimit, Limit: searchMaxLimit,
} }
for name, v := range attrs { for name, v := range attrs {
value := string(v) value := string(v)
@ -3010,12 +3010,12 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
} }
switch name { switch name {
case "after": case "after":
opts.start = timestamp opts.Start = timestamp
case "before": case "before":
opts.end = timestamp opts.End = timestamp
} }
case "from": case "from":
opts.from = value opts.From = value
case "in": case "in":
u, upstreamName, err := dc.unmarshalEntity(value) u, upstreamName, err := dc.unmarshalEntity(value)
if err != nil { if err != nil {
@ -3025,9 +3025,9 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
}} }}
} }
uc = u uc = u
opts.in = u.network.casemap(upstreamName) opts.In = u.network.casemap(upstreamName)
case "text": case "text":
opts.text = value opts.Text = value
case "limit": case "limit":
limit, err := strconv.Atoi(value) limit, err := strconv.Atoi(value)
if err != nil || limit <= 0 { if err != nil || limit <= 0 {
@ -3036,7 +3036,7 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
Params: []string{"SEARCH", "INVALID_PARAMS", name, "Invalid limit"}, Params: []string{"SEARCH", "INVALID_PARAMS", name, "Invalid limit"},
}} }}
} }
opts.limit = limit opts.Limit = limit
} }
} }
if uc == nil { if uc == nil {
@ -3045,11 +3045,11 @@ func (dc *downstreamConn) handleMessageRegistered(ctx context.Context, msg *irc.
Params: []string{"SEARCH", "INVALID_PARAMS", "in", "The in parameter is mandatory"}, Params: []string{"SEARCH", "INVALID_PARAMS", "in", "The in parameter is mandatory"},
}} }}
} }
if opts.limit > searchMaxLimit { if opts.Limit > searchMaxLimit {
opts.limit = searchMaxLimit opts.Limit = searchMaxLimit
} }
messages, err := store.Search(ctx, &uc.network.Network, opts) messages, err := store.Search(ctx, &uc.network.Network, &opts)
if err != nil { if err != nil {
dc.logger.Printf("failed fetching messages for search: %v", err) dc.logger.Printf("failed fetching messages for search: %v", err)
return ircError{&irc.Message{ return ircError{&irc.Message{

View File

@ -60,13 +60,13 @@ type chatHistoryMessageStore interface {
LoadAfterTime(ctx context.Context, start, end time.Time, options *loadMessageOptions) ([]*irc.Message, error) LoadAfterTime(ctx context.Context, start, end time.Time, options *loadMessageOptions) ([]*irc.Message, error)
} }
type searchOptions struct { type searchMessageOptions struct {
start time.Time Start time.Time
end time.Time End time.Time
limit int Limit int
from string From string
in string In string
text string Text string
} }
// searchMessageStore is a message store that supports server-side search // searchMessageStore is a message store that supports server-side search
@ -75,7 +75,7 @@ type searchMessageStore interface {
messageStore messageStore
// Search returns messages matching the specified options. // Search returns messages matching the specified options.
Search(ctx context.Context, network *database.Network, search searchOptions) ([]*irc.Message, error) Search(ctx context.Context, network *database.Network, options *searchMessageOptions) ([]*irc.Message, error)
} }
type msgIDType uint type msgIDType uint

View File

@ -701,10 +701,10 @@ func (ms *fsMessageStore) ListTargets(ctx context.Context, network *database.Net
return targets, nil return targets, nil
} }
func (ms *fsMessageStore) Search(ctx context.Context, network *database.Network, opts searchOptions) ([]*irc.Message, error) { func (ms *fsMessageStore) Search(ctx context.Context, network *database.Network, opts *searchMessageOptions) ([]*irc.Message, error) {
text := strings.ToLower(opts.text) text := strings.ToLower(opts.Text)
selector := func(m *irc.Message) bool { selector := func(m *irc.Message) bool {
if opts.from != "" && m.User != opts.from { if opts.From != "" && m.User != opts.From {
return false return false
} }
if text != "" && !strings.Contains(strings.ToLower(m.Params[1]), text) { if text != "" && !strings.Contains(strings.ToLower(m.Params[1]), text) {
@ -714,13 +714,13 @@ func (ms *fsMessageStore) Search(ctx context.Context, network *database.Network,
} }
loadOptions := loadMessageOptions{ loadOptions := loadMessageOptions{
Network: network, Network: network,
Entity: opts.in, Entity: opts.In,
Limit: opts.limit, Limit: opts.Limit,
} }
if !opts.start.IsZero() { if !opts.Start.IsZero() {
return ms.getAfterTime(ctx, opts.start, opts.end, &loadOptions, selector) return ms.getAfterTime(ctx, opts.Start, opts.End, &loadOptions, selector)
} else { } else {
return ms.getBeforeTime(ctx, opts.end, opts.start, &loadOptions, selector) return ms.getBeforeTime(ctx, opts.End, opts.Start, &loadOptions, selector)
} }
} }