Skip to content

Commit

Permalink
Add server-side article validation
Browse files Browse the repository at this point in the history
  • Loading branch information
azaky committed Oct 11, 2020
1 parent 545935c commit b26fbf1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
35 changes: 33 additions & 2 deletions server/game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const util = require('./util');
const fetch = require('node-fetch');

let rooms = [];

Expand Down Expand Up @@ -85,7 +86,28 @@ const calculateLeaderboard = room => {
return room.leaderboard;
};

const handler = socket => {
const validateArticle = async title => {
try {
const response = await fetch(`https://en.wikipedia.org/api/rest_v1/page/summary/${encodeURIComponent(title)}`);
const body = await response.json();
if (body.type === 'https://mediawiki.org/wiki/HyperSwitch/errors/not_found') {
return '';
} else {
// return canonical name instead of the original title
return body.titles.canonical;
}
} catch (e) {
console.error(`Error validating article [${title}]:`, e);
return '';
}
};

const validateArticles = async titles => {
const validated = await Promise.all(titles.map(validateArticle));
return validated.filter(title => title !== '');
};

const handler = async socket => {
console.log('a user connected!');
console.log(socket.handshake.query);
let {username, roomId} = socket.handshake.query;
Expand Down Expand Up @@ -159,13 +181,22 @@ const handler = socket => {
});

// update rules etc.
socket.on('update', (data, ack) => {
socket.on('update', async (data, ack) => {
if (room.host !== username) {
console.log(`[room=${room.roomId}] [${username}] is not host and attempted to perform update, ignoring`);
return;
}
console.log(`[room=${room.roomId}] [${username}] updates data:`, data);

if (data.currentRound && data.currentRound.start) {
data.currentRound.start = await validateArticle(data.currentRound.start);
}
if (data.currentRound && data.currentRound.target) {
data.currentRound.target = await validateArticle(data.currentRound.target);
}
if (data.rules && data.rules.bannedArticles) {
data.rules.bannedArticles = await validateArticles(data.rules.bannedArticles);
}
// TODO: perform validation to data
util.mergeDeep(room, data);

Expand Down
5 changes: 5 additions & 0 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"cors": "^2.8.5",
"express": "^4.17.1",
"node-fetch": "^2.6.1",
"socket.io": "^2.3.0"
}
}

0 comments on commit b26fbf1

Please sign in to comment.