+
diff --git a/src/categories.js b/src/categories.js
index 2361dae4ee..01c831c5af 100644
--- a/src/categories.js
+++ b/src/categories.js
@@ -9,7 +9,7 @@ var RDB = require('./redis.js'),
(function(Categories) {
Categories.getCategoryById = function(category_id, current_user, callback) {
-
+
Categories.getCategoryData(category_id, function(err, categoryData) {
if (err) return callback(err);
@@ -49,7 +49,7 @@ var RDB = require('./redis.js'),
next(null, topicsData);
}, category_id);
}
-
+
function getModerators(next) {
Categories.getModerators(category_id, next);
}
@@ -162,16 +162,16 @@ var RDB = require('./redis.js'),
}
Categories.markAsRead = function(cid, uid) {
- RDB.sadd('cid:' + cid + ':read_by_uid', uid);
+ RDB.sadd('cid:' + cid + ':read_by_uid', uid);
}
Categories.hasReadCategories = function(cids, uid, callback) {
var batch = RDB.multi();
for (var i=0, ii=cids.length; i count) {
postData = postData.slice(0, count);
}
@@ -214,8 +214,8 @@ var RDB = require('./redis.js'),
function movePost(pid, callback) {
posts.getPostField(pid, 'timestamp', function(timestamp) {
- RDB.zrem('categories:recent_posts:cid:' + oldCid, pid);
- RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
+ RDB.zrem('categories:recent_posts:cid:' + oldCid, pid);
+ RDB.zadd('categories:recent_posts:cid:' + cid, timestamp, pid);
});
}
@@ -240,20 +240,20 @@ var RDB = require('./redis.js'),
else callback(new Error('No category found!'));
});
}
-
+
Categories.getCategoryField = function(cid, field, callback) {
RDB.hget('category:' + cid, field, callback);
}
Categories.getCategoryFields = function(cid, fields, callback) {
RDB.hmgetObject('category:' + cid, fields, function(err, data) {
- if(err === null)
+ if(err === null)
callback(data);
else
winston.err(err);
- });
+ });
}
-
+
Categories.setCategoryField = function(cid, field, value) {
RDB.hset('category:' + cid, field, value);
}
@@ -267,12 +267,12 @@ var RDB = require('./redis.js'),
callback({'categories' : []});
return;
}
-
+
var categories = [];
function getCategory(cid, callback) {
Categories.getCategoryData(cid, function(err, categoryData) {
-
+
if(err) {
callback(err);
return;
@@ -284,20 +284,20 @@ var RDB = require('./redis.js'),
categories.push(categoryData);
callback(null);
}) ;
- });
+ });
}
-
+
async.eachSeries(cids, getCategory, function(err) {
if(err) {
winston.err(err);
callback(null);
return;
}
-
- callback({'categories': categories});
+
+ callback({'categories': categories});
});
-
- };
+
+ };
}(exports));
diff --git a/src/favourites.js b/src/favourites.js
index 894a301311..00d1fc40fd 100644
--- a/src/favourites.js
+++ b/src/favourites.js
@@ -16,21 +16,23 @@ var RDB = require('./redis.js'),
return;
}
- posts.getPostField(pid, 'uid', function(uid_of_poster) {
+ posts.getPostFields(pid, ['uid', 'timestamp'], function(postData) {
Favourites.hasFavourited(pid, uid, function(hasFavourited) {
if (hasFavourited == false) {
RDB.sadd('pid:' + pid + ':users_favourited', uid);
+ RDB.zadd('uid:' + uid + ':favourites', postData.timestamp, pid);
+
RDB.hincrby('post:' + pid, 'reputation', 1);
- if (uid !== uid_of_poster) {
- user.incrementUserFieldBy(uid_of_poster, 'reputation', 1, function(err, newreputation) {
- RDB.zadd('users:reputation', newreputation, uid_of_poster);
+ if (uid !== postData.uid) {
+ user.incrementUserFieldBy(postData.uid, 'reputation', 1, function(err, newreputation) {
+ RDB.zadd('users:reputation', newreputation, postData.uid);
});
}
if (room_id) {
- io.sockets.in(room_id).emit('event:rep_up', {uid: uid !== uid_of_poster ? uid_of_poster : 0, pid: pid});
+ io.sockets.in(room_id).emit('event:rep_up', {uid: uid !== postData.uid ? postData.uid : 0, pid: pid});
}
socket.emit('api:posts.favourite', {
@@ -58,10 +60,12 @@ var RDB = require('./redis.js'),
Favourites.hasFavourited(pid, uid, function(hasFavourited) {
if (hasFavourited == true) {
-
+
RDB.srem('pid:' + pid + ':users_favourited', uid);
+ RDB.zrem('uid:' + uid + ':favourites', pid);
+
RDB.hincrby('post:' + pid, 'reputation', -1);
-
+
if (uid !== uid_of_poster) {
user.incrementUserFieldBy(uid_of_poster, 'reputation', -1, function(err, newreputation) {
RDB.zadd('users:reputation', newreputation, uid_of_poster);
@@ -84,7 +88,7 @@ var RDB = require('./redis.js'),
Favourites.hasFavourited = function(pid, uid, callback) {
RDB.sismember('pid:' + pid + ':users_favourited', uid, function(err, hasFavourited) {
RDB.handle(err);
-
+
callback(hasFavourited);
});
}
@@ -96,10 +100,10 @@ var RDB = require('./redis.js'),
for (var i=0, ii=pids.length; i 262144) {
res.send({
error: 'Images must be smaller than 256kb!'
});
return;
}
-
+
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
-
+
if(allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
res.send({
error: 'Allowed image types are png, jpg and gif!'
});
- return;
+ return;
}
user.getUserField(req.user.uid, 'uploadedpicture', function(oldpicture) {
@@ -136,7 +136,7 @@ var user = require('./../user.js'),
var absolutePath = path.join(global.configuration['ROOT_DIRECTORY'], global.nconf.get('upload_path'), path.basename(oldpicture));
fs.unlink(absolutePath, function(err) {
- if(err) {
+ if(err) {
winston.error('[%d] %s', Date.now(), + err);
}
@@ -144,7 +144,7 @@ var user = require('./../user.js'),
});
});
});
-
+
function uploadUserPicture(uid, extension, tempPath, res) {
if(!extension) {
res.send({
@@ -155,9 +155,9 @@ var user = require('./../user.js'),
var filename = uid + '-profileimg' + extension;
var uploadPath = path.join(global.configuration['ROOT_DIRECTORY'], global.nconf.get('upload_path'), filename);
-
+
winston.info('Attempting upload to: '+ uploadPath);
-
+
var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(uploadPath);
@@ -195,24 +195,24 @@ var user = require('./../user.js'),
if(!req.user)
return res.redirect('/403');
-
+
user.get_uid_by_userslug(req.params.userslug, function(uid) {
if(!uid) {
res.redirect('/404');
return;
}
-
+
app.build_header({ req: req, res: res }, function(err, header) {
res.send(header + app.create_route('users/'+req.params.userslug+'/following','following') + templates['footer']);
});
});
});
-
+
app.get('/users/:userslug/followers', function(req, res) {
if(!req.user)
return res.redirect('/403');
-
+
user.get_uid_by_userslug(req.params.userslug, function(uid) {
if(!uid) {
res.redirect('/404');
@@ -224,6 +224,22 @@ var user = require('./../user.js'),
});
});
+ app.get('/users/:userslug/favourites', function(req, res) {
+
+ if(!req.user)
+ return res.redirect('/403');
+
+ user.get_uid_by_userslug(req.params.userslug, function(uid) {
+ if(!uid) {
+ res.redirect('/404');
+ return;
+ }
+ app.build_header({ req: req, res: res }, function(err, header) {
+ res.send(header + app.create_route('users/'+req.params.userslug+'/favourites','favourites') + templates['footer']);
+ });
+ });
+ });
+
app.get('/api/users/:userslug/following', function(req, res) {
var callerUID = req.user ? req.user.uid : 0;
@@ -234,7 +250,7 @@ var user = require('./../user.js'),
userData.followingCount = followingData.length;
res.json(userData);
});
-
+
} else {
res.json(404, { error: 'User not found!' }) ;
}
@@ -243,7 +259,7 @@ var user = require('./../user.js'),
app.get('/api/users/:userslug/followers', function(req, res) {
var callerUID = req.user ? req.user.uid : 0;
-
+
getUserDataByUserSlug(req.params.userslug, callerUID, function(userData) {
if(userData) {
user.getFollowers(userData.uid, function(followersData) {
@@ -253,7 +269,7 @@ var user = require('./../user.js'),
});
} else {
res.json(404, { error: 'User not found!' }) ;
- }
+ }
});
});
@@ -273,7 +289,7 @@ var user = require('./../user.js'),
res.json(404, { error: 'User not found!' }) ;
return;
}
-
+
if(uid !== callerUID || callerUID === "0") {
res.json(403, { error: 'Not allowed!' });
return;
@@ -287,9 +303,39 @@ var user = require('./../user.js'),
res.json(userData);
} else {
res.json(404, { error: 'User not found!' }) ;
- }
+ }
+ });
+ });
+ });
+
+ app.get('/api/users/:userslug/favourites', function(req, res, next) {
+ var callerUID = req.user ? req.user.uid : 0;
+
+ user.get_uid_by_userslug(req.params.userslug, function(uid) {
+ if(!uid) {
+ res.json(404, { error: 'User not found!' }) ;
+ return;
+ }
+
+ if(uid !== callerUID || callerUID === "0") {
+ res.json(403, { error: 'Not allowed!' });
+ return;
+ }
+
+ user.getUserFields(uid, ['username','userslug'], function(userData) {
+ if(userData) {
+ posts.getFavourites(uid, function(err, posts) {
+ if(err)
+ return next(err);
+ userData.posts = posts;
+ userData.show_nofavourites = posts.length?'hide':'show';
+ res.json(userData);
+ });
+ } else {
+ res.json(404, { error: 'User not found!' }) ;
+ }
});
- });
+ });
});
app.get('/api/users/:userslug', function(req, res) {
@@ -313,7 +359,7 @@ var user = require('./../user.js'),
} else {
res.json(404, { error: 'User not found!' }) ;
}
- });
+ });
});
app.get('/api/users', getUsersSortedByJoinDate);
@@ -321,14 +367,14 @@ var user = require('./../user.js'),
app.get('/api/users-sort-reputation', getUsersSortedByReputation);
app.get('/api/users-latest', getUsersSortedByJoinDate);
app.get('/api/users-search', getUsersForSearch);
-
-
+
+
function getUsersSortedByJoinDate(req, res) {
user.getUsers('users:joindate', 0, 49, function(err, data) {
res.json({ search_display: 'none', loadmore_display:'block', users:data });
});
}
-
+
function getUsersSortedByPosts(req, res) {
user.getUsers('users:postcount', 0, 49, function(err, data) {
res.json({ search_display: 'none', loadmore_display:'block', users:data });
@@ -340,19 +386,19 @@ var user = require('./../user.js'),
res.json({ search_display: 'none', loadmore_display:'block', users:data });
});
}
-
- function getUsersForSearch(req, res) {
+
+ function getUsersForSearch(req, res) {
res.json({ search_display: 'block', loadmore_display:'none', users: [] });
}
function getUserDataByUserSlug(userslug, callerUID, callback) {
user.get_uid_by_userslug(userslug, function(uid) {
-
+
if(uid === null) {
callback(null);
return;
}
-
+
user.getUserData(uid, function(data) {
if(data) {
data.joindate = utils.relativeTime(data.joindate);
@@ -362,21 +408,21 @@ var user = require('./../user.js'),
} else {
data.age = new Date().getFullYear() - new Date(data.birthday).getFullYear();
}
-
+
function canSeeEmail() {
return callerUID === uid || (data.email && (data.showemail && data.showemail === "1"));
}
- if(!canSeeEmail())
+ if(!canSeeEmail())
data.email = "";
if(callerUID === uid && (!data.showemail || data.showemail === "0"))
data.emailClass = "";
- else
+ else
data.emailClass = "hide";
data.show_banned = data.banned === '1'?'':'hide';
-
+
data.uid = uid;
data.yourid = callerUID;
data.theirid = uid;
@@ -392,7 +438,7 @@ var user = require('./../user.js'),
callback(null);
}
});
-
+
});
}