2023-05-31 07:11:10 -05:00
|
|
|
package chat
|
|
|
|
|
2023-06-08 06:50:34 -05:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2023-05-31 07:11:10 -05:00
|
|
|
|
2023-06-08 06:50:34 -05:00
|
|
|
type ClientMap struct {
|
|
|
|
Clients map[*Client]bool
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var ClientHandler = ClientMap{
|
|
|
|
Clients: map[*Client]bool{},
|
|
|
|
Mutex: sync.Mutex{},
|
|
|
|
}
|
2023-05-31 07:11:10 -05:00
|
|
|
|
2023-06-08 06:50:34 -05:00
|
|
|
func (c *ClientMap) AddClient(client *Client) {
|
|
|
|
c.Lock()
|
|
|
|
c.Clients[client] = true
|
|
|
|
c.Unlock()
|
2023-05-31 07:11:10 -05:00
|
|
|
}
|
|
|
|
|
2023-06-08 06:50:34 -05:00
|
|
|
func (c *ClientMap) DeleteClient(ID string) {
|
|
|
|
c.Lock()
|
|
|
|
|
|
|
|
for client := range c.Clients {
|
2023-05-31 07:11:10 -05:00
|
|
|
if client.ID == ID {
|
2023-06-08 06:50:34 -05:00
|
|
|
delete(c.Clients, client)
|
2023-05-31 07:11:10 -05:00
|
|
|
}
|
|
|
|
}
|
2023-06-08 06:50:34 -05:00
|
|
|
c.Unlock()
|
2023-05-31 07:11:10 -05:00
|
|
|
}
|
|
|
|
|
2023-06-08 06:50:34 -05:00
|
|
|
func (c *ClientMap) FindClientsByStreamer(streamer string) []*Client {
|
|
|
|
c.Lock()
|
|
|
|
|
2023-05-31 07:11:10 -05:00
|
|
|
var clients []*Client
|
2023-06-08 06:50:34 -05:00
|
|
|
for client := range c.Clients {
|
2023-05-31 07:11:10 -05:00
|
|
|
// check if the client is following the given streamer
|
|
|
|
for s, _ := range client.FollowingStreamers {
|
|
|
|
if s == streamer {
|
|
|
|
clients = append(clients, client)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-08 06:50:34 -05:00
|
|
|
c.Unlock()
|
2023-05-31 07:11:10 -05:00
|
|
|
return clients
|
|
|
|
}
|