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 Issues #475

Merged
merged 3 commits into from
Nov 27, 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
4 changes: 2 additions & 2 deletions src/components/chats/ChatPreview/ChatUnreadCount.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useLastReadTimeFromStorage from '@/components/chats/hooks/useLastReadMessageTimeFromStorage'
import useUnreadCount from '@/components/chats/hooks/useUnreadCount'
import { cx } from '@/utils/class-names'
import { ComponentProps } from 'react'
import { ComponentProps, useMemo } from 'react'

export type ChatUnreadCountProps = ComponentProps<'div'> & {
chatId: string
Expand All @@ -12,7 +12,7 @@ export default function ChatUnreadCount({
...props
}: ChatUnreadCountProps) {
const { getLastReadTime } = useLastReadTimeFromStorage(chatId)
const lastReadTime = getLastReadTime() ?? ''
const lastReadTime = useMemo(() => getLastReadTime() ?? '', [getLastReadTime])
const unreadCount = useUnreadCount(chatId, lastReadTime)

if (unreadCount <= 0) return null
Expand Down
3 changes: 2 additions & 1 deletion src/services/subsocial/datahub/generated-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ export type GetPostsQueryVariables = Exact<{
ids?: InputMaybe<
Array<Scalars['String']['input']> | Scalars['String']['input']
>
pageSize: Scalars['Int']['input']
}>

export type GetPostsQuery = {
Expand Down Expand Up @@ -758,7 +759,7 @@ export const DatahubPostFragment = gql`
}
`
export const GetPosts = gql`
query GetPosts($ids: [String!]) {
query GetPosts($ids: [String!], $pageSize: Int!) {
findPosts(where: { persistentIds: $ids }) {
data {
...DatahubPostFragment
Expand Down
31 changes: 21 additions & 10 deletions src/services/subsocial/datahub/posts/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PostData } from '@subsocial/api/types'
import { gql } from 'graphql-request'
import { getPostsFollowersCountFromSquid } from '../../posts/fetcher'
import {
GetOptimisticPostsQuery,
GetOptimisticPostsQueryVariables,
Expand Down Expand Up @@ -86,8 +87,8 @@ export const DATAHUB_POST_FRAGMENT = gql`

const GET_POSTS = gql`
${DATAHUB_POST_FRAGMENT}
query GetPosts($ids: [String!]) {
findPosts(where: { persistentIds: $ids }) {
query GetPosts($ids: [String!], $pageSize: Int!) {
findPosts(where: { persistentIds: $ids, pageSize: $pageSize }) {
data {
...DatahubPostFragment
}
Expand Down Expand Up @@ -124,15 +125,25 @@ export async function getPostsFromDatahub(postIds: string[]) {
let optimisticPosts: PostData[] = []

if (persistentIds.length > 0) {
const res = await datahubQueryRequest<
GetPostsQuery,
GetPostsQueryVariables
>({
document: GET_POSTS,
variables: { ids: persistentIds },
})
persistentPosts = res.findPosts.data.map((post) => {
const [datahubResPromise, squidResPromise] = await Promise.allSettled([
datahubQueryRequest<GetPostsQuery, GetPostsQueryVariables>({
document: GET_POSTS,
variables: { ids: persistentIds, pageSize: persistentIds.length },
}),
getPostsFollowersCountFromSquid(persistentIds),
] as const)
const followersCountMap = new Map<string, number>()
if (squidResPromise.status === 'fulfilled') {
squidResPromise.value.forEach((post) => {
followersCountMap.set(post.id, post.followersCount)
})
}
if (datahubResPromise.status !== 'fulfilled') {
throw new Error(datahubResPromise.reason)
}
persistentPosts = datahubResPromise.value.findPosts.data.map((post) => {
post.id = post.persistentId || post.id
post.followersCount = followersCountMap.get(post.id) || 0
return { ...mapDatahubPostFragment(post), requestedId: post.id }
})
}
Expand Down
27 changes: 26 additions & 1 deletion src/services/subsocial/posts/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { SubsocialQueryData } from '@/subsocial-query/subsocial/query'
import { gql } from 'graphql-request'
import { getPostsFromDatahub } from '../datahub/posts/fetcher'
import { POST_FRAGMENT } from '../squid/fragments'
import { GetPostsQuery, GetPostsQueryVariables } from '../squid/generated'
import {
GetPostsFollowersCountQuery,
GetPostsFollowersCountQueryVariables,
GetPostsQuery,
GetPostsQueryVariables,
} from '../squid/generated'
import { mapPostFragment } from '../squid/mappers'
import { squidRequest } from '../squid/utils'
import { standaloneDynamicFetcherWrapper } from '../utils'
Expand Down Expand Up @@ -38,3 +43,23 @@ export const getPostsFromSubsocial = standaloneDynamicFetcherWrapper({
squid: getPostsFromSquid,
datahub: getPostsFromDatahub,
})

const GET_POSTS_FOLLOWERS_COUNT = gql`
query GetPostsFollowersCount($ids: [String!]) {
posts(where: { id_in: $ids }) {
id
followersCount
}
}
`
export async function getPostsFollowersCountFromSquid(postIds: string[]) {
if (postIds.length === 0) return []
const res = await squidRequest<
GetPostsFollowersCountQuery,
GetPostsFollowersCountQueryVariables
>({
document: GET_POSTS_FOLLOWERS_COUNT,
variables: { ids: postIds },
})
return res.posts
}
33 changes: 15 additions & 18 deletions src/services/subsocial/squid/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5870,14 +5870,6 @@ export type WhereIdInput = {
id: Scalars['String']['input'];
};

export type GetMessagesCountAfterTimeQueryVariables = Exact<{
chatId: Scalars['String']['input'];
time: Scalars['DateTime']['input'];
}>;


export type GetMessagesCountAfterTimeQuery = { __typename?: 'Query', postsConnection: { __typename?: 'PostsConnection', totalCount: number } };

export type GetEvmAddressesQueryVariables = Exact<{
addresses?: InputMaybe<Array<Scalars['String']['input']> | Scalars['String']['input']>;
}>;
Expand All @@ -5892,6 +5884,13 @@ export type GetPostsQueryVariables = Exact<{

export type GetPostsQuery = { __typename?: 'Query', posts: Array<{ __typename?: 'Post', content?: string | null, createdAtBlock?: any | null, createdAtTime?: any | null, title?: string | null, body?: string | null, summary?: string | null, isShowMore?: boolean | null, image?: string | null, link?: string | null, downvotesCount: number, hidden: boolean, id: string, isComment: boolean, kind?: PostKind | null, repliesCount: number, sharesCount: number, upvotesCount: number, updatedAtTime?: any | null, inReplyToKind?: InReplyToKind | null, followersCount: number, canonical?: string | null, tagsOriginal?: string | null, createdByAccount: { __typename?: 'Account', id: string }, inReplyToPost?: { __typename?: 'Post', id: string } | null, ownedByAccount: { __typename?: 'Account', id: string }, space?: { __typename?: 'Space', id: string } | null, rootPost?: { __typename?: 'Post', id: string, space?: { __typename?: 'Space', id: string } | null } | null, sharedPost?: { __typename?: 'Post', id: string } | null, extensions: Array<{ __typename?: 'ContentExtension', image?: string | null, amount?: any | null, chain?: string | null, collectionId?: string | null, decimals?: number | null, extensionSchemaId: ContentExtensionSchemaId, id: string, nftId?: string | null, token?: string | null, txHash?: string | null, message?: string | null, nonce?: string | null, url?: string | null, recipient?: { __typename?: 'Account', id: string } | null, fromEvm?: { __typename?: 'EvmAccount', id: string } | null, toEvm?: { __typename?: 'EvmAccount', id: string } | null, pinnedResources: Array<{ __typename?: 'ExtensionPinnedResource', post?: { __typename?: 'Post', id: string } | null }> }> }> };

export type GetPostsFollowersCountQueryVariables = Exact<{
ids?: InputMaybe<Array<Scalars['String']['input']> | Scalars['String']['input']>;
}>;


export type GetPostsFollowersCountQuery = { __typename?: 'Query', posts: Array<{ __typename?: 'Post', id: string, followersCount: number }> };

export type GetPostsByContentQueryVariables = Exact<{
search: Scalars['String']['input'];
spaceIds: Array<Scalars['String']['input']> | Scalars['String']['input'];
Expand Down Expand Up @@ -6029,16 +6028,6 @@ export const PostFragment = gql`
}
}
`;
export const GetMessagesCountAfterTime = gql`
query GetMessagesCountAfterTime($chatId: String!, $time: DateTime!) {
postsConnection(
where: {createdAtTime_gt: $time, rootPost: {id_eq: $chatId}}
orderBy: id_ASC
) {
totalCount
}
}
`;
export const GetEvmAddresses = gql`
query getEvmAddresses($addresses: [String!]) {
evmSubstrateAccountLinks(
Expand All @@ -6060,6 +6049,14 @@ export const GetPosts = gql`
}
}
${PostFragment}`;
export const GetPostsFollowersCount = gql`
query GetPostsFollowersCount($ids: [String!]) {
posts(where: {id_in: $ids}) {
id
followersCount
}
}
`;
export const GetPostsByContent = gql`
query GetPostsByContent($search: String!, $spaceIds: [String!]!, $postIds: [String!]!) {
posts(
Expand Down