mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	favourite changes
favourites.js no longer makes socket calls, moved that code into socket.io/posts.js. it also makes a single socket call when you downvote a post that you previously upvoted.
This commit is contained in:
		| @@ -17,5 +17,9 @@ | |||||||
| 	"title-too-short" : "Please enter a longer title. At least %1 characters.", | 	"title-too-short" : "Please enter a longer title. At least %1 characters.", | ||||||
| 	"title-too-long" : "Please enter a shorter title. Titles can't be longer than %1 characters.", | 	"title-too-long" : "Please enter a shorter title. Titles can't be longer than %1 characters.", | ||||||
| 	"too-many-posts" : "You can only post every %1 seconds.'", | 	"too-many-posts" : "You can only post every %1 seconds.'", | ||||||
| 	"file-too-big" : "Maximum allowed file size is %1 kbs" | 	"file-too-big" : "Maximum allowed file size is %1 kbs", | ||||||
|  |  | ||||||
|  | 	"cant-vote-self-post": "You cannot vote for your own post", | ||||||
|  | 	"already-favourited": "You already favourited this post", | ||||||
|  | 	"already-unfavourited": "You alread unfavourited this post" | ||||||
| } | } | ||||||
| @@ -69,15 +69,8 @@ | |||||||
|  |  | ||||||
| 	"favourite": "Favourite", | 	"favourite": "Favourite", | ||||||
| 	"favourites": "Favourites", | 	"favourites": "Favourites", | ||||||
| 	"favourites.not_logged_in.title": "Not Logged In", |  | ||||||
| 	"favourites.not_logged_in.message": "Please log in in order to favourite this post", |  | ||||||
| 	"favourites.has_no_favourites": "You don't have any favourites, favourite some posts to see them here!", | 	"favourites.has_no_favourites": "You don't have any favourites, favourite some posts to see them here!", | ||||||
|  |  | ||||||
| 	"vote.not_logged_in.title": "Not Logged In", |  | ||||||
| 	"vote.not_logged_in.message": "Please log in in order to vote", |  | ||||||
| 	"vote.cant_vote_self.title": "Invalid Vote", |  | ||||||
| 	"vote.cant_vote_self.message": "You cannot vote for your own post", |  | ||||||
|  |  | ||||||
| 	"loading_more_posts": "Loading More Posts", | 	"loading_more_posts": "Loading More Posts", | ||||||
| 	"move_topic": "Move Topic", | 	"move_topic": "Move Topic", | ||||||
| 	"move_post": "Move Post", | 	"move_post": "Move Post", | ||||||
|   | |||||||
| @@ -143,7 +143,7 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', | |||||||
|  |  | ||||||
|  |  | ||||||
| 		ajaxify.register_events([ | 		ajaxify.register_events([ | ||||||
| 			'event:rep_up', 'event:rep_down', 'event:favourited', 'event:unfavourited', 'event:new_post', 'get_users_in_room', | 			'event:voted', 'event:favourited', 'event:new_post', 'get_users_in_room', | ||||||
| 			'event:topic_deleted', 'event:topic_restored', 'event:topic:locked', | 			'event:topic_deleted', 'event:topic_restored', 'event:topic:locked', | ||||||
| 			'event:topic_unlocked', 'event:topic_pinned', 'event:topic_unpinned', | 			'event:topic_unlocked', 'event:topic_pinned', 'event:topic_unpinned', | ||||||
| 			'event:topic_moved', 'event:post_edited', 'event:post_deleted', 'event:post_restored', | 			'event:topic_moved', 'event:post_edited', 'event:post_deleted', 'event:post_restored', | ||||||
| @@ -241,20 +241,12 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', | |||||||
| 			app.populateOnlineUsers(); | 			app.populateOnlineUsers(); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('event:rep_up', function(data) { | 		socket.on('event:voted', function(data) { | ||||||
| 			adjust_rep(1, data.pid, data.uid); | 			updatePostVotesAndUserReputation(data); | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		socket.on('event:rep_down', function(data) { |  | ||||||
| 			adjust_rep(-1, data.pid, data.uid); |  | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('event:favourited', function(data) { | 		socket.on('event:favourited', function(data) { | ||||||
| 			adjust_favourites(1, data.pid, data.uid); | 			updateFavouriteCount(data.post.pid, data.post.reputation); | ||||||
| 		}); |  | ||||||
|  |  | ||||||
| 		socket.on('event:unfavourited', function(data) { |  | ||||||
| 			adjust_favourites(-1, data.pid, data.uid); |  | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('event:new_post', function(data) { | 		socket.on('event:new_post', function(data) { | ||||||
| @@ -338,37 +330,31 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', | |||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('posts.upvote', function(data) { | 		socket.on('posts.upvote', function(pid) { | ||||||
| 			if (data && data.pid) { | 			toggleUpvoteDownvote(pid, true, false); | ||||||
| 				$('li[data-pid="' + data.pid + '"] .upvote').addClass('btn-primary upvoted'); |  | ||||||
| 			} |  | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('posts.downvote', function(data) { | 		socket.on('posts.downvote', function(pid) { | ||||||
| 			if (data && data.pid) { | 			toggleUpvoteDownvote(pid, false, true); | ||||||
| 				$('li[data-pid="' + data.pid + '"] .downvote').addClass('btn-primary downvoted'); |  | ||||||
| 			} |  | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('posts.unvote', function(data) { | 		socket.on('posts.unvote', function(pid) { | ||||||
| 			if (data && data.pid) { | 			toggleUpvoteDownvote(pid, false, false); | ||||||
| 				var post = $('li[data-pid="' + data.pid + '"]'); |  | ||||||
|  |  | ||||||
| 				post.find('.upvote').removeClass('btn-primary upvoted'); |  | ||||||
| 				post.find('.downvote').removeClass('btn-primary downvoted'); |  | ||||||
| 			} |  | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('posts.favourite', function(data) { | 		function toggleUpvoteDownvote(pid, upvote, downvote) { | ||||||
| 			if (data && data.pid) { | 			var post = $('li[data-pid="' + pid + '"]'); | ||||||
| 				toggleFavourite(data.pid, true); |  | ||||||
| 			} | 			post.find('.upvote').toggleClass('btn-primary upvoted', upvote); | ||||||
|  | 			post.find('.downvote').toggleClass('btn-primary downvoted', downvote); | ||||||
|  | 		} | ||||||
|  |  | ||||||
|  | 		socket.on('posts.favourite', function(pid) { | ||||||
|  | 			toggleFavourite(pid, true); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		socket.on('posts.unfavourite', function(data) { | 		socket.on('posts.unfavourite', function(pid) { | ||||||
| 			if (data && data.pid) { | 			toggleFavourite(pid, false); | ||||||
| 				toggleFavourite(data.pid, false); |  | ||||||
| 			} |  | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		function toggleFavourite(pid, isFavourited) { | 		function toggleFavourite(pid, isFavourited) { | ||||||
| @@ -406,26 +392,16 @@ define(['forum/pagination', 'forum/topic/threadTools', 'forum/topic/postTools', | |||||||
| 			$('.thread_active_users [data-uid="' + uid + '"]').removeClass('replying'); | 			$('.thread_active_users [data-uid="' + uid + '"]').removeClass('replying'); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		function adjust_rep(value, pid, uid) { | 		function updatePostVotesAndUserReputation(data) { | ||||||
| 			var votes = $('li[data-pid="' + pid + '"] .votes'), | 			var votes = $('li[data-pid="' + data.post.pid + '"] .votes'), | ||||||
| 				reputationElements = $('.reputation[data-uid="' + uid + '"]'), | 				reputationElements = $('.reputation[data-uid="' + data.post.uid + '"]'); | ||||||
| 				currentVotes = parseInt(votes.attr('data-votes'), 10), |  | ||||||
| 				reputation = parseInt(reputationElements.attr('data-reputation'), 10); |  | ||||||
|  |  | ||||||
| 			currentVotes += value; | 			votes.html(data.post.votes).attr('data-votes', data.post.votes); | ||||||
| 			reputation += value; | 			reputationElements.html(data.user.reputation).attr('data-reputation', data.user.reputation); | ||||||
|  |  | ||||||
| 			votes.html(currentVotes).attr('data-votes', currentVotes); |  | ||||||
| 			reputationElements.html(reputation).attr('data-reputation', reputation); |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		function adjust_favourites(value, pid, uid) { | 		function updateFavouriteCount(pid, value) { | ||||||
| 			var favourites = $('li[data-pid="' + pid + '"] .favouriteCount'), | 			$('li[data-pid="' + pid + '"] .favouriteCount').html(value).attr('data-favourites', value); | ||||||
| 				currentFavourites = parseInt(favourites.attr('data-favourites'), 10); |  | ||||||
|  |  | ||||||
| 			currentFavourites += value; |  | ||||||
|  |  | ||||||
| 			favourites.html(currentFavourites).attr('data-favourites', currentFavourites); |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		function set_locked_state(locked, alert) { | 		function set_locked_state(locked, alert) { | ||||||
|   | |||||||
| @@ -111,6 +111,10 @@ define(['composer', 'share'], function(composer, share) { | |||||||
| 		socket.emit(method, { | 		socket.emit(method, { | ||||||
| 			pid: pid, | 			pid: pid, | ||||||
| 			room_id: app.currentRoom | 			room_id: app.currentRoom | ||||||
|  | 		}, function(err) { | ||||||
|  | 			if (err) { | ||||||
|  | 				app.alertError(err.message); | ||||||
|  | 			} | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		return false; | 		return false; | ||||||
| @@ -123,6 +127,10 @@ define(['composer', 'share'], function(composer, share) { | |||||||
| 		socket.emit(currentState ? 'posts.unvote' : method , { | 		socket.emit(currentState ? 'posts.unvote' : method , { | ||||||
| 			pid: post.attr('data-pid'), | 			pid: post.attr('data-pid'), | ||||||
| 			room_id: app.currentRoom | 			room_id: app.currentRoom | ||||||
|  | 		}, function(err) { | ||||||
|  | 			if (err) { | ||||||
|  | 				app.alertError(err.message); | ||||||
|  | 			} | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		return false; | 		return false; | ||||||
|   | |||||||
| @@ -8,67 +8,50 @@ var async = require('async'), | |||||||
| (function (Favourites) { | (function (Favourites) { | ||||||
| 	"use strict"; | 	"use strict"; | ||||||
|  |  | ||||||
| 	function vote(type, unvote, pid, room_id, uid, socket, callback) { | 	function vote(type, unvote, pid, uid, callback) { | ||||||
| 		var	websockets = require('./socket.io'); | 		uid = parseInt(uid, 10); | ||||||
|  |  | ||||||
| 		if (uid === 0) { | 		if (uid === 0) { | ||||||
| 			return socket.emit('event:alert', { | 			return callback(new Error('[[error:not-logged-in]]')); | ||||||
| 				alert_id: 'post_vote', |  | ||||||
| 				title: '[[topic:vote.not_logged_in.title]]', |  | ||||||
| 				message: '[[topic:vote.not_logged_in.message]]', |  | ||||||
| 				type: 'danger', |  | ||||||
| 				timeout: 5000 |  | ||||||
| 			}); |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		posts.getPostFields(pid, ['uid', 'timestamp'], function (err, postData) { | 		posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function (err, postData) { | ||||||
| 			if (uid === parseInt(postData.uid, 10)) { | 			if (err) { | ||||||
| 				socket.emit('event:alert', { | 				return callback(err); | ||||||
| 					alert_id: 'post_vote', |  | ||||||
| 					title: '[[topic:vote.cant_vote_self.title]]', |  | ||||||
| 					message: '[[topic:vote.cant_vote_self.message]]', |  | ||||||
| 					type: 'danger', |  | ||||||
| 					timeout: 5000 |  | ||||||
| 				}); |  | ||||||
|  |  | ||||||
| 				if (callback) { |  | ||||||
| 					callback(false); |  | ||||||
| 				} |  | ||||||
|  |  | ||||||
| 				return false; |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if(type === 'upvote' || !unvote) { | 			if (uid === parseInt(postData.uid, 10)) { | ||||||
| 				db.sortedSetAdd('uid: ' + uid + ':upvote', postData.timestamp, pid); | 				return callback(new Error('[[error:cant-vote-self-post]]')); | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			if(type === 'upvote' && !unvote) { | ||||||
|  | 				db.sortedSetAdd('uid:' + uid + ':upvote', postData.timestamp, pid); | ||||||
| 			} else { | 			} else { | ||||||
| 				db.sortedSetRemove('uid: ' + uid + ':upvote', pid); | 				db.sortedSetRemove('uid:' + uid + ':upvote', pid); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if(type === 'upvote' || unvote) { | 			if(type === 'upvote' || unvote) { | ||||||
| 				db.sortedSetRemove('uid: ' + uid + ':downvote', pid); | 				db.sortedSetRemove('uid:' + uid + ':downvote', pid); | ||||||
| 			} else { | 			} else { | ||||||
| 				db.sortedSetAdd('uid: ' + uid + ':downvote', postData.timestamp, pid); | 				db.sortedSetAdd('uid:' + uid + ':downvote', postData.timestamp, pid); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) { | 			user[type === 'upvote' ? 'incrementUserFieldBy' : 'decrementUserFieldBy'](postData.uid, 'reputation', 1, function (err, newreputation) { | ||||||
| 				db.sortedSetAdd('users:reputation', newreputation, postData.uid); | 				if (err) { | ||||||
| 			}); | 					return callback(err); | ||||||
|  |  | ||||||
| 			if (room_id) { |  | ||||||
| 				websockets.in(room_id).emit('event:' + (type === 'upvote' ? 'rep_up' : 'rep_down'), { |  | ||||||
| 					uid: postData.uid, |  | ||||||
| 					pid: pid |  | ||||||
| 				}); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			socket.emit('posts.' + (unvote ? 'unvote' : type), { |  | ||||||
| 				pid: pid |  | ||||||
| 			}); |  | ||||||
|  |  | ||||||
| 			adjustPostVotes(pid, uid, type, unvote, function() { |  | ||||||
| 				if (callback) { |  | ||||||
| 					callback(); |  | ||||||
| 				} | 				} | ||||||
|  |  | ||||||
|  | 				db.sortedSetAdd('users:reputation', newreputation, postData.uid); | ||||||
|  |  | ||||||
|  | 				adjustPostVotes(pid, uid, type, unvote, function(err, votes) { | ||||||
|  | 					postData.votes = votes; | ||||||
|  | 					callback(err, { | ||||||
|  | 						user: { | ||||||
|  | 							reputation: newreputation | ||||||
|  | 						}, | ||||||
|  | 						post: postData | ||||||
|  | 					}); | ||||||
|  | 				}); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
| @@ -79,21 +62,19 @@ var async = require('async'), | |||||||
| 		async.series([ | 		async.series([ | ||||||
| 			function(next) { | 			function(next) { | ||||||
| 				if (unvote) { | 				if (unvote) { | ||||||
| 					db.setRemove('pid:' + pid + ':' + type, uid, function(err) { | 					db.setRemove('pid:' + pid + ':' + type, uid, next); | ||||||
| 						next(err); |  | ||||||
| 					}); |  | ||||||
| 				} else { | 				} else { | ||||||
| 					db.setAdd('pid:' + pid + ':' + type, uid, function(err) { | 					db.setAdd('pid:' + pid + ':' + type, uid, next); | ||||||
| 						next(err); |  | ||||||
| 					}); |  | ||||||
| 				} | 				} | ||||||
| 			}, | 			}, | ||||||
| 			function(next) { | 			function(next) { | ||||||
| 				db.setRemove('pid:' + pid + ':' + notType, uid, function(err) { | 				db.setRemove('pid:' + pid + ':' + notType, uid, next); | ||||||
| 					next(err); |  | ||||||
| 				}); |  | ||||||
| 			} | 			} | ||||||
| 		], function(err) { | 		], function(err) { | ||||||
|  | 			if (err) { | ||||||
|  | 				return callback(err); | ||||||
|  | 			} | ||||||
|  |  | ||||||
| 			async.parallel({ | 			async.parallel({ | ||||||
| 				upvotes: function(next) { | 				upvotes: function(next) { | ||||||
| 					db.setCount('pid:' + pid + ':upvote', next); | 					db.setCount('pid:' + pid + ':upvote', next); | ||||||
| @@ -102,48 +83,46 @@ var async = require('async'), | |||||||
| 					db.setCount('pid:' + pid + ':downvote', next); | 					db.setCount('pid:' + pid + ':downvote', next); | ||||||
| 				} | 				} | ||||||
| 			}, function(err, results) { | 			}, function(err, results) { | ||||||
| 				posts.setPostField(pid, 'votes', parseInt(results.upvotes, 10) - parseInt(results.downvotes, 10)); | 				if (err) { | ||||||
|  | 					return callback(err); | ||||||
|  | 				} | ||||||
|  | 				var voteCount = parseInt(results.upvotes, 10) - parseInt(results.downvotes, 10); | ||||||
|  | 				posts.setPostField(pid, 'votes', voteCount, function(err) { | ||||||
|  | 					callback(err, voteCount); | ||||||
|  | 				}); | ||||||
| 			}); | 			}); | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 			if (callback) { | 	Favourites.upvote = function(pid, uid, callback) { | ||||||
| 				callback(); | 		toggleVote('upvote', pid, uid, callback); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
|  | 	Favourites.downvote = function(pid, uid, callback) { | ||||||
|  | 		toggleVote('downvote', pid, uid, callback); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
|  | 	function toggleVote(type, pid, uid, callback) { | ||||||
|  | 		Favourites.unvote(pid, uid, function(err) { | ||||||
|  | 			if (err) { | ||||||
|  | 				return callback(err); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			vote(type, false, pid, uid, callback); | ||||||
| 		}); | 		}); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	Favourites.upvote = function(pid, room_id, uid, socket) { | 	Favourites.unvote = function(pid, uid, callback) { | ||||||
| 		toggleVote('upvote', pid, room_id, uid, socket); |  | ||||||
| 	}; |  | ||||||
|  |  | ||||||
| 	Favourites.downvote = function(pid, room_id, uid, socket) { |  | ||||||
| 		toggleVote('downvote', pid, room_id, uid, socket); |  | ||||||
| 	}; |  | ||||||
|  |  | ||||||
| 	function toggleVote(type, pid, room_id, uid, socket) { |  | ||||||
| 		Favourites.unvote(pid, room_id, uid, socket, function(err) { |  | ||||||
| 			vote(type, false, pid, room_id, uid, socket); |  | ||||||
| 		}); |  | ||||||
| 	} |  | ||||||
|  |  | ||||||
| 	Favourites.unvote = function(pid, room_id, uid, socket, callback) { |  | ||||||
| 		var	websockets = require('./socket.io'); |  | ||||||
|  |  | ||||||
| 		Favourites.hasVoted(pid, uid, function(err, voteStatus) { | 		Favourites.hasVoted(pid, uid, function(err, voteStatus) { | ||||||
| 			if (voteStatus.upvoted || voteStatus.downvoted) { | 			if (err) { | ||||||
| 				socket.emit('posts.unvote', { | 				return callback(err); | ||||||
| 					pid: pid |  | ||||||
| 				}); |  | ||||||
|  |  | ||||||
| 				return vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, room_id, uid, socket, function() { |  | ||||||
| 					if (callback) { |  | ||||||
| 						callback(err); |  | ||||||
| 					} |  | ||||||
| 				}); |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (callback) { | 			if (!voteStatus || (!voteStatus.upvoted && !voteStatus.downvoted)) { | ||||||
| 				callback(err); | 				return callback(); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			vote(voteStatus.upvoted ? 'downvote' : 'upvote', true, pid, uid, callback); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| @@ -164,77 +143,64 @@ var async = require('async'), | |||||||
| 		}, callback); | 		}, callback); | ||||||
| 	}; | 	}; | ||||||
|  |  | ||||||
| 	Favourites.favourite = function (pid, room_id, uid, socket) { | 	Favourites.favourite = function (pid, uid, callback) { | ||||||
| 		var	websockets = require('./socket.io'); | 		toggleFavourite('favourite', pid, uid, callback); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
|  | 	Favourites.unfavourite = function(pid, uid, callback) { | ||||||
|  | 		toggleFavourite('unfavourite', pid, uid, callback); | ||||||
|  | 	}; | ||||||
|  |  | ||||||
|  | 	function toggleFavourite(type, pid, uid, callback) { | ||||||
| 		if (uid === 0) { | 		if (uid === 0) { | ||||||
| 			return socket.emit('event:alert', { | 			return callback(new Error('[[error:not-logged-in]]')); | ||||||
| 				alert_id: 'post_favourite', |  | ||||||
| 				title: '[[topic:favourites.not_logged_in.title]]', |  | ||||||
| 				message: '[[topic:favourites.not_logged_in.message]]', |  | ||||||
| 				type: 'danger', |  | ||||||
| 				timeout: 5000 |  | ||||||
| 			}); |  | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		posts.getPostFields(pid, ['uid', 'timestamp'], function (err, postData) { | 		posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function (err, postData) { | ||||||
|  | 			if (err) { | ||||||
|  | 				return callback(err); | ||||||
|  | 			} | ||||||
|  |  | ||||||
| 			Favourites.hasFavourited(pid, uid, function (err, hasFavourited) { | 			Favourites.hasFavourited(pid, uid, function (err, hasFavourited) { | ||||||
| 				if (!hasFavourited) { | 				if (err) { | ||||||
|  | 					return callback(err); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				if (type === 'favourite' && hasFavourited) { | ||||||
|  | 					return callback(new Error('[[error:already-favourited]]')); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				if (type === 'unfavourite' && !hasFavourited) { | ||||||
|  | 					return callback(new Error('[[error:alrady-unfavourited]]')); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				if (type === 'favourite') { | ||||||
| 					db.sortedSetAdd('uid:' + uid + ':favourites', postData.timestamp, pid); | 					db.sortedSetAdd('uid:' + uid + ':favourites', postData.timestamp, pid); | ||||||
| 					db.setAdd('pid:' + pid + ':users_favourited', uid, function(err) { | 				} else { | ||||||
| 						db.setCount('pid:' + pid + ':users_favourited', function(err, count) { |  | ||||||
| 							posts.setPostField(pid, 'reputation', count); |  | ||||||
| 						}); |  | ||||||
| 					}); |  | ||||||
|  |  | ||||||
| 					if (room_id) { |  | ||||||
| 						websockets.in(room_id).emit('event:favourited', { |  | ||||||
| 							uid: uid !== postData.uid ? postData.uid : 0, |  | ||||||
| 							pid: pid |  | ||||||
| 						}); |  | ||||||
| 					} |  | ||||||
|  |  | ||||||
| 					socket.emit('posts.favourite', { |  | ||||||
| 						pid: pid |  | ||||||
| 					}); |  | ||||||
| 				} |  | ||||||
| 			}); |  | ||||||
| 		}); |  | ||||||
| 	}; |  | ||||||
|  |  | ||||||
| 	Favourites.unfavourite = function(pid, room_id, uid, socket) { |  | ||||||
| 		var	websockets = require('./socket.io'); |  | ||||||
|  |  | ||||||
| 		if (uid === 0) { |  | ||||||
| 			return; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		posts.getPostField(pid, 'uid', function (err, uid_of_poster) { |  | ||||||
| 			Favourites.hasFavourited(pid, uid, function (err, hasFavourited) { |  | ||||||
| 				if (hasFavourited) { |  | ||||||
| 					db.sortedSetRemove('uid:' + uid + ':favourites', pid); | 					db.sortedSetRemove('uid:' + uid + ':favourites', pid); | ||||||
| 					db.setRemove('pid:' + pid + ':users_favourited', uid, function(err) { | 				} | ||||||
| 						db.setCount('pid:' + pid + ':users_favourited', function(err, count) { |  | ||||||
| 							posts.setPostField(pid, 'reputation', count); |  | ||||||
|  | 				db[type === 'favourite' ? 'setAdd' : 'setRemove']('pid:' + pid + ':users_favourited', uid, function(err) { | ||||||
|  | 					if (err) { | ||||||
|  | 						return callback(err); | ||||||
|  | 					} | ||||||
|  |  | ||||||
|  | 					db.setCount('pid:' + pid + ':users_favourited', function(err, count) { | ||||||
|  | 						if (err) { | ||||||
|  | 							return callback(err); | ||||||
|  | 						} | ||||||
|  | 						postData.reputation = count; | ||||||
|  | 						posts.setPostField(pid, 'reputation', count, function(err) { | ||||||
|  | 							callback(err, { | ||||||
|  | 								post: postData | ||||||
|  | 							}); | ||||||
| 						}); | 						}); | ||||||
| 					}); | 					}); | ||||||
|  | 				}); | ||||||
| 					if (room_id) { |  | ||||||
| 						websockets.in(room_id).emit('event:unfavourited', { |  | ||||||
| 							uid: uid !== uid_of_poster ? uid_of_poster : 0, |  | ||||||
| 							pid: pid |  | ||||||
| 						}); |  | ||||||
| 					} |  | ||||||
|  |  | ||||||
| 					if (socket) { |  | ||||||
| 						socket.emit('posts.unfavourite', { |  | ||||||
| 							pid: pid |  | ||||||
| 						}); |  | ||||||
| 					} |  | ||||||
| 				} |  | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
| 	}; | 	} | ||||||
|  |  | ||||||
| 	Favourites.hasFavourited = function(pid, uid, callback) { | 	Favourites.hasFavourited = function(pid, uid, callback) { | ||||||
| 		db.isSetMember('pid:' + pid + ':users_favourited', uid, callback); | 		db.isSetMember('pid:' + pid + ':users_favourited', uid, callback); | ||||||
|   | |||||||
| @@ -11,7 +11,7 @@ var	async = require('async'), | |||||||
| 	notifications = require('../notifications'), | 	notifications = require('../notifications'), | ||||||
| 	groups = require('../groups'), | 	groups = require('../groups'), | ||||||
| 	user = require('../user'), | 	user = require('../user'), | ||||||
| 	index = require('./index'), | 	websockets = require('./index'), | ||||||
|  |  | ||||||
| 	SocketPosts = {}; | 	SocketPosts = {}; | ||||||
|  |  | ||||||
| @@ -40,38 +40,50 @@ SocketPosts.reply = function(socket, data, callback) { | |||||||
| 				posts: [postData] | 				posts: [postData] | ||||||
| 			}; | 			}; | ||||||
|  |  | ||||||
| 			index.server.sockets.emit('event:new_post', socketData); | 			websockets.server.sockets.emit('event:new_post', socketData); | ||||||
|  |  | ||||||
| 			callback(); | 			callback(); | ||||||
| 		} | 		} | ||||||
| 	}); | 	}); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketPosts.upvote = function(socket, data) { | SocketPosts.upvote = function(socket, data, callback) { | ||||||
| 	favouriteCommand('upvote', socket, data); | 	favouriteCommand('upvote', 'voted', socket, data, callback); | ||||||
| 	sendNotificationToPostOwner(data, socket.uid, 'has upvoted your post'); | 	sendNotificationToPostOwner(data, socket.uid, 'has upvoted your post'); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketPosts.downvote = function(socket, data) { | SocketPosts.downvote = function(socket, data, callback) { | ||||||
| 	favouriteCommand('downvote', socket, data); | 	favouriteCommand('downvote', 'voted', socket, data, callback); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketPosts.unvote = function(socket, data) { | SocketPosts.unvote = function(socket, data, callback) { | ||||||
| 	favouriteCommand('unvote', socket, data); | 	favouriteCommand('unvote', 'voted', socket, data, callback); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketPosts.favourite = function(socket, data) { | SocketPosts.favourite = function(socket, data, callback) { | ||||||
| 	favouriteCommand('favourite', socket, data); | 	favouriteCommand('favourite', 'favourited', socket, data, callback); | ||||||
| 	sendNotificationToPostOwner(data, socket.uid, 'has favourited your post'); | 	sendNotificationToPostOwner(data, socket.uid, 'has favourited your post'); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| SocketPosts.unfavourite = function(socket, data) { | SocketPosts.unfavourite = function(socket, data, callback) { | ||||||
| 	favouriteCommand('unfavourite', socket, data); | 	favouriteCommand('unfavourite', 'favourited', socket, data, callback); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| function favouriteCommand(command, socket, data) { | function favouriteCommand(command, eventName, socket, data, callback) { | ||||||
|  |  | ||||||
| 	if(data && data.pid && data.room_id) { | 	if(data && data.pid && data.room_id) { | ||||||
| 		favourites[command](data.pid, data.room_id, socket.uid, socket); | 		favourites[command](data.pid, socket.uid, function(err, result) { | ||||||
|  | 			if (err) { | ||||||
|  | 				return callback(err); | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			socket.emit('posts.' + command, data.pid); | ||||||
|  |  | ||||||
|  | 			if(data.room_id && result && eventName) { | ||||||
|  | 				websockets.in(data.room_id).emit('event:' + eventName, result); | ||||||
|  | 			} | ||||||
|  | 			callback(); | ||||||
|  | 		}); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -140,7 +152,7 @@ SocketPosts.edit = function(socket, data, callback) { | |||||||
| 			return callback(err); | 			return callback(err); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		index.server.sockets.in('topic_' + results.topic.tid).emit('event:post_edited', { | 		websockets.server.sockets.in('topic_' + results.topic.tid).emit('event:post_edited', { | ||||||
| 			pid: data.pid, | 			pid: data.pid, | ||||||
| 			title: results.topic.title, | 			title: results.topic.title, | ||||||
| 			isMainPost: results.topic.isMainPost, | 			isMainPost: results.topic.isMainPost, | ||||||
| @@ -172,7 +184,7 @@ function deleteOrRestore(command, socket, data, callback) { | |||||||
| 		module.parent.exports.emitTopicPostStats(); | 		module.parent.exports.emitTopicPostStats(); | ||||||
|  |  | ||||||
| 		var eventName = command === 'restore' ? 'event:post_restored' : 'event:post_deleted'; | 		var eventName = command === 'restore' ? 'event:post_restored' : 'event:post_deleted'; | ||||||
| 		index.server.sockets.in('topic_' + data.tid).emit(eventName, { | 		websockets.server.sockets.in('topic_' + data.tid).emit(eventName, { | ||||||
| 			pid: data.pid | 			pid: data.pid | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user