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

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package channels
  2. import (
  3. "context"
  4. "log"
  5. "time"
  6. "git.aiterp.net/rpdata/logbot3/internal/models"
  7. "git.aiterp.net/rpdata/logbot3/internal/models/changes"
  8. )
  9. // SubscribeLogged returns a channel that gets all the channel changes.
  10. func SubscribeLogged(ctx context.Context) (<-chan models.Channel, error) {
  11. earliest := time.Now()
  12. channels, err := ListLogged(ctx)
  13. if err != nil {
  14. return nil, err
  15. }
  16. output := make(chan models.Channel, len(channels)+8)
  17. for _, channel := range channels {
  18. output <- channel
  19. }
  20. go func() {
  21. for {
  22. time.Sleep(time.Second * 5)
  23. // Get the changes.
  24. changes, err := changes.List(ctx, &changes.Filter{EarliestDate: &earliest, Keys: []models.ChangeKey{{Model: "Channel", ID: "*"}}})
  25. if ctx.Err() != nil {
  26. return
  27. } else if err != nil {
  28. log.Println("Failed to get chnges:", err)
  29. continue
  30. }
  31. // Group changes to the same model.
  32. for _, change := range changes {
  33. for _, obj := range change.Objects {
  34. channel, err := obj.Channel()
  35. if err != nil {
  36. continue
  37. }
  38. output <- channel
  39. }
  40. }
  41. }
  42. }()
  43. return output, nil
  44. }