2022-09-15 20:10:46 +00:00
|
|
|
// Copyright 2021-2022 Signal Messenger, LLC
|
2021-06-29 19:58:29 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-09-15 20:10:46 +00:00
|
|
|
import { createSelector } from 'reselect';
|
|
|
|
import { collectFirst } from '../../util/iterables';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType } from '../reducer';
|
2022-09-15 20:10:46 +00:00
|
|
|
import { getConversations } from './conversations';
|
|
|
|
import { getPropsForAttachment } from './message';
|
2021-06-29 19:58:29 +00:00
|
|
|
|
|
|
|
export const isPaused = (state: StateType): boolean => {
|
2022-09-15 20:10:46 +00:00
|
|
|
return state.audioPlayer.active === undefined;
|
2021-06-29 19:58:29 +00:00
|
|
|
};
|
2022-09-15 20:10:46 +00:00
|
|
|
|
|
|
|
export const selectActiveVoiceNoteMessageId = (
|
|
|
|
state: StateType
|
|
|
|
): string | undefined => state.audioPlayer.active?.id;
|
|
|
|
|
|
|
|
export const selectNextConsecutiveVoiceNoteMessageId = createSelector(
|
|
|
|
getConversations,
|
|
|
|
selectActiveVoiceNoteMessageId,
|
|
|
|
(
|
|
|
|
conversations,
|
|
|
|
activeVoiceNoteMessageId
|
|
|
|
): { id: string; url: string } | undefined => {
|
|
|
|
if (!activeVoiceNoteMessageId) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentMessage =
|
|
|
|
conversations.messagesLookup[activeVoiceNoteMessageId];
|
|
|
|
const conversationMessages =
|
|
|
|
conversations.messagesByConversation[currentMessage.conversationId];
|
|
|
|
|
|
|
|
if (!conversationMessages) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const idx = conversationMessages.messageIds.indexOf(
|
|
|
|
activeVoiceNoteMessageId
|
|
|
|
);
|
|
|
|
const nextIdx = idx + 1;
|
|
|
|
|
|
|
|
if (!(nextIdx in conversationMessages.messageIds)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nextMessageId = conversationMessages.messageIds[nextIdx];
|
|
|
|
const nextMessage = conversations.messagesLookup[nextMessageId];
|
|
|
|
|
|
|
|
if (!nextMessage.attachments) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const voiceNoteUrl = collectFirst(
|
|
|
|
nextMessage.attachments.map(getPropsForAttachment),
|
|
|
|
a => (a && a.isVoiceMessage && a.url ? a.url : undefined)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (!voiceNoteUrl) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
id: nextMessageId,
|
|
|
|
url: voiceNoteUrl,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|