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.
 
 
 
 
 
 

102 lines
1.8 KiB

package device
import (
"git.aiterp.net/lucifer3/server/internal/gentools"
"log"
"sort"
"strings"
)
var exclusivePrefixes = []string{
"lucifer:name:",
"lucifer:icon:",
"lucifer:room:",
"lucifer:group:",
}
type PointerMatcher interface {
Match(str string) bool
}
type Pointer struct {
ID string `json:"id"`
Aliases []string `json:"aliases"`
}
func (p *Pointer) AddAlias(alias string) (added *string, removed *string) {
if !strings.HasPrefix(alias, "lucifer:") {
return
}
for _, alias2 := range p.Aliases {
if alias2 == alias {
return
}
}
for _, prefix := range exclusivePrefixes {
if strings.HasPrefix(alias, prefix) {
for i, alias2 := range p.Aliases {
if strings.HasPrefix(alias2, prefix) {
p.Aliases = append(p.Aliases[:0:0], p.Aliases...)
p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
removed = gentools.ShallowCopy(&alias2)
break
}
}
break
}
}
added = gentools.ShallowCopy(&alias)
p.Aliases = append(p.Aliases, alias)
sort.Strings(p.Aliases)
return
}
func (p *Pointer) RemoveAlias(alias string) bool {
if strings.HasPrefix(alias, "lucifer:name:") {
// Name cannot be removed like this.
return false
}
for i, alias2 := range p.Aliases {
if alias2 == alias {
p.Aliases = append(p.Aliases[:0:0], p.Aliases...)
p.Aliases = append(p.Aliases[:i], p.Aliases[i+1:]...)
return true
}
}
return false
}
func (p *Pointer) Name() string {
for _, alias := range p.Aliases {
if strings.HasPrefix(alias, "lucifer:name:") {
return alias
}
}
return ""
}
func (p *Pointer) Matches(matcher PointerMatcher) bool {
defer func() {
if err := recover(); err != nil {
log.Println("[PANIC]", err)
}
}()
if matcher.Match(p.ID) {
return true
}
for _, alias := range p.Aliases {
if matcher.Match(alias) {
return true
}
}
return false
}