Skip to content

Commit 252badb

Browse files
committed
Add support for limited polls
1 parent 6a1136e commit 252badb

File tree

1 file changed

+74
-49
lines changed

1 file changed

+74
-49
lines changed

src/noteshandler.js

Lines changed: 74 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export class NotesConnection extends CommonConnection {
8585
curtoken = token.decoded
8686
socket.emit('authtoken', { token: token.token })
8787
}
88+
socket.emit('userhash', purenotes.userhash)
8889

8990
{
9091
const presinfo = this.getPresentationinfo(purenotes)
@@ -111,15 +112,15 @@ export class NotesConnection extends CommonConnection {
111112
const userhash = purenotes.userhash
112113

113114
this.notepadio.to(purenotes.roomname).emit('chatquestion', {
114-
displayname: displayname,
115+
displayname,
115116
text: cmd.text,
116117
encData: cmd.encData,
117118
keyindex: cmd.keyindex,
118119
iv: cmd.iv,
119120
videoquestion: cmd.videoquestion
120121
? { id: purenotes.socketid }
121122
: undefined,
122-
userhash: userhash
123+
userhash
123124
})
124125

125126
// console.log("chat send", cmd.text,socket.decoded_token);
@@ -141,51 +142,52 @@ export class NotesConnection extends CommonConnection {
141142
})
142143
})
143144

144-
socket.on(
145-
'castvote',
146-
async function (data, callback) {
147-
// console.log('db cv data', data)
148-
// console.log('db cv callback', callback)
149-
/* console.log("db cv1", data.pollid && data.pollid.match(/^[0-9a-zA-Z]{9}$/.test));
150-
console.log("db cv2",data.selection && ((data.selection && typeof data.selection=="string" && data.selection.match(/^[0-9a-zA-Z]{9}$/))));
151-
console.log("db cv3",(( typeof data.selection=="string" && data.selection.match(/^[0-9a-zA-Z]{9}$/))));
152-
console.log("db cv3",( Array.isArray(data.selection) && data.selection.filter((el)=>(el.match(/^[0-9a-zA-Z]{9}$/))).length>0)); */
145+
socket.on('castvote', async (data, callback) => {
146+
if (
147+
data.pollid &&
148+
/^[0-9a-zA-Z]{9}$/.test(data.pollid) &&
149+
data.selection &&
150+
((typeof data.selection === 'string' &&
151+
/^[0-9a-zA-Z]{9}$/.test(data.selection)) ||
152+
(Array.isArray(data.selection) &&
153+
data.selection.filter((el) => /^[0-9a-zA-Z]{9}$/.test(el)).length >
154+
0))
155+
) {
156+
const pollstate = await this.getPollinfo(purenotes)
153157
if (
154-
data.pollid &&
155-
/^[0-9a-zA-Z]{9}$/.test(data.pollid) &&
156-
data.selection &&
157-
((typeof data.selection === 'string' &&
158-
/^[0-9a-zA-Z]{9}$/.test(data.selection)) ||
159-
(Array.isArray(data.selection) &&
160-
data.selection.filter((el) => /^[0-9a-zA-Z]{9}$/.test(el))
161-
.length > 0))
158+
pollstate.limited &&
159+
!pollstate.participants.includes(purenotes.userhash)
162160
) {
163-
const useruuid = socket.decoded_token.user.useruuid
164-
try {
165-
const ballothash = createHash('sha256')
166-
ballothash.update(useruuid + data.pollid)
167-
168-
const salt = await this.redis.get(
169-
'pollsalt:lecture:' +
170-
purenotes.lectureuuid +
171-
':poll:' +
172-
data.pollid
173-
)
174-
ballothash.update(salt)
175-
const ballotid = ballothash.digest('hex')
176-
this.notepadio.to(purenotes.roomname).emit('castvote', {
177-
ballotid: ballotid,
178-
vote: data.selection,
179-
pollid: data.pollid
180-
})
181-
callback({ ballot: ballotid })
182-
} catch (err) {
183-
console.log('failed to get salt', err)
184-
callback({ error: 'failure' })
185-
}
186-
} else callback({ error: 'failure' })
187-
}.bind(this)
188-
)
161+
console.log('not eglible to vote!')
162+
callback({ error: 'not eglible to vote!' })
163+
return
164+
}
165+
if (pollstate.data.id !== data.pollid) {
166+
callback({ error: 'pollid does not match current running poll!' })
167+
return
168+
}
169+
const useruuid = socket.decoded_token.user.useruuid
170+
try {
171+
const ballothash = createHash('sha256')
172+
ballothash.update(useruuid + data.pollid)
173+
174+
const salt = await this.redis.get(
175+
'pollsalt:lecture:' + purenotes.lectureuuid + ':poll:' + data.pollid
176+
)
177+
ballothash.update(salt)
178+
const ballotid = ballothash.digest('hex')
179+
this.notepadio.to(purenotes.roomname).emit('castvote', {
180+
ballotid: ballotid,
181+
vote: data.selection,
182+
pollid: data.pollid
183+
})
184+
callback({ ballot: ballotid })
185+
} catch (err) {
186+
console.log('failed to get salt', err)
187+
callback({ error: 'failure' })
188+
}
189+
} else callback({ error: 'failure' })
190+
})
189191

190192
socket.on('getrouting', async (cmd, callback) => {
191193
let tempOut
@@ -327,7 +329,15 @@ export class NotesConnection extends CommonConnection {
327329

328330
{
329331
const pollstate = await this.getPollinfo(purenotes)
330-
if (pollstate) socket.emit(pollstate.command, pollstate.data)
332+
if (pollstate) {
333+
let canVote = true
334+
if (pollstate.limited) {
335+
if (!pollstate.participants.includes(purenotes.userhash)) {
336+
canVote = false
337+
}
338+
}
339+
socket.emit(pollstate.command, { ...pollstate.data, canVote })
340+
}
331341
}
332342
}
333343

@@ -458,9 +468,24 @@ export class NotesConnection extends CommonConnection {
458468
const pollinfo = await this.redis.hGetAll(
459469
'lecture:' + args.lectureuuid + ':pollstate'
460470
)
461-
if (pollinfo.command && pollinfo.data)
462-
return { command: pollinfo.command, data: JSON.parse(pollinfo.data) }
463-
else return null
471+
if (pollinfo.command && pollinfo.data) {
472+
const limited = !!(pollinfo.limited === 'true')
473+
let participants
474+
if (limited) {
475+
if (!pollinfo.participants) {
476+
console.log('getPollinfo no participants for limited poll')
477+
return null
478+
}
479+
participants = JSON.parse(pollinfo.participants)
480+
}
481+
482+
return {
483+
command: pollinfo.command,
484+
data: JSON.parse(pollinfo.data),
485+
limited,
486+
participants
487+
}
488+
} else return null
464489
} catch (error) {
465490
console.log('getPollInfo failed', error)
466491
return null

0 commit comments

Comments
 (0)