/
create-room.go
59 lines (49 loc) · 1.32 KB
/
create-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
package rhandlers
import (
"encoding/json"
"github.com/gorilla/websocket"
"github.com/jessehorne/resolute/pkg/v1/rstructs"
"github.com/jessehorne/resolute/pkg/v1/util"
)
type CreateRoomRequestData struct {
Name string `json:"name"`
Username string `json:"username"`
ClientRoomID string `json:"client_room_id"`
PublicKeyString string `json:"public_key_string"`
}
type CreateRoomRequest struct {
Cmd string `json:"cmd"`
Data CreateRoomRequestData `json:"data"`
}
type CreateRoomResponse struct {
Cmd string `json:"cmd"`
Room *rstructs.Room `json:"room"`
ClientRoomID string `json:"client_room_id"`
}
func CreateRoomHandler(s *rstructs.State, userID string, c *websocket.Conn, data []byte) error {
var cr CreateRoomRequest
err := json.Unmarshal(data, &cr)
if err != nil {
return err
}
pubKey, err := util.ParsePublicKey(cr.Data.PublicKeyString)
if err != nil {
// TODO?
return nil
}
newRoom := rstructs.NewRoom(cr.Data.Name, userID)
newRoom.OwnerID = userID
newRoom.AddUser(&rstructs.User{
UserID: userID,
Username: cr.Data.Username,
Conn: c,
PublicKey: pubKey,
})
s.AddRoom(newRoom)
c.WriteJSON(CreateRoomResponse{
Cmd: "create-room",
Room: newRoom,
ClientRoomID: cr.Data.ClientRoomID,
})
return nil
}