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
33 lines
683 B
package channels
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/globalsign/mgo"
|
|
|
|
"git.aiterp.net/rpdata/api/models"
|
|
)
|
|
|
|
// Ensure adds a channel if it doesn't exist. If logged is set and the found channel isn't logged,
|
|
// that is changed.
|
|
func Ensure(name string, logged bool) (*models.Channel, error) {
|
|
if len(name) < 3 && !strings.HasPrefix(name, "#") {
|
|
return nil, ErrInvalidName
|
|
}
|
|
|
|
channel, err := FindName(name)
|
|
if err == mgo.ErrNotFound {
|
|
return Add(name, logged, false, "", "")
|
|
} else if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if logged && !channel.Logged {
|
|
channel, err = Edit(*channel, &logged, nil, nil, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return channel, nil
|
|
}
|