mirror of
https://github.com/Discreetly/server.git
synced 2026-01-10 21:38:02 -05:00
updated /join and invite code stuff
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,5 @@
|
||||
node_modules
|
||||
dist
|
||||
dist/
|
||||
*.rdb
|
||||
*.log
|
||||
.env
|
||||
|
||||
@@ -28,12 +28,6 @@ export const rooms: RoomGroupI[] = [
|
||||
membership: { identityCommitments: [idcommitment_1, idcommitment_2, idcommitment_5] },
|
||||
rateLimit: 1000
|
||||
},
|
||||
{
|
||||
id: genId(serverID, '1EthRoom'),
|
||||
name: '1EthRoom',
|
||||
membership: { identityCommitments: [idcommitment_1, idcommitment_2, idcommitment_5] },
|
||||
rateLimit: 100
|
||||
},
|
||||
{
|
||||
id: genId(serverID, 'Test'),
|
||||
name: 'Test',
|
||||
@@ -47,6 +41,12 @@ export const rooms: RoomGroupI[] = [
|
||||
]
|
||||
},
|
||||
rateLimit: 10000
|
||||
},
|
||||
{
|
||||
id: genId(serverID, '1EthRoom'),
|
||||
name: '1EthRoom',
|
||||
membership: { identityCommitments: [] },
|
||||
rateLimit: 100
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -56,24 +56,7 @@ export const rooms: RoomGroupI[] = [
|
||||
{
|
||||
id: genId(serverID, 'Devconnect 2023'),
|
||||
name: 'Devconnect 2023',
|
||||
membership: { identityCommitments: [idcommitment_1, idcommitment_3, idcommitment_5] },
|
||||
rateLimit: 1000
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'Games',
|
||||
rooms: [
|
||||
{
|
||||
id: genId(serverID, 'Gunfire Reborn'),
|
||||
name: 'Gunfire Reborn',
|
||||
membership: { identityCommitments: [idcommitment_2, idcommitment_3, idcommitment_4] },
|
||||
rateLimit: 1000
|
||||
},
|
||||
{
|
||||
id: genId(serverID, 'Halo Infinite'),
|
||||
name: 'Halo Infinite',
|
||||
membership: { identityCommitments: [idcommitment_2, idcommitment_3, idcommitment_4] },
|
||||
membership: { identityCommitments: [] },
|
||||
rateLimit: 1000
|
||||
}
|
||||
]
|
||||
|
||||
31
dist/config/rooms.js
vendored
31
dist/config/rooms.js
vendored
@@ -28,12 +28,6 @@ exports.rooms = [
|
||||
membership: { identityCommitments: [idcommitment_1, idcommitment_2, idcommitment_5] },
|
||||
rateLimit: 1000
|
||||
},
|
||||
{
|
||||
id: (0, discreetly_interfaces_1.genId)(serverID, '1EthRoom'),
|
||||
name: '1EthRoom',
|
||||
membership: { identityCommitments: [idcommitment_1, idcommitment_2, idcommitment_5] },
|
||||
rateLimit: 100
|
||||
},
|
||||
{
|
||||
id: (0, discreetly_interfaces_1.genId)(serverID, 'Test'),
|
||||
name: 'Test',
|
||||
@@ -47,6 +41,12 @@ exports.rooms = [
|
||||
]
|
||||
},
|
||||
rateLimit: 10000
|
||||
},
|
||||
{
|
||||
id: (0, discreetly_interfaces_1.genId)(serverID, '1EthRoom'),
|
||||
name: '1EthRoom',
|
||||
membership: { identityCommitments: [] },
|
||||
rateLimit: 100
|
||||
}
|
||||
]
|
||||
},
|
||||
@@ -56,24 +56,7 @@ exports.rooms = [
|
||||
{
|
||||
id: (0, discreetly_interfaces_1.genId)(serverID, 'Devconnect 2023'),
|
||||
name: 'Devconnect 2023',
|
||||
membership: { identityCommitments: [idcommitment_1, idcommitment_3, idcommitment_5] },
|
||||
rateLimit: 1000
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
name: 'Games',
|
||||
rooms: [
|
||||
{
|
||||
id: (0, discreetly_interfaces_1.genId)(serverID, 'Gunfire Reborn'),
|
||||
name: 'Gunfire Reborn',
|
||||
membership: { identityCommitments: [idcommitment_2, idcommitment_3, idcommitment_4] },
|
||||
rateLimit: 1000
|
||||
},
|
||||
{
|
||||
id: (0, discreetly_interfaces_1.genId)(serverID, 'Halo Infinite'),
|
||||
name: 'Halo Infinite',
|
||||
membership: { identityCommitments: [idcommitment_2, idcommitment_3, idcommitment_4] },
|
||||
membership: { identityCommitments: [] },
|
||||
rateLimit: 1000
|
||||
}
|
||||
]
|
||||
|
||||
39
dist/src/server.js
vendored
39
dist/src/server.js
vendored
@@ -48,18 +48,16 @@ redisClient.get('ccm').then(function (cc) {
|
||||
}
|
||||
}
|
||||
});
|
||||
redisClient.get('ccm').then(function (res) { return console.log(res); });
|
||||
redisClient.get('rooms').then(function (rooms) {
|
||||
rooms = JSON.parse(rooms);
|
||||
if (rooms) {
|
||||
console.log('Rooms', rooms);
|
||||
loadedRooms = rooms;
|
||||
}
|
||||
else {
|
||||
loadedRooms = rooms_1.rooms;
|
||||
redisClient.set('rooms', JSON.stringify(loadedRooms));
|
||||
}
|
||||
console.log('Loaded Rooms:', loadedRooms);
|
||||
console.log('Rooms:', JSON.stringify(loadedRooms, null, 2));
|
||||
});
|
||||
redisClient.on('error', function (err) { return console.log('Redis Client Error', err); });
|
||||
io.on('connection', function (socket) {
|
||||
@@ -100,26 +98,33 @@ app.get('/api/rooms', function (req, res) {
|
||||
app.get('/api/rooms/:id', function (req, res) {
|
||||
// TODO This should return the room info for the given room ID
|
||||
console.log('fetching room info', req.params.id);
|
||||
var room = loadedRooms.flatMap(function (rooms) { return rooms.rooms; }).filter(function (room) { return room.id === req.params.id; });
|
||||
var room = loadedRooms
|
||||
.flatMap(function (rooms) { return rooms.rooms; })
|
||||
.filter(function (room) { return room.id === req.params.id; });
|
||||
res.json(room);
|
||||
});
|
||||
// TODO api endpoint that creates new rooms and generates invite codes for them
|
||||
app.post('/join', function (req, res) {
|
||||
var _a = req.body, code = _a.code, idc = _a.idc;
|
||||
var data = req.body;
|
||||
console.log(data);
|
||||
var code = data.code, idc = data.idc;
|
||||
console.log('claiming code:', code, 'with identityCommitment', idc);
|
||||
var result = ccm.claimCode(code);
|
||||
// fake id for groupID's of loadedRooms - will replace when groupID's have real ID's
|
||||
var id = "11265330281159962366877930944095553344292465623956771902429854381297987195502";
|
||||
if (result.status === 'CLAIMED') {
|
||||
(0, utils_1.getGroupId)(code);
|
||||
redisClient.set('ccm', JSON.stringify(ccm.getClaimCodeSets()));
|
||||
(0, utils_1.addIdentityToRoom)(id, idc);
|
||||
console.log('Code claimed');
|
||||
var groupID = result.groupID;
|
||||
(0, utils_1.addIdentityToRoom)(groupID, idc);
|
||||
console.log('Code claimed:', code);
|
||||
res.status(200).json({ groupID: groupID });
|
||||
}
|
||||
else {
|
||||
console.error('Code already claimed');
|
||||
res.status(451).json({ status: 'invalid' });
|
||||
}
|
||||
res.status(200).json({ code: code });
|
||||
});
|
||||
app.get('/logclaimcodes', function (req, res) {
|
||||
console.log('-----CLAIMCODES-----');
|
||||
console.log(JSON.stringify(ccm.getClaimCodeSets(), null, 2));
|
||||
console.log('-----ENDOFCODES-----');
|
||||
});
|
||||
app.listen(http_port, function () {
|
||||
console.log("Http Server is running at http://localhost:".concat(http_port));
|
||||
@@ -127,8 +132,8 @@ app.listen(http_port, function () {
|
||||
socket_server.listen(socket_port, function () {
|
||||
console.log("Socket Server is running at http://localhost:".concat(socket_port));
|
||||
});
|
||||
// // Disconnect from redis on exit
|
||||
// process.on('SIGINT', () => {
|
||||
// console.log('disconnecting redis');
|
||||
// redisClient.disconnect().then(process.exit());
|
||||
// });
|
||||
// Disconnect from redis on exit
|
||||
process.on('SIGINT', function () {
|
||||
console.log('disconnecting redis');
|
||||
redisClient.disconnect().then(process.exit());
|
||||
});
|
||||
|
||||
@@ -7,7 +7,9 @@ import { serverConfig, rooms as defaultRooms, rooms } from '../config/rooms';
|
||||
import { MessageI, RoomGroupI } from 'discreetly-interfaces';
|
||||
import verifyProof from './verifier';
|
||||
import ClaimCodeManager from 'discreetly-claimcodes';
|
||||
import { addIdentityToRoom, getGroupId } from './utils'
|
||||
import type { ClaimCodeStatus } from 'discreetly-claimcodes';
|
||||
import { addIdentityToRoom } from './utils';
|
||||
|
||||
// Deal with bigints in JSON
|
||||
(BigInt.prototype as any).toJSON = function () {
|
||||
return this.toString();
|
||||
@@ -59,18 +61,15 @@ redisClient.get('ccm').then((cc) => {
|
||||
}
|
||||
});
|
||||
|
||||
redisClient.get('ccm').then((res) => console.log(res));
|
||||
|
||||
redisClient.get('rooms').then((rooms) => {
|
||||
rooms = JSON.parse(rooms);
|
||||
if (rooms) {
|
||||
console.log('Rooms', rooms);
|
||||
loadedRooms = rooms as RoomGroupI[];
|
||||
} else {
|
||||
loadedRooms = defaultRooms;
|
||||
redisClient.set('rooms', JSON.stringify(loadedRooms));
|
||||
}
|
||||
console.log('Loaded Rooms:', loadedRooms);
|
||||
console.log('Rooms:', JSON.stringify(loadedRooms, null, 2));
|
||||
});
|
||||
|
||||
redisClient.on('error', (err) => console.log('Redis Client Error', err));
|
||||
@@ -123,28 +122,36 @@ app.get('/api/rooms', (req, res) => {
|
||||
app.get('/api/rooms/:id', (req, res) => {
|
||||
// TODO This should return the room info for the given room ID
|
||||
console.log('fetching room info', req.params.id);
|
||||
const room = loadedRooms.flatMap(rooms => rooms.rooms).filter(room => room.id === req.params.id)
|
||||
const room = loadedRooms
|
||||
.flatMap((rooms) => rooms.rooms)
|
||||
.filter((room) => room.id === req.params.id);
|
||||
res.json(room);
|
||||
});
|
||||
|
||||
// TODO api endpoint that creates new rooms and generates invite codes for them
|
||||
|
||||
app.post('/join', (req, res) => {
|
||||
const { code, idc } = req.body;
|
||||
const data = req.body;
|
||||
console.log(data);
|
||||
const { code, idc } = data;
|
||||
console.log('claiming code:', code, 'with identityCommitment', idc);
|
||||
const result = ccm.claimCode(code);
|
||||
const result: ClaimCodeStatus = ccm.claimCode(code);
|
||||
|
||||
// fake id for groupID's of loadedRooms - will replace when groupID's have real ID's
|
||||
const id = "11265330281159962366877930944095553344292465623956771902429854381297987195502"
|
||||
if (result.status === 'CLAIMED') {
|
||||
getGroupId(code);
|
||||
redisClient.set('ccm', JSON.stringify(ccm.getClaimCodeSets()));
|
||||
addIdentityToRoom(id, idc);
|
||||
console.log('Code claimed');
|
||||
const groupID = result.groupID;
|
||||
addIdentityToRoom(groupID, idc);
|
||||
console.log('Code claimed:', code);
|
||||
res.status(200).json({ groupID });
|
||||
} else {
|
||||
console.error('Code already claimed');
|
||||
res.status(451).json({ status: 'invalid' });
|
||||
}
|
||||
res.status(200).json({ code });
|
||||
});
|
||||
|
||||
app.get('/logclaimcodes', (req, res) => {
|
||||
console.log('-----CLAIMCODES-----');
|
||||
console.log(JSON.stringify(ccm.getClaimCodeSets(), null, 2));
|
||||
console.log('-----ENDOFCODES-----');
|
||||
});
|
||||
|
||||
app.listen(http_port, () => {
|
||||
@@ -155,8 +162,8 @@ socket_server.listen(socket_port, () => {
|
||||
console.log(`Socket Server is running at http://localhost:${socket_port}`);
|
||||
});
|
||||
|
||||
// // Disconnect from redis on exit
|
||||
// process.on('SIGINT', () => {
|
||||
// console.log('disconnecting redis');
|
||||
// redisClient.disconnect().then(process.exit());
|
||||
// });
|
||||
// Disconnect from redis on exit
|
||||
process.on('SIGINT', () => {
|
||||
console.log('disconnecting redis');
|
||||
redisClient.disconnect().then(process.exit());
|
||||
});
|
||||
|
||||
28
src/utils.ts
28
src/utils.ts
@@ -4,33 +4,19 @@ const redisClient = createClient();
|
||||
redisClient.connect();
|
||||
|
||||
export const addIdentityToRoom = (groupId, IdentityCommitment) => {
|
||||
redisClient.get('rooms').then(res => {
|
||||
redisClient.get('rooms').then((res) => {
|
||||
const data = JSON.parse(res);
|
||||
const roomIndex = data[0].rooms.findIndex(room => room.id === groupId);
|
||||
const roomIndex = data[0].rooms.findIndex((room) => room.id === groupId);
|
||||
const roomToAdd = data[0].rooms[roomIndex];
|
||||
// console.log(roomToAdd.membership.identityCommitments.find(identity => identity === idc));
|
||||
if (!roomToAdd.membership.identityCommitments.find(identity => identity === IdentityCommitment)) {
|
||||
if (
|
||||
!roomToAdd.membership.identityCommitments.find((identity) => identity === IdentityCommitment)
|
||||
) {
|
||||
roomToAdd.membership.identityCommitments.push(IdentityCommitment);
|
||||
const updatedRooms = JSON.stringify(data);
|
||||
redisClient.set('rooms', updatedRooms)
|
||||
console.log('Successfully added identity');
|
||||
redisClient.set('rooms', updatedRooms);
|
||||
} else {
|
||||
console.log('Identity already exists in room');
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export const getGroupId = (claimCode) => {
|
||||
redisClient.get('ccm').then(cc => {
|
||||
const data = JSON.parse(cc);
|
||||
for (const group in data) {
|
||||
const codes = data[group].claimCodes;
|
||||
for (const claim of codes) {
|
||||
if (claim.code === claimCode) {
|
||||
console.log("GROUPID", data[group].groupID);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"target": "ES5",
|
||||
"module": "ES6"
|
||||
"target": "ES2020",
|
||||
"module": "ES2020"
|
||||
},
|
||||
"files": [
|
||||
"./src/server.ts"
|
||||
|
||||
Reference in New Issue
Block a user