Baris Usakli 11 years ago
commit 468688615f

@ -0,0 +1,3 @@
{
"title": "Notifications"
}

@ -0,0 +1,31 @@
define(function() {
var Notifications = {};
Notifications.init = function() {
var listEl = $('.notifications-list'),
markAllReadEl = document.getElementById('mark-all-notifs-read');
$('span.timeago').timeago();
// Allow the user to click anywhere in the LI
listEl.on('click', 'li', function(e) {
this.querySelector('a').click();
});
// Mark all as read button
$(markAllReadEl).click(function() {
socket.emit('api:notifications.mark_all_read', {}, function() {
ajaxify.go('notifications');
app.alert({
alert_id: "notifications:mark_all_read",
title: "All Notifications Read",
message: "Successfully marked all notifications read",
type: 'success',
timeout: 2500
})
});
});
}
return Notifications;
});

@ -0,0 +1,14 @@
<h2>[[notifications:title]]</h2>
<button type="button" class="btn btn-default" id="mark-all-notifs-read">Mark All as Read</button>
<ul class="notifications-list">
<!-- BEGIN notifications -->
<li data-nid="{notifications.nid}" class="{notifications.readClass}">
<p class="timestamp">
<span class="timeago" title="{notifications.datetimeISO}"></span>
</p>
<a href="..{notifications.path}">{notifications.text}</a>
</li>
<!-- END notifications -->
</ul>

