signal-desktop/ts/state/selectors/audioPlayer.ts

216 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { createSelector } from 'reselect';
2023-02-24 23:18:57 +00:00
import {
getIntl,
getUserACI,
getUserConversationId,
getUserNumber,
} from './user';
import {
getAttachmentUrlForPath,
getMessagePropStatus,
getSource,
2023-08-16 20:54:39 +00:00
getSourceServiceId,
2023-02-24 23:18:57 +00:00
} from './message';
import {
getConversationByIdSelector,
getConversations,
getConversationSelector,
getSelectedConversationId,
} from './conversations';
import type { StateType } from '../reducer';
2023-02-24 23:18:57 +00:00
import * as log from '../../logging/log';
import type { MessageWithUIFieldsType } from '../ducks/conversations';
import type { MessageAttributesType } from '../../model-types.d';
import { getMessageIdForLogging } from '../../util/idForLogging';
import * as Attachment from '../../types/Attachment';
import type { ActiveAudioPlayerStateType } from '../ducks/audioPlayer';
import { isPlayed } from '../../types/Attachment';
import type { ServiceIdString } from '../../types/ServiceId';
2023-02-24 23:18:57 +00:00
export type VoiceNoteForPlayback = {
id: string;
// undefined if download is pending
url: string | undefined;
type: 'incoming' | 'outgoing';
source: string | undefined;
2023-08-16 20:54:39 +00:00
sourceServiceId: ServiceIdString | undefined;
2023-02-24 23:18:57 +00:00
isPlayed: boolean;
messageIdForLogging: string;
timestamp: number;
};
export const isPaused = (state: StateType): boolean => {
return state.audioPlayer.active === undefined;
};
2023-02-24 23:18:57 +00:00
export const selectAudioPlayerActive = (
state: StateType
2023-02-24 23:18:57 +00:00
): ActiveAudioPlayerStateType | undefined => {
return state.audioPlayer.active;
};
export const selectVoiceNoteTitle = createSelector(
getUserNumber,
getUserACI,
getUserConversationId,
getConversationSelector,
getIntl,
(ourNumber, ourAci, ourConversationId, conversationSelector, i18n) => {
2023-02-24 23:18:57 +00:00
return (
2023-08-16 20:54:39 +00:00
message: Pick<
MessageAttributesType,
'type' | 'source' | 'sourceServiceId'
>
2023-02-24 23:18:57 +00:00
) => {
const source = getSource(message, ourNumber);
2023-08-16 20:54:39 +00:00
const sourceServiceId = getSourceServiceId(message, ourAci);
2023-02-24 23:18:57 +00:00
const conversation =
2023-08-16 20:54:39 +00:00
!source && !sourceServiceId
2023-02-24 23:18:57 +00:00
? conversationSelector(ourConversationId)
2023-08-16 20:54:39 +00:00
: conversationSelector(sourceServiceId || source);
2023-02-24 23:18:57 +00:00
2023-03-30 00:03:25 +00:00
return conversation.isMe ? i18n('icu:you') : conversation.title;
2023-02-24 23:18:57 +00:00
};
}
);
export function extractVoiceNoteForPlayback(
message: MessageAttributesType,
ourConversationId: string | undefined
): VoiceNoteForPlayback | undefined {
const { type } = message;
if (type !== 'incoming' && type !== 'outgoing') {
return;
}
if (!message.attachments) {
return;
}
const attachment = message.attachments[0];
if (!attachment || !Attachment.isAudio(message.attachments)) {
return;
}
const voiceNoteUrl = attachment.path
? getAttachmentUrlForPath(attachment.path)
: undefined;
const status = getMessagePropStatus(message, ourConversationId);
return {
id: message.id,
url: voiceNoteUrl,
type,
isPlayed: isPlayed(type, status, message.readStatus),
messageIdForLogging: getMessageIdForLogging(message),
timestamp: message.timestamp,
source: message.source,
2023-08-16 20:54:39 +00:00
sourceServiceId: message.sourceServiceId,
2023-02-24 23:18:57 +00:00
};
}
2023-02-24 23:18:57 +00:00
/** Data necessary to playback a voice note and any consecutive notes */
export type VoiceNoteAndConsecutiveForPlayback = {
conversationId: string;
voiceNote: VoiceNoteForPlayback;
previousMessageId: string | undefined;
consecutiveVoiceNotes: ReadonlyArray<VoiceNoteForPlayback>;
playbackRate: number;
// timestamp of the message after all the once in the queue
nextMessageTimestamp: number | undefined;
};
export const selectVoiceNoteAndConsecutive = createSelector(
getConversations,
2023-02-24 23:18:57 +00:00
getSelectedConversationId,
getConversationByIdSelector,
getUserConversationId,
(
conversations,
2023-02-24 23:18:57 +00:00
selectedConversationId,
getConversationById,
ourConversationId
) => {
return (
messageId: string
): VoiceNoteAndConsecutiveForPlayback | undefined => {
const message = conversations.messagesLookup[messageId];
if (!message) {
log.warn('selectVoiceNoteData: message not found', {
message: messageId,
});
return;
}
const voiceNote = extractVoiceNoteForPlayback(message, ourConversationId);
if (!voiceNote) {
log.warn('selectVoiceNoteData: message not a voice note', {
message: messageId,
});
return undefined;
}
if (!selectedConversationId) {
log.warn('selectVoiceNoteData: no selected conversation id', {
message: messageId,
});
return undefined;
}
const conversationMessages =
conversations.messagesByConversation[selectedConversationId];
if (!conversationMessages) {
log.warn('selectedVoiceNote: no conversation messages', {
message: messageId,
});
return;
}
let idx = conversationMessages.messageIds.indexOf(messageId);
// useful if inserting into an active queue
const previousMessageId = conversationMessages.messageIds[idx - 1];
const consecutiveVoiceNotes: Array<VoiceNoteForPlayback> = [];
let nextMessageId: string;
let nextMessage: MessageWithUIFieldsType | undefined;
let nextVoiceNote: VoiceNoteForPlayback | undefined;
do {
idx += 1;
nextMessageId = conversationMessages.messageIds[idx];
if (!nextMessageId) {
nextMessage = undefined;
break;
}
nextMessage = conversations.messagesLookup[nextMessageId];
if (!nextMessage) {
break;
}
if (nextMessage.deletedForEveryone) {
continue;
}
nextVoiceNote = extractVoiceNoteForPlayback(
nextMessage,
ourConversationId
);
if (nextVoiceNote) {
consecutiveVoiceNotes.push(nextVoiceNote);
}
} while (nextVoiceNote);
const conversation = getConversationById(selectedConversationId);
return {
conversationId: selectedConversationId,
voiceNote,
consecutiveVoiceNotes,
playbackRate: conversation?.voiceNotePlaybackRate ?? 1,
previousMessageId,
nextMessageTimestamp: nextMessage?.timestamp,
};
};
}
);