Skip to content

Commit 3657584

Browse files
committed
start to refonte ws collaboration
1 parent 502d18e commit 3657584

2 files changed

Lines changed: 30 additions & 62 deletions

File tree

src/assets/ts/composables/useRoom.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ async function useRoom (roomId: string)
7272
note,
7373
owner: note.user_id,
7474

75-
share,
75+
share: {
76+
...share,
77+
params: {
78+
...share.params,
79+
passwd: undefined
80+
}
81+
},
7682

7783
ydoc,
7884
awareness,

src/wsocket/routes/collaboration.ts

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,44 @@
11
import { Server, Socket } from "socket.io";
2-
import notes from "../../assets/ts/notes.js";
32
import { Note } from "../../assets/ts/types.js";
43
import * as Y from "yjs";
54
import * as awarenessProtocol from "y-protocols/awareness";
6-
import Share from "../../assets/ts/db/share/Share.js";
5+
import useRoom from "../../assets/ts/composables/useRoom.js";
6+
import { clerkClient } from "@clerk/express";
77

88

99

10-
const save_note = async (note: Note): Promise<void> => {
11-
await notes.updateNote({
12-
...note,
13-
updated_at: new Date().getTime()
14-
});
15-
}
16-
17-
18-
const get_note = async (uuid: string): Promise<Note | undefined> => {
19-
const res = await notes.getNoteByUUIDNoUserID(uuid);
20-
if (res.note) return res.note;
21-
}
22-
23-
24-
const docs = new Map<string, {
25-
ydoc: Y.Doc,
26-
awareness: awarenessProtocol.Awareness,
27-
saveInterval?: NodeJS.Timeout,
28-
title: string,
29-
icon: string
30-
}>();
31-
32-
3310

3411
export default (io: Server, socket: Socket) => {
3512

36-
let currentRoom: string | null = null;
37-
38-
socket.on("join-room", async ({ room, userId }: { room: string, userId: string }) => {
13+
socket.on("join-room", async ({ room: roomId }: { room: string }) => {
3914

40-
if (!room) return;
41-
42-
currentRoom = room; // Stocker la room
43-
socket.join(`room:${room}`);
44-
45-
let docData = docs.get(room);
46-
const share = await Share.get(room);
47-
48-
if (!docData)
49-
{
50-
51-
const ydoc = new Y.Doc();
52-
const awareness = new awarenessProtocol.Awareness(ydoc);
53-
const note = await get_note(room);
54-
55-
const title = note?.title || "";
56-
const icon = note?.icon || "";
57-
58-
const saveInterval = setInterval(async () => {
59-
clearInterval(saveInterval);
60-
}, 10000);
61-
62-
docs.set(room, { ydoc, awareness, saveInterval, title, icon });
63-
docData = { ydoc, awareness, saveInterval, title, icon };
15+
if (!roomId) return;
16+
const userId = socket.data.userId;
17+
18+
const { room } = await useRoom(roomId);
6419

20+
if (userId !== room.owner && !room.share.visitor.includes(userId)) {
21+
socket.emit('error', 'Unauthorized');
22+
return;
6523
}
6624

67-
const { ydoc, awareness } = docData;
25+
socket.join('room:' + roomId);
6826

69-
const initialState = Y.encodeStateAsUpdate(ydoc);
70-
const stateArray = Array.from(initialState);
71-
socket.emit("sync", stateArray);
72-
73-
socket.emit("title-update", docData.title);
74-
socket.emit("icon-update", docData.icon);
27+
28+
socket.emit("initial-state", ({ room }));
7529

7630
if (userId)
7731
{
78-
if (share && userId == share.owner_id) return;
79-
socket.emit('new_user', userId);
32+
if (room.share && userId == room.share.owner_id) return;
33+
const user = await clerkClient.users.getUser(userId);
34+
socket.to('room:'+roomId).emit('user-join', { user: {
35+
id: user.id,
36+
username: user.username,
37+
firstName: user.firstName,
38+
lastName: user.lastName,
39+
fullname: user.fullName,
40+
imageUrl: user.imageUrl,
41+
} });
8042
}
8143

8244
});

0 commit comments

Comments
 (0)