2016-07-27 10:38:43 +02:00
|
|
|
import Posts from "meteor/nova:posts";
|
|
|
|
import Users from 'meteor/nova:users';
|
|
|
|
|
|
|
|
var hasSubscribedItem = function (item, user) {
|
|
|
|
return item.subscribers && item.subscribers.indexOf(user._id) != -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
var addSubscribedItem = function (userId, item, collectionName) {
|
|
|
|
var field = 'telescope.subscribedItems.' + collectionName;
|
|
|
|
var add = {};
|
|
|
|
add[field] = item;
|
|
|
|
Meteor.users.update({_id: userId}, {
|
|
|
|
$addToSet: add
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var removeSubscribedItem = function (userId, itemId, collectionName) {
|
|
|
|
var field = 'telescope.subscribedItems.' + collectionName;
|
|
|
|
var remove = {};
|
|
|
|
remove[field] = {itemId: itemId};
|
|
|
|
Meteor.users.update({_id: userId}, {
|
|
|
|
$pull: remove
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-07-27 18:00:40 +02:00
|
|
|
export var subscribeItem = function (collection, itemId, user) {
|
2016-07-27 10:38:43 +02:00
|
|
|
|
|
|
|
var item = collection.findOne(itemId),
|
|
|
|
collectionName = collection._name.slice(0,1).toUpperCase() + collection._name.slice(1);
|
|
|
|
|
|
|
|
if (!user || !item || hasSubscribedItem(item, user))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// author can't subscribe item
|
|
|
|
if (item.userId && item.userId === user._id)
|
2016-08-07 19:26:08 +09:00
|
|
|
return false;
|
2016-07-27 10:38:43 +02:00
|
|
|
|
|
|
|
// Subscribe
|
|
|
|
var result = collection.update({_id: itemId, subscribers: { $ne: user._id }}, {
|
|
|
|
$addToSet: {subscribers: user._id},
|
|
|
|
$inc: {subscriberCount: 1}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result > 0) {
|
|
|
|
// Add item to list of subscribed items
|
|
|
|
var obj = {
|
|
|
|
itemId: item._id,
|
|
|
|
subscribedAt: new Date()
|
|
|
|
};
|
|
|
|
addSubscribedItem(user._id, obj, collectionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2016-07-27 18:00:40 +02:00
|
|
|
export var unsubscribeItem = function (collection, itemId, user) {
|
|
|
|
|
|
|
|
var item = collection.findOne(itemId),
|
2016-07-27 10:38:43 +02:00
|
|
|
collectionName = collection._name.slice(0,1).toUpperCase()+collection._name.slice(1);
|
|
|
|
|
|
|
|
if (!user || !item || !hasSubscribedItem(item, user))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Unsubscribe
|
|
|
|
var result = collection.update({_id: itemId, subscribers: user._id }, {
|
|
|
|
$pull: {subscribers: user._id},
|
|
|
|
$inc: {subscriberCount: -1}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result > 0) {
|
|
|
|
// Remove item from list of subscribed items
|
|
|
|
removeSubscribedItem(user._id, itemId, collectionName);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
Meteor.methods({
|
2016-08-07 19:26:08 +09:00
|
|
|
"posts.subscribe": function(postId) {
|
2016-07-27 10:38:43 +02:00
|
|
|
check(postId, String);
|
|
|
|
return subscribeItem.call(this, Posts, postId, Meteor.user());
|
|
|
|
},
|
2016-08-07 19:26:08 +09:00
|
|
|
"posts.unsubscribe": function(postId) {
|
2016-07-27 10:38:43 +02:00
|
|
|
check(postId, String);
|
|
|
|
return unsubscribeItem.call(this, Posts, postId, Meteor.user());
|
|
|
|
}
|
|
|
|
});
|