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

fix: use count_links for followers count #197

Merged
merged 2 commits into from
Sep 1, 2023
Merged
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
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 || 0"
:followers-count="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>
Loading