2021-09-29 20:23:06 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
2021-09-29 20:23:06 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2021-09-29 20:23:06 +00:00
|
|
|
import * as log from '../../logging/log';
|
2021-11-15 21:54:33 +00:00
|
|
|
import type { InMemoryAttachmentDraftType } from '../../types/Attachment';
|
2021-09-29 20:23:06 +00:00
|
|
|
import { SignalService as Proto } from '../../protobuf';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2021-09-29 20:23:06 +00:00
|
|
|
import { fileToBytes } from '../../util/fileToBytes';
|
|
|
|
import { recorder } from '../../services/audioRecorder';
|
|
|
|
import { stringToMIMEType } from '../../types/MIME';
|
2022-12-09 17:35:34 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
2021-09-29 20:23:06 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2023-01-05 00:22:36 +00:00
|
|
|
import { getComposerStateForConversation } from './composer';
|
2023-03-02 20:55:40 +00:00
|
|
|
|
2023-01-27 17:51:53 +00:00
|
|
|
import * as Errors from '../../types/errors';
|
2023-01-21 00:31:30 +00:00
|
|
|
import {
|
|
|
|
ErrorDialogAudioRecorderType,
|
|
|
|
RecordingState,
|
|
|
|
} from '../../types/AudioRecorder';
|
2021-09-29 20:23:06 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
export type AudioRecorderStateType = ReadonlyDeep<{
|
2023-01-13 20:07:26 +00:00
|
|
|
recordingState: RecordingState;
|
|
|
|
errorDialogAudioRecorderType?: ErrorDialogAudioRecorderType;
|
|
|
|
}>;
|
2021-09-29 20:23:06 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
const CANCEL_RECORDING = 'audioRecorder/CANCEL_RECORDING';
|
|
|
|
const COMPLETE_RECORDING = 'audioRecorder/COMPLETE_RECORDING';
|
|
|
|
const ERROR_RECORDING = 'audioRecorder/ERROR_RECORDING';
|
2021-11-11 23:33:35 +00:00
|
|
|
const NOW_RECORDING = 'audioRecorder/NOW_RECORDING';
|
2021-09-29 20:23:06 +00:00
|
|
|
const START_RECORDING = 'audioRecorder/START_RECORDING';
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type CancelRecordingAction = ReadonlyDeep<{
|
2021-09-29 20:23:06 +00:00
|
|
|
type: typeof CANCEL_RECORDING;
|
|
|
|
payload: undefined;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type CompleteRecordingAction = ReadonlyDeep<{
|
2021-09-29 20:23:06 +00:00
|
|
|
type: typeof COMPLETE_RECORDING;
|
|
|
|
payload: undefined;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type ErrorRecordingAction = ReadonlyDeep<{
|
2021-09-29 20:23:06 +00:00
|
|
|
type: typeof ERROR_RECORDING;
|
|
|
|
payload: ErrorDialogAudioRecorderType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type StartRecordingAction = ReadonlyDeep<{
|
2021-09-29 20:23:06 +00:00
|
|
|
type: typeof START_RECORDING;
|
|
|
|
payload: undefined;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type NowRecordingAction = ReadonlyDeep<{
|
2021-11-11 23:33:35 +00:00
|
|
|
type: typeof NOW_RECORDING;
|
|
|
|
payload: undefined;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-09-29 20:23:06 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type AudioPlayerActionType = ReadonlyDeep<
|
2021-09-29 20:23:06 +00:00
|
|
|
| CancelRecordingAction
|
|
|
|
| CompleteRecordingAction
|
|
|
|
| ErrorRecordingAction
|
2021-11-11 23:33:35 +00:00
|
|
|
| NowRecordingAction
|
2023-01-13 20:07:26 +00:00
|
|
|
| StartRecordingAction
|
|
|
|
>;
|
2021-09-29 20:23:06 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
cancelRecording,
|
|
|
|
completeRecording,
|
|
|
|
errorRecording,
|
|
|
|
startRecording,
|
|
|
|
};
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
export const useAudioRecorderActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
2021-09-29 20:23:06 +00:00
|
|
|
|
2023-01-05 00:22:36 +00:00
|
|
|
function startRecording(
|
|
|
|
conversationId: string
|
|
|
|
): ThunkAction<
|
2021-09-29 20:23:06 +00:00
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
2021-11-11 23:33:35 +00:00
|
|
|
StartRecordingAction | NowRecordingAction | ErrorRecordingAction
|
2021-09-29 20:23:06 +00:00
|
|
|
> {
|
2021-09-30 20:13:47 +00:00
|
|
|
return async (dispatch, getState) => {
|
2023-01-05 00:22:36 +00:00
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
if (
|
|
|
|
getComposerStateForConversation(state.composer, conversationId)
|
|
|
|
.attachments.length
|
|
|
|
) {
|
2021-09-29 20:23:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-01-05 00:22:36 +00:00
|
|
|
if (state.audioRecorder.recordingState !== RecordingState.Idle) {
|
2021-11-11 23:33:35 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-29 20:23:06 +00:00
|
|
|
|
2021-11-11 23:33:35 +00:00
|
|
|
dispatch({
|
|
|
|
type: START_RECORDING,
|
|
|
|
payload: undefined,
|
|
|
|
});
|
2021-10-27 16:16:09 +00:00
|
|
|
|
2021-09-30 20:13:47 +00:00
|
|
|
try {
|
2021-11-11 23:33:35 +00:00
|
|
|
const started = await recorder.start();
|
|
|
|
|
|
|
|
if (started) {
|
|
|
|
dispatch({
|
|
|
|
type: NOW_RECORDING,
|
|
|
|
payload: undefined,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch({
|
|
|
|
type: ERROR_RECORDING,
|
|
|
|
payload: ErrorDialogAudioRecorderType.ErrorRecording,
|
|
|
|
});
|
|
|
|
}
|
2021-09-30 20:13:47 +00:00
|
|
|
} catch (err) {
|
2023-01-27 17:51:53 +00:00
|
|
|
log.error('AudioRecorder/ERROR_RECORDING', Errors.toLogFormat(err));
|
2021-09-30 20:13:47 +00:00
|
|
|
dispatch({
|
|
|
|
type: ERROR_RECORDING,
|
|
|
|
payload: ErrorDialogAudioRecorderType.ErrorRecording,
|
|
|
|
});
|
2021-10-27 16:16:09 +00:00
|
|
|
}
|
2021-09-29 20:23:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function completeRecordingAction(): CompleteRecordingAction {
|
|
|
|
return {
|
|
|
|
type: COMPLETE_RECORDING,
|
|
|
|
payload: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
export function completeRecording(
|
2021-09-29 20:23:06 +00:00
|
|
|
conversationId: string,
|
2023-03-02 20:55:40 +00:00
|
|
|
onRecordingComplete: (rec: InMemoryAttachmentDraftType) => unknown
|
2021-09-29 20:23:06 +00:00
|
|
|
): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
CancelRecordingAction | CompleteRecordingAction
|
|
|
|
> {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
const isSelectedConversation =
|
|
|
|
state.conversations.selectedConversationId === conversationId;
|
|
|
|
|
|
|
|
if (!isSelectedConversation) {
|
|
|
|
log.warn(
|
|
|
|
'completeRecording: Recording started in one conversation and completed in another'
|
|
|
|
);
|
|
|
|
dispatch(cancelRecording());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const blob = await recorder.stop();
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (!blob) {
|
|
|
|
throw new Error('completeRecording: no blob returned');
|
|
|
|
}
|
|
|
|
const data = await fileToBytes(blob);
|
|
|
|
|
2021-11-15 21:54:33 +00:00
|
|
|
const voiceNoteAttachment: InMemoryAttachmentDraftType = {
|
|
|
|
pending: false,
|
2021-09-29 20:23:06 +00:00
|
|
|
contentType: stringToMIMEType(blob.type),
|
|
|
|
data,
|
|
|
|
size: data.byteLength,
|
|
|
|
flags: Proto.AttachmentPointer.Flags.VOICE_MESSAGE,
|
|
|
|
};
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
onRecordingComplete(voiceNoteAttachment);
|
2021-09-29 20:23:06 +00:00
|
|
|
} finally {
|
|
|
|
dispatch(completeRecordingAction());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-15 18:51:33 +00:00
|
|
|
function cancelRecording(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
CancelRecordingAction
|
|
|
|
> {
|
|
|
|
return async dispatch => {
|
|
|
|
await recorder.stop();
|
|
|
|
recorder.clear();
|
2021-09-29 20:23:06 +00:00
|
|
|
|
2021-10-15 18:51:33 +00:00
|
|
|
dispatch({
|
|
|
|
type: CANCEL_RECORDING,
|
|
|
|
payload: undefined,
|
|
|
|
});
|
2021-09-29 20:23:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function errorRecording(
|
|
|
|
errorDialogAudioRecorderType: ErrorDialogAudioRecorderType
|
|
|
|
): ErrorRecordingAction {
|
2022-12-21 18:41:48 +00:00
|
|
|
void recorder.stop();
|
2021-09-29 20:23:06 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
type: ERROR_RECORDING,
|
|
|
|
payload: errorDialogAudioRecorderType,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
2024-03-12 16:29:31 +00:00
|
|
|
export function getEmptyState(): AudioRecorderStateType {
|
2021-09-29 20:23:06 +00:00
|
|
|
return {
|
2021-11-11 23:33:35 +00:00
|
|
|
recordingState: RecordingState.Idle,
|
2021-09-29 20:23:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2024-03-12 16:29:31 +00:00
|
|
|
state: Readonly<AudioRecorderStateType> = getEmptyState(),
|
2021-09-29 20:23:06 +00:00
|
|
|
action: Readonly<AudioPlayerActionType>
|
2024-03-12 16:29:31 +00:00
|
|
|
): AudioRecorderStateType {
|
2021-09-29 20:23:06 +00:00
|
|
|
if (action.type === START_RECORDING) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
errorDialogAudioRecorderType: undefined,
|
2021-11-11 23:33:35 +00:00
|
|
|
recordingState: RecordingState.Initializing,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === NOW_RECORDING) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
errorDialogAudioRecorderType: undefined,
|
|
|
|
recordingState: RecordingState.Recording,
|
2021-09-29 20:23:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === CANCEL_RECORDING || action.type === COMPLETE_RECORDING) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
errorDialogAudioRecorderType: undefined,
|
2021-11-11 23:33:35 +00:00
|
|
|
recordingState: RecordingState.Idle,
|
2021-09-29 20:23:06 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === ERROR_RECORDING) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
errorDialogAudioRecorderType: action.payload,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|