GraphQL API and utilities for the rpdata project
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.

33 lines
720 B

  1. package channels
  2. import (
  3. "strings"
  4. "github.com/globalsign/mgo"
  5. "git.aiterp.net/rpdata/api/models"
  6. )
  7. // Ensure adds a channel if it doesn't exist. If logged is set and the found channel isn't logged,
  8. // that is changed.
  9. func Ensure(name string, logged bool) (models.Channel, error) {
  10. if len(name) < 3 && !strings.HasPrefix(name, "#") {
  11. return models.Channel{}, ErrInvalidName
  12. }
  13. channel, err := FindName(name)
  14. if err == mgo.ErrNotFound {
  15. return Add(name, logged, false, "", "")
  16. } else if err != nil {
  17. return models.Channel{}, err
  18. }
  19. if logged && !channel.Logged {
  20. channel, err = Edit(channel, &logged, nil, nil, nil)
  21. if err != nil {
  22. return models.Channel{}, err
  23. }
  24. }
  25. return channel, nil
  26. }