2023-08-09 00:53:06 +00:00
|
|
|
// Copyright 2023 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2024-08-23 20:09:19 +00:00
|
|
|
import type { ThunkAction, ThunkDispatch } from 'redux-thunk';
|
2024-08-22 18:12:25 +00:00
|
|
|
import { debounce, omit } from 'lodash';
|
2023-08-09 00:53:06 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2024-03-11 18:18:55 +00:00
|
|
|
import {
|
|
|
|
clearCallHistoryDataAndSync,
|
|
|
|
markAllCallHistoryReadAndSync,
|
|
|
|
} from '../../util/callDisposition';
|
2023-08-09 00:53:06 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
|
|
|
import type { ToastActionType } from './toast';
|
|
|
|
import { showToast } from './toast';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../../sql/Client';
|
2023-08-09 00:53:06 +00:00
|
|
|
import { ToastType } from '../../types/Toast';
|
|
|
|
import type { CallHistoryDetails } from '../../types/CallDisposition';
|
2023-08-10 22:16:51 +00:00
|
|
|
import * as log from '../../logging/log';
|
|
|
|
import * as Errors from '../../types/errors';
|
2023-08-22 21:01:36 +00:00
|
|
|
import { drop } from '../../util/drop';
|
2024-06-26 00:58:38 +00:00
|
|
|
import {
|
|
|
|
getCallHistoryLatestCall,
|
|
|
|
getCallHistorySelector,
|
|
|
|
} from '../selectors/callHistory';
|
2024-08-06 19:29:13 +00:00
|
|
|
import {
|
|
|
|
getCallsHistoryForRedux,
|
|
|
|
getCallsHistoryUnreadCountForRedux,
|
2024-08-26 14:26:21 +00:00
|
|
|
loadCallHistory,
|
2024-08-06 19:29:13 +00:00
|
|
|
} from '../../services/callHistoryLoader';
|
|
|
|
import { makeLookup } from '../../util/makeLookup';
|
2023-08-09 00:53:06 +00:00
|
|
|
|
|
|
|
export type CallHistoryState = ReadonlyDeep<{
|
|
|
|
// This informs the app that underlying call history data has changed.
|
|
|
|
edition: number;
|
2023-08-14 23:28:47 +00:00
|
|
|
unreadCount: number;
|
2023-08-09 00:53:06 +00:00
|
|
|
callHistoryByCallId: Record<string, CallHistoryDetails>;
|
|
|
|
}>;
|
|
|
|
|
2023-09-27 19:42:30 +00:00
|
|
|
const CALL_HISTORY_ADD = 'callHistory/ADD';
|
|
|
|
const CALL_HISTORY_REMOVE = 'callHistory/REMOVE';
|
2023-08-10 22:16:51 +00:00
|
|
|
const CALL_HISTORY_RESET = 'callHistory/RESET';
|
2024-08-06 19:29:13 +00:00
|
|
|
const CALL_HISTORY_RELOAD = 'callHistory/RELOAD';
|
2023-08-14 23:28:47 +00:00
|
|
|
const CALL_HISTORY_UPDATE_UNREAD = 'callHistory/UPDATE_UNREAD';
|
2023-08-09 00:53:06 +00:00
|
|
|
|
2023-09-27 19:42:30 +00:00
|
|
|
export type CallHistoryAdd = ReadonlyDeep<{
|
|
|
|
type: typeof CALL_HISTORY_ADD;
|
2023-08-09 00:53:06 +00:00
|
|
|
payload: CallHistoryDetails;
|
|
|
|
}>;
|
|
|
|
|
2023-09-27 19:42:30 +00:00
|
|
|
export type CallHistoryRemove = ReadonlyDeep<{
|
|
|
|
type: typeof CALL_HISTORY_REMOVE;
|
|
|
|
payload: CallHistoryDetails['callId'];
|
|
|
|
}>;
|
|
|
|
|
2023-08-10 22:16:51 +00:00
|
|
|
export type CallHistoryReset = ReadonlyDeep<{
|
|
|
|
type: typeof CALL_HISTORY_RESET;
|
2023-08-09 00:53:06 +00:00
|
|
|
}>;
|
|
|
|
|
2024-08-06 19:29:13 +00:00
|
|
|
export type CallHistoryReload = ReadonlyDeep<{
|
|
|
|
type: typeof CALL_HISTORY_RELOAD;
|
|
|
|
payload: {
|
|
|
|
callsHistory: ReadonlyArray<CallHistoryDetails>;
|
|
|
|
callsHistoryUnreadCount: number;
|
|
|
|
};
|
|
|
|
}>;
|
|
|
|
|
2023-08-14 23:28:47 +00:00
|
|
|
export type CallHistoryUpdateUnread = ReadonlyDeep<{
|
|
|
|
type: typeof CALL_HISTORY_UPDATE_UNREAD;
|
|
|
|
payload: number;
|
|
|
|
}>;
|
|
|
|
|
2023-08-09 00:53:06 +00:00
|
|
|
export type CallHistoryAction = ReadonlyDeep<
|
2023-09-27 19:42:30 +00:00
|
|
|
| CallHistoryAdd
|
|
|
|
| CallHistoryRemove
|
|
|
|
| CallHistoryReset
|
2024-08-06 19:29:13 +00:00
|
|
|
| CallHistoryReload
|
2023-09-27 19:42:30 +00:00
|
|
|
| CallHistoryUpdateUnread
|
2023-08-09 00:53:06 +00:00
|
|
|
>;
|
|
|
|
|
|
|
|
export function getEmptyState(): CallHistoryState {
|
|
|
|
return {
|
|
|
|
edition: 0,
|
2023-08-14 23:28:47 +00:00
|
|
|
unreadCount: 0,
|
2023-08-09 00:53:06 +00:00
|
|
|
callHistoryByCallId: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-08-23 20:09:19 +00:00
|
|
|
const updateCallHistoryUnreadCountDebounced = debounce(
|
|
|
|
async (
|
|
|
|
dispatch: ThunkDispatch<RootStateType, unknown, CallHistoryUpdateUnread>
|
|
|
|
) => {
|
2023-08-14 23:28:47 +00:00
|
|
|
try {
|
2024-07-22 18:16:33 +00:00
|
|
|
const unreadCount = await DataReader.getCallHistoryUnreadCount();
|
2023-08-14 23:28:47 +00:00
|
|
|
dispatch({ type: CALL_HISTORY_UPDATE_UNREAD, payload: unreadCount });
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'Error updating call history unread count',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2024-08-23 20:09:19 +00:00
|
|
|
},
|
2024-08-22 18:12:25 +00:00
|
|
|
300
|
|
|
|
);
|
|
|
|
|
2024-08-23 20:09:19 +00:00
|
|
|
function updateCallHistoryUnreadCount(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
CallHistoryUpdateUnread
|
|
|
|
> {
|
|
|
|
return async dispatch => {
|
|
|
|
await updateCallHistoryUnreadCountDebounced(dispatch);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-14 23:28:47 +00:00
|
|
|
function markCallHistoryRead(
|
|
|
|
conversationId: string,
|
|
|
|
callId: string
|
|
|
|
): ThunkAction<void, RootStateType, unknown, CallHistoryUpdateUnread> {
|
|
|
|
return async dispatch => {
|
|
|
|
try {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.markCallHistoryRead(callId);
|
2023-08-22 21:01:36 +00:00
|
|
|
drop(window.ConversationController.get(conversationId)?.updateUnread());
|
2023-08-14 23:28:47 +00:00
|
|
|
} catch (error) {
|
2023-08-22 21:01:36 +00:00
|
|
|
log.error(
|
|
|
|
'markCallHistoryRead: Error marking call history read',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
dispatch(updateCallHistoryUnreadCount());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-06-26 00:58:38 +00:00
|
|
|
export function markCallHistoryReadInConversation(
|
|
|
|
callId: string
|
|
|
|
): ThunkAction<void, RootStateType, unknown, CallHistoryUpdateUnread> {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const callHistorySelector = getCallHistorySelector(getState());
|
|
|
|
const callHistory = callHistorySelector(callId);
|
|
|
|
if (callHistory == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await markAllCallHistoryReadAndSync(callHistory, true);
|
|
|
|
} finally {
|
|
|
|
dispatch(updateCallHistoryUnreadCount());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-08-22 21:01:36 +00:00
|
|
|
function markCallsTabViewed(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
CallHistoryUpdateUnread
|
|
|
|
> {
|
2024-06-26 00:58:38 +00:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const latestCall = getCallHistoryLatestCall(getState());
|
|
|
|
if (latestCall != null) {
|
|
|
|
await markAllCallHistoryReadAndSync(latestCall, false);
|
|
|
|
dispatch(updateCallHistoryUnreadCount());
|
|
|
|
}
|
2023-08-14 23:28:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-06-10 15:23:43 +00:00
|
|
|
export function addCallHistory(
|
|
|
|
callHistory: CallHistoryDetails
|
|
|
|
): CallHistoryAdd {
|
2023-08-09 00:53:06 +00:00
|
|
|
return {
|
2023-09-27 19:42:30 +00:00
|
|
|
type: CALL_HISTORY_ADD,
|
2023-08-09 00:53:06 +00:00
|
|
|
payload: callHistory,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-27 19:42:30 +00:00
|
|
|
function removeCallHistory(
|
|
|
|
callId: CallHistoryDetails['callId']
|
|
|
|
): CallHistoryRemove {
|
|
|
|
return {
|
|
|
|
type: CALL_HISTORY_REMOVE,
|
|
|
|
payload: callId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetCallHistory(): CallHistoryReset {
|
|
|
|
return { type: CALL_HISTORY_RESET };
|
|
|
|
}
|
|
|
|
|
2023-08-09 00:53:06 +00:00
|
|
|
function clearAllCallHistory(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
2023-08-10 22:16:51 +00:00
|
|
|
CallHistoryReset | ToastActionType
|
2023-08-09 00:53:06 +00:00
|
|
|
> {
|
2024-06-26 00:58:38 +00:00
|
|
|
return async (dispatch, getState) => {
|
2023-08-10 22:16:51 +00:00
|
|
|
try {
|
2024-06-26 00:58:38 +00:00
|
|
|
const latestCall = getCallHistoryLatestCall(getState());
|
|
|
|
if (latestCall != null) {
|
|
|
|
await clearCallHistoryDataAndSync(latestCall);
|
|
|
|
dispatch(showToast({ toastType: ToastType.CallHistoryCleared }));
|
|
|
|
}
|
2023-08-10 22:16:51 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error('Error clearing call history', Errors.toLogFormat(error));
|
|
|
|
} finally {
|
2024-08-06 19:29:13 +00:00
|
|
|
// Just force a reload, even if the clear failed.
|
|
|
|
dispatch(reloadCallHistory());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reloadCallHistory(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
CallHistoryReload
|
|
|
|
> {
|
|
|
|
return async dispatch => {
|
|
|
|
try {
|
2024-08-26 14:26:21 +00:00
|
|
|
await loadCallHistory();
|
2024-08-06 19:29:13 +00:00
|
|
|
const callsHistory = getCallsHistoryForRedux();
|
|
|
|
const callsHistoryUnreadCount = getCallsHistoryUnreadCountForRedux();
|
|
|
|
dispatch({
|
|
|
|
type: CALL_HISTORY_RELOAD,
|
|
|
|
payload: { callsHistory, callsHistoryUnreadCount },
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
log.error('Error reloading call history', Errors.toLogFormat(error));
|
2023-08-10 22:16:51 +00:00
|
|
|
}
|
2023-08-09 00:53:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
2023-09-27 19:42:30 +00:00
|
|
|
addCallHistory,
|
|
|
|
removeCallHistory,
|
|
|
|
resetCallHistory,
|
2024-08-26 14:26:21 +00:00
|
|
|
reloadCallHistory,
|
2023-08-09 00:53:06 +00:00
|
|
|
clearAllCallHistory,
|
2023-08-14 23:28:47 +00:00
|
|
|
updateCallHistoryUnreadCount,
|
|
|
|
markCallHistoryRead,
|
2023-08-22 21:01:36 +00:00
|
|
|
markCallsTabViewed,
|
2023-08-09 00:53:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const useCallHistoryActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
|
|
|
export function reducer(
|
|
|
|
state: CallHistoryState = getEmptyState(),
|
|
|
|
action: CallHistoryAction
|
|
|
|
): CallHistoryState {
|
|
|
|
switch (action.type) {
|
2023-08-10 22:16:51 +00:00
|
|
|
case CALL_HISTORY_RESET:
|
2023-08-09 00:53:06 +00:00
|
|
|
return { ...state, edition: state.edition + 1, callHistoryByCallId: {} };
|
2023-09-27 19:42:30 +00:00
|
|
|
case CALL_HISTORY_ADD:
|
2023-08-09 00:53:06 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-11-27 22:07:13 +00:00
|
|
|
edition: state.edition + 1,
|
2023-08-09 00:53:06 +00:00
|
|
|
callHistoryByCallId: {
|
|
|
|
...state.callHistoryByCallId,
|
|
|
|
[action.payload.callId]: action.payload,
|
|
|
|
},
|
|
|
|
};
|
2023-09-27 19:42:30 +00:00
|
|
|
case CALL_HISTORY_REMOVE:
|
|
|
|
return {
|
|
|
|
...state,
|
2023-11-27 22:07:13 +00:00
|
|
|
edition: state.edition + 1,
|
2023-09-27 19:42:30 +00:00
|
|
|
callHistoryByCallId: omit(state.callHistoryByCallId, action.payload),
|
|
|
|
};
|
2023-08-14 23:28:47 +00:00
|
|
|
case CALL_HISTORY_UPDATE_UNREAD:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
unreadCount: action.payload,
|
|
|
|
};
|
2024-08-06 19:29:13 +00:00
|
|
|
case CALL_HISTORY_RELOAD:
|
|
|
|
return {
|
|
|
|
edition: state.edition + 1,
|
|
|
|
unreadCount: action.payload.callsHistoryUnreadCount,
|
|
|
|
callHistoryByCallId: makeLookup(action.payload.callsHistory, 'callId'),
|
|
|
|
};
|
2023-08-09 00:53:06 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|