Skip to content

Commit

Permalink
feat: profile stats and operations
Browse files Browse the repository at this point in the history
  • Loading branch information
reecejohnson committed Sep 10, 2023
1 parent 6cb9528 commit 7be751f
Show file tree
Hide file tree
Showing 31 changed files with 332 additions and 7 deletions.
44 changes: 43 additions & 1 deletion examples/node/scripts/profile/profileStats.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
import { isSuccessfulLensProfileManagerResponse } from "@lens-protocol/client";
import { getAuthenticatedClientFromEthersWallet } from "../shared/getAuthenticatedClient";
import { setupWallet } from "../shared/setupWallet";
import {
CustomFiltersType,
OpenActionCategoryType,
OpenActionModuleType,
} from "@lens-protocol/client/src/graphql/types.generated";

async function main() {
const wallet = setupWallet();
const lensClient = await getAuthenticatedClientFromEthersWallet(wallet);

const recommendedProfiles = await lensClient.profile.fetch({ profileId: "YOUR_PROFILE_ID" });
// stats across the whole protocol
const protocolWideStats = await lensClient.profile.stats({ profileId: "PROFILE_ID" });

// stats for a specified apps
const statsForSpecifiedApps = await lensClient.profile.stats(
{
profileId: "PROFILE_ID",
},
{ profileStatsArg: { forApps: ["APP_ID", "ANOTHER_APP_ID"] } }
);

// filter open actions
const filteredOpenActions = await lensClient.profile.stats(
{
profileId: "PROFILE_ID",
},
{
profileStatsCountOpenActionArgs: {
anyOf: [
{
address: "0x00",
type: OpenActionModuleType.SimpleCollectOpenActionModule,
category: OpenActionCategoryType.Collect,
},
],
},
}
);

// stats for a specified app and with custom filters
const customFilteredStats = await lensClient.profile.stats(
{
profileId: "PROFILE_ID",
},
{
profileStatsArg: { forApps: ["APP_ID"], customFilters: [CustomFiltersType.Gardeners] },
}
);
}

main();
6 changes: 6 additions & 0 deletions packages/client/src/consts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ export type LensConfig = {
* @see {@link MediaTransformsConfig} for more information
*/
mediaTransforms?: MediaTransformsConfig;

/**
* The app ids to use to read data from the Lens Protocol.
* If not provided, all apps will be used.
*/
forApps?: string[];
};
36 changes: 36 additions & 0 deletions packages/client/src/graphql/fragments.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ export type ProfilePictureSetFragment = {
image: ImageFragment;
};

export type ProfileStatsFragment = {
id: string;
followers: number;
following: number;
comments: number;
posts: number;
mirrors: number;
quotes: number;
publications: number;
countOpenActions: number;
upvoteReactions: number;
downvoteReactions: number;
};

export type ProfileFieldsFragment = {
__typename: 'Profile';
id: string;
Expand Down Expand Up @@ -93,6 +107,7 @@ export type ProfileFieldsFragment = {
isFollowingMe: OptimisticStatusResultFragment;
};
guardian: { protected: boolean; cooldownEndsOn: string | null } | null;
stats: ProfileStatsFragment;
};

