Skip to content

Commit b3019c7

Browse files
authored
Merge pull request #44 from CS3219-AY2425S1/PEER-232-Match-Initiation-UI
PEER-232: Match Initiation UI
2 parents b6e8d5b + 3b32837 commit b3019c7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+2167
-359
lines changed

.eslintrc.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
"ecmaVersion": "latest",
1212
"sourceType": "module"
1313
},
14-
"plugins": ["import", "unused-imports", "react", "@typescript-eslint", "tailwindcss", "simple-import-sort"],
14+
"plugins": [
15+
"import",
16+
"unused-imports",
17+
"react",
18+
"@typescript-eslint",
19+
"tailwindcss",
20+
"react-hooks",
21+
"simple-import-sort"
22+
],
1523
"rules": {
1624
"@typescript-eslint/no-explicit-any": "warn",
1725
"@typescript-eslint/explicit-module-boundary-types": "off",
@@ -95,4 +103,4 @@
95103
}
96104
}
97105
]
98-
}
106+
}

backend/collaboration/src/controller/collab-controller.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ export async function getCollabRoom(req: Request, res: Response) {
1111
userid2: userid2 as string,
1212
questionid: questionid as string,
1313
};
14+
1415
try {
1516
const result = await getCollabRoomService(payload);
17+
1618
if (result.error) {
1719
return res.status(result.code).json({
1820
error: result.error.message ?? 'An error occurred',
1921
});
2022
}
23+
2124
return res.status(result.code).json(result.data);
2225
} catch (err) {
2326
return res
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './utils';
21
export * from './persistence';
2+
export * from './utils';

backend/collaboration/src/lib/y-postgres/persistence.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import * as Y from 'yjs';
21
import { PostgresqlPersistence } from 'y-postgresql';
2+
import * as Y from 'yjs';
33

4-
import type { IWSSharedDoc } from '@/types/interfaces';
54
import { dbConfig } from '@/config';
5+
import type { IWSSharedDoc } from '@/types/interfaces';
66

77
import { setPersistence } from './utils';
88

backend/collaboration/src/lib/y-postgres/utils.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import * as awarenessProtocol from 'y-protocols/awareness';
2-
import * as syncProtocol from 'y-protocols/sync';
3-
41
import * as decoding from 'lib0/decoding';
52
import * as encoding from 'lib0/encoding';
63
import * as map from 'lib0/map';
4+
import * as awarenessProtocol from 'y-protocols/awareness';
5+
import * as syncProtocol from 'y-protocols/sync';
76

87
import type { IPersistence, IWSSharedDoc } from '@/types/interfaces';
98

@@ -38,9 +37,11 @@ export const getYDoc = (docname: string, gc = true) =>
3837
map.setIfUndefined(docs, docname, () => {
3938
const doc = new WSSharedDoc(docname);
4039
doc.gc = gc;
40+
4141
if (persistence !== null) {
4242
persistence.bindState(docname, doc);
4343
}
44+
4445
docs.set(docname, doc);
4546
return doc;
4647
});
@@ -50,6 +51,7 @@ const messageListener = (conn: any, doc: IWSSharedDoc, message: Uint8Array) => {
5051
const encoder = encoding.createEncoder();
5152
const decoder = decoding.createDecoder(message);
5253
const messageType = decoding.readVarUint(decoder);
54+
5355
switch (messageType) {
5456
case messageSync:
5557
encoding.writeVarUint(encoder, messageSync);
@@ -61,7 +63,9 @@ const messageListener = (conn: any, doc: IWSSharedDoc, message: Uint8Array) => {
6163
if (encoding.length(encoder) > 1) {
6264
send(doc, conn, encoding.toUint8Array(encoder));
6365
}
66+
6467
break;
68+
6569
case messageAwareness: {
6670
awarenessProtocol.applyAwarenessUpdate(
6771
doc.awareness,
@@ -80,9 +84,11 @@ const closeConn = (doc: IWSSharedDoc, conn: any) => {
8084
if (doc.conns.has(conn)) {
8185
const controlledIds = doc.conns.get(conn);
8286
doc.conns.delete(conn);
87+
8388
if (controlledIds) {
8489
awarenessProtocol.removeAwarenessStates(doc.awareness, Array.from(controlledIds), null);
8590
}
91+
8692
if (doc.conns.size === 0 && persistence !== null) {
8793
// if persisted, we store state and destroy ydocument
8894
persistence.writeState(doc.name, doc).then(() => {
@@ -91,13 +97,15 @@ const closeConn = (doc: IWSSharedDoc, conn: any) => {
9197
docs.delete(doc.name);
9298
}
9399
}
100+
94101
conn.close();
95102
};
96103

97104
export const send = (doc: IWSSharedDoc, conn: any, m: Uint8Array) => {
98105
if (conn.readyState !== wsReadyStateConnecting && conn.readyState !== wsReadyStateOpen) {
99106
closeConn(doc, conn);
100107
}
108+
101109
try {
102110
conn.send(m, (err: any) => {
103111
if (err !== null) {
@@ -128,9 +136,11 @@ export const setupWSConnection = (
128136
if (doc.conns.has(conn)) {
129137
closeConn(doc, conn);
130138
}
139+
131140
clearInterval(pingInterval);
132141
} else if (doc.conns.has(conn)) {
133142
pongReceived = false;
143+
134144
try {
135145
conn.ping();
136146
} catch (e) {
@@ -146,6 +156,7 @@ export const setupWSConnection = (
146156
conn.on('pong', () => {
147157
pongReceived = true;
148158
});
159+
149160
// put the following in a variables in a block so the interval handlers don't keep in in
150161
// scope
151162
{
@@ -155,6 +166,7 @@ export const setupWSConnection = (
155166
syncProtocol.writeSyncStep1(encoder, doc);
156167
send(doc, conn, encoding.toUint8Array(encoder));
157168
const awarenessStates = doc.awareness.getStates();
169+
158170
if (awarenessStates.size > 0) {
159171
const encoder = encoding.createEncoder();
160172
encoding.writeVarUint(encoder, messageAwareness);

backend/collaboration/src/lib/y-postgres/ws-shared-doc.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import * as Y from 'yjs';
2-
import * as syncProtocol from 'y-protocols/sync';
3-
import * as awarenessProtocol from 'y-protocols/awareness';
4-
51
import * as encoding from 'lib0/encoding';
2+
import * as awarenessProtocol from 'y-protocols/awareness';
3+
import * as syncProtocol from 'y-protocols/sync';
4+
import * as Y from 'yjs';
65

7-
import type { IWSSharedDoc } from '@/types/interfaces';
86
import { GC_ENABLED } from '@/config';
7+
import type { IWSSharedDoc } from '@/types/interfaces';
8+
99
import { messageAwareness, messageSync } from './constants';
1010
import { send } from './utils';
1111

@@ -44,8 +44,10 @@ export class WSSharedDoc extends Y.Doc implements IWSSharedDoc {
4444
conn: object | null
4545
) => {
4646
const changedClients = added.concat(updated, removed);
47+
4748
if (conn !== null) {
4849
const connControlledIDs = /** @type {Set<number>} */ this.conns.get(conn);
50+
4951
if (connControlledIDs !== undefined) {
5052
added.forEach((clientID) => {
5153
connControlledIDs.add(clientID);
@@ -55,6 +57,7 @@ export class WSSharedDoc extends Y.Doc implements IWSSharedDoc {
5557
});
5658
}
5759
}
60+
5861
// broadcast awareness update
5962
const encoder = encoding.createEncoder();
6063
encoding.writeVarUint(encoder, messageAwareness);
@@ -67,6 +70,7 @@ export class WSSharedDoc extends Y.Doc implements IWSSharedDoc {
6770
send(this, c, buff);
6871
});
6972
};
73+
7074
this.awareness.on('update', awarenessChangeHandler);
7175
this.on('update', updateHandler);
7276
}

backend/collaboration/src/routes/room.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { getCollabRoom } from '@/controller/collab-controller';
21
import express from 'express';
32

3+
import { getCollabRoom } from '@/controller/collab-controller';
4+
45
const router = express.Router();
56

67
router.get('/', getCollabRoom);

backend/collaboration/src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import http from 'http';
12
import { exit } from 'process';
23

34
import cors from 'cors';
4-
import http from 'http';
55
import express, { json } from 'express';
66
import { StatusCodes } from 'http-status-codes';
77
import pino from 'pino-http';
@@ -10,6 +10,7 @@ import { UI_HOST } from '@/config';
1010
import { config, db } from '@/lib/db';
1111
import { logger } from '@/lib/utils';
1212
import roomRoutes from '@/routes/room';
13+
1314
import { setUpWSServer } from './ws';
1415

1516
const app = express();

backend/collaboration/src/service/get/collab-get-service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import crypto from 'crypto';
2+
13
import { StatusCodes } from 'http-status-codes';
4+
25
import { IGetCollabRoomPayload, IGetCollabRoomResponse } from './types';
3-
import crypto from 'crypto';
46

57
export async function getCollabRoomService(
68
payload: IGetCollabRoomPayload
79
): Promise<IGetCollabRoomResponse> {
810
const { userid1, userid2, questionid } = payload;
11+
912
if (!userid1 || !userid2 || !questionid) {
1013
return {
1114
code: StatusCodes.UNPROCESSABLE_ENTITY,
@@ -14,6 +17,7 @@ export async function getCollabRoomService(
1417
},
1518
};
1619
}
20+
1721
const combinedString = `uid1=${userid1}|uid2=${userid2}|qid=${questionid}`;
1822
const hash = crypto.createHash('sha256');
1923
const uniqueRoomName = hash.update(combinedString).digest('hex');

backend/collaboration/src/ws.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import http from 'http';
2+
23
import { WebSocketServer } from 'ws';
34

45
import { setUpPersistence, setupWSConnection } from '@/lib/y-postgres';

0 commit comments

Comments
 (0)