all files / src/socket.io/topics/ unread.js

16.67% Statements 10/60
0% Branches 0/34
5.26% Functions 1/19
16.67% Lines 10/60
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                                                                                                                                                                         
'use strict';
 
var async = require('async');
 
var user = require('../../user');
var topics = require('../../topics');
 
module.exports = function(SocketTopics) {
 
	SocketTopics.markAsRead = function(socket, tids, callback) {
		if (!Array.isArray(tids) || !socket.uid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		topics.markAsRead(tids, socket.uid, function(err) {
			if (err) {
				return callback(err);
			}
 
			topics.pushUnreadCount(socket.uid);
 
			for (var i=0; i<tids.length; ++i) {
				topics.markTopicNotificationsRead(tids[i], socket.uid);
			}
			callback();
		});
	};
 
	SocketTopics.markTopicNotificationsRead = function(socket, tid, callback) {
		if (!tid || !socket.uid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
		topics.markTopicNotificationsRead(tid, socket.uid);
	};
 
	SocketTopics.markAllRead = function(socket, data, callback) {
		topics.markAllRead(socket.uid, function(err) {
			if (err) {
				return callback(err);
			}
 
			topics.pushUnreadCount(socket.uid);
 
			callback();
		});
	};
 
	SocketTopics.markCategoryTopicsRead = function(socket, cid, callback) {
		topics.getUnreadTids(cid, socket.uid, 0, -1, function(err, tids) {
			if (err) {
				return callback(err);
			}
 
			SocketTopics.markAsRead(socket, tids, callback);
		});
	};
 
	SocketTopics.markUnread = function(socket, tid, callback) {
		if (!tid || !socket.uid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
		topics.markUnread(tid, socket.uid, function(err) {
			if (err) {
				return callback(err);
			}
 
			topics.pushUnreadCount(socket.uid);
			callback();
		});
	};
 
	SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {
		if (!Array.isArray(tids)) {
			return callback(new Error('[[error:invalid-tid]]'));
		}
 
		if (!socket.uid) {
			return callback(new Error('[[error:no-privileges]]'));
		}
 
		user.isAdministrator(socket.uid, function(err, isAdmin) {
			if (err) {
				return callback(err);
			}
 
			async.each(tids, function(tid, next) {
				async.waterfall([
					function(next) {
						topics.exists(tid, next);
					},
					function(exists, next) {
						if (!exists) {
							return next(new Error('[[error:invalid-tid]]'));
						}
						topics.getTopicField(tid, 'cid', next);
					},
					function(cid, next) {
						user.isModerator(socket.uid, cid, next);
					},
					function(isMod, next) {
						if (!isAdmin && !isMod) {
							return next(new Error('[[error:no-privileges]]'));
						}
						topics.markAsUnreadForAll(tid, next);
					},
					function(next) {
						topics.updateRecent(tid, Date.now(), next);
					}
				], next);
			}, function(err) {
				if (err) {
					return callback(err);
				}
				topics.pushUnreadCount(socket.uid);
				callback();
			});
		});
	};
};