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 @@ -955,15 +955,42 @@ utils: PinMessageUtils, ): Promise => { const { messageID, threadInfo, action } = input; - const { farcasterPinMessage } = utils; + const { farcasterPinMessage, dispatch, viewerID } = utils; + invariant(viewerID, 'Viewer ID should be present'); const conversationId = conversationIDFromFarcasterThreadID(threadInfo.id); + const time = Date.now(); await farcasterPinMessage({ conversationId, messageId: messageID, action, }); + + const currentPinnedMessageIDs = threadInfo.pinnedMessageIDs ?? []; + // Farcaster supports only one pinned message at a time + const updatedPinnedMessageIDs = + action === 'pin' + ? [messageID] + : currentPinnedMessageIDs.filter(id => id !== messageID); + + const updateInfos = [ + { + type: updateTypes.UPDATE_THREAD, + id: uuid.v4(), + time, + threadInfo: { + ...threadInfo, + pinnedMessageIDs: updatedPinnedMessageIDs, + pinnedCount: updatedPinnedMessageIDs.length, + }, + }, + ]; + + dispatch({ + type: processFarcasterOpsActionType, + payload: { rawMessageInfos: [], updateInfos }, + }); }, fetchPinnedMessages: 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 @@ -380,13 +380,15 @@ export type ProtocolPinMessageInput = { +messageID: string, - +threadInfo: ThreadInfo, + +threadInfo: RawThreadInfo, +action: 'pin' | 'unpin', }; export type PinMessageUtils = { +keyserverToggleMessagePin: ToggleMessagePinRequest => Promise, +farcasterPinMessage: PinMessageInput => Promise, +dispatchActionPromise: DispatchActionPromise, + +dispatch: Dispatch, + +viewerID: ?string, }; export type ProtocolFetchPinnedMessagesInput = { diff --git a/lib/utils/pin-message-utils.js b/lib/utils/pin-message-utils.js --- a/lib/utils/pin-message-utils.js +++ b/lib/utils/pin-message-utils.js @@ -3,6 +3,7 @@ import * as React from 'react'; import { useDispatchActionPromise } from './redux-promise-utils.js'; +import { useDispatch, useSelector } from './redux-utils.js'; import { useGetLatestMessageEdit } from '../hooks/latest-message-edit.js'; import { useFetchPinnedMessages, @@ -15,29 +16,40 @@ function usePinMessageAction(): ( messageID: string, - threadInfo: ThreadInfo, + threadID: string, action: 'pin' | 'unpin', ) => Promise { const keyserverToggleMessagePin = useToggleMessagePin(); const farcasterPinMessage = usePinMessage(); const dispatchActionPromise = useDispatchActionPromise(); + const dispatch = useDispatch(); + const viewerID = useSelector( + state => state.currentUserInfo && state.currentUserInfo.id, + ); + const threadInfos = useSelector(state => state.threadStore.threadInfos); return React.useCallback( - async ( - messageID: string, - threadInfo: ThreadInfo, - action: 'pin' | 'unpin', - ) => { + async (messageID: string, threadID: string, action: 'pin' | 'unpin') => { + const threadInfo = threadInfos[threadID]; await threadSpecs[threadInfo.type].protocol().pinMessage( { messageID, threadInfo, action }, { keyserverToggleMessagePin, farcasterPinMessage, dispatchActionPromise, + dispatch, + viewerID, }, ); }, - [keyserverToggleMessagePin, farcasterPinMessage, dispatchActionPromise], + [ + keyserverToggleMessagePin, + farcasterPinMessage, + dispatchActionPromise, + dispatch, + viewerID, + threadInfos, + ], ); } diff --git a/native/chat/toggle-pin-modal.react.js b/native/chat/toggle-pin-modal.react.js --- a/native/chat/toggle-pin-modal.react.js +++ b/native/chat/toggle-pin-modal.react.js @@ -59,12 +59,12 @@ const onPress = React.useCallback(() => { invariant(messageInfo.id, 'messageInfo.id should be defined'); - void pinMessageAction(messageInfo.id, threadInfo, modalInfo.action); + void pinMessageAction(messageInfo.id, threadInfo.id, modalInfo.action); navigation.goBack(); }, [ pinMessageAction, messageInfo.id, - threadInfo, + threadInfo.id, modalInfo.action, navigation, ]); diff --git a/web/modals/chat/toggle-pin-modal.react.js b/web/modals/chat/toggle-pin-modal.react.js --- a/web/modals/chat/toggle-pin-modal.react.js +++ b/web/modals/chat/toggle-pin-modal.react.js @@ -76,15 +76,15 @@ ); void pinMessageAction( engagementTargetMessageID, - threadInfo, + threadInfo.id, modalInfo.action, ); popModal(); }, [ + threadInfo.id, modalInfo.action, pinMessageAction, engagementTargetMessageID, - threadInfo, popModal, ]);