Skip to content
Open
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions dnas/herd/zomes/coordinator/posts/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,29 @@ pub fn downvote_comment(original_post_hash: ActionHash) -> ExternResult<()> {
Ok(())
}

#[hdk_extern]
pub fn rmvote_comment(comment_hash: ActionHash) -> ExternResult<()> {
get_links(
agent_info()?.agent_initial_pubkey.clone(),
LinkTypes::MyVotedComments,
None,
)?.iter().for_each(|link| {
if link.target.clone().to_hex() == comment_hash.to_hex() {
let _ = delete_link(link.create_link_hash.clone());
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is giving me the following error:
{type: "error", data: {type: "internal_error", data: "Source chain error: InvalidCommit error: There are no link types in this integrity zome"}}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazing thanks for raising this. I think you caught a bug in holochain core. I created a minimal example and raised in with the core team: holochain/holochain#2541

}
});
get_links(
comment_hash,
LinkTypes::CommentVoteByAgent,
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(())
}

#[hdk_extern]
pub fn get_my_vote_on_comment(comment_hash: ActionHash) -> ExternResult<Option<VoteTag>> {
// Get all votes on this comment
Expand Down
11 changes: 11 additions & 0 deletions dnas/herd/zomes/coordinator/posts/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion dnas/herd/zomes/integrity/posts/src/votes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
14 changes: 7 additions & 7 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions tests/src/posts/posts/post.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions ui/src/components/BaseVoteInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div
class="text-2xl font-bold "
:class="{'text-accent': myVote === 1, 'hover:text-accent-focus': myVote !== 1}"
@click="$emit('upvote')"
@click="myVote === 1 ? $emit('rmvote') : $emit('upvote')"
>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -27,7 +27,7 @@
<div
class="text-2xl font-bold hover:text-accent-focus"
:class="{'text-accent': myVote === -1, 'hover:text-accent-focus': myVote !== -1}"
@click="$emit('downvote')"
@click="myVote === -1 ? $emit('rmvote') : $emit('downvote')"
>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -45,7 +45,7 @@
<script lang="ts" setup>
import { defineEmits } from 'vue'

defineEmits(['upvote', 'downvote']);
defineEmits(['upvote', 'downvote', 'rmvote']);

withDefaults(defineProps<{
votes?: number,
Expand Down
21 changes: 20 additions & 1 deletion ui/src/herd/posts/CommentVotes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:my-vote="myVote"
@upvote="upvote"
@downvote="downvote"
@rmvote="rmvote"
/>
</template>

Expand All @@ -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<AppAgentClient>).value;


Expand Down Expand Up @@ -73,6 +74,24 @@ 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,
});
emit('rmvote');
runFetchMyVote();
} catch (e: any) {
toast.error("Failed to vote on post: ", e.data.data);
console.log(e);
}
};

const {data: myVote, run: runFetchMyVote } = useRequest(fetchMyVote, {
onError: (e: any) => {
toast.error(`Failed to fetch post votes count: ${e.data.data}`);
Expand Down
21 changes: 20 additions & 1 deletion ui/src/herd/posts/PostVotes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:my-vote="myVote"
@upvote="upvote"
@downvote="downvote"
@rmvote="rmvote"
/>
</template>

Expand All @@ -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<AppAgentClient>).value;

const getMyVote = async () => {
Expand Down Expand Up @@ -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;

Expand Down