diff --git a/lib/actions/user-actions.js b/lib/actions/user-actions.js --- a/lib/actions/user-actions.js +++ b/lib/actions/user-actions.js @@ -32,6 +32,7 @@ } from '../shared/crypto-utils.js'; import { fetchLatestDeviceList } from '../shared/device-list-utils.js'; import { useProcessAndSendDMOperation } from '../shared/dm-ops/process-dm-ops.js'; +import { useUpdateFarcasterSubscription } from '../shared/farcaster/farcaster-api.js'; import type { AuthMetadata } from '../shared/identity-client-context'; import { IdentityClientContext } from '../shared/identity-client-context.js'; import threadWatcher from '../shared/thread-watcher.js'; @@ -1384,6 +1385,7 @@ state => state.currentUserInfo && state.currentUserInfo.id, ); const keyserverCall = useKeyserverCall(updateSubscription); + const updateFarcasterSubscription = useUpdateFarcasterSubscription(); return React.useCallback( async (input: UseUpdateSubscriptionInput) => @@ -1392,9 +1394,15 @@ { processAndSendDMOperation, keyserverUpdateSubscription: keyserverCall, + updateFarcasterSubscription, }, ), - [keyserverCall, processAndSendDMOperation, viewerID], + [ + keyserverCall, + processAndSendDMOperation, + updateFarcasterSubscription, + viewerID, + ], ); } diff --git a/lib/shared/farcaster/farcaster-api.js b/lib/shared/farcaster/farcaster-api.js --- a/lib/shared/farcaster/farcaster-api.js +++ b/lib/shared/farcaster/farcaster-api.js @@ -285,10 +285,33 @@ ); } +export type UpdateFarcasterSubscriptionInput = { + +conversationId: string, + +muted: boolean, +}; + +function useUpdateFarcasterSubscription(): ( + input: UpdateFarcasterSubscriptionInput, +) => Promise { + const { sendFarcasterRequest } = useTunnelbroker(); + return React.useCallback( + async (input: UpdateFarcasterSubscriptionInput) => { + await sendFarcasterRequest({ + apiVersion: 'v2', + endpoint: 'direct-cast-conversation-notifications', + method: { type: 'POST' }, + payload: JSON.stringify(input), + }); + }, + [sendFarcasterRequest], + ); +} + export { useSendFarcasterTextMessage, useFetchFarcasterMessages, useFetchFarcasterConversation, useFetchFarcasterInbox, useUpdateFarcasterGroupNameAndDescription, + useUpdateFarcasterSubscription, }; diff --git a/lib/shared/threads/protocols/farcaster-thread-protocol.js b/lib/shared/threads/protocols/farcaster-thread-protocol.js --- a/lib/shared/threads/protocols/farcaster-thread-protocol.js +++ b/lib/shared/threads/protocols/farcaster-thread-protocol.js @@ -45,8 +45,10 @@ ChangeThreadSettingsUtils, ProtocolChangeThreadSettingsInput, ProtocolSendTextMessageInput, + ProtocolUpdateSubscriptionInput, SendTextMessageUtils, ThreadProtocol, + UpdateSubscriptionUtils, } from '../thread-spec.js'; const farcasterThreadProtocol: ThreadProtocol = { @@ -210,8 +212,32 @@ throw new Error('addThreadMembers method is not yet implemented'); }, - updateSubscription: async (): Promise => { - throw new Error('updateSubscription method is not yet implemented'); + updateSubscription: async ( + protocolInput: ProtocolUpdateSubscriptionInput, + utils: UpdateSubscriptionUtils, + ): Promise => { + const { input } = protocolInput; + const { threadInfo, updatedFields } = input; + const { home, pushNotifs } = updatedFields; + const { updateFarcasterSubscription } = utils; + + invariant( + home === pushNotifs, + 'background notifs are not supported for Farcaster', + ); + + const muted = !home && !pushNotifs; + const conversationId = conversationIDFromFarcasterThreadID(threadInfo.id); + + await updateFarcasterSubscription({ conversationId, muted }); + + return { + threadID: threadInfo.id, + subscription: { + ...threadInfo.currentUser.subscription, + ...updatedFields, + }, + }; }, leaveThread: async () => { diff --git a/lib/shared/threads/thread-spec.js b/lib/shared/threads/thread-spec.js --- a/lib/shared/threads/thread-spec.js +++ b/lib/shared/threads/thread-spec.js @@ -92,6 +92,7 @@ FetchFarcasterConversationInput, FetchFarcasterMessageInput, FetchFarcasterMessageResult, + UpdateFarcasterSubscriptionInput, } from '../farcaster/farcaster-api.js'; import type { FetchThickMessagesType } from '../message-utils.js'; import type { @@ -233,6 +234,7 @@ export type UpdateSubscriptionUtils = { +processAndSendDMOperation: OutboundDMOperationSpecification => Promise, +keyserverUpdateSubscription: SubscriptionUpdateRequest => Promise, + +updateFarcasterSubscription: UpdateFarcasterSubscriptionInput => Promise, }; export type ProtocolLeaveThreadInput = {