export type ProfileFragment = { invitedBy: ProfileFieldsFragment | null } & ProfileFieldsFragment;
Expand Down Expand Up @@ -1010,6 +1025,23 @@ export const OptimisticStatusResultFragmentDoc = gql`
isFinalisedOnchain
}
`;
export const ProfileStatsFragmentDoc = gql`
fragment ProfileStats on ProfileStats {
id
followers(request: $profileStatsArg)
following(request: $profileStatsArg)
comments(request: $profileStatsArg)
posts(request: $profileStatsArg)
mirrors(request: $profileStatsArg)
quotes(request: $profileStatsArg)
mirrors(request: $profileStatsArg)
quotes(request: $profileStatsArg)
publications(request: $profileStatsArg)
upvoteReactions: reactions(request: { type: UPVOTE })
downvoteReactions: reactions(request: { type: DOWNVOTE })
countOpenActions(request: $profileStatsCountOpenActionArgs)
}
`;
export const ProfileFieldsFragmentDoc = gql`
fragment ProfileFields on Profile {
__typename
Expand Down Expand Up @@ -1077,6 +1109,9 @@ export const ProfileFieldsFragmentDoc = gql`
protected
cooldownEndsOn
}
stats(request: $profileStatsArg) {
...ProfileStats
}
invitesLeft
createdAt
}
Expand All @@ -1086,6 +1121,7 @@ export const ProfileFieldsFragmentDoc = gql`
${RevertFollowModuleSettingsFragmentDoc}
${UnknownFollowModuleSettingsFragmentDoc}
${OptimisticStatusResultFragmentDoc}
${ProfileStatsFragmentDoc}
`;
export const ProfileFragmentDoc = gql`
fragment Profile on Profile {
Expand Down
19 changes: 19 additions & 0 deletions packages/client/src/graphql/fragments.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ fragment ProfilePictureSet on ImageSet {
# }
}

fragment ProfileStats on ProfileStats {
id
followers(request: $profileStatsArg)
following(request: $profileStatsArg)
comments(request: $profileStatsArg)
posts(request: $profileStatsArg)
mirrors(request: $profileStatsArg)
quotes(request: $profileStatsArg)
mirrors(request: $profileStatsArg)
quotes(request: $profileStatsArg)
publications(request: $profileStatsArg)
upvoteReactions: reactions(request: { type: UPVOTE })
downvoteReactions: reactions(request: { type: DOWNVOTE })
countOpenActions(request: $profileStatsCountOpenActionArgs)
}

fragment ProfileFields on Profile {
__typename
id
Expand Down Expand Up @@ -195,6 +211,9 @@ fragment ProfileFields on Profile {
protected
cooldownEndsOn
}
stats(request: $profileStatsArg) {
...ProfileStats
}
invitesLeft
createdAt
}
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './buildPaginatedQueryResult';
export * from './poll';
export * from './requireAuthHeaders';
export * from './sdkAuthHeaderWrapper';
export * from './profileStatistics';
20 changes: 20 additions & 0 deletions packages/client/src/helpers/profileStatistics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { LensConfig } from '../consts/config';
import { ProfileStatsArg, ProfileStatsCountOpenActionArgs } from '../graphql/types.generated';

export type ProfileQueryOptions = {
profileStatsArg?: ProfileStatsArg;
profileStatsCountOpenActionArgs?: ProfileStatsCountOpenActionArgs;
};

export function buildProfileQueryOptions(args: {
config: LensConfig;
profileStatsArg?: ProfileStatsArg;
profileStatsCountOpenActionArgs?: ProfileStatsCountOpenActionArgs;
}): ProfileQueryOptions {
return {
profileStatsArg: args.profileStatsArg ?? { forApps: args.config.forApps },
profileStatsCountOpenActionArgs: args.profileStatsCountOpenActionArgs ?? {
anyOf: [],
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export type ExplorePublicationsQueryVariables = Types.Exact<{
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
publicationOperationsActedArgs?: Types.InputMaybe<Types.PublicationOperationsActedArgs>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type ExplorePublicationsQuery = {
Expand All @@ -50,6 +52,8 @@ export type ExploreProfilesQueryVariables = Types.Exact<{
request: Types.ExploreProfilesRequest;
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type ExploreProfilesQuery = {
Expand All @@ -63,6 +67,8 @@ export const ExplorePublicationsDocument = gql`
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: explorePublications(request: $request) {
items {
Expand All @@ -87,6 +93,8 @@ export const ExploreProfilesDocument = gql`
$request: ExploreProfilesRequest!
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: exploreProfiles(request: $request) {
items {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ query ExplorePublications(
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: explorePublications(request: $request) {
items {
Expand All @@ -25,6 +27,8 @@ query ExploreProfiles(
$request: ExploreProfilesRequest!
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: exploreProfiles(request: $request) {
items {
Expand Down
12 changes: 12 additions & 0 deletions packages/client/src/submodules/feed/graphql/feed.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export type FeedQueryVariables = Types.Exact<{
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
publicationOperationsActedArgs?: Types.InputMaybe<Types.PublicationOperationsActedArgs>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type FeedQuery = {
Expand All @@ -58,6 +60,8 @@ export type FeedHighlightsQueryVariables = Types.Exact<{
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
publicationOperationsActedArgs?: Types.InputMaybe<Types.PublicationOperationsActedArgs>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type FeedHighlightsQuery = {
Expand All @@ -70,6 +74,8 @@ export type ForYouQueryVariables = Types.Exact<{
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
publicationOperationsActedArgs?: Types.InputMaybe<Types.PublicationOperationsActedArgs>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type ForYouQuery = {
Expand Down Expand Up @@ -123,6 +129,8 @@ export const FeedDocument = gql`
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: feed(request: $request) {
items {
Expand All @@ -143,6 +151,8 @@ export const FeedHighlightsDocument = gql`
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: feedHighlights(request: $request) {
items {
Expand All @@ -169,6 +179,8 @@ export const ForYouDocument = gql`
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: forYou(request: $request) {
items {
Expand Down
6 changes: 6 additions & 0 deletions packages/client/src/submodules/feed/graphql/feed.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ query Feed(
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: feed(request: $request) {
items {
Expand All @@ -53,6 +55,8 @@ query FeedHighlights(
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: feedHighlights(request: $request) {
items {
Expand All @@ -75,6 +79,8 @@ query ForYou(
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$publicationOperationsActedArgs: PublicationOperationsActedArgs = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: forYou(request: $request) {
items {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type InvitedResultFragment = {
export type InvitedProfilesQueryVariables = Types.Exact<{
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type InvitedProfilesQuery = { invitedProfiles: Array<InvitedResultFragment> };
Expand Down Expand Up @@ -46,6 +48,8 @@ export const InvitedProfilesDocument = gql`
query InvitedProfiles(
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
invitedProfiles {
...InvitedResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fragment InvitedResult on InvitedResult {
query InvitedProfiles(
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
invitedProfiles {
...InvitedResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ export type MomokaTransactionQueryVariables = Types.Exact<{
request: Types.MomokaTransactionRequest;
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type MomokaTransactionQuery = {
Expand All @@ -118,6 +120,8 @@ export type MomokaTransactionsQueryVariables = Types.Exact<{
request: Types.MomokaTransactionsRequest;
profileCoverTransform?: Types.InputMaybe<Types.ImageTransform>;
profilePictureTransform?: Types.InputMaybe<Types.ImageTransform>;
profileStatsArg?: Types.InputMaybe<Types.ProfileStatsArg>;
profileStatsCountOpenActionArgs?: Types.InputMaybe<Types.ProfileStatsCountOpenActionArgs>;
}>;

export type MomokaTransactionsQuery = {
Expand Down Expand Up @@ -285,6 +289,8 @@ export const MomokaTransactionDocument = gql`
$request: MomokaTransactionRequest!
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: momokaTransaction(request: $request) {
... on MomokaPostTransaction {
Expand All @@ -311,6 +317,8 @@ export const MomokaTransactionsDocument = gql`
$request: MomokaTransactionsRequest!
$profileCoverTransform: ImageTransform = {}
$profilePictureTransform: ImageTransform = {}
$profileStatsArg: ProfileStatsArg = {}
$profileStatsCountOpenActionArgs: ProfileStatsCountOpenActionArgs = {}
) {
result: momokaTransactions(request: $request) {
items {
Expand Down
Loading

0 comments on commit 7be751f

Please sign in to comment.