Skip to content

Commit f2fb11f

Browse files
committed
Merge remote-tracking branch 'origin/collab-post-ms6-risky' into deploy-risky
2 parents 7942b6b + b13d1b2 commit f2fb11f

File tree

8 files changed

+334
-112
lines changed

8 files changed

+334
-112
lines changed

collab/main.go

Lines changed: 132 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
verify "collab/verify"
4+
"collab/verify"
55
"context"
66
"encoding/json"
77
"io"
@@ -10,11 +10,22 @@ import (
1010
"os"
1111
"strconv"
1212
"sync"
13+
"time"
1314

1415
"github.com/gin-gonic/gin"
1516
"github.com/gorilla/websocket"
1617
)
1718

19+
// types
20+
const (
21+
AUTH = "auth"
22+
AUTH_SUCCESS = "auth_success"
23+
AUTH_FAIL = "auth_fail"
24+
CLOSE_SESSION = "close_session"
25+
CONTENT_CHANGE = "content_change"
26+
PING = "ping"
27+
)
28+
1829
var upgrader = websocket.Upgrader{
1930
CheckOrigin: func(r *http.Request) bool {
2031
return true
@@ -37,10 +48,12 @@ type Hub struct {
3748
}
3849

3950
type Message struct {
40-
Type string `json:"type"`
41-
RoomID string `json:"roomId"`
42-
Content []byte `json:"data"`
43-
UserID string `json:"userId"`
51+
Type string `json:"type"`
52+
RoomID string `json:"roomId"`
53+
Content string `json:"data"`
54+
UserID string `json:"userId"`
55+
Token string `json:"token"`
56+
MatchHash string `json:"matchHash"`
4457
}
4558

4659
func verifyToken(token string) (bool, string) {
@@ -127,10 +140,19 @@ func (h *Hub) Run() {
127140
case message := <-h.broadcast:
128141
h.mutex.Lock()
129142
// Update the current workspace for this RoomID
130-
h.workspaces[message.RoomID] = string(message.Content)
143+
h.workspaces[message.RoomID] = message.Content
131144
for client := range h.clients {
132145
if client.roomID == message.RoomID {
133-
err := client.conn.WriteMessage(websocket.TextMessage, message.Content)
146+
147+
log.Println("Original message: ", message)
148+
149+
msgJson, _ := json.Marshal(message)
150+
151+
log.Printf("Sending message to client: %s", msgJson)
152+
153+
err := client.conn.WriteMessage(websocket.TextMessage,
154+
msgJson,
155+
)
134156
if err != nil {
135157
log.Printf("Error sending message: %v", err)
136158
client.conn.Close()
@@ -142,7 +164,7 @@ func (h *Hub) Run() {
142164
}
143165

144166

145-
167+
146168
}
147169
}
148170

@@ -165,7 +187,6 @@ func serveWs(
165187
return
166188
}
167189

168-
169190
client := &Client{conn: conn, roomID: roomID}
170191
hub.register <- client
171192

@@ -214,27 +235,35 @@ func handleMessages(
214235
break
215236
}
216237

217-
var msgData map[string]interface{}
238+
log.Printf("Raw message received: %s", string(message))
239+
240+
var msgData Message
218241
if err := json.Unmarshal(message, &msgData); err != nil {
219242
log.Printf("Failed to parse message: %v", err)
220243
continue
221244
}
222245

246+
log.Printf("Raw message parsed: %s", msgData)
223247

224-
if msgData["type"] == "auth" {
225-
token, tokenOk := msgData["token"].(string)
226-
if !tokenOk {
248+
if msgData.Type == AUTH {
249+
token := msgData.Token
250+
if token == "" {
227251
log.Println("Authentication failed - no token attached")
228-
client.conn.WriteMessage(
229-
websocket.TextMessage,
230-
[]byte("Authentication failed"),
231-
)
252+
253+
msg := Message{
254+
Type: AUTH_FAIL,
255+
RoomID: client.roomID,
256+
Content: "Authentication failed - no token attached",
257+
}
258+
msgJson, _ := json.Marshal(msg)
259+
client.conn.WriteMessage(websocket.TextMessage, msgJson)
232260
client.conn.Close()
233261
break
234262
}
235263
isSuccess := false
236-
match, matchOk := msgData["matchHash"].(string)
237-
if matchOk && !authenticateClient(token, match, client, roomMappings, persistMappings) {
264+
match := msgData.MatchHash
265+
if match != "" &&
266+
!authenticateClient(token, match, client, roomMappings, persistMappings) {
238267
log.Println(
239268
"failed to find a matching room from match hash, proceeding with persistence check",
240269
)
@@ -247,70 +276,130 @@ func handleMessages(
247276
isSuccess = true
248277
}
249278
if !isSuccess {
250-
client.conn.WriteMessage(
251-
websocket.TextMessage,
252-
[]byte("Authentication failed"),
253-
)
279+
msg := Message{
280+
Type: AUTH_FAIL,
281+
RoomID: client.roomID,
282+
Content: "Authentication failed",
283+
}
284+
msgJson, _ := json.Marshal(msg)
285+
client.conn.WriteMessage(websocket.TextMessage, msgJson)
286+
254287
client.conn.Close()
255288
break
256289
}
257290
client.authenticated = true
258-
client.conn.WriteMessage(
259-
websocket.TextMessage,
260-
[]byte("Auth Success"),
261-
)
291+
292+
serverContent := hub.workspaces[client.roomID]
293+
294+
newMsg := Message{
295+
Type: AUTH_SUCCESS,
296+
RoomID: client.roomID,
297+
Content: serverContent,
298+
}
299+
msgJson, _ := json.Marshal(newMsg)
300+
client.conn.WriteMessage(websocket.TextMessage, msgJson)
301+
262302
log.Println("Client authenticated successfully")
263303
}
264304

265-
if msgData["type"] == "close_session" {
305+
// old logic before type changes
306+
// if msgData["type"] == "ping" {
307+
// //receives ping from client1, need to send a ping to client2
308+
// //eventually, if present, client2 will send the ping back, which will be broadcasted back to client1.
309+
310+
// userID, _ := msgData["userId"].(string)
311+
// request := Message {
312+
// RoomID: client.roomID,
313+
// UserID: userID,
314+
// Content: []byte("ping request"),
315+
// }
316+
317+
// hub.broadcast <- request
318+
// }
319+
320+
if msgData.Type == CLOSE_SESSION {
266321
closeMessage := Message{
267322
RoomID: client.roomID,
268-
Content: []byte("The session has been closed by a user."),
323+
Content: "The session has been closed by a user.",
269324
}
270-
targetId := msgData["userId"].(string)
325+
targetId := msgData.UserID
271326
data, err := persistMappings.Conn.HGetAll(context.Background(), targetId).Result()
272327
if err != nil {
273328
log.Printf("Error retrieving data for userID %s: %v", targetId, err)
274329
} else {
275330
_, err1 := persistMappings.Conn.Del(context.Background(), targetId).Result()
276331
if err1 != nil {
277-
log.Printf("Error deleting data for userID %s: %v", targetId, err1);
332+
log.Printf("Error deleting data for userID %s: %v", targetId, err1)
278333
}
279334
_, err2 := persistMappings.Conn.Del(context.Background(), data["otherUser"]).Result()
280335
if err2 != nil {
281-
log.Printf("Error deleting data for other user %s: %v", data["otherUser"], err2);
336+
log.Printf("Error deleting data for other user %s: %v", data["otherUser"], err2)
282337
}
283338
}
284339
hub.broadcast <- closeMessage
285-
}
340+
} else if msgData.Type == CONTENT_CHANGE {
341+
// Broadcast the message to other clients
342+
hub.broadcast <- Message{
343+
RoomID: client.roomID,
344+
Content: msgData.Content,
345+
Type: msgData.Type,
346+
UserID: msgData.UserID,
347+
}
348+
} else if msgData.Type == PING {
349+
// Broadcast the message to other clients
350+
hub.broadcast <- Message{
351+
RoomID: client.roomID,
352+
Type: msgData.Type,
353+
UserID: msgData.UserID,
354+
}
286355

287-
// Broadcast the message to other clients
288-
userID, _ := msgData["userId"].(string)
289-
hub.broadcast <- Message{RoomID: client.roomID, Content: message, UserID: userID}
356+
extendExpiryTime(msgData.UserID, persistMappings)
357+
} else {
358+
log.Printf("Unknown message type: %s", msgData.Type)
359+
}
290360
}
291361
}
292362

363+
func extendExpiryTime(userId string, persistMappings *verify.PersistMappings) {
364+
365+
ctx := context.Background()
366+
if err := persistMappings.Conn.Expire(ctx, userId, time.Minute * 10).Err(); err != nil {
367+
log.Println("Error extending room time on ping: ", err.Error())
368+
} else {
369+
370+
log.Printf("expiration reset for 10 minutes for user %s: ", userId)
371+
}
372+
373+
374+
}
375+
376+
type ClientWorkspace struct {
377+
Clients int `json:"clients"`
378+
Workspace string `json:"workspace"`
379+
}
380+
293381
// Status endpoint that shows the number of clients and the current color for each roomID
294382
func statusHandler(hub *Hub) gin.HandlerFunc {
383+
295384
return func(c *gin.Context) {
296385
hub.mutex.Lock()
297386
defer hub.mutex.Unlock()
298387

299-
status := make(map[string]interface{})
388+
status := make(map[string]ClientWorkspace)
300389
for client := range hub.clients {
301390
roomID := client.roomID
302391
currentStatus, ok := status[roomID]
303392
if !ok {
304393
// Initialize status for a new roomID
305-
status[roomID] = map[string]interface{}{
306-
"clients": 1,
307-
"workspace": hub.workspaces[roomID],
394+
status[roomID] = ClientWorkspace{
395+
Clients: 1,
396+
Workspace: hub.workspaces[roomID],
308397
}
309398
} else {
310399
// Update the client count for an existing roomID
311-
status[roomID] = map[string]interface{}{
312-
"clients": currentStatus.(map[string]interface{})["clients"].(int) + 1,
313-
"workspace": hub.workspaces[roomID],
400+
status[roomID] = ClientWorkspace{
401+
Clients: currentStatus.Clients + 1,
402+
Workspace: hub.workspaces[roomID],
314403
}
315404
}
316405
}

collab/verify/persistMappings.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ func VerifyPersist(persistMappings *PersistMappings, roomID string, userID strin
3030
return false
3131
}
3232

33+
log.Printf("current roomID: %s, expected roomID: %s", data["roomId"], roomID)
34+
3335
return data["roomId"] == roomID
3436
}

collab/verify/roomMappings.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package verify
33
import (
44
"context"
55
"log"
6+
//"time"
67

78
redis "github.com/go-redis/redis/v8"
89
)
@@ -54,5 +55,13 @@ func VerifyRoomAndMoveToPersist(
5455
log.Printf("error sending room to persistent storage: %s", err.Error())
5556
}
5657

58+
/*
59+
if err := persistMappings.Conn.Expire(ctx, userId, 20 * time.Second).Err(); err != nil {
60+
log.Printf("error setting expiration for persisting room storage: %s", err.Error())
61+
} else {
62+
log.Printf("expiration set for 10 minutes for user %s: ", userId)
63+
64+
}
65+
*/
5766
return true
5867
}

comms/server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ io.on("connection", (socket) => {
2727
socket.to(data.target).emit("peerConnected");
2828
});
2929

30+
// leave the room - this is performed as part of a cleanup function.
31+
socket.on("leaveRoom", (data) => {
32+
socket.leave(data.target);
33+
});
34+
3035
// propagate the socket events for starting and handshaking a call forward.
3136
socket.on("startCall", (data) => {
3237
console.log(socket.id + " is starting call in "+ data.target);

peerprep/app/questions/[question]/[roomID]/question.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ interface Props {
1212
authToken?: string;
1313
userId?: string;
1414
matchHash?: string;
15-
1615
}
1716

1817
interface DifficultyChipProps {
@@ -29,7 +28,13 @@ function DifficultyChip({ diff }: DifficultyChipProps) {
2928
);
3029
}
3130

32-
function QuestionBlock({ question, roomID, authToken, userId, matchHash }: Props) {
31+
function QuestionBlock({
32+
question,
33+
roomID,
34+
authToken,
35+
userId,
36+
matchHash,
37+
}: Props) {
3338
return (
3439
<>
3540
<div className={styles.qn_container}>
@@ -64,7 +69,6 @@ function QuestionBlock({ question, roomID, authToken, userId, matchHash }: Props
6469
</div>
6570
<div className={styles.editor_container}>
6671
<CollabEditor
67-
question={question}
6872
roomID={roomID}
6973
authToken={authToken}
7074
matchHash={matchHash}

peerprep/app/questions/[question]/question.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function QuestionBlock({ question }: Props) {
8484
}
8585
</div>
8686
<div className={styles.editor_container}>
87-
<CollabEditor question={question} />
87+
<CollabEditor />
8888
</div>
8989
</>
9090
);

0 commit comments

Comments
 (0)