Skip to content

Commit

Permalink
fix: use count_links for followers count
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyg committed Aug 31, 2023
1 parent b23624c commit 9329d79
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,34 @@ pub fn get_followers_for_creator(
Ok(agents)
}

#[hdk_extern]
pub fn count_creators_for_follower(
follower: AgentPubKey,
) -> ExternResult<usize> {
let query = LinkQuery::new(
follower,
LinkTypeFilter::single_type(
ZomeIndex(2),
LinkType(0), // LinkTypes::FollowerToCreators
),
);
count_links(query)
}

#[hdk_extern]
pub fn count_followers_for_creator(
creator: AgentPubKey,
) -> ExternResult<usize> {
let query = LinkQuery::new(
creator,
LinkTypeFilter::single_type(
ZomeIndex(2),
LinkType(1), // LinkTypes::CreatorToFollowers
),
);
count_links(query)
}

#[hdk_extern]
pub fn get_follower_links_for_creator(
input: GetFollowersForCreatorInput,
Expand Down
2 changes: 2 additions & 0 deletions ui/src/components/BaseAgentProfileDetail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
v-if="!isMyProfile"
:agentPubKey="agentPubKey"
:big="bigFollowButton"
@toggle-follow="(val) => emit('toggle-follow', val)"
/>
<div v-else-if="!hideEditButton" class="flex flex-col space-y-2">
<button
Expand Down Expand Up @@ -222,6 +223,7 @@ const emit = defineEmits([
"click-edit-profile",
"click-creators",
"click-followers",
"toggle-follow",
]);
const client = (inject("client") as ComputedRef<AppAgentClient>).value;
Expand Down
49 changes: 45 additions & 4 deletions ui/src/pages/AgentProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
:profile="profileWithContext?.profile"
:joined-timestamp="profileWithContext?.joinedTimestamp"
:agentPubKey="agentPubKey"
:creators-count="creators ? creators.length : 0"
:followers-count="followers ? followers.length : 0"
:creators-count="creatorsCount ? creatorsCount : 0"
:followers-count="followersCount ? followersCount : 0"
class="bg-base-200/75 rounded-3xl"
style="-webkit-backdrop-filter: blur(10px)"
enable-copy-agent-pub-key
Expand All @@ -22,6 +22,7 @@
if (creators && creators.length > 0) showCreatorsListDialog = true;
}
"
@toggle-follow="refetchFollowersCount"
/>
<EditAgentProfileDialog
v-if="profileWithContext"
Expand Down Expand Up @@ -240,7 +241,7 @@ const {
watch(errorProfile, showError);
const fetchFollowers = async () => {
const agents: AgentPubKey[] = await await client.callZome({
const agents: AgentPubKey[] = await client.callZome({
role_name: "mewsfeed",
zome_name: "follows",
fn_name: "get_followers_for_creator",
Expand Down Expand Up @@ -278,7 +279,7 @@ const { data: followers, error: errorFollowers } = useQuery({
watch(errorFollowers, showError);
const fetchCreators = async () => {
const agents: AgentPubKey[] = await await client.callZome({
const agents: AgentPubKey[] = await client.callZome({
role_name: "mewsfeed",
zome_name: "follows",
fn_name: "get_creators_for_follower",
Expand Down Expand Up @@ -314,4 +315,44 @@ const { data: creators, error: errorCreators } = useQuery({
queryFn: fetchCreators,
});
watch(errorCreators, showError);
const fetchCreatorsCount = async (): Promise<number> =>
client.callZome({
role_name: "mewsfeed",
zome_name: "follows",
fn_name: "count_creators_for_follower",
payload: decodeHashFromBase64(route.params.agentPubKey as string),
});
const { data: creatorsCount, error: errorCreatorsCount } = useQuery({
queryKey: [
"follows",
"count_creators_for_follower",
route.params.agentPubKey as string,
],
queryFn: fetchCreatorsCount,
});
watch(errorCreatorsCount, showError);
const fetchFollowersCount = async (): Promise<number> =>
client.callZome({
role_name: "mewsfeed",
zome_name: "follows",
fn_name: "count_followers_for_creator",
payload: decodeHashFromBase64(route.params.agentPubKey as string),
});
const {
data: followersCount,
error: errorFollowersCount,
refetch: refetchFollowersCount,
} = useQuery({
queryKey: [
"follows",
"count_followers_for_creator",
route.params.agentPubKey as string,
],
queryFn: fetchFollowersCount,
});
watch(errorFollowersCount, showError);
</script>

0 comments on commit 9329d79

Please sign in to comment.