mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
have the mutation simulation and server operation returning the same result, add missing viewableIf, pass comment object to Telescope.operateOnItem
This commit is contained in:
parent
c1e1c68c23
commit
7c88d27d62
5 changed files with 95 additions and 109 deletions
|
@ -125,8 +125,8 @@ const resolvers = {
|
|||
},
|
||||
Mutation: {
|
||||
postVote(root, {postId, voteType}, context) {
|
||||
Meteor._sleepForMs(2000); // wait 2 seconds
|
||||
console.log("sleep done")
|
||||
Meteor._sleepForMs(2000); // wait 2 seconds for demonstration purpose
|
||||
console.log("sleep done");
|
||||
const post = Posts.findOne(postId);
|
||||
return Users.canDo(context.currentUser, `posts.${voteType}`) ? Telescope.operateOnItem(Posts, post, context.currentUser, voteType) : false;
|
||||
},
|
||||
|
|
|
@ -89,25 +89,29 @@ const VoteWithMutation = graphql(gql`
|
|||
postVote(postId: $postId, voteType: $voteType) {
|
||||
_id
|
||||
baseScore
|
||||
downvotes
|
||||
downvoters {
|
||||
_id
|
||||
}
|
||||
upvotes
|
||||
upvoters {
|
||||
_id
|
||||
}
|
||||
}
|
||||
}
|
||||
`, {
|
||||
props: ({ownProps, mutate}) => ({
|
||||
vote: ({post, voteType, currentUser}) => {
|
||||
const { baseScore, _id } = Telescope.operateOnItem(Posts, post, currentUser, voteType, true);
|
||||
|
||||
// console.log("// votedItem")
|
||||
// console.log(votedItem)
|
||||
const votedItem = Telescope.operateOnItem(Posts, post, currentUser, voteType, true);
|
||||
|
||||
return mutate({
|
||||
variables: {postId: post._id, voteType},
|
||||
optimisticResponse: {
|
||||
__typename: 'Mutation',
|
||||
postVote: {
|
||||
__typename: 'Post',
|
||||
_id,
|
||||
baseScore,
|
||||
...votedItem,
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
||||
}),
|
||||
|
|
|
@ -178,7 +178,7 @@ function CommentsNewUpvoteOwnComment (comment) {
|
|||
var commentAuthor = Users.findOne(comment.userId);
|
||||
|
||||
// upvote comment
|
||||
Telescope.operateOnItem(Comments, comment._id, commentAuthor, "upvote");
|
||||
Telescope.operateOnItem(Comments, comment, commentAuthor, "upvote");
|
||||
|
||||
return comment;
|
||||
}
|
||||
|
|
|
@ -83,7 +83,7 @@ Telescope.schemas.userData = new SimpleSchema({
|
|||
type: [Telescope.schemas.votes],
|
||||
publish: false,
|
||||
optional: true,
|
||||
// viewableIf: alwaysPublic,
|
||||
viewableIf: alwaysPublic,
|
||||
},
|
||||
/**
|
||||
An array containing posts downvotes
|
||||
|
@ -92,7 +92,7 @@ Telescope.schemas.userData = new SimpleSchema({
|
|||
type: [Telescope.schemas.votes],
|
||||
publish: false,
|
||||
optional: true,
|
||||
// viewableIf: alwaysPublic,
|
||||
viewableIf: alwaysPublic,
|
||||
},
|
||||
/**
|
||||
The user's email. Modifiable.
|
||||
|
@ -192,7 +192,7 @@ Telescope.schemas.userData = new SimpleSchema({
|
|||
type: [Telescope.schemas.votes],
|
||||
publish: false,
|
||||
optional: true,
|
||||
// viewableIf: alwaysPublic,
|
||||
viewableIf: alwaysPublic,
|
||||
},
|
||||
/**
|
||||
An array containing posts upvotes
|
||||
|
@ -201,7 +201,7 @@ Telescope.schemas.userData = new SimpleSchema({
|
|||
type: [Telescope.schemas.votes],
|
||||
publish: false,
|
||||
optional: true,
|
||||
// viewableIf: alwaysPublic,
|
||||
viewableIf: alwaysPublic,
|
||||
},
|
||||
/**
|
||||
A link to the user's homepage
|
||||
|
|
|
@ -20,133 +20,115 @@ Telescope.operateOnItem = function (collection, item, user, operation, isSimulat
|
|||
// console.log(user)
|
||||
// console.log(operation)
|
||||
|
||||
if (isSimulation) {
|
||||
// ------------------------------ Optimistic UI Simulation ------------------------------ //
|
||||
// ---------------------------- "Real" Server-Side Operation -------------------------- //
|
||||
|
||||
const simulatedItem = {
|
||||
__typename: 'Post',
|
||||
_id: item._id,
|
||||
upvoters: item.upvoters,
|
||||
upvotes: item.upvotes,
|
||||
baseScore: item.baseScore
|
||||
};
|
||||
// make sure item and user are defined, and user can perform the operation
|
||||
const collectionName = item.__typename ? Telescope.utils.getCollectionNameFromTypename(item.__typename) : item.getCollectionName();
|
||||
|
||||
// console.log("// before simulation")
|
||||
// console.log(operation)
|
||||
// console.log(_.clone(simulatedItem))
|
||||
if (
|
||||
!item ||
|
||||
!user ||
|
||||
!Users.canDo(user, `${collectionName}.${operation}`) ||
|
||||
operation === "upvote" && hasUpvotedItem ||
|
||||
operation === "downvote" && hasDownvotedItem ||
|
||||
operation === "cancelUpvote" && !hasUpvotedItem ||
|
||||
operation === "cancelDownvote" && !hasDownvotedItem
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (operation) {
|
||||
if (typeof item.upvoters === 'undefined') {
|
||||
item.upvoters = [];
|
||||
}
|
||||
|
||||
case "upvote":
|
||||
if (hasDownvotedItem) {
|
||||
Telescope.operateOnItem(collection, item, user, "cancelDownvote", true);
|
||||
}
|
||||
simulatedItem.upvoters.push(user._id);
|
||||
simulatedItem.upvotes += 1;
|
||||
simulatedItem.baseScore += votePower;
|
||||
break;
|
||||
if (typeof item.downvoters === 'undefined') {
|
||||
item.downvoters = [];
|
||||
}
|
||||
|
||||
case "downvote":
|
||||
if (hasUpvotedItem) {
|
||||
Telescope.operateOnItem(collection, item, user, "cancelUpvote", true);
|
||||
}
|
||||
simulatedItem.downvoters.push(user._id);
|
||||
simulatedItem.downvotes += 1;
|
||||
simulatedItem.baseScore -= votePower;
|
||||
// ------------------------------ Sync Callbacks ------------------------------ //
|
||||
|
||||
case "cancelUpvote":
|
||||
simulatedItem.upvoters = _.reject(simulatedItem.upvoters, u => u._id === user._id);
|
||||
simulatedItem.upvotes -= 1;
|
||||
simulatedItem.baseScore -= votePower;
|
||||
break;
|
||||
item = Telescope.callbacks.run(operation, item, user);
|
||||
|
||||
case "cancelDownvote":
|
||||
simulatedItem.downvoters = _.reject(simulatedItem.downvoters, u => u._id === user._id);
|
||||
simulatedItem.downvoters -= 1;
|
||||
simulatedItem.baseScore += votePower;
|
||||
break;
|
||||
}
|
||||
switch (operation) {
|
||||
|
||||
// console.log("// after simulation")
|
||||
// console.log(_.clone(simulatedItem))
|
||||
|
||||
return simulatedItem;
|
||||
case "upvote":
|
||||
if (hasDownvotedItem) {
|
||||
Telescope.operateOnItem(collection, item, user, "cancelDownvote", isSimulation);
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
// ------------------------------ "Real" Server-Side Operation ------------------------------ //
|
||||
|
||||
// make sure item and user are defined, and user can perform the operation
|
||||
if (
|
||||
!item ||
|
||||
!user ||
|
||||
!Users.canDo(user, `${item.getCollectionName()}.${operation}`) ||
|
||||
operation === "upvote" && hasUpvotedItem ||
|
||||
operation === "downvote" && hasDownvotedItem ||
|
||||
operation === "cancelUpvote" && !hasUpvotedItem||
|
||||
operation === "cancelDownvote" && !hasDownvotedItem
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// ------------------------------ Sync Callbacks ------------------------------ //
|
||||
item = Telescope.callbacks.run(operation, item, user);
|
||||
|
||||
switch (operation) {
|
||||
|
||||
case "upvote":
|
||||
|
||||
if (hasDownvotedItem) {
|
||||
Telescope.operateOnItem(collection, item, user, "cancelDownvote", isSimulation);
|
||||
}
|
||||
const upvoter = isSimulation ? {__typename: "User", _id: user._id} : user._id
|
||||
item.upvoters.push(upvoter);
|
||||
item.upvotes += 1;
|
||||
item.baseScore += votePower;
|
||||
|
||||
if (!isSimulation) {
|
||||
update = {
|
||||
$addToSet: {upvoters: user._id},
|
||||
$inc: {upvotes: 1, baseScore: votePower}
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case "downvote":
|
||||
case "downvote":
|
||||
if (hasUpvotedItem) {
|
||||
Telescope.operateOnItem(collection, item, user, "cancelUpvote", isSimulation);
|
||||
}
|
||||
|
||||
if (hasUpvotedItem) {
|
||||
Telescope.operateOnItem(collection, item, user, "cancelUpvote", isSimulation);
|
||||
}
|
||||
const downvoter = isSimulation ? {__typename: "User", _id: user._id} : user._id
|
||||
item.downvoters.push(downvoter);
|
||||
item.downvotes += 1;
|
||||
item.baseScore -= votePower;
|
||||
|
||||
if (!isSimulation) {
|
||||
update = {
|
||||
$addToSet: {downvoters: user._id},
|
||||
$inc: {downvotes: 1, baseScore: -votePower}
|
||||
}
|
||||
break;
|
||||
|
||||
case "cancelUpvote":
|
||||
}
|
||||
break;
|
||||
|
||||
case "cancelUpvote":
|
||||
item.upvoters = item.upvoters.filter(u => typeof u === 'string' ? u !== user._id : u._id !== user._id);
|
||||
item.upvotes -= 1;
|
||||
item.baseScore -= votePower;
|
||||
|
||||
if (!isSimulation) {
|
||||
update = {
|
||||
$pull: {upvoters: user._id},
|
||||
$inc: {upvotes: -1, baseScore: -votePower}
|
||||
};
|
||||
break;
|
||||
|
||||
case "cancelDownvote":
|
||||
}
|
||||
break;
|
||||
|
||||
case "cancelDownvote":
|
||||
item.downvoters = item.downvoters.filter(u => typeof u === 'string' ? u !== user._id : u._id !== user._id);
|
||||
item.downvotes -= 1;
|
||||
item.baseScore += votePower;
|
||||
|
||||
if (!isSimulation) {
|
||||
update = {
|
||||
$pull: {downvoters: user._id},
|
||||
$inc: {downvotes: -1, baseScore: votePower}
|
||||
};
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (!isSimulation) {
|
||||
update["$set"] = {inactive: false};
|
||||
var result = collection.update({_id: item._id}, update);
|
||||
|
||||
|
||||
const result = collection.update({_id: item._id}, update);
|
||||
|
||||
if (result > 0) {
|
||||
|
||||
// extend item with baseScore to help calculate newScore
|
||||
item = _.extend(item, {baseScore: (item.baseScore + votePower)});
|
||||
|
||||
// --------------------- Server-Side Async Callbacks --------------------- //
|
||||
Telescope.callbacks.runAsync(operation+".async", item, user, collection, operation);
|
||||
|
||||
return item;
|
||||
|
||||
Telescope.callbacks.runAsync(operation+".async", item, user, collection, operation);
|
||||
}
|
||||
}
|
||||
|
||||
// if (isSimulation) {
|
||||
// console.log('item from apollo store', item);
|
||||
// } else {
|
||||
// console.log('item from mongo db', item);
|
||||
// }
|
||||
|
||||
return item;
|
||||
};
|
Loading…
Add table
Reference in a new issue