Adopted userIDView for views that always need to fetch the user ID

This commit is contained in:
Andrew Richardson 2014-02-18 20:24:15 -08:00
Родитель b4103656f3
Коммит 53267c771e
3 изменённых файлов: 72 добавлений и 154 удалений

Просмотреть файл

@ -21,40 +21,31 @@ module.exports = function(server) {
isRequired: true
}
}
}, db.redisView(function(client, done, req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var GET = req.params;
var email = req._email;
user.getUserIDFromEmail(client, email, function(err, id) {
if (err || !id) {
res.json(500, {error: err || 'db_error'});
(function(cb) {
var friends = 'friends:' + id;
if (GET.only === 'online') {
return client.sinter(friends, 'authenticated', cb);
} else if (GET.only === 'played' && GET.game) {
return client.sinter(friends, 'gamePlayed:' + GET.game, cb);
} else if (GET.only === 'playedOnline' && GET.game) {
return client.sinter(friends, 'authenticated', 'gamePlayed:' + GET.game, cb);
} else if (GET.only === 'playing' && GET.game) {
return client.sinter(friends, 'gamePlaying:' + GET.game, cb);
} else {
return client.smembers(friends, cb);
}
})(function(err, friends) {
if (err || !friends) {
res.json(400, {error: 'no_friends'});
done();
return;
}
(function(cb) {
var friends = 'friends:' + id;
if (GET.only === 'online') {
return client.sinter(friends, 'authenticated', cb);
} else if (GET.only === 'played' && GET.game) {
return client.sinter(friends, 'gamePlayed:' + GET.game, cb);
} else if (GET.only === 'playedOnline' && GET.game) {
return client.sinter(friends, 'authenticated', 'gamePlayed:' + GET.game, cb);
} else if (GET.only === 'playing' && GET.game) {
return client.sinter(friends, 'gamePlaying:' + GET.game, cb);
} else {
return client.smembers(friends, cb);
}
})(function(err, friends) {
if (err || !friends) {
res.json(400, {error: 'no_friends'});
done();
return;
}
user.getPublicUserObjList(client, friends, function(objs) {
done();
res.json(objs);
});
user.getPublicUserObjList(client, friends, function(objs) {
done();
res.json(objs);
});
});
}));
@ -78,30 +69,18 @@ module.exports = function(server) {
isRequired: true
}
}
}, db.redisView(function(client, done, req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var POST = req.params;
var email = req._email;
var recipient = POST.recipient;
user.getUserIDFromEmail(client, email, function(err, id) {
if (err || !id) {
res.json(500, {error: err || 'db_error'});
client.hexists('users', recipient, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'bad_recipient'});
done();
return;
}
checkRecipient(id);
checkNotFriends(id);
});
function checkRecipient(id) {
client.hexists('users', recipient, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'bad_recipient'});
done();
return;
}
checkNotFriends(id);
});
}
function checkNotFriends(id) {
client.sismember('friends:' + id, recipient, function(err, resp) {
if (err || resp) {
@ -158,32 +137,23 @@ module.exports = function(server) {
isRequired: true
}
}
}, db.redisView(function(client, done, req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var GET = req.params;
var email = req._email;
user.getUserIDFromEmail(client, email, function(err, id) {
if (err || !id) {
res.json(500, {error: err || 'db_error'});
done();
return;
}
client.sdiff(
'friendRequests:' + id,
'ignoredFriendRequests:' + id,
function(err, friends) {
if (err || !friends) {
res.json(500, {error: 'no_friends'});
done();
return;
}
user.getPublicUserObjList(client, friends, function(objs) {
done();
res.json(objs);
});
client.sdiff(
'friendRequests:' + id,
'ignoredFriendRequests:' + id,
function(err, friends) {
if (err || !friends) {
res.json(500, {error: 'no_friends'});
done();
return;
}
);
});
user.getPublicUserObjList(client, friends, function(objs) {
done();
res.json(objs);
});
}
);
}));
server.post({
@ -203,31 +173,19 @@ module.exports = function(server) {
isRequired: true
}
}
}, db.redisView(function(client, done, req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var POST = req.params;
var email = req._email;
var acceptee = POST.acceptee;
user.getUserIDFromEmail(client, email, function(err, id) {
if (err || !id) {
res.json(500, {error: err || 'db_error'});
client.sismember('friendRequests:' + id, acceptee, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'request_not_found'});
done();
return;
}
checkRequestExists(id);
addFriends(id);
});
function checkRequestExists(id) {
client.sismember('friendRequests:' + id, acceptee, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'request_not_found'});
done();
return;
}
addFriends(id);
});
}
function addFriends(id) {
client.sadd('friends:' + acceptee, id);
client.sadd('friends:' + id, acceptee);
@ -269,27 +227,19 @@ module.exports = function(server) {
isRequired: true
}
}
}, db.redisView(function(client, done, req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var POST = req.params;
var email = req._email;
var rejectee = POST.rejectee;
user.getUserIDFromEmail(client, email, function(err, id) {
if (err || !id) {
res.json(500, {error: err || 'db_error'});
client.sismember('friendRequests:' + id, rejectee, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'request_not_found'});
done();
return;
}
client.sismember('friendRequests:' + id, rejectee, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'request_not_found'});
done();
return;
}
client.sadd('ignoreFriendRequests:' + id, rejectee);
res.json(202, {success: true});
done();
});
client.sadd('ignoreFriendRequests:' + id, rejectee);
res.json(202, {success: true});
done();
});
}));
@ -310,31 +260,19 @@ module.exports = function(server) {
isRequired: true
}
}
}, db.redisView(function(client, done, req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var POST = req.params;
var email = req._email;
var exfriend = POST.exfriend;
user.getUserIDFromEmail(client, email, function(err, id) {
if (err || !id) {
res.json(500, {error: err || 'db_error'});
client.sismember('friends:' + id, exfriend, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'friend_not_found'});
done();
return;
}
checkFriendExists(id);
unfriendFriend(id);
});
function checkFriendExists(id) {
client.sismember('friends:' + id, exfriend, function(err, resp) {
if (err || !resp) {
res.json(400, {error: 'friend_not_found'});
done();
return;
}
unfriendFriend(id);
});
}
function unfriendFriend(id) {
client.srem('friends:' + exfriend, id);
client.srem('friends:' + id, exfriend);

Просмотреть файл

@ -34,27 +34,16 @@ module.exports = function(server) {
isRequired: false,
}
}
}, db.redisView(function(client, done, req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var DATA = req.params;
var email = req._email;
user.getUserIDFromEmail(client, email, function(err, userID) {
if (err || !userID) {
res.json(500, {error: err || 'db_error'});
done();
return;
var dataToUpdate = _.pick(DATA, 'username', 'email');
user.updateUser(client, id, dataToUpdate, function(err, newUserData) {
if (err) {
res.json(500, {error: err});
} else {
res.json(202, {success: true});
}
var dataToUpdate = _.pick(DATA, 'username', 'email');
user.updateUser(client, userID, dataToUpdate, function(err, newUserData) {
if (err) {
res.json(500, {error: err});
} else {
res.json(202, {success: true});
}
done();
});
done();
});
}));
};

Просмотреть файл

@ -23,9 +23,8 @@ module.exports = function(server) {
isRequired: true
}
}
}, function(req, res) {
}, user.userIDView(function(client, done, req, res, id) {
var POST = req.params;
var email = req._email;
// TODO: Accept ID *or* slug.
var game = POST.game;
@ -34,21 +33,13 @@ module.exports = function(server) {
return;
}
var redisClient = db.redis();
user.getUserIDFromEmail(redisClient, email, function(err, id) {
if (err || !id) {
redisClient.end();
res.json(400, {error: err || 'no_user_found'});
client.sadd('gamesPurchased:' + id, game, function(err) {
done();
if (err) {
res.json(500, {error: 'internal_db_error'});
return;
}
redisClient.sadd('gamesPurchased:' + id, game, function(err) {
redisClient.end();
if (err) {
res.json(500, {error: 'internal_db_error'});
return;
}
res.json({success: true});
});
res.json({success: true});
});
});
};