all files / src/socket.io/ notifications.js

31.82% Statements 14/44
0% Branches 0/24
0% Functions 0/12
31.82% Lines 14/44
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                           
"use strict";
 
var async = require('async');
var user = require('../user');
var notifications = require('../notifications');
var utils = require('../../public/src/utils');
 
var SocketNotifs = {};
 
SocketNotifs.get = function(socket, data, callback) {
	if (data && Array.isArray(data.nids) && socket.uid) {
		user.notifications.getNotifications(data.nids, socket.uid, callback);
	} else {
		user.notifications.get(socket.uid, callback);
	}
};
 
SocketNotifs.loadMore = function(socket, data, callback) {
	if (!data || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) {
		return callback(new Error('[[error:invalid-data]]'));
	}
	if (!socket.uid) {
		return callback(new Error('[[error:no-privileges]]'));
	}
	var start = parseInt(data.after, 10);
	var stop = start + 20;
	user.notifications.getAll(socket.uid, start, stop, function(err, notifications) {
		if (err) {
			return callback(err);
		}
		callback(null, {notifications: notifications, nextStart: stop});
	});
};
 
SocketNotifs.getCount = function(socket, data, callback) {
	user.notifications.getUnreadCount(socket.uid, callback);
};
 
SocketNotifs.deleteAll = function(socket, data, callback) {
	if (!socket.uid) {
		return callback(new Error('[[error:no-privileges]]'));
	}
 
	user.notifications.deleteAll(socket.uid, callback);
};
 
SocketNotifs.markRead = function(socket, nid, callback) {
	notifications.markRead(nid, socket.uid, callback);
};
 
SocketNotifs.markUnread = function(socket, nid, callback) {
	notifications.markUnread(nid, socket.uid, callback);
};
 
SocketNotifs.markAllRead = function(socket, data, callback) {
	notifications.markAllRead(socket.uid, callback);
};
 
SocketNotifs.generatePath = function(socket, nid, callback) {
	if (!socket.uid) {
		return callback(new Error('[[error:no-privileges]]'));;
	}
	async.waterfall([
		function (next) {
			notifications.get(nid, next);
		},
		function (notification, next) {
			if (!notification) {
				return next(null, '');
			}
			user.notifications.generateNotificationPaths([notification], socket.uid, next);
		},
		function (notificationsData, next) {
			if (notificationsData && notificationsData.length) {
				next(null, notificationsData[0].path);
			} else {
				next();
			}
		}
	], callback);
};
 
module.exports = SocketNotifs;