Added Target interface and target types

This commit is contained in:
2018-06-25 09:45:00 +02:00
parent bca4614a07
commit 65e4385b71
8 changed files with 384 additions and 2 deletions
+31
View File
@@ -242,6 +242,32 @@ func (list *List) Users() []User {
return result
}
// Patch allows editing a limited subset of the user's properties.
func (list *List) Patch(nick string, patch UserPatch) (ok bool) {
list.mutex.Lock()
defer list.mutex.Unlock()
for _, user := range list.users {
if strings.EqualFold(nick, user.Nick) {
if patch.Account != "" || patch.ClearAccount {
user.Account = patch.Account
}
if patch.User != "" {
user.User = patch.User
}
if patch.Host != "" {
user.Host = patch.Host
}
return true
}
}
return false
}
// SetAutoSort enables or disables automatic sorting, which by default is enabled.
// Dislabing it makes sense when doing a massive operation. Enabling it will trigger
// a sort.
@@ -264,6 +290,11 @@ func (list *List) Clear() {
list.mutex.Unlock()
}
// Immutable gets an immutable version of the list.
func (list *List) Immutable() Immutable {
return Immutable{list: list}
}
func (list *List) sort() {
sort.Slice(list.users, func(i, j int) bool {
a := list.users[i]
+8
View File
@@ -11,6 +11,14 @@ type User struct {
PrefixedNick string `json:"prefixedNick"`
}
// UserPatch is used in List.Patch to apply changes to a user
type UserPatch struct {
User string
Host string
Account string
ClearAccount bool
}
// HighestMode returns the highest mode.
func (user *User) HighestMode() rune {
if len(user.Modes) == 0 {