diff --git a/src/state/queries/messages/list-conversations.tsx b/src/state/queries/messages/list-conversations.tsx index 53a35c427c..a91073eeb8 100644 --- a/src/state/queries/messages/list-conversations.tsx +++ b/src/state/queries/messages/list-conversations.tsx @@ -259,18 +259,26 @@ export function ListConvosProviderInner({ queryClient.setQueriesData({queryKey: RQKEY('request')}, updateFn) } } else if (ChatBskyConvoDefs.isLogReadMessage(log)) { - const logRef: ChatBskyConvoDefs.LogReadMessage = log queryClient.setQueriesData( {queryKey: [RQKEY_ROOT]}, (old?: ConvoListQueryData) => - optimisticUpdate(logRef.convoId, old, convo => ({ + optimisticUpdate(log.convoId, old, convo => ({ ...convo, unreadCount: 0, - rev: logRef.rev, + rev: log.rev, + })), + ) + } else if (ChatBskyConvoDefs.isLogReadConvo(log)) { + queryClient.setQueriesData( + {queryKey: [RQKEY_ROOT]}, + (old?: ConvoListQueryData) => + optimisticUpdate(log.convoId, old, convo => ({ + ...convo, + unreadCount: 0, + rev: log.rev, })), ) } else if (ChatBskyConvoDefs.isLogAcceptConvo(log)) { - const logRef: ChatBskyConvoDefs.LogAcceptConvo = log const requests = queryClient.getQueryData( RQKEY('request'), ) @@ -285,8 +293,7 @@ export function ListConvosProviderInner({ } queryClient.setQueryData( RQKEY('request'), - (old?: ConvoListQueryData) => - optimisticDelete(logRef.convoId, old), + (old?: ConvoListQueryData) => optimisticDelete(log.convoId, old), ) queryClient.setQueriesData( {queryKey: RQKEY('accepted')}, @@ -313,67 +320,141 @@ export function ListConvosProviderInner({ }, ) } else if (ChatBskyConvoDefs.isLogMuteConvo(log)) { - const logRef: ChatBskyConvoDefs.LogMuteConvo = log queryClient.setQueriesData( {queryKey: [RQKEY_ROOT]}, (old?: ConvoListQueryData) => - optimisticUpdate(logRef.convoId, old, convo => ({ + optimisticUpdate(log.convoId, old, convo => ({ ...convo, muted: true, - rev: logRef.rev, + rev: log.rev, })), ) } else if (ChatBskyConvoDefs.isLogUnmuteConvo(log)) { - const logRef: ChatBskyConvoDefs.LogUnmuteConvo = log queryClient.setQueriesData( {queryKey: [RQKEY_ROOT]}, (old?: ConvoListQueryData) => - optimisticUpdate(logRef.convoId, old, convo => ({ + optimisticUpdate(log.convoId, old, convo => ({ ...convo, muted: false, - rev: logRef.rev, + rev: log.rev, })), ) - } else if (ChatBskyConvoDefs.isLogAddReaction(log)) { - const logRef: ChatBskyConvoDefs.LogAddReaction = log + } else if (ChatBskyConvoDefs.isLogLockConvo(log)) { queryClient.setQueriesData( {queryKey: [RQKEY_ROOT]}, (old?: ConvoListQueryData) => - optimisticUpdate(logRef.convoId, old, convo => ({ + optimisticUpdate(log.convoId, old, convo => { + if (ChatBskyConvoDefs.isGroupConvo(convo.kind)) { + return { + ...convo, + kind: { + ...convo.kind, + lockStatus: 'locked', + }, + rev: log.rev, + } + } + return { + ...convo, + rev: log.rev, + } + }), + ) + } else if (ChatBskyConvoDefs.isLogUnlockConvo(log)) { + queryClient.setQueriesData( + {queryKey: [RQKEY_ROOT]}, + (old?: ConvoListQueryData) => + optimisticUpdate(log.convoId, old, convo => { + if (ChatBskyConvoDefs.isGroupConvo(convo.kind)) { + return { + ...convo, + kind: { + ...convo.kind, + lockStatus: 'unlocked', + }, + rev: log.rev, + } + } + return { + ...convo, + rev: log.rev, + } + }), + ) + } else if (ChatBskyConvoDefs.isLogLockConvoPermanently(log)) { + queryClient.setQueriesData( + {queryKey: [RQKEY_ROOT]}, + (old?: ConvoListQueryData) => + optimisticUpdate(log.convoId, old, convo => { + if (ChatBskyConvoDefs.isGroupConvo(convo.kind)) { + return { + ...convo, + kind: { + ...convo.kind, + lockStatus: 'locked-permanently', + }, + rev: log.rev, + } + } + return { + ...convo, + rev: log.rev, + } + }), + ) + } else if ( + ChatBskyConvoDefs.isLogCreateJoinLink(log) || + ChatBskyConvoDefs.isLogEditJoinLink(log) || + ChatBskyConvoDefs.isLogEnableJoinLink(log) || + ChatBskyConvoDefs.isLogDisableJoinLink(log) + ) { + // Join link data not included in the log event, trigger refetch to get it + debouncedRefetch() + } else if ( + ChatBskyConvoDefs.isLogIncomingJoinRequest(log) || + ChatBskyConvoDefs.isLogApproveJoinRequest(log) || + ChatBskyConvoDefs.isLogRejectJoinRequest(log) || + ChatBskyConvoDefs.isLogOutgoingJoinRequest(log) + ) { + // TODO Update join request count here when available. -dsb + } else if (ChatBskyConvoDefs.isLogAddReaction(log)) { + queryClient.setQueriesData( + {queryKey: [RQKEY_ROOT]}, + (old?: ConvoListQueryData) => + optimisticUpdate(log.convoId, old, convo => ({ ...convo, lastReaction: { $type: 'chat.bsky.convo.defs#messageAndReactionView', - reaction: logRef.reaction, - message: logRef.message, + reaction: log.reaction, + message: log.message, }, - rev: logRef.rev, + rev: log.rev, })), ) } else if (ChatBskyConvoDefs.isLogRemoveReaction(log)) { - const logRef: ChatBskyConvoDefs.LogRemoveReaction = log queryClient.setQueriesData( {queryKey: [RQKEY_ROOT]}, (old?: ConvoListQueryData) => - optimisticUpdate(logRef.convoId, old, convo => { + optimisticUpdate(log.convoId, old, convo => { if ( // if the convo is the same - logRef.convoId === convo.id && + log.convoId === convo.id && ChatBskyConvoDefs.isMessageAndReactionView( convo.lastReaction, ) && - ChatBskyConvoDefs.isMessageView(logRef.message) && + ChatBskyConvoDefs.isMessageView(log.message) && // ...and the message is the same - convo.lastReaction.message.id === logRef.message.id && + convo.lastReaction.message.id === log.message.id && // ...and the reaction is the same convo.lastReaction.reaction.sender.did === - logRef.reaction.sender.did && - convo.lastReaction.reaction.value === logRef.reaction.value + log.reaction.sender.did && + convo.lastReaction.reaction.value === log.reaction.value ) { return { ...convo, // ...remove the reaction. hopefully they didn't react twice in a row! lastReaction: undefined, - rev: logRef.rev, + rev: log.rev, } } else { return convo diff --git a/src/state/queries/messages/list-convo-members.ts b/src/state/queries/messages/list-convo-members.ts index 99573a90e3..5a59a31882 100644 --- a/src/state/queries/messages/list-convo-members.ts +++ b/src/state/queries/messages/list-convo-members.ts @@ -76,6 +76,35 @@ export function useListConvoMembersQuery({ ) { mutateList(list => list.filter(m => m.did !== data.member.did)) } + } else if (ChatBskyConvoDefs.isLogMemberJoin(log)) { + const data = log.message.data + if ( + bsky.dangerousIsType( + data, + ChatBskyConvoDefs.isSystemMessageDataMemberJoin, + ) + ) { + const newMember = log.relatedProfiles.find( + r => r.did === data.member.did, + ) + if (newMember) { + mutateList(list => + list.some(m => m.did === newMember.did) + ? list + : list.concat(newMember), + ) + } + } + } else if (ChatBskyConvoDefs.isLogMemberLeave(log)) { + const data = log.message.data + if ( + bsky.dangerousIsType( + data, + ChatBskyConvoDefs.isSystemMessageDataMemberLeave, + ) + ) { + mutateList(list => list.filter(m => m.did !== data.member.did)) + } } } },