From cecd5ba6d26850c3aa5a5d648489592f55df8c65 Mon Sep 17 00:00:00 2001 From: "Marcelo R. Bianchi" Date: Sat, 3 Jun 2023 01:35:30 -0300 Subject: [PATCH 1/4] feat: rmvote --- .../zomes/coordinator/posts/src/comment.rs | 17 ++++++++++++++ dnas/herd/zomes/coordinator/posts/src/post.rs | 11 +++++++++ dnas/herd/zomes/integrity/posts/src/votes.rs | 2 +- flake.lock | 14 +++++------ ui/src/components/BaseVoteInput.vue | 6 ++--- ui/src/herd/posts/CommentVotes.vue | 23 ++++++++++++++++++- ui/src/herd/posts/PostVotes.vue | 21 ++++++++++++++++- 7 files changed, 81 insertions(+), 13 deletions(-) diff --git a/dnas/herd/zomes/coordinator/posts/src/comment.rs b/dnas/herd/zomes/coordinator/posts/src/comment.rs index 7a288ba..c0d85b0 100644 --- a/dnas/herd/zomes/coordinator/posts/src/comment.rs +++ b/dnas/herd/zomes/coordinator/posts/src/comment.rs @@ -178,6 +178,23 @@ pub fn downvote_comment(original_post_hash: ActionHash) -> ExternResult<()> { Ok(()) } +#[hdk_extern] +pub fn rmvote_comment(original_post_hash: ActionHash) -> ExternResult<()> { + create_link( + agent_info()?.agent_initial_pubkey.clone(), + original_post_hash.clone(), + LinkTypes::MyVotedComments, + (), + )?; + create_link( + original_post_hash, + agent_info()?.agent_initial_pubkey, + LinkTypes::CommentVoteByAgent, + make_vote_link_tag(0)?, + )?; + Ok(()) +} + #[hdk_extern] pub fn get_my_vote_on_comment(comment_hash: ActionHash) -> ExternResult> { // Get all votes on this comment diff --git a/dnas/herd/zomes/coordinator/posts/src/post.rs b/dnas/herd/zomes/coordinator/posts/src/post.rs index 1aafe23..ce51463 100644 --- a/dnas/herd/zomes/coordinator/posts/src/post.rs +++ b/dnas/herd/zomes/coordinator/posts/src/post.rs @@ -133,6 +133,17 @@ pub fn upvote_post(original_post_hash: ActionHash) -> ExternResult<()> { Ok(()) } +#[hdk_extern] +pub fn rmvote_post(original_post_hash: ActionHash) -> ExternResult<()> { + create_link( + original_post_hash, + agent_info()?.agent_initial_pubkey, + LinkTypes::PostVoteByAgent, + make_vote_link_tag(0)?, + )?; + Ok(()) +} + #[hdk_extern] pub fn downvote_post(original_post_hash: ActionHash) -> ExternResult<()> { create_link( diff --git a/dnas/herd/zomes/integrity/posts/src/votes.rs b/dnas/herd/zomes/integrity/posts/src/votes.rs index 33ad4a9..db80939 100644 --- a/dnas/herd/zomes/integrity/posts/src/votes.rs +++ b/dnas/herd/zomes/integrity/posts/src/votes.rs @@ -60,7 +60,7 @@ pub fn validate_create_link_vote_by_agent( // TODO: make this a DNA property // Value must be -1 or 1 - if vote_tag.value != -1 && vote_tag.value != 1 { + if vote_tag.value > 1 || vote_tag.value < -1 { return Ok( ValidateCallbackResult::Invalid( String::from("VotePostToAgent tag value must be 1 or -1"), diff --git a/flake.lock b/flake.lock index 039c2f3..7df344b 100644 --- a/flake.lock +++ b/flake.lock @@ -156,16 +156,16 @@ "holochain": { "flake": false, "locked": { - "lastModified": 1675455504, - "narHash": "sha256-619bpPtO0IUSzPzLNzHERuvqGblpjO65rsw3jdxoEkQ=", + "lastModified": 1681507583, + "narHash": "sha256-lRnums2gv1oXVwo4gMF2QAnzEu8prwxg1uKjUzNwJV4=", "owner": "holochain", "repo": "holochain", - "rev": "ed5b7bb461c2a8bfd4d2633bad604a20b8f2da03", + "rev": "ac50baed6b53e9d0552729e69e1e20312e4edc08", "type": "github" }, "original": { "owner": "holochain", - "ref": "holochain-0.1.3", + "ref": "holochain-0.1.4", "repo": "holochain", "type": "github" } @@ -400,11 +400,11 @@ }, "locked": { "dir": "versions/0_1", - "lastModified": 1679811419, - "narHash": "sha256-Dpz28/2wNsacIw8B4YpV1pKvyy0DT6fSkCRfAbPfKUo=", + "lastModified": 1685758650, + "narHash": "sha256-nXnDyPKEXxbSThrDABtSL1Sha5eqf8Hiy222shMhKTg=", "owner": "holochain", "repo": "holochain", - "rev": "c7957817622b1714b6e592c4b38a52216cd21280", + "rev": "566b4360fcafc6d0c3cc2c2d34fb51ddeba4eb57", "type": "github" }, "original": { diff --git a/ui/src/components/BaseVoteInput.vue b/ui/src/components/BaseVoteInput.vue index 542ca73..62df587 100644 --- a/ui/src/components/BaseVoteInput.vue +++ b/ui/src/components/BaseVoteInput.vue @@ -3,7 +3,7 @@
import { defineEmits } from 'vue' -defineEmits(['upvote', 'downvote']); +defineEmits(['upvote', 'downvote', 'rmvote']); withDefaults(defineProps<{ votes?: number, diff --git a/ui/src/herd/posts/CommentVotes.vue b/ui/src/herd/posts/CommentVotes.vue index 1214067..a5b59f7 100644 --- a/ui/src/herd/posts/CommentVotes.vue +++ b/ui/src/herd/posts/CommentVotes.vue @@ -4,6 +4,7 @@ :my-vote="myVote" @upvote="upvote" @downvote="downvote" + @rmvote="rmvote" /> @@ -20,7 +21,7 @@ const props = defineProps<{ votes?: number }>(); -const emit = defineEmits(['upvote', 'downvote']); +const emit = defineEmits(['upvote', 'downvote', 'rmvote']); const client = (inject('client') as ComputedRef).value; @@ -66,6 +67,7 @@ const downvote = async () => { fn_name: 'downvote_comment', payload: props.originalActionHash, }); + console.log('emit downvote') emit('downvote'); runFetchMyVote(); } catch (e: any) { @@ -73,6 +75,25 @@ const downvote = async () => { } }; +const rmvote = async () => { + if(myVote.value === 0) return; + + try { + await client.callZome({ + cell_id: [props.dnaHash, client.myPubKey], + zome_name: 'posts', + fn_name: 'rmvote_comment', + payload: props.originalActionHash, + }); + console.log('emit rmvote') + emit('rmvote'); + runFetchMyVote(); + } catch (e: any) { + toast.error("Failed to vote on post: ", e.data.data); + console.log('error:',e); + } +}; + const {data: myVote, run: runFetchMyVote } = useRequest(fetchMyVote, { onError: (e: any) => { toast.error(`Failed to fetch post votes count: ${e.data.data}`); diff --git a/ui/src/herd/posts/PostVotes.vue b/ui/src/herd/posts/PostVotes.vue index 812f8f0..5b83d3b 100644 --- a/ui/src/herd/posts/PostVotes.vue +++ b/ui/src/herd/posts/PostVotes.vue @@ -6,6 +6,7 @@ :my-vote="myVote" @upvote="upvote" @downvote="downvote" + @rmvote="rmvote" /> @@ -26,7 +27,7 @@ const props = withDefaults(defineProps<{ size: 'lg' }); -const emit = defineEmits(['upvote', 'downvote']); +const emit = defineEmits(['upvote', 'downvote', 'rmvote']); const client = (inject('client') as ComputedRef).value; const getMyVote = async () => { @@ -64,6 +65,24 @@ const upvote = async() => { } }; +const rmvote = async () => { + if(myVote.value === 0) return; + + try { + await client.callZome({ + cell_id: [props.dnaHash, client.myPubKey], + cap_secret: null, + zome_name: 'posts', + fn_name: 'rmvote_post', + payload: props.postHash, + }); + emit('rmvote'); + runGetMyVote(); + } catch (e: any) { + toast.error(`Failed to rmvote post: ${e.data.data}`); + } +}; + const downvote = async () => { if(myVote.value === -1) return; From c95f0c4658f51dfcf458b5254fefb90191c7f9a2 Mon Sep 17 00:00:00 2001 From: "Marcelo R. Bianchi" Date: Sat, 3 Jun 2023 01:43:13 -0300 Subject: [PATCH 2/4] Removes some console.log --- ui/src/herd/posts/CommentVotes.vue | 3 --- 1 file changed, 3 deletions(-) diff --git a/ui/src/herd/posts/CommentVotes.vue b/ui/src/herd/posts/CommentVotes.vue index a5b59f7..09d899d 100644 --- a/ui/src/herd/posts/CommentVotes.vue +++ b/ui/src/herd/posts/CommentVotes.vue @@ -67,7 +67,6 @@ const downvote = async () => { fn_name: 'downvote_comment', payload: props.originalActionHash, }); - console.log('emit downvote') emit('downvote'); runFetchMyVote(); } catch (e: any) { @@ -85,12 +84,10 @@ const rmvote = async () => { fn_name: 'rmvote_comment', payload: props.originalActionHash, }); - console.log('emit rmvote') emit('rmvote'); runFetchMyVote(); } catch (e: any) { toast.error("Failed to vote on post: ", e.data.data); - console.log('error:',e); } }; From 3541dc85f1ad58a5b7f1db001d373dd53b055674 Mon Sep 17 00:00:00 2001 From: "Marcelo R. Bianchi" Date: Sat, 3 Jun 2023 02:45:15 -0300 Subject: [PATCH 3/4] Adds rmvote to tests --- flake.lock | 6 +++--- tests/src/posts/posts/post.test.ts | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/flake.lock b/flake.lock index 7df344b..9662fc9 100644 --- a/flake.lock +++ b/flake.lock @@ -400,11 +400,11 @@ }, "locked": { "dir": "versions/0_1", - "lastModified": 1685758650, - "narHash": "sha256-nXnDyPKEXxbSThrDABtSL1Sha5eqf8Hiy222shMhKTg=", + "lastModified": 1685773320, + "narHash": "sha256-Yw6wWMzR+cbCmrQBaA+O5qRS3Is5EK+YqBTFRbLo8xI=", "owner": "holochain", "repo": "holochain", - "rev": "566b4360fcafc6d0c3cc2c2d34fb51ddeba4eb57", + "rev": "c46c6bf2b9db1a2a14d7ca3350cf05f32d210e98", "type": "github" }, "original": { diff --git a/tests/src/posts/posts/post.test.ts b/tests/src/posts/posts/post.test.ts index 2743513..b982033 100644 --- a/tests/src/posts/posts/post.test.ts +++ b/tests/src/posts/posts/post.test.ts @@ -334,6 +334,8 @@ test('get_all_posts_sorted_by_votes sorts by number of votes desc, then timestam }); await pause(2000); + + // All agents see the same post ordering const alice_posts_sorted = await alice.namedCells.get('herd').callZome({ @@ -359,6 +361,23 @@ test('get_all_posts_sorted_by_votes sorts by number of votes desc, then timestam fn_name: "get_all_posts_sorted_by_votes", }); t.deepEqual(john_posts_sorted, [record.signed_action.hashed.hash, record3.signed_action.hashed.hash, record2.signed_action.hashed.hash]) + + // Bob removes his vote to alice's post + await bob.namedCells.get('herd').callZome({ + zome_name: "posts", + fn_name: "rmvote_post", + payload: record.signed_action.hashed.hash, + }); + + await pause(2000); + + const metadata: Record = await alice.namedCells.get('herd').callZome({ + zome_name: "posts", + fn_name: "get_post_metadata", + payload: record.signed_action.hashed.hash, + }); + + t.deepEqual(3, (decode((metadata.entry as any).Present.entry) as any).upvotes); }, true, From 450ac15a24535e152c7132567bf4321c3e95c307 Mon Sep 17 00:00:00 2001 From: "Marcelo R. Bianchi" Date: Thu, 29 Jun 2023 12:58:49 -0300 Subject: [PATCH 4/4] rmvote rm created links --- .../zomes/coordinator/posts/src/comment.rs | 26 ++++++++++++------- ui/src/herd/posts/CommentVotes.vue | 1 + 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/dnas/herd/zomes/coordinator/posts/src/comment.rs b/dnas/herd/zomes/coordinator/posts/src/comment.rs index c0d85b0..e85d90b 100644 --- a/dnas/herd/zomes/coordinator/posts/src/comment.rs +++ b/dnas/herd/zomes/coordinator/posts/src/comment.rs @@ -179,19 +179,25 @@ pub fn downvote_comment(original_post_hash: ActionHash) -> ExternResult<()> { } #[hdk_extern] -pub fn rmvote_comment(original_post_hash: ActionHash) -> ExternResult<()> { - create_link( +pub fn rmvote_comment(comment_hash: ActionHash) -> ExternResult<()> { + get_links( agent_info()?.agent_initial_pubkey.clone(), - original_post_hash.clone(), LinkTypes::MyVotedComments, - (), - )?; - create_link( - original_post_hash, - agent_info()?.agent_initial_pubkey, + None, + )?.iter().for_each(|link| { + if link.target.clone().to_hex() == comment_hash.to_hex() { + let _ = delete_link(link.create_link_hash.clone()); + } + }); + get_links( + comment_hash, LinkTypes::CommentVoteByAgent, - make_vote_link_tag(0)?, - )?; + None, + )?.iter().for_each(|link| { + if link.target.clone().to_hex() == agent_info().unwrap().agent_initial_pubkey.to_hex() { + let _ = delete_link(link.create_link_hash.clone()); + } + }); Ok(()) } diff --git a/ui/src/herd/posts/CommentVotes.vue b/ui/src/herd/posts/CommentVotes.vue index 09d899d..bb26b47 100644 --- a/ui/src/herd/posts/CommentVotes.vue +++ b/ui/src/herd/posts/CommentVotes.vue @@ -88,6 +88,7 @@ const rmvote = async () => { runFetchMyVote(); } catch (e: any) { toast.error("Failed to vote on post: ", e.data.data); + console.log(e); } };