From 99467f229c10b3725b280f71609eff38d6d3e6bf Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 12 Mar 2021 17:53:50 +0530 Subject: [PATCH 01/24] implemented per-stream data views --- src/index.js | 23 +++++++++++++++---- src/models/casters.js | 2 +- src/models/events.js | 4 ++-- src/models/hosts.js | 2 +- src/models/matches.js | 2 +- src/models/rosters.js | 5 ---- src/models/streams.js | 14 ++++++++---- src/ws/crud.js | 24 ++++++++++---------- src/ws/handler.js | 9 +++++--- src/ws/streams.js | 53 +++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 105 insertions(+), 33 deletions(-) create mode 100644 src/ws/streams.js diff --git a/src/index.js b/src/index.js index 9b7e7d7..3c37c27 100644 --- a/src/index.js +++ b/src/index.js @@ -4,6 +4,7 @@ const url = require('url') const app = require('./app') const { get } = require('./uuids') const handler = require('./ws/handler') +const { subscribe } = require('./ws/streams') mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, @@ -15,15 +16,24 @@ db.once('open', () => console.log('Connected to mongodb instance!')) const port = process.env.PORT || 5000; -wss = new WebSocket.Server({ +wss1 = new WebSocket.Server({ + noServer: true, +}) + +wss2 = new WebSocket.Server({ noServer: true, }) -wss.on('connection', (ws, req) => { +wss1.on('connection', (ws, req) => { const id = req.id handler(ws, id) }) +wss2.on('connection', (ws, req) => { + const id = req.id + subscribe(ws, id) +}) + const server = app.listen(port, () => { console.log(`Express listening: http://localhost:${port}`) }) @@ -32,9 +42,14 @@ server.on('upgrade', (req, socket, head) => { const pathname = url.parse(req.url).pathname.split('/') const token = pathname[2] if (pathname[1] === 'ws' && get(token)) { - wss.handleUpgrade(req, socket, head, (socket) => { + wss1.handleUpgrade(req, socket, head, (socket) => { + req.id = token + wss1.emit('connection', socket, req); + }) + } else if (pathname[1] === 'stream') { + wss2.handleUpgrade(req, socket, head, (socket) => { req.id = token - wss.emit('connection', socket, req); + wss2.emit('connection', socket, req); }) } }) \ No newline at end of file diff --git a/src/models/casters.js b/src/models/casters.js index 112fa9d..42e659d 100644 --- a/src/models/casters.js +++ b/src/models/casters.js @@ -16,7 +16,7 @@ const casters = new Schema({ image: { type: String, required: true, - }, + } }); const Caster = model('Casters', casters) diff --git a/src/models/events.js b/src/models/events.js index 6a3ae5c..a51457c 100644 --- a/src/models/events.js +++ b/src/models/events.js @@ -5,9 +5,9 @@ const events = new mongoose.Schema({ type: String, required: true, unique: true, - }, + } }); -const Event = mongoose.model('Events', events) +const Event = mongoose.model('Event', events) module.exports = Event \ No newline at end of file diff --git a/src/models/hosts.js b/src/models/hosts.js index 72a05a0..83e1d7e 100644 --- a/src/models/hosts.js +++ b/src/models/hosts.js @@ -16,7 +16,7 @@ const hosts = new Schema({ image: { type: String, required: true, - }, + } }); const Host = model('Hosts', hosts) diff --git a/src/models/matches.js b/src/models/matches.js index 0fcb5f1..fc4b978 100644 --- a/src/models/matches.js +++ b/src/models/matches.js @@ -51,7 +51,7 @@ const matches = new Schema({ games: { type: [gameScores], required: true, - }, + } }) const Match = model('Matches', matches) diff --git a/src/models/rosters.js b/src/models/rosters.js index 63c7210..5289397 100644 --- a/src/models/rosters.js +++ b/src/models/rosters.js @@ -39,11 +39,6 @@ const players = new Schema({ }) const rosters = new Schema({ - event: { - type: Schema.Types.ObjectId, - ref: 'Events', - required: true, - }, name: { type: String, required: true, diff --git a/src/models/streams.js b/src/models/streams.js index 6bbd74d..5f7b87d 100644 --- a/src/models/streams.js +++ b/src/models/streams.js @@ -7,13 +7,19 @@ const streams = new Schema({ }, event: { type: Schema.Types.ObjectId, - refs: 'Events', - requried: true, + ref: 'Event', }, matches: { type: [Schema.Types.ObjectId], - refs: 'matches', - requried: true, + ref: 'Matches', + }, + casters: { + type: [Schema.Types.ObjectId], + ref: 'Casters', + }, + hosts: { + type: [Schema.Types.ObjectId], + refs: 'Hosts', }, }) diff --git a/src/ws/crud.js b/src/ws/crud.js index d8becd2..5acd3c7 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -7,10 +7,10 @@ const hosts = require('../models/hosts') const eventFns = { getAll: async () => { - return await events.find().exec() + return await events.find().populate('streams').exec() }, getById: async (id) => { - return await events.findById(id).exec() + return await events.findById(id).populate('streams').exec() }, update: async(id, data) => { return await events.findByIdAndUpdate(id, data).exec() @@ -33,10 +33,10 @@ const eventFns = { const rosterFns = { getAll: async () => { - return await rosters.find().exec() + return await rosters.find().populate('event').populate('match').exec() }, getById: async (id) => { - return await rosters.findById(id).exec() + return await rosters.findById(id).populate('event').populate('match').exec() }, update: async(id, data) => { return await rosters.findByIdAndUpdate(id, data).exec() @@ -59,10 +59,10 @@ const rosterFns = { const matchFns = { getAll: async () => { - return await matches.find().exec() + return await matches.find().populate('orange').populate('blue').populate('stream').exec() }, getById: async (id) => { - return await matches.findById(id).exec() + return await matches.findById(id).populate('orange').populate('blue').populate('stream').exec() }, update: async(id, data) => { return await matches.findByIdAndUpdate(id, data).exec() @@ -85,10 +85,10 @@ const matchFns = { const streamFns = { getAll: async () => { - return await streams.find().exec() + return await streams.find().populate('event').populate('matches').populate('casters').populate('hosts').exec() }, getById: async (id) => { - return await streams.findById(id).exec() + return await streams.findById(id).populate('event').populate('matches').populate('casters').populate('hosts').exec() }, update: async(id, data) => { return await streams.findByIdAndUpdate(id, data).exec() @@ -111,10 +111,10 @@ const streamFns = { const casterFns = { getAll: async () => { - return await casters.find().exec() + return await casters.find().populate('streams').exec() }, getById: async (id) => { - return await casters.findById(id).exec() + return await casters.findById(id).populate('streams').exec() }, update: async(id, data) => { return await casters.findByIdAndUpdate(id, data).exec() @@ -137,10 +137,10 @@ const casterFns = { const hostFns = { getAll: async () => { - return await hosts.find().exec() + return await hosts.find().populate('streams').exec() }, getById: async (id) => { - return await hosts.findById(id).exec() + return await hosts.findById(id).populate('streams').exec() }, update: async(id, data) => { return await hosts.findByIdAndUpdate(id, data).exec() diff --git a/src/ws/handler.js b/src/ws/handler.js index 339507b..d741e05 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -12,7 +12,7 @@ const handler = (ws, id) => { ws.send(JSON.stringify({ event: 'info', - data: 'Welcome to APL Nuke v1.0.0!' + data: 'Welcome to APL Nuke v1.1.0!' })) ws.on('message', (msg) => handleMsg(msg, id)) @@ -36,11 +36,14 @@ const handleMsg = async (msg, id) => { console.log(`received event for ${channel} with data %s`, d.data) switch (event) { case 'create': - await crud[channel].create(d.data) + const da = await crud[channel].create(d.data) + connections[id].connection.send(JSON.stringify(da)) + fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll()) break; case 'update': - await crud[channel].update(d.data.id, d.data.data) + const du = await crud[channel].update(d.data.id, d.data.data) + connections[id].connection.send(JSON.stringify(du)) fanoutMsg(channel, await crud[channel].getAll()) break; case 'delete': diff --git a/src/ws/streams.js b/src/ws/streams.js new file mode 100644 index 0000000..a14b7a8 --- /dev/null +++ b/src/ws/streams.js @@ -0,0 +1,53 @@ +const { v4 } = require('uuid') +const crud = require('./crud') + +const connections = {} + +const subscribe = (ws, sid) => { + const id = v4() + connections[id] = { + connection: ws, + streamid: sid, + } + + ws.send(JSON.stringify({ + event: 'info', + data: 'Welcome to APL Nuke v1.1.0!', + })) + + sendInitial(id) + + ws.on('close', () => { + delete connections[id] + }) +} + +const cModel = { + 'events': 'event', + 'matches': 'matches', + 'casters': 'casters', + 'hosts': 'hosts', +} + +const recvUpdate = async (channel, data) => { + const streams = await crud['streams'].getAll() + const stream = streams.filter(x => x[cModel[channel]]._id === data._id)[0] + const c = connections.filter(x => x.streamid === stream._id) + Object.keys(c).forEach((k) => { + c[k].connection.send(JSON.stringify(stream)) + }) +} + +const sendInitial = async (id) => { + const c = connections[id] + const stream = await crud['streams'].getById(c.streamid) + c.connection.send(JSON.stringify({ + event: 'streams:read', + data: stream, + })) +} + +module.exports = { + subscribe, + recvUpdate, +} \ No newline at end of file From 2064f0036fa878e400328b65a9862f4ce78f8f08 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sat, 13 Mar 2021 23:28:17 +0530 Subject: [PATCH 02/24] change stats to per-team instead of per-player --- src/index.js | 1 + src/models/matches.js | 5 ++--- src/models/rosters.js | 18 ++++++++---------- src/ws/crud.js | 8 ++++---- src/ws/handler.js | 23 +++++++++++++++++------ src/ws/streams.js | 7 ++----- 6 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/index.js b/src/index.js index 3c37c27..de98a56 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ const { subscribe } = require('./ws/streams') mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true, + useFindAndModify: false, }) const db = mongoose.connection diff --git a/src/models/matches.js b/src/models/matches.js index fc4b978..3474059 100644 --- a/src/models/matches.js +++ b/src/models/matches.js @@ -3,11 +3,11 @@ const { Schema, model } = require('mongoose') const gameScores = new Schema({ orange: { type: Number, - required: true, + default: 0, }, blue: { type: Number, - required: true, + default : 0, }, }) @@ -50,7 +50,6 @@ const matches = new Schema({ }, games: { type: [gameScores], - required: true, } }) diff --git a/src/models/rosters.js b/src/models/rosters.js index 5289397..6241d7b 100644 --- a/src/models/rosters.js +++ b/src/models/rosters.js @@ -3,23 +3,23 @@ const { Schema, model } = require('mongoose') const stats = new Schema({ goals: { type: Number, - required: true, + default: 0, }, assists: { type: Number, - required: true, + default: 0, }, saves: { type: Number, - required: true, + default: 0, }, shots: { type: Number, - required: true, + default: 0, }, demos: { type: Number, - required: true, + default: 0, }, }) @@ -32,10 +32,6 @@ const players = new Schema({ type: String, required: true, }, - stats: { - type: [stats], - required: true, - }, }) const rosters = new Schema({ @@ -49,8 +45,10 @@ const rosters = new Schema({ }, players: { type: [players], - required: true, }, + stats: { + type: stats, + } }) const Roster = model('Rosters', rosters) diff --git a/src/ws/crud.js b/src/ws/crud.js index 5acd3c7..bf5f9fb 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -33,10 +33,10 @@ const eventFns = { const rosterFns = { getAll: async () => { - return await rosters.find().populate('event').populate('match').exec() + return await rosters.find().exec() }, getById: async (id) => { - return await rosters.findById(id).populate('event').populate('match').exec() + return await rosters.findById(id).exec() }, update: async(id, data) => { return await rosters.findByIdAndUpdate(id, data).exec() @@ -49,10 +49,10 @@ const rosterFns = { const ev = new rosters(data) ev.save((err) => { if (err) - throw err; + throw err }) } catch (e) { - throw e; + throw e } } } diff --git a/src/ws/handler.js b/src/ws/handler.js index d741e05..43009bf 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -1,6 +1,7 @@ const { pop } = require('../uuids') const { channelEvents } = require('./channels') const crud = require('./crud') +const { recvUpdate } = require('./streams') const connections = {}; @@ -28,26 +29,32 @@ const handleMsg = async (msg, id) => { if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) { const channel = d.subscribe.split(':')[0] connections[id].events.push(d.subscribe) - connections[id].connection.send(JSON.stringify(await crud[channel].getAll())) + const dm = { + event: `${channel}:read`, + data: await crud[channel].getAll(), + } + connections[id].connection.send(JSON.stringify(dm)) } else if (d.hasOwnProperty('event') && channelEvents.indexOf(d.event) !== -1) { const ev = d.event.split(':') const channel = ev[0] const event = ev[1] console.log(`received event for ${channel} with data %s`, d.data) + let dm; switch (event) { case 'create': - const da = await crud[channel].create(d.data) - connections[id].connection.send(JSON.stringify(da)) + dm = await crud[channel].create(d.data) + recvUpdate(channel, dm) fanoutMsg(channel, await crud[channel].getAll()) fanoutMsg(channel, await crud[channel].getAll()) break; case 'update': - const du = await crud[channel].update(d.data.id, d.data.data) - connections[id].connection.send(JSON.stringify(du)) + dm = await crud[channel].update(d.data.id, d.data.data) + recvUpdate(channel, dm) fanoutMsg(channel, await crud[channel].getAll()) break; case 'delete': await crud[channel].delete(d.data.id) + recvUpdate(channel, 'delete') fanoutMsg(channel, await crud[channel].getAll()) break; } @@ -60,7 +67,11 @@ const handleMsg = async (msg, id) => { const fanoutMsg = (channel, data) => { Object.keys(connections).forEach((k) => { if (connections[k].events.indexOf(`${channel}:read`) !== -1) { - connections[k].connection.send(JSON.stringify(data)) + const d = { + event: `${channel}:read`, + data, + } + connections[k].connection.send(JSON.stringify(d)) } }) } diff --git a/src/ws/streams.js b/src/ws/streams.js index a14b7a8..a65464c 100644 --- a/src/ws/streams.js +++ b/src/ws/streams.js @@ -30,11 +30,8 @@ const cModel = { } const recvUpdate = async (channel, data) => { - const streams = await crud['streams'].getAll() - const stream = streams.filter(x => x[cModel[channel]]._id === data._id)[0] - const c = connections.filter(x => x.streamid === stream._id) - Object.keys(c).forEach((k) => { - c[k].connection.send(JSON.stringify(stream)) + Object.keys(connections).forEach((k) => { + sendInitial(k) }) } From 4be96cf548a3d07cf7ab57b53ce34696c8c48f53 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 04:35:52 +0530 Subject: [PATCH 03/24] remove required attr for caster/host image --- src/models/casters.js | 1 - src/models/hosts.js | 1 - 2 files changed, 2 deletions(-) diff --git a/src/models/casters.js b/src/models/casters.js index 42e659d..f47ab3f 100644 --- a/src/models/casters.js +++ b/src/models/casters.js @@ -15,7 +15,6 @@ const casters = new Schema({ }, image: { type: String, - required: true, } }); diff --git a/src/models/hosts.js b/src/models/hosts.js index 83e1d7e..19e4ead 100644 --- a/src/models/hosts.js +++ b/src/models/hosts.js @@ -15,7 +15,6 @@ const hosts = new Schema({ }, image: { type: String, - required: true, } }); From b4d4268e6e6b23a49fa6b3a8d886c168525639f3 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 04:39:08 +0530 Subject: [PATCH 04/24] fix remove populate --- src/ws/crud.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index bf5f9fb..c26aa87 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -111,10 +111,10 @@ const streamFns = { const casterFns = { getAll: async () => { - return await casters.find().populate('streams').exec() + return await casters.find().exec() }, getById: async (id) => { - return await casters.findById(id).populate('streams').exec() + return await casters.findById(id).exec() }, update: async(id, data) => { return await casters.findByIdAndUpdate(id, data).exec() @@ -137,10 +137,10 @@ const casterFns = { const hostFns = { getAll: async () => { - return await hosts.find().populate('streams').exec() + return await hosts.find().exec() }, getById: async (id) => { - return await hosts.findById(id).populate('streams').exec() + return await hosts.findById(id).exec() }, update: async(id, data) => { return await hosts.findByIdAndUpdate(id, data).exec() From b30712a74af3d0978ca5e24424db535808a246b6 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 04:47:55 +0530 Subject: [PATCH 05/24] change throw to console warning --- src/ws/crud.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index c26aa87..13fb514 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -23,10 +23,10 @@ const eventFns = { const ev = new events(data) ev.save((err) => { if (err) - throw err; + throw err }) } catch (e) { - throw e; + console.warn(e) } } } @@ -52,7 +52,7 @@ const rosterFns = { throw err }) } catch (e) { - throw e + console.warn(e) } } } @@ -78,7 +78,7 @@ const matchFns = { throw err; }) } catch (e) { - throw e; + console.warn(e) } } } @@ -104,7 +104,7 @@ const streamFns = { throw err; }) } catch (e) { - throw e; + console.warn(e) } } } @@ -130,7 +130,7 @@ const casterFns = { throw err; }) } catch (e) { - throw e; + console.warn(e) } } } @@ -156,7 +156,7 @@ const hostFns = { throw err; }) } catch (e) { - throw e; + console.warn(e) } } } From b6103b43d536ad7baabe2e20a5fda6c5087f055a Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 04:49:44 +0530 Subject: [PATCH 06/24] stop throwing pls --- src/ws/crud.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index 13fb514..91018c6 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -23,7 +23,7 @@ const eventFns = { const ev = new events(data) ev.save((err) => { if (err) - throw err + console.warn(err) }) } catch (e) { console.warn(e) @@ -49,7 +49,7 @@ const rosterFns = { const ev = new rosters(data) ev.save((err) => { if (err) - throw err + console.warn(err) }) } catch (e) { console.warn(e) @@ -75,7 +75,7 @@ const matchFns = { const ev = new matches(data) ev.save((err) => { if (err) - throw err; + console.warn(err) }) } catch (e) { console.warn(e) @@ -101,7 +101,7 @@ const streamFns = { const ev = new streams(data) ev.save((err) => { if (err) - throw err; + console.warn(err) }) } catch (e) { console.warn(e) @@ -127,7 +127,7 @@ const casterFns = { const ev = new casters(data) ev.save((err) => { if (err) - throw err; + console.warn(err) }) } catch (e) { console.warn(e) @@ -153,7 +153,7 @@ const hostFns = { const ev = new hosts(data) ev.save((err) => { if (err) - throw err; + console.warn(err) }) } catch (e) { console.warn(e) From 98bd4f60b6e453dcb194553d882559b4edfe4761 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 04:57:44 +0530 Subject: [PATCH 07/24] model issue --- src/models/matches.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/matches.js b/src/models/matches.js index 3474059..1a4a5d2 100644 --- a/src/models/matches.js +++ b/src/models/matches.js @@ -41,11 +41,11 @@ const matches = new Schema({ series: { orange: { type: Number, - required: true, + default: 0, }, blue: { type: Number, - required: true, + default: 0, }, }, games: { From 8d604819fcf570e9697168cd5586c070592f5b21 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 04:59:05 +0530 Subject: [PATCH 08/24] model issue --- src/ws/crud.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index 91018c6..3b57f63 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -59,10 +59,10 @@ const rosterFns = { const matchFns = { getAll: async () => { - return await matches.find().populate('orange').populate('blue').populate('stream').exec() + return await matches.find().populate('orange').populate('blue').exec() }, getById: async (id) => { - return await matches.findById(id).populate('orange').populate('blue').populate('stream').exec() + return await matches.findById(id).populate('orange').populate('blue').exec() }, update: async(id, data) => { return await matches.findByIdAndUpdate(id, data).exec() From c6d1a7498841ee4e1ca8c9b3bd00ee4f69061440 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 05:01:40 +0530 Subject: [PATCH 09/24] model issue --- src/models/matches.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/matches.js b/src/models/matches.js index 1a4a5d2..919da0a 100644 --- a/src/models/matches.js +++ b/src/models/matches.js @@ -14,12 +14,12 @@ const gameScores = new Schema({ const matches = new Schema({ orange: { type: Schema.Types.ObjectId, - refs: 'Rosters', + ref: 'Rosters', required: true, }, blue: { type: Schema.Types.ObjectId, - refs: 'Rosters', + ref: 'Rosters', required: true, }, started: { From b53b50f83174c85ade04a0761f37629ce3dc5b99 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 05:37:09 +0530 Subject: [PATCH 10/24] model issue --- src/models/streams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/streams.js b/src/models/streams.js index 5f7b87d..5a64d58 100644 --- a/src/models/streams.js +++ b/src/models/streams.js @@ -19,7 +19,7 @@ const streams = new Schema({ }, hosts: { type: [Schema.Types.ObjectId], - refs: 'Hosts', + ref: 'Hosts', }, }) From 057d8201faaacb6e2d4043be4d1b283e7e692705 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 05:44:01 +0530 Subject: [PATCH 11/24] model issue --- src/models/streams.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/models/streams.js b/src/models/streams.js index 5a64d58..2fbf63b 100644 --- a/src/models/streams.js +++ b/src/models/streams.js @@ -9,18 +9,18 @@ const streams = new Schema({ type: Schema.Types.ObjectId, ref: 'Event', }, - matches: { - type: [Schema.Types.ObjectId], + matches: [{ + type: Schema.Types.ObjectId, ref: 'Matches', - }, - casters: { - type: [Schema.Types.ObjectId], + }], + casters: [{ + type: Schema.Types.ObjectId, ref: 'Casters', - }, - hosts: { - type: [Schema.Types.ObjectId], + }], + hosts: [{ + type: Schema.Types.ObjectId, ref: 'Hosts', - }, + }], }) const Stream = model('Streams', streams) From 584bdc1863636e0d4815dcb442fb8e0b1cfb39b6 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 05:50:09 +0530 Subject: [PATCH 12/24] model issue --- src/ws/crud.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index 3b57f63..702e9a4 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -85,7 +85,7 @@ const matchFns = { const streamFns = { getAll: async () => { - return await streams.find().populate('event').populate('matches').populate('casters').populate('hosts').exec() + return await streams.find().populate('event').populate('matches matches.orange matches.blue').populate('casters').populate('hosts').exec() }, getById: async (id) => { return await streams.findById(id).populate('event').populate('matches').populate('casters').populate('hosts').exec() From 398eca33e58cc62968bb717507733e7b6e7adcaf Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 05:52:32 +0530 Subject: [PATCH 13/24] model issue --- src/ws/crud.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index 702e9a4..397e1fc 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -85,7 +85,11 @@ const matchFns = { const streamFns = { getAll: async () => { - return await streams.find().populate('event').populate('matches matches.orange matches.blue').populate('casters').populate('hosts').exec() + return await streams.find().populate('event').populate({ + path: 'matches', + populate: { path: 'orange' }, + populate: { path: 'blue' }, + }).populate('casters').populate('hosts').exec() }, getById: async (id) => { return await streams.findById(id).populate('event').populate('matches').populate('casters').populate('hosts').exec() From 4b0860cbf43b0eab06f7447951d8e2edcc887cf6 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 05:53:32 +0530 Subject: [PATCH 14/24] model issue --- src/ws/crud.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index 397e1fc..bfc771b 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -87,8 +87,7 @@ const streamFns = { getAll: async () => { return await streams.find().populate('event').populate({ path: 'matches', - populate: { path: 'orange' }, - populate: { path: 'blue' }, + populate: [{ path: 'orange' }, { path: 'blue' }], }).populate('casters').populate('hosts').exec() }, getById: async (id) => { From 1bf671fdfe9eaf0fcefc819e982264c2eea695e1 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sun, 14 Mar 2021 06:05:54 +0530 Subject: [PATCH 15/24] model issue --- src/ws/crud.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/ws/crud.js b/src/ws/crud.js index bfc771b..5b71685 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -85,10 +85,11 @@ const matchFns = { const streamFns = { getAll: async () => { - return await streams.find().populate('event').populate({ - path: 'matches', - populate: [{ path: 'orange' }, { path: 'blue' }], - }).populate('casters').populate('hosts').exec() + return await streams.find().exec() + // return await streams.find().populate('event').populate({ + // path: 'matches', + // populate: [{ path: 'orange' }, { path: 'blue' }], + // }).populate('casters').populate('hosts').exec() }, getById: async (id) => { return await streams.findById(id).populate('event').populate('matches').populate('casters').populate('hosts').exec() From c4c1c862498e839a35b83fe740c44660c353e130 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 13:37:52 +0530 Subject: [PATCH 16/24] add option to get populated data for streams --- src/ws/channels.js | 4 +++- src/ws/crud.js | 10 ++++++---- src/ws/handler.js | 3 +++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ws/channels.js b/src/ws/channels.js index 5fea53a..b4e94e3 100644 --- a/src/ws/channels.js +++ b/src/ws/channels.js @@ -14,7 +14,9 @@ const events = [ 'delete', ]; -const channelEvents = []; +const channelEvents = [ + 'streams:full', +]; channels.forEach((c) => { events.forEach((e) => { diff --git a/src/ws/crud.js b/src/ws/crud.js index 5b71685..671d057 100644 --- a/src/ws/crud.js +++ b/src/ws/crud.js @@ -86,10 +86,12 @@ const matchFns = { const streamFns = { getAll: async () => { return await streams.find().exec() - // return await streams.find().populate('event').populate({ - // path: 'matches', - // populate: [{ path: 'orange' }, { path: 'blue' }], - // }).populate('casters').populate('hosts').exec() + }, + getAllPop: async () => { + return await streams.find().populate('event').populate({ + path: 'matches', + populate: [{ path: 'orange' }, { path: 'blue' }], + }).populate('casters').populate('hosts').exec() }, getById: async (id) => { return await streams.findById(id).populate('event').populate('matches').populate('casters').populate('hosts').exec() diff --git a/src/ws/handler.js b/src/ws/handler.js index 43009bf..b11aa30 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -33,6 +33,9 @@ const handleMsg = async (msg, id) => { event: `${channel}:read`, data: await crud[channel].getAll(), } + if (channel === 'streams' && d.subscribe === 'streams:full') { + dm.data = await crud[channel].getAllPop() + } connections[id].connection.send(JSON.stringify(dm)) } else if (d.hasOwnProperty('event') && channelEvents.indexOf(d.event) !== -1) { const ev = d.event.split(':') From 8d5bc05cf7e3631a85d8108864c66a25b3e5ac67 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 13:45:12 +0530 Subject: [PATCH 17/24] handle streams:full properly --- src/ws/handler.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ws/handler.js b/src/ws/handler.js index b11aa30..47aa53a 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -34,6 +34,7 @@ const handleMsg = async (msg, id) => { data: await crud[channel].getAll(), } if (channel === 'streams' && d.subscribe === 'streams:full') { + dm.event = 'streams:full' dm.data = await crud[channel].getAllPop() } connections[id].connection.send(JSON.stringify(dm)) @@ -75,6 +76,12 @@ const fanoutMsg = (channel, data) => { data, } connections[k].connection.send(JSON.stringify(d)) + } else if (connections[k].events.indexOf(`streams:full`) !== -1) { + const d = { + event: `streams:full`, + data, + } + connections[k].connection.send(JSON.stringify(d)) } }) } From 496a0b9fff5b9b1a2d278cf5d1d0ca2ed01eeb27 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 15:24:51 +0530 Subject: [PATCH 18/24] console log incoming sub --- src/ws/handler.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ws/handler.js b/src/ws/handler.js index 47aa53a..d1ee0fa 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -27,6 +27,7 @@ const handleMsg = async (msg, id) => { try { const d = JSON.parse(msg) if (d.hasOwnProperty('subscribe') && channelEvents.indexOf(d.subscribe) !== -1) { + console.log('received sub for ', d.subscribe) const channel = d.subscribe.split(':')[0] connections[id].events.push(d.subscribe) const dm = { From 487f97e7e56a6934f1b0c9438f6d66f3785d0a3b Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 23:32:53 +0530 Subject: [PATCH 19/24] idk --- src/ws/handler.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/ws/handler.js b/src/ws/handler.js index d1ee0fa..6f5a1b7 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -50,7 +50,6 @@ const handleMsg = async (msg, id) => { dm = await crud[channel].create(d.data) recvUpdate(channel, dm) fanoutMsg(channel, await crud[channel].getAll()) - fanoutMsg(channel, await crud[channel].getAll()) break; case 'update': dm = await crud[channel].update(d.data.id, d.data.data) @@ -77,10 +76,10 @@ const fanoutMsg = (channel, data) => { data, } connections[k].connection.send(JSON.stringify(d)) - } else if (connections[k].events.indexOf(`streams:full`) !== -1) { + } else if (connections[k].events.indexOf('streams:full') !== -1) { const d = { - event: `streams:full`, - data, + event: 'streams:full', + data: await crud['streams'].getAllPop(), } connections[k].connection.send(JSON.stringify(d)) } From b7856fa42c92bc01f2f9c6d11523aa92a861b032 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 23:34:12 +0530 Subject: [PATCH 20/24] idk --- src/ws/handler.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ws/handler.js b/src/ws/handler.js index 6f5a1b7..4c5742c 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -77,9 +77,10 @@ const fanoutMsg = (channel, data) => { } connections[k].connection.send(JSON.stringify(d)) } else if (connections[k].events.indexOf('streams:full') !== -1) { + const m = await crud['streams'].getAllPop() const d = { event: 'streams:full', - data: await crud['streams'].getAllPop(), + data: m, } connections[k].connection.send(JSON.stringify(d)) } From 2faced9d437501bbee7fc5334cacf5260670c71f Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 23:34:39 +0530 Subject: [PATCH 21/24] idk --- src/ws/handler.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ws/handler.js b/src/ws/handler.js index 4c5742c..17684f9 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -68,7 +68,7 @@ const handleMsg = async (msg, id) => { } } -const fanoutMsg = (channel, data) => { +const fanoutMsg = async (channel, data) => { Object.keys(connections).forEach((k) => { if (connections[k].events.indexOf(`${channel}:read`) !== -1) { const d = { From 5ca90f131a70ecd7a9d1e36d55aa4c65fd184eb7 Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 23:35:11 +0530 Subject: [PATCH 22/24] idk --- src/ws/handler.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ws/handler.js b/src/ws/handler.js index 17684f9..6d7d97e 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -68,8 +68,8 @@ const handleMsg = async (msg, id) => { } } -const fanoutMsg = async (channel, data) => { - Object.keys(connections).forEach((k) => { +const fanoutMsg = (channel, data) => { + Object.keys(connections).forEach( async (k) => { if (connections[k].events.indexOf(`${channel}:read`) !== -1) { const d = { event: `${channel}:read`, From 2d9a34f8bc4db0f73542822f89f6466636f07d7d Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Fri, 19 Mar 2021 23:36:39 +0530 Subject: [PATCH 23/24] idk --- src/ws/handler.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/ws/handler.js b/src/ws/handler.js index 6d7d97e..4afe418 100644 --- a/src/ws/handler.js +++ b/src/ws/handler.js @@ -76,13 +76,14 @@ const fanoutMsg = (channel, data) => { data, } connections[k].connection.send(JSON.stringify(d)) - } else if (connections[k].events.indexOf('streams:full') !== -1) { - const m = await crud['streams'].getAllPop() - const d = { - event: 'streams:full', - data: m, + if (connections[k].events.indexOf('streams:full') !== -1) { + const m = await crud['streams'].getAllPop() + const d = { + event: 'streams:full', + data: m, + } + connections[k].connection.send(JSON.stringify(d)) } - connections[k].connection.send(JSON.stringify(d)) } }) } From 4d89438955301ad5f84b37e48c39ad4f1e779a1c Mon Sep 17 00:00:00 2001 From: Ayush Mukherjee Date: Sat, 20 Mar 2021 11:33:34 +0530 Subject: [PATCH 24/24] return name with token for API; version bump --- package.json | 2 +- src/api/index.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index f15fec3..2c530a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuke", - "version": "1.0.0", + "version": "1.1.9", "description": "APL Esports' Nuke Server", "main": "index.js", "scripts": { diff --git a/src/api/index.js b/src/api/index.js index d786458..68d8756 100644 --- a/src/api/index.js +++ b/src/api/index.js @@ -23,6 +23,7 @@ router.post('/', async (req, res) => { push(id) res.json({ session: id, + name: r.data.entries[0].name, }) } else { res.status(403).json({