@ -3,17 +3,24 @@ var RDB = require('./redis.js'),
utils = require('../public/src/utils.js'),
notifications = {
get: function(nid, callback) {
RDB.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId', function(err, notification) {
callback({
nid: nid,
text: notification[0],
score: notification[1],
path: notification[2],
datetime: notification[3],
uniqueId: notification[4]
get: function(nid, uid, callback) {
RDB.multi()
.hmget('notifications:' + nid, 'text', 'score', 'path', 'datetime', 'uniqueId')
.zrank('uid:' + uid + ':notifications:read', nid)
.exec(function(err, results) {
var notification = results[0]
readIdx = results[1];
callback({
nid: nid,
text: notification[0],
score: notification[1],
path: notification[2],
datetime: notification[3],
uniqueId: notification[4],
read: readIdx !== null ? true : false
});
});
});
},
create: function(text, path, uniqueId, callback) {
/**
@ -39,7 +46,7 @@ var RDB = require('./redis.js'),
var numUids = uids.length,
x;
notifications.get(nid, function(notif_data) {
notifications.get(nid, null, function(notif_data) {
for (x = 0; x < numUids; x++) {
if (parseInt(uids[x]) > 0) {
(function(uid) {
@ -59,7 +66,7 @@ var RDB = require('./redis.js'),
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
if (nids && nids.length > 0) {
async.each(nids, function(nid, next) {
notifications.get(nid, function(nid_info) {
notifications.get(nid, uid, function(nid_info) {
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:unread', nid);
next();
});
@ -73,7 +80,7 @@ var RDB = require('./redis.js'),
RDB.zrange('uid:' + uid + ':notifications:read', 0, -1, function(err, nids) {
if (nids && nids.length > 0) {
async.each(nids, function(nid, next) {
notifications.get(nid, function(nid_info) {
notifications.get(nid, uid, function(nid_info) {
if (nid_info.uniqueId === uniqueId) RDB.zrem('uid:' + uid + ':notifications:read', nid);
next();
});
@ -89,7 +96,7 @@ var RDB = require('./redis.js'),
},
mark_read: function(nid, uid, callback) {
if (parseInt(uid) > 0) {
notifications.get(nid, function(notif_data) {
notifications.get(nid, uid, function(notif_data) {
RDB.zrem('uid:' + uid + ':notifications:unread', nid);
RDB.zadd('uid:' + uid + ':notifications:read', notif_data.datetime, nid);
if (callback) callback();

@ -61,9 +61,20 @@ var RDB = require('./redis.js'),
PostTools.edit = function(uid, pid, title, content) {
var success = function() {
posts.setPostField(pid, 'content', content);
posts.setPostField(pid, 'edited', Date.now());
posts.setPostField(pid, 'editor', uid);
async.waterfall([
function(next) {
posts.setPostField(pid, 'edited', Date.now());
next(null);
},
function(next) {
posts.setPostField(pid, 'editor', uid);
next(null);
},
function(next) {
posts.setPostField(pid, 'content', content);
next(null);
}
]);
postSearch.remove(pid, function() {
postSearch.index(content, pid);

@ -24,9 +24,16 @@ var RDB = require('./redis.js'),
RDB.handle(err);
if (pids.length) {
Posts.getPostsByPids(pids, function(err, posts) {
callback(posts);
});
plugins.fireHook('filter:post.getTopic', pids, function(err, posts) {
if (!err & 0 < posts.length) {
Posts.getPostsByPids(pids, function(err, posts) {
plugins.fireHook('action:post.gotTopic', posts);
callback(posts);
});
} else {
callback(posts);
}
});
} else {
callback([]);
}
@ -131,6 +138,7 @@ var RDB = require('./redis.js'),
});
}
// TODO: this function is never called except from some debug route. clean up?
Posts.getPostData = function(pid, callback) {
RDB.hgetall('post:' + pid, function(err, data) {
if (err === null) {
@ -146,7 +154,14 @@ var RDB = require('./redis.js'),
Posts.getPostFields = function(pid, fields, callback) {
RDB.hmgetObject('post:' + pid, fields, function(err, data) {
if (err === null) {
callback(data);
// TODO: I think the plugins system needs an optional 'parameters' paramter so I don't have to do this:
data = data || {};
data.pid = pid;
data.fields = fields;
plugins.fireHook('filter:post.getFields', data, function(err, data) {
callback(data);
});
} else {
console.log(err);
}
@ -155,15 +170,28 @@ var RDB = require('./redis.js'),
Posts.getPostField = function(pid, field, callback) {
RDB.hget('post:' + pid, field, function(err, data) {
if (err === null)
callback(data);
else
if (err === null) {
// TODO: I think the plugins system needs an optional 'parameters' paramter so I don't have to do this:
data = data || {};
data.pid = pid;
data.field = field;
plugins.fireHook('filter:post.getField', data, function(err, data) {
callback(data);
});
} else {
console.log(err);
}
});
}
Posts.setPostField = function(pid, field, value) {
Posts.setPostField = function(pid, field, value, done) {
RDB.hset('post:' + pid, field, value);
plugins.fireHook('action:post.setField', {
'pid': pid,
'field': field,
'value': value
}, done);
}
Posts.getPostsByPids = function(pids, callback) {
@ -406,13 +434,17 @@ var RDB = require('./redis.js'),
}
Posts.getPostsByUid = function(uid, start, end, callback) {
user.getPostIds(uid, start, end, function(pids) {
if (pids && pids.length) {
Posts.getPostsByPids(pids, function(err, posts) {
callback(posts);
plugins.fireHook('filter:post.getTopic', pids, function(err, posts) {
if (!err & 0 < posts.length) {
Posts.getPostsByPids(pids, function(err, posts) {
plugins.fireHook('action:post.gotTopic', posts);
callback(posts);
});
} else {
callback(posts);
}
});
} else
callback([]);

@ -154,6 +154,16 @@ var user = require('./../user.js'),
});
});
app.get('/notifications', function(req, res) {
if (req.user && req.user.uid) {
user.notifications.getAll(req.user.uid, null, null, function(err, notifications) {
res.json({
notifications: notifications
});
});
} else res.send(403);
});
app.get('/confirm/:id', function (req, res) {
user.email.confirm(req.params.id, function (data) {
if (data.status === 'ok') {

@ -303,7 +303,7 @@ var RDB = require('./redis.js'),
pids.reverse();
async.detectSeries(pids, function(pid, next) {
RDB.hget('post:' + pid, 'deleted', function(err, deleted) {
posts.getPostField(pid, 'deleted', function(deleted) {
if (deleted === '0') next(true);
else next(false);
});

@ -31,7 +31,7 @@ Upgrade.upgrade = function() {
async.each(keys, function(key, next) {
RDB.zrange(key, 0, -1, function(err, nids) {
async.each(nids, function(nid, next) {
notifications.get(nid, function(notif_data) {
notifications.get(nid, null, function(notif_data) {
RDB.zadd(key, notif_data.datetime, nid, next);
});
}, next);

@ -902,7 +902,7 @@ var utils = require('./../public/src/utils.js'),
if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) {
notifications.get(nid, function(notif_data) {
notifications.get(nid, uid, function(notif_data) {
unread.push(notif_data);
next();
});
@ -924,7 +924,7 @@ var utils = require('./../public/src/utils.js'),
if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) {
notifications.get(nid, function(notif_data) {
notifications.get(nid, uid, function(notif_data) {
read.push(notif_data);
next();
});
@ -945,13 +945,44 @@ var utils = require('./../public/src/utils.js'),
callback(notifications);
});
},
getAll: function(uid, limit, before, callback) {
var now = new Date();
if (!limit || parseInt(limit) <= 0) limit = 25;
if (before) before = new Date(parseInt(before, 10));
RDB.multi()
.zrevrangebyscore('uid:' + uid + ':notifications:read', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit)
.zrevrangebyscore('uid:' + uid + ':notifications:unread', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit)
.exec(function(err, results) {
// Merge the read and unread notifications
var nids = results[0].concat(results[1]);
async.map(nids, function(nid, next) {
notifications.get(nid, uid, function(notif_data) {
next(null, notif_data);
});
}, function(err, notifs) {
notifs = notifs.sort(function(a, b) {
return parseInt(b.datetime, 10) - parseInt(a.datetime, 10);
}).map(function(notif) {
notif.datetimeISO = new Date(parseInt(notif.datetime, 10)).toISOString();
notif.readClass = !notif.read ? 'unread' : '';
return notif;
});
callback(err, notifs);
})
});
},
getUnreadCount: function(uid, callback) {
RDB.zcount('uid:' + uid + ':notifications:unread', 0, 10, callback);
},
getUnreadByUniqueId: function(uid, uniqueId, callback) {
RDB.zrange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) {
async.filter(nids, function(nid, next) {
notifications.get(nid, function(notifObj) {
notifications.get(nid, uid, function(notifObj) {
if (notifObj.uniqueId === uniqueId) next(true);
else next(false);
});

@ -286,7 +286,7 @@ var express = require('express'),
// Basic Routes (entirely client-side parsed, goal is to move the rest of the crap in this file into this one section)
(function () {
var routes = ['login', 'register', 'account', 'recent', 'unread', 'popular', 'active', '403', '404'];
var routes = ['login', 'register', 'account', 'recent', 'unread', 'notifications', '403', '404'];
for (var i = 0, ii = routes.length; i < ii; i++) {
(function (route) {

Loading…
Cancel
Save