about summary refs log tree commit diff
path: root/src/state/queries/messages
diff options
context:
space:
mode:
Diffstat (limited to 'src/state/queries/messages')
-rw-r--r--src/state/queries/messages/actor-declaration.ts2
-rw-r--r--src/state/queries/messages/list-conversations.tsx50
2 files changed, 30 insertions, 22 deletions
diff --git a/src/state/queries/messages/actor-declaration.ts b/src/state/queries/messages/actor-declaration.ts
index 828b85d9e..34fb10935 100644
--- a/src/state/queries/messages/actor-declaration.ts
+++ b/src/state/queries/messages/actor-declaration.ts
@@ -69,12 +69,10 @@ export function useDeleteActorDeclaration() {
   return useMutation({
     mutationFn: async () => {
       if (!currentAccount) throw new Error('Not signed in')
-      // TODO(sam): remove validate: false once PDSes have the new lexicon
       const result = await agent.api.com.atproto.repo.deleteRecord({
         repo: currentAccount.did,
         collection: 'chat.bsky.actor.declaration',
         rkey: 'self',
-        validate: false,
       })
       return result
     },
diff --git a/src/state/queries/messages/list-conversations.tsx b/src/state/queries/messages/list-conversations.tsx
index ae379f962..8c9d6c429 100644
--- a/src/state/queries/messages/list-conversations.tsx
+++ b/src/state/queries/messages/list-conversations.tsx
@@ -101,7 +101,7 @@ export function ListConvosProviderInner({
       events => {
         if (events.type !== 'logs') return
 
-        events.logs.forEach(log => {
+        for (const log of events.logs) {
           if (ChatBskyConvoDefs.isLogBeginConvo(log)) {
             debouncedRefetch()
           } else if (ChatBskyConvoDefs.isLogLeaveConvo(log)) {
@@ -110,30 +110,40 @@ export function ListConvosProviderInner({
             )
           } else if (ChatBskyConvoDefs.isLogDeleteMessage(log)) {
             queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) =>
-              optimisticUpdate(log.convoId, old, convo =>
-                log.message.id === convo.lastMessage?.id
-                  ? {
-                      ...convo,
-                      rev: log.rev,
-                      lastMessage: log.message,
-                    }
-                  : convo,
-              ),
+              optimisticUpdate(log.convoId, old, convo => {
+                if (
+                  (ChatBskyConvoDefs.isDeletedMessageView(log.message) ||
+                    ChatBskyConvoDefs.isMessageView(log.message)) &&
+                  (ChatBskyConvoDefs.isDeletedMessageView(convo.lastMessage) ||
+                    ChatBskyConvoDefs.isMessageView(convo.lastMessage))
+                ) {
+                  return log.message.id === convo.lastMessage.id
+                    ? {
+                        ...convo,
+                        rev: log.rev,
+                        lastMessage: log.message,
+                      }
+                    : convo
+                } else {
+                  return convo
+                }
+              }),
             )
           } else if (ChatBskyConvoDefs.isLogCreateMessage(log)) {
+            // Store in a new var to avoid TS errors due to closures.
+            const logRef: ChatBskyConvoDefs.LogCreateMessage = log
+
             queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => {
               if (!old) return old
 
               function updateConvo(convo: ChatBskyConvoDefs.ConvoView) {
-                if (!ChatBskyConvoDefs.isLogCreateMessage(log)) return convo
-
                 let unreadCount = convo.unreadCount
                 if (convo.id !== currentConvoId) {
                   if (
-                    ChatBskyConvoDefs.isMessageView(log.message) ||
-                    ChatBskyConvoDefs.isDeletedMessageView(log.message)
+                    ChatBskyConvoDefs.isMessageView(logRef.message) ||
+                    ChatBskyConvoDefs.isDeletedMessageView(logRef.message)
                   ) {
-                    if (log.message.sender.did !== currentAccount?.did) {
+                    if (logRef.message.sender.did !== currentAccount?.did) {
                       unreadCount++
                     }
                   }
@@ -143,8 +153,8 @@ export function ListConvosProviderInner({
 
                 return {
                   ...convo,
-                  rev: log.rev,
-                  lastMessage: log.message,
+                  rev: logRef.rev,
+                  lastMessage: logRef.message,
                   unreadCount,
                 }
               }
@@ -152,10 +162,10 @@ export function ListConvosProviderInner({
               function filterConvoFromPage(
                 convo: ChatBskyConvoDefs.ConvoView[],
               ) {
-                return convo.filter(c => c.id !== log.convoId)
+                return convo.filter(c => c.id !== logRef.convoId)
               }
 
-              const existingConvo = getConvoFromQueryData(log.convoId, old)
+              const existingConvo = getConvoFromQueryData(logRef.convoId, old)
 
               if (existingConvo) {
                 return {
@@ -186,7 +196,7 @@ export function ListConvosProviderInner({
               }
             })
           }
-        })
+        }
       },
       {
         // get events for all chats