2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2021 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';
|
2023-02-24 23:18:57 +00:00
|
|
|
import {
|
|
|
|
getIntl,
|
|
|
|
getUserACI,
|
|
|
|
getUserConversationId,
|
|
|
|
getUserNumber,
|
|
|
|
} from './user';
|
2024-07-11 19:44:09 +00:00
|
|
|
import { getMessagePropStatus, getSource, getSourceServiceId } from './message';
|
2023-02-24 23:18:57 +00:00
|
|
|
import {
|
|
|
|
getConversationByIdSelector,
|
|
|
|
getConversations,
|
|
|
|
getConversationSelector,
|
|
|
|
getSelectedConversationId,
|
|
|
|
} from './conversations';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType } from '../reducer';
|
2023-02-24 23:18:57 +00:00
|
|
|
import * as log from '../../logging/log';
|
2024-07-11 19:44:09 +00:00
|
|
|
import { getLocalAttachmentUrl } from '../../util/getLocalAttachmentUrl';
|
2023-02-24 23:18:57 +00:00
|
|
|
import type { MessageWithUIFieldsType } from '../ducks/conversations';
|
2024-07-24 20:14:11 +00:00
|
|
|
import type { ReadonlyMessageAttributesType } from '../../model-types.d';
|
2023-02-24 23:18:57 +00:00
|
|
|
import { getMessageIdForLogging } from '../../util/idForLogging';
|
|
|
|
import * as Attachment from '../../types/Attachment';
|
|
|
|
import type { ActiveAudioPlayerStateType } from '../ducks/audioPlayer';
|
|
|
|
import { isPlayed } from '../../types/Attachment';
|
2023-08-10 16:43:33 +00:00
|
|
|
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;
|
|
|
|
};
|
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
|
|
|
|
2023-02-24 23:18:57 +00:00
|
|
|
export const selectAudioPlayerActive = (
|
2022-09-15 20:10:46 +00:00
|
|
|
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,
|
2023-08-10 16:43:33 +00:00
|
|
|
(ourNumber, ourAci, ourConversationId, conversationSelector, i18n) => {
|
2023-02-24 23:18:57 +00:00
|
|
|
return (
|
2023-08-16 20:54:39 +00:00
|
|
|
message: Pick<
|
2024-07-24 20:14:11 +00:00
|
|
|
ReadonlyMessageAttributesType,
|
2023-08-16 20:54:39 +00:00
|
|
|
'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(
|
2024-07-24 20:14:11 +00:00
|
|
|
message: ReadonlyMessageAttributesType,
|
2023-02-24 23:18:57 +00:00
|
|
|
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
|
2024-07-11 19:44:09 +00:00
|
|
|
? getLocalAttachmentUrl(attachment)
|
2023-02-24 23:18:57 +00:00
|
|
|
: 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
|
|
|
};
|
|
|
|
}
|
2022-09-15 20:10:46 +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(
|
2022-09-15 20:10:46 +00:00
|
|
|
getConversations,
|
2023-02-24 23:18:57 +00:00
|
|
|
getSelectedConversationId,
|
|
|
|
getConversationByIdSelector,
|
|
|
|
getUserConversationId,
|
2022-09-15 20:10:46 +00:00
|
|
|
(
|
|
|
|
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,
|
|
|
|
};
|
2022-09-15 20:10:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|