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
683 B

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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 nil, 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 nil, err
  18. }
  19. if logged && !channel.Logged {
  20. channel, err = Edit(*channel, &logged, nil, nil, nil)
  21. if err != nil {
  22. return nil, err
  23. }
  24. }
  25. return channel, nil
  26. }