2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
2021-03-10 20:36:58 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-09-15 20:10:46 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2022-12-09 17:35:34 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
2021-09-17 22:24:21 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2022-09-15 20:10:46 +00:00
|
|
|
|
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2023-02-28 13:07:40 +00:00
|
|
|
import { setVoiceNotePlaybackRate } from './conversations';
|
2023-02-24 23:18:57 +00:00
|
|
|
import { extractVoiceNoteForPlayback } from '../selectors/audioPlayer';
|
|
|
|
import type {
|
|
|
|
VoiceNoteAndConsecutiveForPlayback,
|
|
|
|
VoiceNoteForPlayback,
|
|
|
|
} from '../selectors/audioPlayer';
|
2021-03-10 20:36:58 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2023-02-24 23:18:57 +00:00
|
|
|
MessagesAddedActionType,
|
2021-06-29 19:58:29 +00:00
|
|
|
MessageDeletedActionType,
|
|
|
|
MessageChangedActionType,
|
2023-03-20 22:23:53 +00:00
|
|
|
TargetedConversationChangedActionType,
|
2022-09-15 20:10:46 +00:00
|
|
|
ConversationChangedActionType,
|
|
|
|
} from './conversations';
|
|
|
|
import * as log from '../../logging/log';
|
2023-02-24 23:18:57 +00:00
|
|
|
import { isAudio } from '../../types/Attachment';
|
|
|
|
import { getAttachmentUrlForPath } from '../selectors/message';
|
2023-02-28 13:07:40 +00:00
|
|
|
import { assertDev } from '../../util/assert';
|
2021-03-10 20:36:58 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
/** Some audio identified by a URL (currently only used for drafts) */
|
|
|
|
type AudioPlayerContentDraft = ReadonlyDeep<{
|
|
|
|
conversationId: string;
|
|
|
|
url: string;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
/** A voice note, with a queue for consecutive playback */
|
|
|
|
export type AudioPlayerContentVoiceNote = ReadonlyDeep<{
|
2023-02-24 23:18:57 +00:00
|
|
|
conversationId: string;
|
|
|
|
context: string;
|
|
|
|
current: VoiceNoteForPlayback;
|
|
|
|
queue: ReadonlyArray<VoiceNoteForPlayback>;
|
|
|
|
nextMessageTimestamp: number | undefined;
|
|
|
|
// playing because it followed a message
|
|
|
|
// false on the first of a consecutive group
|
|
|
|
isConsecutive: boolean;
|
|
|
|
ourConversationId: string | undefined;
|
|
|
|
}>;
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type ActiveAudioPlayerStateType = ReadonlyDeep<{
|
|
|
|
playing: boolean;
|
|
|
|
currentTime: number;
|
|
|
|
playbackRate: number;
|
2023-02-28 13:07:40 +00:00
|
|
|
duration: number | undefined; // never zero or NaN
|
|
|
|
startPosition: number;
|
2023-03-02 20:55:40 +00:00
|
|
|
content: AudioPlayerContentVoiceNote | AudioPlayerContentDraft;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-09-15 20:10:46 +00:00
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-namespace */
|
|
|
|
export namespace AudioPlayerContent {
|
|
|
|
export function isVoiceNote(
|
|
|
|
content: ActiveAudioPlayerStateType['content']
|
|
|
|
): content is AudioPlayerContentVoiceNote {
|
|
|
|
return (
|
2023-04-10 16:31:45 +00:00
|
|
|
// satisfies keyof AudioPlayerContentVoiceNote
|
|
|
|
('current' as const) in content
|
2023-03-02 20:55:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
export function isDraft(
|
|
|
|
content: ActiveAudioPlayerStateType['content']
|
|
|
|
): content is AudioPlayerContentDraft {
|
|
|
|
return !isVoiceNote(content);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type AudioPlayerStateType = ReadonlyDeep<{
|
2023-02-24 23:18:57 +00:00
|
|
|
active: ActiveAudioPlayerStateType | undefined;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-03-10 20:36:58 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type SetMessageAudioAction = ReadonlyDeep<{
|
2022-09-15 20:10:46 +00:00
|
|
|
type: 'audioPlayer/SET_MESSAGE_AUDIO';
|
|
|
|
payload:
|
2023-03-02 20:55:40 +00:00
|
|
|
| ((AudioPlayerContentVoiceNote | AudioPlayerContentDraft) & {
|
2022-09-15 20:10:46 +00:00
|
|
|
playbackRate: number;
|
2023-03-02 20:55:40 +00:00
|
|
|
startPosition: number;
|
|
|
|
})
|
2022-09-15 20:10:46 +00:00
|
|
|
| undefined;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-09-15 20:10:46 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SetPlaybackRate = ReadonlyDeep<{
|
2022-09-15 20:10:46 +00:00
|
|
|
type: 'audioPlayer/SET_PLAYBACK_RATE';
|
|
|
|
payload: number;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-09-15 20:10:46 +00:00
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
export type SetIsPlayingAction = ReadonlyDeep<{
|
2022-09-15 20:10:46 +00:00
|
|
|
type: 'audioPlayer/SET_IS_PLAYING';
|
|
|
|
payload: boolean;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-03-10 20:36:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type CurrentTimeUpdated = ReadonlyDeep<{
|
2022-09-15 20:10:46 +00:00
|
|
|
type: 'audioPlayer/CURRENT_TIME_UPDATED';
|
|
|
|
payload: number;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-09-15 20:10:46 +00:00
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
type SetPosition = ReadonlyDeep<{
|
|
|
|
type: 'audioPlayer/SET_POSITION';
|
|
|
|
payload: number;
|
|
|
|
}>;
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type MessageAudioEnded = ReadonlyDeep<{
|
2022-09-15 20:10:46 +00:00
|
|
|
type: 'audioPlayer/MESSAGE_AUDIO_ENDED';
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-09-15 20:10:46 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type DurationChanged = ReadonlyDeep<{
|
2022-09-15 20:10:46 +00:00
|
|
|
type: 'audioPlayer/DURATION_CHANGED';
|
2023-02-28 13:07:40 +00:00
|
|
|
payload: number | undefined;
|
2023-02-24 23:18:57 +00:00
|
|
|
}>;
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type AudioPlayerActionType = ReadonlyDeep<
|
2022-09-15 20:10:46 +00:00
|
|
|
| SetMessageAudioAction
|
|
|
|
| SetIsPlayingAction
|
|
|
|
| SetPlaybackRate
|
|
|
|
| MessageAudioEnded
|
|
|
|
| CurrentTimeUpdated
|
2023-01-13 20:07:26 +00:00
|
|
|
| DurationChanged
|
2023-02-28 13:07:40 +00:00
|
|
|
| SetPosition
|
2023-01-13 20:07:26 +00:00
|
|
|
>;
|
2021-03-10 20:36:58 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
2023-03-02 20:55:40 +00:00
|
|
|
loadVoiceNoteAudio,
|
|
|
|
loadVoiceNoteDraftAudio,
|
2022-09-15 20:10:46 +00:00
|
|
|
setPlaybackRate,
|
2023-02-28 13:07:40 +00:00
|
|
|
currentTimeUpdated,
|
|
|
|
durationChanged,
|
2022-09-15 20:10:46 +00:00
|
|
|
setIsPlaying,
|
2023-02-28 13:07:40 +00:00
|
|
|
setPosition,
|
2023-02-24 23:18:57 +00:00
|
|
|
pauseVoiceNotePlayer,
|
|
|
|
unloadMessageAudio,
|
2023-02-28 13:07:40 +00:00
|
|
|
messageAudioEnded,
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
function messageAudioEnded(): MessageAudioEnded {
|
|
|
|
return {
|
|
|
|
type: 'audioPlayer/MESSAGE_AUDIO_ENDED',
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function durationChanged(value: number | undefined): DurationChanged {
|
|
|
|
assertDev(
|
|
|
|
!Number.isNaN(value) && (value === undefined || value > 0),
|
|
|
|
`Duration must be > 0 if defined, got ${value}`
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
type: 'audioPlayer/DURATION_CHANGED',
|
|
|
|
payload: value,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
export const useAudioPlayerActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
2021-03-10 20:36:58 +00:00
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
function currentTimeUpdated(value: number): CurrentTimeUpdated {
|
2021-03-10 20:36:58 +00:00
|
|
|
return {
|
2022-09-15 20:10:46 +00:00
|
|
|
type: 'audioPlayer/CURRENT_TIME_UPDATED',
|
|
|
|
payload: value,
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
function setPosition(positionAsRatio: number): SetPosition {
|
|
|
|
return {
|
|
|
|
type: 'audioPlayer/SET_POSITION',
|
|
|
|
payload: positionAsRatio,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-15 20:10:46 +00:00
|
|
|
function setPlaybackRate(
|
|
|
|
rate: number
|
|
|
|
): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
SetPlaybackRate | ConversationChangedActionType
|
|
|
|
> {
|
2023-02-24 23:18:57 +00:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { audioPlayer } = getState();
|
|
|
|
const { active } = audioPlayer;
|
2023-02-28 13:07:40 +00:00
|
|
|
if (!active) {
|
2023-02-24 23:18:57 +00:00
|
|
|
log.warn('audioPlayer.setPlaybackRate: No active message audio');
|
|
|
|
return;
|
|
|
|
}
|
2022-09-15 20:10:46 +00:00
|
|
|
dispatch({
|
|
|
|
type: 'audioPlayer/SET_PLAYBACK_RATE',
|
|
|
|
payload: rate,
|
|
|
|
});
|
|
|
|
|
|
|
|
// update the preference for the conversation
|
2023-02-24 23:18:57 +00:00
|
|
|
const { conversationId } = active.content;
|
2022-09-15 20:10:46 +00:00
|
|
|
dispatch(
|
|
|
|
setVoiceNotePlaybackRate({
|
|
|
|
conversationId,
|
|
|
|
rate,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
/**
|
|
|
|
* Load message audio into the "content", the smart MiniPlayer will then play it
|
|
|
|
*/
|
2023-03-02 20:55:40 +00:00
|
|
|
function loadVoiceNoteAudio({
|
2023-02-24 23:18:57 +00:00
|
|
|
voiceNoteData,
|
|
|
|
position,
|
|
|
|
context,
|
|
|
|
ourConversationId,
|
2023-03-02 20:55:40 +00:00
|
|
|
playbackRate,
|
2023-02-24 23:18:57 +00:00
|
|
|
}: {
|
|
|
|
voiceNoteData: VoiceNoteAndConsecutiveForPlayback;
|
|
|
|
position: number;
|
|
|
|
context: string;
|
|
|
|
ourConversationId: string;
|
2023-03-02 20:55:40 +00:00
|
|
|
playbackRate: number;
|
2023-02-24 23:18:57 +00:00
|
|
|
}): SetMessageAudioAction {
|
|
|
|
const {
|
|
|
|
conversationId,
|
|
|
|
voiceNote,
|
|
|
|
consecutiveVoiceNotes,
|
2023-03-02 20:55:40 +00:00
|
|
|
// playbackRate,
|
2023-02-24 23:18:57 +00:00
|
|
|
nextMessageTimestamp,
|
|
|
|
} = voiceNoteData;
|
|
|
|
return {
|
|
|
|
type: 'audioPlayer/SET_MESSAGE_AUDIO',
|
|
|
|
payload: {
|
|
|
|
conversationId,
|
|
|
|
context,
|
|
|
|
current: voiceNote,
|
|
|
|
queue: consecutiveVoiceNotes,
|
|
|
|
isConsecutive: false,
|
|
|
|
nextMessageTimestamp,
|
|
|
|
ourConversationId,
|
|
|
|
startPosition: position,
|
|
|
|
playbackRate,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-09-15 20:10:46 +00:00
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
export function loadVoiceNoteDraftAudio(
|
|
|
|
content: AudioPlayerContentDraft & {
|
|
|
|
playbackRate: number;
|
|
|
|
startPosition: number;
|
|
|
|
}
|
|
|
|
): SetMessageAudioAction {
|
|
|
|
return {
|
|
|
|
type: 'audioPlayer/SET_MESSAGE_AUDIO',
|
|
|
|
payload: content,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
function setIsPlaying(value: boolean): SetIsPlayingAction {
|
|
|
|
return {
|
|
|
|
type: 'audioPlayer/SET_IS_PLAYING',
|
|
|
|
payload: value,
|
2023-02-24 23:18:57 +00:00
|
|
|
};
|
|
|
|
}
|
2022-09-15 20:10:46 +00:00
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
/**
|
|
|
|
* alias for callers that just want to pause any voice notes before starting
|
|
|
|
* their own playback: story viewer, media viewer, calling
|
|
|
|
*/
|
|
|
|
export function pauseVoiceNotePlayer(): ReturnType<typeof setIsPlaying> {
|
|
|
|
return setIsPlaying(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function unloadMessageAudio(): SetMessageAudioAction {
|
|
|
|
return {
|
|
|
|
type: 'audioPlayer/SET_MESSAGE_AUDIO',
|
|
|
|
payload: undefined,
|
2022-09-15 20:10:46 +00:00
|
|
|
};
|
|
|
|
}
|
2021-03-10 20:36:58 +00:00
|
|
|
|
2022-02-23 18:48:40 +00:00
|
|
|
export function getEmptyState(): AudioPlayerStateType {
|
2021-03-10 20:36:58 +00:00
|
|
|
return {
|
2022-09-15 20:10:46 +00:00
|
|
|
active: undefined,
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
|
|
|
state: Readonly<AudioPlayerStateType> = getEmptyState(),
|
2021-06-29 19:58:29 +00:00
|
|
|
action: Readonly<
|
|
|
|
| AudioPlayerActionType
|
|
|
|
| MessageDeletedActionType
|
|
|
|
| MessageChangedActionType
|
2023-02-24 23:18:57 +00:00
|
|
|
| MessagesAddedActionType
|
2023-03-20 22:23:53 +00:00
|
|
|
| TargetedConversationChangedActionType
|
2021-06-29 19:58:29 +00:00
|
|
|
>
|
2021-03-10 20:36:58 +00:00
|
|
|
): AudioPlayerStateType {
|
2023-02-24 23:18:57 +00:00
|
|
|
const { active } = state;
|
|
|
|
|
2022-09-15 20:10:46 +00:00
|
|
|
if (action.type === 'audioPlayer/SET_MESSAGE_AUDIO') {
|
2021-03-10 20:36:58 +00:00
|
|
|
const { payload } = action;
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
if (payload === undefined) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const { playbackRate, startPosition, ...content } = payload;
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
`audioPlayer/SET_MESSAGE_AUDIO: Starting playback for conversation ${content.conversationId}`
|
|
|
|
);
|
2021-03-10 20:36:58 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-04-11 17:28:04 +00:00
|
|
|
active: {
|
|
|
|
currentTime: 0,
|
|
|
|
duration: undefined,
|
|
|
|
playing: true,
|
|
|
|
playbackRate,
|
|
|
|
content,
|
|
|
|
startPosition,
|
|
|
|
},
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (action.type === 'audioPlayer/CURRENT_TIME_UPDATED') {
|
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
2021-03-10 20:36:58 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-02-24 23:18:57 +00:00
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
currentTime: action.payload,
|
|
|
|
},
|
2022-09-15 20:10:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (action.type === 'audioPlayer/DURATION_CHANGED') {
|
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
2022-09-15 20:10:46 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-02-24 23:18:57 +00:00
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
duration: action.payload,
|
|
|
|
},
|
2022-09-15 20:10:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (action.type === 'audioPlayer/SET_IS_PLAYING') {
|
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
2022-09-15 20:10:46 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-02-24 23:18:57 +00:00
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
playing: action.payload,
|
|
|
|
},
|
2022-09-15 20:10:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-28 13:07:40 +00:00
|
|
|
if (action.type === 'audioPlayer/SET_POSITION') {
|
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
startPosition: action.payload,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (action.type === 'audioPlayer/SET_PLAYBACK_RATE') {
|
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
2022-09-15 20:10:46 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-02-24 23:18:57 +00:00
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
playbackRate: action.payload,
|
|
|
|
},
|
2022-09-15 20:10:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (action.type === 'MESSAGES_ADDED') {
|
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
const { content } = active;
|
|
|
|
|
|
|
|
if (!content) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
if (!AudioPlayerContent.isVoiceNote(content)) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (content.conversationId !== action.payload.conversationId) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
const updatedQueue: Array<VoiceNoteForPlayback> = [...content.queue];
|
|
|
|
|
|
|
|
for (const message of action.payload.messages) {
|
|
|
|
if (message.deletedForEveryone) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (message.timestamp < content.current.timestamp) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// in range of the queue
|
|
|
|
if (
|
|
|
|
content.nextMessageTimestamp === undefined ||
|
|
|
|
message.timestamp < content.nextMessageTimestamp
|
|
|
|
) {
|
|
|
|
if (message.type !== 'incoming' && message.type !== 'outgoing') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const voiceNote = extractVoiceNoteForPlayback(
|
|
|
|
message,
|
|
|
|
content.ourConversationId
|
|
|
|
);
|
|
|
|
|
|
|
|
// index of the message in the queue after this one
|
|
|
|
const idx = updatedQueue.findIndex(
|
|
|
|
m => m.timestamp > message.timestamp
|
|
|
|
);
|
|
|
|
|
|
|
|
// break up consecutive queue: drop values older than this message
|
|
|
|
if (!voiceNote && idx !== -1) {
|
|
|
|
updatedQueue.splice(idx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// insert a new voice note
|
|
|
|
if (voiceNote) {
|
|
|
|
if (idx === -1) {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
`audioPlayer/MESSAGES_ADDED: Adding voice note ${voiceNote.messageIdForLogging} to end of queue`
|
|
|
|
);
|
2023-02-24 23:18:57 +00:00
|
|
|
updatedQueue.push(voiceNote);
|
|
|
|
} else {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
`audioPlayer/MESSAGES_ADDED: Adding voice note ${voiceNote.messageIdForLogging} to queue at index ${idx}`
|
|
|
|
);
|
2023-02-24 23:18:57 +00:00
|
|
|
updatedQueue.splice(idx, 0, voiceNote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updatedQueue.length === content.queue.length) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2022-09-15 20:10:46 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-02-24 23:18:57 +00:00
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
content: {
|
|
|
|
...content,
|
|
|
|
queue: updatedQueue,
|
|
|
|
},
|
|
|
|
},
|
2022-09-15 20:10:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (action.type === 'audioPlayer/MESSAGE_AUDIO_ENDED') {
|
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
const { content } = active;
|
|
|
|
if (!content) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
if (AudioPlayerContent.isDraft(content)) {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
'audioPlayer/MESSAGE_AUDIO_ENDED: Voice note was draft, stopping playback'
|
|
|
|
);
|
2023-03-02 20:55:40 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
const { queue } = content;
|
|
|
|
|
|
|
|
const [nextVoiceNote, ...newQueue] = queue;
|
|
|
|
|
|
|
|
if (nextVoiceNote) {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
`audioPlayer/MESSAGE_AUDIO_ENDED: Starting next voice note ${nextVoiceNote.messageIdForLogging}`
|
|
|
|
);
|
2023-02-24 23:18:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: {
|
|
|
|
...active,
|
2023-02-28 13:07:40 +00:00
|
|
|
startPosition: 0,
|
2023-02-24 23:18:57 +00:00
|
|
|
content: {
|
|
|
|
...content,
|
|
|
|
current: nextVoiceNote,
|
|
|
|
queue: newQueue,
|
|
|
|
isConsecutive: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info('audioPlayer/MESSAGE_AUDIO_ENDED: Stopping playback');
|
2022-09-15 20:10:46 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-02-28 13:07:40 +00:00
|
|
|
active: undefined,
|
2021-03-10 20:36:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
// Reset active when played message is deleted on expiration or DOE.
|
|
|
|
if (
|
|
|
|
action.type === 'MESSAGE_DELETED' ||
|
|
|
|
(action.type === 'MESSAGE_CHANGED' &&
|
|
|
|
action.payload.data.deletedForEveryone)
|
|
|
|
) {
|
2021-06-29 19:58:29 +00:00
|
|
|
const { id } = action.payload;
|
2023-02-24 23:18:57 +00:00
|
|
|
|
|
|
|
if (!active) {
|
2021-06-29 19:58:29 +00:00
|
|
|
return state;
|
|
|
|
}
|
2023-02-24 23:18:57 +00:00
|
|
|
const { content } = active;
|
2021-06-29 19:58:29 +00:00
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
if (!AudioPlayerContent.isVoiceNote(content)) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
// if we deleted the message currently being played
|
|
|
|
// move on to the next message
|
|
|
|
if (content.current.id === id) {
|
|
|
|
const [next, ...rest] = content.queue;
|
|
|
|
|
|
|
|
if (!next) {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
'audioPlayer/MESSAGE_DELETED: Removed currently-playing message, stopping playback'
|
|
|
|
);
|
2023-02-24 23:18:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2023-02-28 13:07:40 +00:00
|
|
|
active: undefined,
|
2023-02-24 23:18:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
'audioPlayer/MESSAGE_DELETED: Removed currently-playing message, moving to next in queue'
|
|
|
|
);
|
2023-02-24 23:18:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
content: {
|
|
|
|
...content,
|
|
|
|
current: next,
|
|
|
|
queue: rest,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we deleted a message on the queue
|
|
|
|
// just update the queue
|
|
|
|
const message = content.queue.find(el => el.id === id);
|
|
|
|
if (message) {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info('audioPlayer/MESSAGE_DELETED: Removed message from the queue');
|
2023-02-24 23:18:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
content: {
|
|
|
|
...content,
|
|
|
|
queue: content.queue.filter(el => el.id !== id),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2021-06-29 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
// if it's a voice note
|
|
|
|
// and this event is letting us know that it has downloaded
|
|
|
|
// update the url if it's in the queue
|
2021-06-29 19:58:29 +00:00
|
|
|
if (action.type === 'MESSAGE_CHANGED') {
|
2023-02-24 23:18:57 +00:00
|
|
|
if (!active) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
const { content } = active;
|
2021-06-29 19:58:29 +00:00
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
if (!content) {
|
2021-06-29 19:58:29 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-03-02 20:55:40 +00:00
|
|
|
if (AudioPlayerContent.isDraft(content)) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
const { id, data } = action.payload;
|
|
|
|
|
|
|
|
const { attachments } = data;
|
|
|
|
const attachment = attachments?.[0];
|
|
|
|
if (
|
|
|
|
!attachments ||
|
|
|
|
!attachment ||
|
|
|
|
!isAudio(attachments) ||
|
|
|
|
!attachment.path
|
|
|
|
) {
|
2021-06-29 19:58:29 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
const url = getAttachmentUrlForPath(attachment.path);
|
|
|
|
|
|
|
|
// if we got the url for the current message
|
|
|
|
if (
|
|
|
|
content.current.id === id &&
|
|
|
|
content.current.url === undefined &&
|
|
|
|
data.id
|
|
|
|
) {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
'audioPlayer/MESSAGE_CHANGED: Adding content url to current-playing message'
|
|
|
|
);
|
2023-02-24 23:18:57 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
content: {
|
|
|
|
...content,
|
|
|
|
current: {
|
|
|
|
...content.current,
|
|
|
|
url,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's in the queue
|
|
|
|
const idx = content.queue.findIndex(v => v.id === id);
|
|
|
|
if (idx !== -1) {
|
2023-04-11 17:28:04 +00:00
|
|
|
log.info(
|
|
|
|
'audioPlayer/MESSAGE_CHANGED: Adding content url to message in queue'
|
|
|
|
);
|
2023-02-24 23:18:57 +00:00
|
|
|
const updatedQueue = [...content.queue];
|
|
|
|
updatedQueue[idx] = {
|
|
|
|
...updatedQueue[idx],
|
|
|
|
url,
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
active: {
|
|
|
|
...active,
|
|
|
|
content: {
|
|
|
|
...content,
|
|
|
|
queue: updatedQueue,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
2021-06-29 19:58:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 20:36:58 +00:00
|
|
|
return state;
|
|
|
|
}
|