Skip to content

Commit

Permalink
[🐴] don't include blocked convos in unread count (#4082)
Browse files Browse the repository at this point in the history
* don't include blocked convos in unread count

* Use moderateProfile

* Handle blocked state in chat list

* Fix logic formatting, add todo

---------

Co-authored-by: Eric Bailey <[email protected]>
  • Loading branch information
mozzius and estrattonbailey authored May 17, 2024
1 parent dd0f57e commit f42f7fa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/screens/Messages/List/ChatListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function ChatListItemReady({
)}
</TimeElapsed>
)}
{convo.muted && (
{(convo.muted || moderation.blocked) && (
<Text
style={[
a.text_sm,
Expand Down Expand Up @@ -200,7 +200,8 @@ function ChatListItemReady({
convo.unreadCount > 0
? a.font_bold
: t.atoms.text_contrast_high,
convo.muted && t.atoms.text_contrast_medium,
(convo.muted || moderation.blocked) &&
t.atoms.text_contrast_medium,
]}>
{lastMessage}
</Text>
Expand All @@ -211,9 +212,10 @@ function ChatListItemReady({
a.absolute,
a.rounded_full,
{
backgroundColor: convo.muted
? t.palette.contrast_200
: t.palette.primary_500,
backgroundColor:
convo.muted || moderation.blocked
? t.palette.contrast_200
: t.palette.primary_500,
height: 7,
width: 7,
},
Expand Down
24 changes: 21 additions & 3 deletions src/state/queries/messages/list-converations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {useCallback, useMemo} from 'react'
import {ChatBskyConvoDefs, ChatBskyConvoListConvos} from '@atproto/api'
import {
ChatBskyConvoDefs,
ChatBskyConvoListConvos,
moderateProfile,
} from '@atproto/api'
import {
InfiniteData,
QueryClient,
Expand All @@ -8,8 +12,9 @@ import {
} from '@tanstack/react-query'

import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent} from '#/state/session'
import {useAgent, useSession} from '#/state/session'
import {decrementBadgeCount} from 'lib/notifications/notifications'

export const RQKEY = ['convo-list']
Expand All @@ -36,16 +41,29 @@ export function useListConvos({refetchInterval}: {refetchInterval: number}) {

export function useUnreadMessageCount() {
const {currentConvoId} = useCurrentConvoId()
const {currentAccount} = useSession()
const convos = useListConvos({
refetchInterval: 30_000,
})
const moderationOpts = useModerationOpts()

const count =
convos.data?.pages
.flatMap(page => page.convos)
.filter(convo => convo.id !== currentConvoId)
.reduce((acc, convo) => {
return acc + (!convo.muted && convo.unreadCount > 0 ? 1 : 0)
const otherMember = convo.members.find(
member => member.did !== currentAccount?.did,
)

if (!otherMember || !moderationOpts) return acc

// TODO could shadow this outside this hook and get optimistic block state
const moderation = moderateProfile(otherMember, moderationOpts)
const shouldIgnore = convo.muted || moderation.blocked
const unreadCount = !shouldIgnore && convo.unreadCount > 0 ? 1 : 0

return acc + unreadCount
}, 0) ?? 0

return useMemo(() => {
Expand Down

0 comments on commit f42f7fa

Please sign in to comment.