3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-22 18:52:41 +01:00

minor optimization to Client.Friends

This commit is contained in:
Shivaram Lingamneni 2020-07-17 04:53:30 -04:00
parent 93530ae397
commit ffd00e1aff

View File

@ -1054,29 +1054,32 @@ func (client *Client) ModeString() (str string) {
} }
// Friends refers to clients that share a channel with this client. // Friends refers to clients that share a channel with this client.
func (client *Client) Friends(capabs ...caps.Capability) (result map[*Session]bool) { func (client *Client) Friends(capabs ...caps.Capability) (result map[*Session]empty) {
result = make(map[*Session]bool) result = make(map[*Session]empty)
// look at the client's own sessions // look at the client's own sessions
for _, session := range client.Sessions() { addFriendsToSet(result, client, capabs...)
if session.capabilities.HasAll(capabs...) {
result[session] = true
}
}
for _, channel := range client.Channels() { for _, channel := range client.Channels() {
for _, member := range channel.Members() { for _, member := range channel.Members() {
for _, session := range member.Sessions() { addFriendsToSet(result, member, capabs...)
if session.capabilities.HasAll(capabs...) {
result[session] = true
}
}
} }
} }
return return
} }
// helper for Friends
func addFriendsToSet(set map[*Session]empty, client *Client, capabs ...caps.Capability) {
client.stateMutex.RLock()
defer client.stateMutex.RUnlock()
for _, session := range client.sessions {
if session.capabilities.HasAll(capabs...) {
set[session] = empty{}
}
}
}
func (client *Client) SetOper(oper *Oper) { func (client *Client) SetOper(oper *Oper) {
client.stateMutex.Lock() client.stateMutex.Lock()
defer client.stateMutex.Unlock() defer client.stateMutex.Unlock()