/
room.go
173 lines (146 loc) · 3.79 KB
/
room.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package client
import (
"errors"
"github.com/jessehorne/resolute/pkg/v1/rhandlers"
"github.com/jessehorne/resolute/pkg/v1/rstructs"
"github.com/jessehorne/resolute/pkg/v1/util"
)
type CRoom struct {
ClientRoomID string
Client *Client
IsOwner bool
ReqName string
ReqUsername string
Room *rstructs.Room
MessageQueue []string
callbacks map[string]interface{}
}
func (r *CRoom) UpdateUsers(users []rstructs.JoinedUser) {
if r.Room.Users == nil {
r.Room.Users = map[string]*rstructs.User{}
}
for _, u := range users {
if u.UserID == r.Client.UserID {
continue
}
pubKey, err := util.ParsePublicKey(u.PublicKeyString)
if err != nil {
continue
}
existingUser, ok := r.Room.Users[u.UserID]
if !ok {
existingUser = &rstructs.User{}
r.Room.Users[u.UserID] = existingUser
}
existingUser.UserID = u.UserID
existingUser.Username = u.Username
existingUser.PublicKeyString = u.PublicKeyString
existingUser.PublicKey = pubKey
}
}
func (r *CRoom) AddJoinedUser(userID, username, pubKey string) {
if r.Room.Users == nil {
r.Room.Users = map[string]*rstructs.User{}
}
pk, err := util.ParsePublicKey(pubKey)
if err != nil {
return
}
existingUser, ok := r.Room.Users[userID]
if !ok {
existingUser = &rstructs.User{}
r.Room.Users[userID] = existingUser
}
existingUser.UserID = userID
existingUser.Username = username
existingUser.PublicKeyString = pubKey
existingUser.PublicKey = pk
}
func (r *CRoom) On(name string, cb interface{}) {
// TODO: add checks to make sure the functions being provided are acceptable
// example: key-onetime requires a function like func(string, string) since it returns (roomID, key)
r.callbacks[name] = cb
}
func (r *CRoom) call(name string, data interface{}) {
cb, ok := r.callbacks[name]
if ok {
if name == "created" {
cb.(func(string))(data.(string))
} else if name == "key-onetime" {
d := data.(map[string]string)
cb.(func(string, string))(d["room_id"], d["key"])
} else if name == "key-forever" {
d := data.(map[string]string)
cb.(func(string, string))(d["room_id"], d["key"])
} else if name == "send-message" {
d := data.(map[string]string)
cb.(func(string, string, string, string))(d["room_id"], d["user_id"], d["username"], d["content"])
} else if name == "joined" {
d := data.(map[string]string)
cb.(func(string, string))(d["room_id"], d["room_name"])
} else if name == "user-joined" {
d := data.(map[string]string)
cb.(func(string, string, string, string, string))(d["room_id"], d["room_name"], d["user_id"], d["username"], d["key_type"])
}
}
}
func (r *CRoom) GetKey(t string) error {
if r.Room == nil {
return errors.New("room not ready")
}
if t == "onetime" {
req := rhandlers.GetRoomOneTimeKeyRequest{
Cmd: "room-key-onetime",
Data: rhandlers.RoomIDReq{
RoomID: r.Room.ID,
},
}
err := r.Client.Conn.WriteJSON(req)
if err != nil {
// TODO
}
} else if t == "forever" {
req := rhandlers.GetRoomForeverKeyRequest{
Cmd: "room-key-forever",
Data: rhandlers.RoomIDReq{
RoomID: r.Room.ID,
},
}
err := r.Client.Conn.WriteJSON(req)
if err != nil {
// TODO
}
}
return nil
}
func (r *CRoom) SendMessage(content string) error {
if r.Room == nil {
return errors.New("room not set")
}
// send message to all users in room encrypted with their public key
for _, u := range r.Room.Users {
if u.UserID == r.Client.UserID {
continue
}
if u.PublicKey == nil {
continue
}
s, err := util.EncryptMessage(u.PublicKey, content)
if err != nil {
continue
}
req := rhandlers.SendMessageReq{
Cmd: "send-message",
Data: rhandlers.SendMessageReqData{
RoomID: r.Room.ID,
ToUserID: u.UserID,
Content: s,
},
}
err = r.Client.Conn.WriteJSON(req)
if err != nil {
// TODO
}
}
return nil
}