1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
package socialcache
import (
"errors"
. "github.com/Philipp15b/go-steam/protocol/steamlang"
. "github.com/Philipp15b/go-steam/steamid"
"sync"
)
// Chats list is a thread safe map
// They can be iterated over like so:
// for id, chat := range client.Social.Chats.GetCopy() {
// log.Println(id, chat.Name)
// }
type ChatsList struct {
mutex sync.RWMutex
byId map[SteamId]*Chat
}
// Returns a new chats list
func NewChatsList() *ChatsList {
return &ChatsList{byId: make(map[SteamId]*Chat)}
}
// Adds a chat to the chat list
func (list *ChatsList) Add(chat Chat) {
list.mutex.Lock()
defer list.mutex.Unlock()
_, exists := list.byId[chat.SteamId]
if !exists { //make sure this doesnt already exist
list.byId[chat.SteamId] = &chat
}
}
// Removes a chat from the chat list
func (list *ChatsList) Remove(id SteamId) {
list.mutex.Lock()
defer list.mutex.Unlock()
delete(list.byId, id)
}
// Adds a chat member to a given chat
func (list *ChatsList) AddChatMember(id SteamId, member ChatMember) {
list.mutex.Lock()
defer list.mutex.Unlock()
chat := list.byId[id]
if chat == nil { //Chat doesn't exist
chat = &Chat{SteamId: id}
list.byId[id] = chat
}
if chat.ChatMembers == nil { //New chat
chat.ChatMembers = make(map[SteamId]ChatMember)
}
chat.ChatMembers[member.SteamId] = member
}
// Removes a chat member from a given chat
func (list *ChatsList) RemoveChatMember(id SteamId, member SteamId) {
list.mutex.Lock()
defer list.mutex.Unlock()
chat := list.byId[id]
if chat == nil { //Chat doesn't exist
return
}
if chat.ChatMembers == nil { //New chat
return
}
delete(chat.ChatMembers, member)
}
// Returns a copy of the chats map
func (list *ChatsList) GetCopy() map[SteamId]Chat {
list.mutex.RLock()
defer list.mutex.RUnlock()
glist := make(map[SteamId]Chat)
for key, chat := range list.byId {
glist[key] = *chat
}
return glist
}
// Returns a copy of the chat of a given SteamId
func (list *ChatsList) ById(id SteamId) (Chat, error) {
list.mutex.RLock()
defer list.mutex.RUnlock()
if val, ok := list.byId[id]; ok {
return *val, nil
}
return Chat{}, errors.New("Chat not found")
}
// Returns the number of chats
func (list *ChatsList) Count() int {
list.mutex.RLock()
defer list.mutex.RUnlock()
return len(list.byId)
}
// A Chat
type Chat struct {
SteamId SteamId `json:",string"`
GroupId SteamId `json:",string"`
ChatMembers map[SteamId]ChatMember
}
// A Chat Member
type ChatMember struct {
SteamId SteamId `json:",string"`
ChatPermissions EChatPermission
ClanPermissions EClanPermission
}
|