diff --git a/lib/actions/activity-actions.js b/lib/actions/activity-actions.js --- a/lib/actions/activity-actions.js +++ b/lib/actions/activity-actions.js @@ -5,9 +5,14 @@ ActivityUpdateSuccessPayload, SetThreadUnreadStatusPayload, SetThreadUnreadStatusRequest, - SetThreadUnreadStatusResult, } from '../types/activity-types.js'; -import type { CallServerEndpoint } from '../utils/call-server-endpoint.js'; +import { extractKeyserverIDFromID } from '../utils/action-utils.js'; +import type { CallKeyserverEndpoint } from '../utils/keyserver-call'; +import { useKeyserverCall } from '../utils/keyserver-call.js'; + +export type UpdateActivityInput = { + +activityUpdates: $ReadOnlyArray, +}; const updateActivityActionTypes = Object.freeze({ started: 'UPDATE_ACTIVITY_STARTED', @@ -16,22 +21,42 @@ }); const updateActivity = ( - callServerEndpoint: CallServerEndpoint, - ): (( - activityUpdates: $ReadOnlyArray, - ) => Promise) => - async activityUpdates => { - const response = await callServerEndpoint('update_activity', { - updates: activityUpdates, - }); + callKeyserverEndpoint: CallKeyserverEndpoint, + ): ((input: UpdateActivityInput) => Promise) => + async input => { + const { activityUpdates } = input; + const requests = {}; + for (const update of activityUpdates) { + const keyserverID = extractKeyserverIDFromID(update.threadID); + if (!requests[keyserverID]) { + requests[keyserverID] = { updates: [] }; + } + requests[keyserverID].updates.push(update); + } + + const responses = await callKeyserverEndpoint('update_activity', requests); + + let unfocusedToUnread = []; + for (const keyserverID in responses) { + unfocusedToUnread = unfocusedToUnread.concat( + responses[keyserverID].unfocusedToUnread, + ); + } + return { activityUpdates, result: { - unfocusedToUnread: response.unfocusedToUnread, + unfocusedToUnread, }, }; }; +function useUpdateActivity(): ( + input: UpdateActivityInput, +) => Promise { + return useKeyserverCall(updateActivity); +} + const setThreadUnreadStatusActionTypes = Object.freeze({ started: 'SET_THREAD_UNREAD_STATUS_STARTED', success: 'SET_THREAD_UNREAD_STATUS_SUCCESS', @@ -39,24 +64,33 @@ }); const setThreadUnreadStatus = ( - callServerEndpoint: CallServerEndpoint, + callKeyserverEndpoint: CallKeyserverEndpoint, ): (( - request: SetThreadUnreadStatusRequest, + input: SetThreadUnreadStatusRequest, ) => Promise) => - async request => { - const response: SetThreadUnreadStatusResult = await callServerEndpoint( + async input => { + const keyserverID = extractKeyserverIDFromID(input.threadID); + const requests = { [keyserverID]: input }; + + const responses = await callKeyserverEndpoint( 'set_thread_unread_status', - request, + requests, ); return { - resetToUnread: response.resetToUnread, - threadID: request.threadID, + resetToUnread: responses[keyserverID].resetToUnread, + threadID: input.threadID, }; }; +function useSetThreadUnreadStatus(): ( + request: SetThreadUnreadStatusRequest, +) => Promise { + return useKeyserverCall(setThreadUnreadStatus); +} + export { updateActivityActionTypes, - updateActivity, + useUpdateActivity, setThreadUnreadStatusActionTypes, - setThreadUnreadStatus, + useSetThreadUnreadStatus, }; diff --git a/lib/hooks/toggle-unread-status.js b/lib/hooks/toggle-unread-status.js --- a/lib/hooks/toggle-unread-status.js +++ b/lib/hooks/toggle-unread-status.js @@ -3,7 +3,7 @@ import * as React from 'react'; import { - setThreadUnreadStatus, + useSetThreadUnreadStatus, setThreadUnreadStatusActionTypes, } from '../actions/activity-actions.js'; import type { @@ -11,10 +11,7 @@ SetThreadUnreadStatusRequest, } from '../types/activity-types.js'; import type { ThreadInfo } from '../types/thread-types.js'; -import { - useDispatchActionPromise, - useServerCall, -} from '../utils/action-utils.js'; +import { useDispatchActionPromise } from '../utils/action-utils.js'; function useToggleUnreadStatus( threadInfo: ThreadInfo, @@ -25,9 +22,7 @@ const { currentUser } = threadInfo; const boundSetThreadUnreadStatus: ( request: SetThreadUnreadStatusRequest, - ) => Promise = useServerCall( - setThreadUnreadStatus, - ); + ) => Promise = useSetThreadUnreadStatus(); const toggleUnreadStatus = React.useCallback(() => { const request = { threadID: threadInfo.id, diff --git a/lib/socket/activity-handler.react.js b/lib/socket/activity-handler.react.js --- a/lib/socket/activity-handler.react.js +++ b/lib/socket/activity-handler.react.js @@ -6,16 +6,13 @@ import { updateActivityActionTypes, - updateActivity, + useUpdateActivity, } from '../actions/activity-actions.js'; import { connectionSelector } from '../selectors/keyserver-selectors.js'; import { getMostRecentNonLocalMessageID } from '../shared/message-utils.js'; import { threadIsPending } from '../shared/thread-utils.js'; import { queueActivityUpdatesActionType } from '../types/activity-types.js'; -import { - useServerCall, - useDispatchActionPromise, -} from '../utils/action-utils.js'; +import { useDispatchActionPromise } from '../utils/action-utils.js'; import { useSelector } from '../utils/redux-utils.js'; type Props = { @@ -62,7 +59,7 @@ const dispatch = useDispatch(); const dispatchActionPromise = useDispatchActionPromise(); - const callUpdateActivity = useServerCall(updateActivity); + const callUpdateActivity = useUpdateActivity(); React.useEffect(() => { const activityUpdates = []; @@ -125,7 +122,7 @@ } dispatchActionPromise( updateActivityActionTypes, - callUpdateActivity(activityUpdates), + callUpdateActivity({ activityUpdates }), ); });