Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to remove old rating when liking/disliking #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,37 @@ const changeRating = function(userId, itemId, options){
}

const removeRating = options.removeRating ? true : false;
const removeOldRating = options.removeOldRating ? true : false;

const feelingItemSet = options.liked ? Key.itemLikedBySet(itemId) : Key.itemDislikedBySet(itemId);
const feelingUserSet = options.liked ? Key.userLikedSet(userId) : Key.userDislikedSet(userId);
const mostFeelingSet = options.liked ? Key.mostLiked() : Key.mostDisliked();

const feelingItemSetOpposite = options.liked ? Key.itemDislikedBySet(itemId) : Key.itemLikedBySet(itemId);
const feelingUserSetOpposite = options.liked ? Key.userDislikedSet(userId) : Key.userLikedSet(userId);
const mostFeelingSetOpposite = options.liked ? Key.mostDisliked() : Key.mostLiked();

return new Promise((resolve, reject) => {
Promise.resolve().then(() => {
// check if the rating is already stored
return client.sismemberAsync(feelingItemSet, userId);
if (removeOldRating) {
// check if opposite rating is already stored
return client.sismemberAsync(feelingItemSetOpposite, userId).then((result) => {
if (result > 0) {
// only remove opposite rating if it already exists
client.zincrby(mostFeelingSetOpposite, -1, itemId);
return client.sremAsync(feelingUserSetOpposite, itemId).then(() => {
return client.sremAsync(feelingItemSetOpposite, userId);
}).then(() => {
return client.sismemberAsync(feelingItemSet, userId);
});
} else {
return client.sismemberAsync(feelingItemSet, userId);
}
});
} else {
// check if the rating is already stored
return client.sismemberAsync(feelingItemSet, userId);
}
}).then((result) => {
// only increment the most feeling set if it doesn't already exist
if (result === 0 && !removeRating) {
Expand Down Expand Up @@ -78,11 +100,13 @@ const changeRating = function(userId, itemId, options){

const liked = function(userId, itemId, options = {}){
options.liked = true;
options.removeOldRating = true;
return changeRating(userId, itemId, options);
};

const disliked = function(userId, itemId, options = {}){
options.liked = false;
options.removeOldRating = true;
return changeRating(userId, itemId, options);
};

Expand Down