}));
}
);
- // Also ask for corr challenges (open + sent to me)
+ // Also ask for corr challenges (open + sent by/to me)
ajax(
"/challenges",
"GET",
response => {
// Gather all senders names, and then retrieve full identity:
// (TODO [perf]: some might be online...)
- const uids = response.challenges.map(c => { return c.uid });
- ajax("/users",
- "GET",
- { ids: uids.join(",") },
- response2 => {
- let names = {};
- response2.users.forEach(u => {names[u.id] = u.name});
- this.challenges = this.challenges.concat(
- response.challenges.map(c => {
- // (just players names in fact)
- const from = {name: names[c.uid], id: c.uid};
- const type = this.classifyObject(c);
- const vname = this.getVname(c.vid);
- return Object.assign({}, c, {type: type, vname: vname, from: from});
- })
- )
- }
- );
+ let names = {};
+ response.challenges.forEach(c => {
+ if (c.uid != this.st.user.id)
+ names[c.uid] = ""; //unknwon for now
+ else if (!!c.target && c.target != this.st.user.id)
+ names[c.target] = "";
+ });
+ const addChallenges = (newChalls) => {
+ names[this.st.user.id] = this.st.user.name; //in case of
+ this.challenges = this.challenges.concat(
+ response.challenges.map(c => {
+ const from = {name: names[c.uid], id: c.uid}; //or just name
+ const type = this.classifyObject(c);
+ const vname = this.getVname(c.vid);
+ return Object.assign({},
+ {
+ type: type,
+ vname: vname,
+ from: from,
+ to: (!!c.target ? names[c.target] : ""),
+ },
+ c);
+ })
+ );
+ };
+ if (names !== {})
+ {
+ ajax("/users",
+ "GET",
+ { ids: Object.keys(names).join(",") },
+ response2 => {
+ response2.users.forEach(u => {names[u.id] = u.name});
+ addChallenges();
+ }
+ );
+ }
+ else
+ addChallenges();
}
);
// 0.1] Ask server for room composition:
{chall:chall}, !!warnDisconnected);
if (!isSent)
return;
- // Remove old challenge if any (only one at a time):
+ // Remove old challenge if any (only one at a time of a given type):
const cIdx = this.challenges.findIndex(c =>
- c.from.sid == this.st.user.sid && c.type == ctype);
+ (c.from.sid == this.st.user.sid || c.from.id == this.st.user.id) && c.type == ctype);
if (cIdx >= 0)
{
// Delete current challenge (will be replaced now)
});
},
- // All challenges except where target is defined and not me
+ // All challenges except where target is defined and not me,
+ // and I'm not the sender.
getByUser: function(uid, cb)
{
db.serialize(function() {
const query =
"SELECT * " +
"FROM Challenges " +
- "WHERE target IS NULL OR target = " + uid;
+ "WHERE target IS NULL" +
+ " OR uid = " + uid +
+ " OR target = " + uid;
db.all(query, (err,challenges) => {
return cb(err, challenges);
});
/////////////////
// NOTIFICATIONS
- tryNotify: function(oppId, message)
+ notify: function(user, message)
{
- UserModel.getOne("id", oppId, (err,opp) => {
- if (!err || !opp.notify)
- return; //error is ignored here (TODO: should be logged)
- const subject = "vchess.club - notification";
- const body = "Hello " + opp.name + "!\n" + message;
- sendEmail(params.mail.noreply, opp.email, subject, body, err => {
- res.json(err || {});
- });
+ const subject = "vchess.club - notification";
+ const body = "Hello " + user.name + "!\n" + message;
+ sendEmail(params.mail.noreply, user.email, subject, body);
+ },
+
+ tryNotify: function(id, message)
+ {
+ UserModel.getOne("id", id, (err,user) => {
+ if (!err || !user.notify)
+ return; //NOTE: error is ignored here
+ UserModel.notify(user, message);
});
},
-var router = require("express").Router();
-var access = require("../utils/access");
+let router = require("express").Router();
+const access = require("../utils/access");
// To avoid a weird preflight AJAX request error in dev mode...
router.get("/", access.ajax, (req,res) => {
const access = require("../utils/access");
const ChallengeModel = require("../models/Challenge");
const UserModel = require("../models/User"); //for name check
+const params = require("../config/parameters");
router.get("/challenges", (req,res) => {
ChallengeModel.getByUser(req.query["uid"], (err,challenges) => {
return res.json(err | {errmsg: "Typo in player name"});
challenge.to = user.id; //ready now to insert challenge
insertChallenge();
+ if (user.notify)
+ UserModel.notify(user, "New challenge: " + params.siteURL + "/");
});
}
else
-var router = require("express").Router();
-var UserModel = require("../models/User");
-var ChallengeModel = require('../models/Challenge');
-var GameModel = require('../models/Game');
-var VariantModel = require('../models/Variant');
-var access = require("../utils/access");
-var params = require("../config/parameters");
+let router = require("express").Router();
+const UserModel = require("../models/User");
+const ChallengeModel = require('../models/Challenge');
+const GameModel = require('../models/Game');
+const VariantModel = require('../models/Variant');
+const access = require("../utils/access");
+const params = require("../config/parameters");
// From main hall, start game between players 0 and 1
router.post("/games", access.logged, access.ajax, (req,res) => {
// AJAX methods to get, create, update or delete a user
-var router = require("express").Router();
-var UserModel = require('../models/User');
-var sendEmail = require('../utils/mailer');
-var genToken = require("../utils/tokenGenerator");
-var access = require("../utils/access");
-var params = require("../config/parameters");
+let router = require("express").Router();
+const UserModel = require('../models/User');
+const sendEmail = require('../utils/mailer');
+const genToken = require("../utils/tokenGenerator");
+const access = require("../utils/access");
+const params = require("../config/parameters");
// NOTE: this method is safe because the sessionToken must be guessed
router.get("/whoami", access.ajax, (req,res) => {
console.log("Subject: " + subject);
let msgText = body.split('\\n');
msgText.forEach(msg => { console.log(msg); });
+ if (!cb)
+ cb = (err) => { if (!!err) console.log(err); }
return cb();
}
+ else if (!cb)
+ cb = () => {}; //default: do nothing (TODO: log somewhere)
// Create reusable transporter object using the default SMTP transport
const transporter = nodemailer.createTransport({