about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Newman <mozzius@protonmail.com>2024-06-18 22:05:59 +0100
committerGitHub <noreply@github.com>2024-06-19 00:05:59 +0300
commit0012d1236ffa4f268fb359931d02335b21610fd6 (patch)
treee217b731a607a332b084a35b5717e64811209984
parent502bcad7017d72fb23c40a268b1e220f892db7da (diff)
downloadvoidsky-0012d1236ffa4f268fb359931d02335b21610fd6.tar.zst
Migrate local thread mutes (#4523)
* migrate thread mutes

* don't try and clear if not logged in yet

* migrate mutes one at a time

* write before mutating

* only migrate mutes of self posts

* use /** @deprecated */

* shouldLike -> shouldMute
-rw-r--r--src/state/cache/thread-mutes.tsx55
-rw-r--r--src/state/persisted/schema.ts3
-rw-r--r--src/state/queries/post.ts4
3 files changed, 58 insertions, 4 deletions
diff --git a/src/state/cache/thread-mutes.tsx b/src/state/cache/thread-mutes.tsx
index b58bd430f..dc5104c14 100644
--- a/src/state/cache/thread-mutes.tsx
+++ b/src/state/cache/thread-mutes.tsx
@@ -1,4 +1,7 @@
-import React from 'react'
+import React, {useEffect} from 'react'
+
+import * as persisted from '#/state/persisted'
+import {useAgent, useSession} from '../session'
 
 type StateContext = Map<string, boolean>
 type SetStateContext = (uri: string, value: boolean) => void
@@ -21,6 +24,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
     },
     [setState],
   )
+
+  useMigrateMutes(setThreadMute)
+
   return (
     <stateContext.Provider value={state}>
       <setStateContext.Provider value={setThreadMute}>
@@ -42,3 +48,50 @@ export function useIsThreadMuted(uri: string, defaultValue = false) {
 export function useSetThreadMute() {
   return React.useContext(setStateContext)
 }
+
+function useMigrateMutes(setThreadMute: SetStateContext) {
+  const agent = useAgent()
+  const {currentAccount} = useSession()
+
+  useEffect(() => {
+    if (currentAccount) {
+      if (
+        !persisted
+          .get('mutedThreads')
+          .some(uri => uri.includes(currentAccount.did))
+      ) {
+        return
+      }
+
+      let cancelled = false
+
+      const migrate = async () => {
+        while (!cancelled) {
+          const threads = persisted.get('mutedThreads')
+
+          const root = threads.findLast(uri => uri.includes(currentAccount.did))
+
+          if (!root) break
+
+          persisted.write(
+            'mutedThreads',
+            threads.filter(uri => uri !== root),
+          )
+
+          setThreadMute(root, true)
+
+          await agent.api.app.bsky.graph
+            .muteThread({root})
+            // not a big deal if this fails, since the post might have been deleted
+            .catch(console.error)
+        }
+      }
+
+      migrate()
+
+      return () => {
+        cancelled = true
+      }
+    }
+  }, [agent, currentAccount, setThreadMute])
+}
diff --git a/src/state/persisted/schema.ts b/src/state/persisted/schema.ts
index b81cf5962..9d5b17d35 100644
--- a/src/state/persisted/schema.ts
+++ b/src/state/persisted/schema.ts
@@ -74,7 +74,6 @@ export const schema = z.object({
       flickr: z.enum(externalEmbedOptions).optional(),
     })
     .optional(),
-  mutedThreads: z.array(z.string()), // should move to server
   invites: z.object({
     copiedInvites: z.array(z.string()),
   }),
@@ -88,6 +87,8 @@ export const schema = z.object({
   disableHaptics: z.boolean().optional(),
   disableAutoplay: z.boolean().optional(),
   kawaii: z.boolean().optional(),
+  /** @deprecated */
+  mutedThreads: z.array(z.string()),
 })
 export type Schema = z.infer<typeof schema>
 
diff --git a/src/state/queries/post.ts b/src/state/queries/post.ts
index 8e77bf6b9..a511d6b3d 100644
--- a/src/state/queries/post.ts
+++ b/src/state/queries/post.ts
@@ -304,8 +304,8 @@ export function useThreadMuteMutationQueue(
 
   const queueToggle = useToggleMutationQueue<boolean>({
     initialState: isThreadMuted,
-    runMutation: async (_prev, shouldLike) => {
-      if (shouldLike) {
+    runMutation: async (_prev, shouldMute) => {
+      if (shouldMute) {
         await threadMuteMutation.mutateAsync({
           uri: rootUri,
         })