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.
 
 

53 lines
1.1 KiB

package channels
import (
"context"
"log"
"time"
"git.aiterp.net/rpdata/logbot3/internal/models"
"git.aiterp.net/rpdata/logbot3/internal/models/changes"
)
// SubscribeLogged returns a channel that gets all the channel changes.
func SubscribeLogged(ctx context.Context) (<-chan models.Channel, error) {
earliest := time.Now()
channels, err := ListLogged(ctx)
if err != nil {
return nil, err
}
output := make(chan models.Channel, len(channels)+8)
for _, channel := range channels {
output <- channel
}
go func() {
for {
time.Sleep(time.Second * 5)
// Get the changes.
changes, err := changes.List(ctx, &changes.Filter{EarliestDate: &earliest, Keys: []models.ChangeKey{{Model: "Channel", ID: "*"}}})
if ctx.Err() != nil {
return
} else if err != nil {
log.Println("Failed to get chnges:", err)
continue
}
// Group changes to the same model.
for _, change := range changes {
for _, obj := range change.Objects {
channel, err := obj.Channel()
if err != nil {
continue
}
output <- channel
}
}
}
}()
return output, nil
}