The new logbot, not committed from the wrong terminal window this time.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

66 lines
1.3 KiB

package logs
import (
"context"
"encoding/json"
"errors"
"git.aiterp.net/rpdata/logbot3/internal/api"
"git.aiterp.net/rpdata/logbot3/internal/models"
)
// ErrNoneOpen is returned by FindOpen if no logs are open, but the query did succeed.
var ErrNoneOpen = errors.New("No open logs")
// FindOpen lists all changes according to the filter.
func FindOpen(ctx context.Context, channelName string) (models.Log, error) {
data, err := api.Global().Query(ctx, findOpenGQL, map[string]interface{}{"channel": channelName}, nil)
if err != nil {
return models.Log{}, err
}
res := findOpenResult{}
err = json.Unmarshal(data, &res)
if err != nil {
return models.Log{}, err
}
if len(res.Logs) == 0 {
return models.Log{}, ErrNoneOpen
}
// This shouldn't happen, but if there are more than one
// open logs for a channel, select the most recent one.
selected := res.Logs[0]
for _, log := range res.Logs[1:] {
if log.Date.After(selected.Date) {
selected = log
}
}
return selected, nil
}
type findOpenResult struct {
Logs []models.Log `json:"logs"`
}
var findOpenGQL = `
query FindOpen($channel:String!) {
logs(filter:{open:true, channels:[$channel]}) {
id
date
channelName
title
eventName
description
open
posts {
id
time
kind
nick
text
}
}
}
`