Replace MessageController with MessageCache

This commit is contained in:
Josh Perez 2023-10-03 20:12:57 -04:00 committed by GitHub
parent ba1a8aad09
commit 7d35216fda
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
73 changed files with 2237 additions and 1229 deletions

View file

@ -67,7 +67,7 @@ import { resolveAttachmentDraftData } from '../../util/resolveAttachmentDraftDat
import { resolveDraftAttachmentOnDisk } from '../../util/resolveDraftAttachmentOnDisk';
import { shouldShowInvalidMessageToast } from '../../util/shouldShowInvalidMessageToast';
import { writeDraftAttachment } from '../../util/writeDraftAttachment';
import { getMessageById } from '../../messages/getMessageById';
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
import { canReply } from '../selectors/message';
import { getContactId } from '../../messages/helpers';
import { getConversationSelector } from '../selectors/conversations';
@ -747,7 +747,9 @@ export function setQuoteByMessageId(
return;
}
const message = messageId ? await getMessageById(messageId) : undefined;
const message = messageId
? await __DEPRECATED$getMessageById(messageId)
: undefined;
const state = getState();
if (

View file

@ -137,7 +137,7 @@ import {
buildUpdateAttributesChange,
initiateMigrationToGroupV2 as doInitiateMigrationToGroupV2,
} from '../../groups';
import { getMessageById } from '../../messages/getMessageById';
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
import type { PanelRenderType, PanelRequestType } from '../../types/Panels';
import type { ConversationQueueJobData } from '../../jobs/conversationJobQueue';
import { isOlderThan } from '../../util/timestamp';
@ -1329,7 +1329,7 @@ function markMessageRead(
return;
}
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`markMessageRead: failed to load message ${messageId}`);
}
@ -1674,7 +1674,7 @@ function deleteMessages({
await Promise.all(
messageIds.map(async messageId => {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`deleteMessages: Message ${messageId} missing!`);
}
@ -1778,7 +1778,7 @@ function setMessageToEdit(
return;
}
const message = (await getMessageById(messageId))?.attributes;
const message = (await __DEPRECATED$getMessageById(messageId))?.attributes;
if (!message) {
return;
}
@ -1855,7 +1855,7 @@ function generateNewGroupLink(
* replace it with an actual action that fits in with the redux approach.
*/
export const markViewed = (messageId: string): void => {
const message = window.MessageController.getById(messageId);
const message = window.MessageCache.__DEPRECATED$getById(messageId);
if (!message) {
throw new Error(`markViewed: Message ${messageId} missing!`);
}
@ -2126,7 +2126,7 @@ function kickOffAttachmentDownload(
options: Readonly<{ messageId: string }>
): ThunkAction<void, RootStateType, unknown, NoopActionType> {
return async dispatch => {
const message = await getMessageById(options.messageId);
const message = await __DEPRECATED$getMessageById(options.messageId);
if (!message) {
throw new Error(
`kickOffAttachmentDownload: Message ${options.messageId} missing!`
@ -2158,7 +2158,7 @@ function markAttachmentAsCorrupted(
options: AttachmentOptions
): ThunkAction<void, RootStateType, unknown, NoopActionType> {
return async dispatch => {
const message = await getMessageById(options.messageId);
const message = await __DEPRECATED$getMessageById(options.messageId);
if (!message) {
throw new Error(
`markAttachmentAsCorrupted: Message ${options.messageId} missing!`
@ -2177,7 +2177,7 @@ function openGiftBadge(
messageId: string
): ThunkAction<void, RootStateType, unknown, ShowToastActionType> {
return async dispatch => {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`openGiftBadge: Message ${messageId} missing!`);
}
@ -2197,7 +2197,7 @@ function retryMessageSend(
messageId: string
): ThunkAction<void, RootStateType, unknown, NoopActionType> {
return async dispatch => {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`retryMessageSend: Message ${messageId} missing!`);
}
@ -2214,7 +2214,7 @@ export function copyMessageText(
messageId: string
): ThunkAction<void, RootStateType, unknown, NoopActionType> {
return async dispatch => {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`copy: Message ${messageId} missing!`);
}
@ -2233,7 +2233,7 @@ export function retryDeleteForEveryone(
messageId: string
): ThunkAction<void, RootStateType, unknown, NoopActionType> {
return async dispatch => {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`retryDeleteForEveryone: Message ${messageId} missing!`);
}
@ -2737,7 +2737,7 @@ function conversationStoppedByMissingVerification(payload: {
};
}
function messageChanged(
export function messageChanged(
id: string,
conversationId: string,
data: MessageAttributesType
@ -2962,7 +2962,7 @@ function pushPanelForConversation(
const message =
state.conversations.messagesLookup[messageId] ||
(await getMessageById(messageId))?.attributes;
(await __DEPRECATED$getMessageById(messageId))?.attributes;
if (!message) {
throw new Error(
'pushPanelForConversation: could not find message for MessageDetails'
@ -3038,7 +3038,7 @@ function deleteMessagesForEveryone(
await Promise.all(
messageIds.map(async messageId => {
try {
const message = window.MessageController.getById(messageId);
const message = window.MessageCache.__DEPRECATED$getById(messageId);
if (!message) {
throw new Error(
`deleteMessageForEveryone: Message ${messageId} missing!`
@ -3394,7 +3394,11 @@ function loadRecentMediaItems(
// Cache these messages in memory to ensure Lightbox can find them
messages.forEach(message => {
window.MessageController.register(message.id, message);
window.MessageCache.__DEPRECATED$register(
message.id,
message,
'loadRecentMediaItems'
);
});
const recentMediaItems = messages
@ -3492,7 +3496,7 @@ export function saveAttachmentFromMessage(
providedAttachment?: AttachmentType
): ThunkAction<void, RootStateType, unknown, ShowToastActionType> {
return async (dispatch, getState) => {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(
`saveAttachmentFromMessage: Message ${messageId} missing!`
@ -3585,7 +3589,7 @@ export function scrollToMessage(
throw new Error('scrollToMessage: No conversation found');
}
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`scrollToMessage: failed to load message ${messageId}`);
}
@ -3599,7 +3603,7 @@ export function scrollToMessage(
let isInMemory = true;
if (!window.MessageController.getById(messageId)) {
if (!window.MessageCache.__DEPRECATED$getById(messageId)) {
isInMemory = false;
}
@ -4009,7 +4013,7 @@ function onConversationOpened(
conversation.onOpenStart();
if (messageId) {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (message) {
drop(conversation.loadAndScroll(messageId));
@ -4138,7 +4142,7 @@ function showArchivedConversations(): ShowArchivedConversationsActionType {
}
function doubleCheckMissingQuoteReference(messageId: string): NoopActionType {
const message = window.MessageController.getById(messageId);
const message = window.MessageCache.__DEPRECATED$getById(messageId);
if (message) {
void message.doubleCheckMissingQuoteReference();
}

View file

@ -21,7 +21,6 @@ import * as Errors from '../../types/errors';
import * as SingleServePromise from '../../services/singleServePromise';
import * as Stickers from '../../types/Stickers';
import * as log from '../../logging/log';
import { getMessageById } from '../../messages/getMessageById';
import { getMessagePropsSelector } from '../selectors/message';
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
import { longRunningTaskWrapper } from '../../util/longRunningTaskWrapper';
@ -559,15 +558,12 @@ function toggleForwardMessagesModal(
const messagesProps = await Promise.all(
messageIds.map(async messageId => {
const message = await getMessageById(messageId);
const messageAttributes = await window.MessageCache.resolveAttributes(
'toggleForwardMessagesModal',
messageId
);
if (!message) {
throw new Error(
`toggleForwardMessagesModal: no message found for ${messageId}`
);
}
const attachments = message.get('attachments') ?? [];
const { attachments = [] } = messageAttributes;
if (!attachments.every(isDownloaded)) {
dispatch(
@ -576,7 +572,7 @@ function toggleForwardMessagesModal(
}
const messagePropsSelector = getMessagePropsSelector(getState());
const messageProps = messagePropsSelector(message.attributes);
const messageProps = messagePropsSelector(messageAttributes);
return messageProps;
})
@ -765,14 +761,10 @@ function showEditHistoryModal(
messageId: string
): ThunkAction<void, RootStateType, unknown, ShowEditHistoryModalActionType> {
return async dispatch => {
const message = await getMessageById(messageId);
if (!message) {
log.warn('showEditHistoryModal: no message found');
return;
}
const messageAttributes = message.attributes;
const messageAttributes = await window.MessageCache.resolveAttributes(
'showEditHistoryModal',
messageId
);
const nextEditHistoryMessages =
copyOverMessageAttributesIntoEditHistory(messageAttributes);

View file

@ -17,7 +17,7 @@ import type { ShowToastActionType } from './toast';
import type { StateType as RootStateType } from '../reducer';
import * as log from '../../logging/log';
import { getMessageById } from '../../messages/getMessageById';
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
import type { MessageAttributesType } from '../../model-types.d';
import { isGIF } from '../../types/Attachment';
import {
@ -137,7 +137,7 @@ function showLightboxForViewOnceMedia(
return async dispatch => {
log.info('showLightboxForViewOnceMedia: attempting to display message');
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(
`showLightboxForViewOnceMedia: Message ${messageId} missing!`
@ -232,7 +232,7 @@ function showLightbox(opts: {
return async (dispatch, getState) => {
const { attachment, messageId } = opts;
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
throw new Error(`showLightbox: Message ${messageId} missing!`);
}
@ -373,7 +373,7 @@ function showLightboxForAdjacentMessage(
sent_at: sentAt,
} = media.message;
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
log.warn('showLightboxForAdjacentMessage: original message is gone');
dispatch({

View file

@ -101,7 +101,11 @@ function loadMediaItems(
await Promise.all(
rawMedia.map(async message => {
const { schemaVersion } = message;
const model = window.MessageController.register(message.id, message);
const model = window.MessageCache.__DEPRECATED$register(
message.id,
message,
'loadMediaItems'
);
if (schemaVersion && schemaVersion < VERSION_NEEDED_FOR_DISPLAY) {
const upgradedMsgAttributes = await upgradeMessageSchema(message);

View file

@ -36,7 +36,7 @@ import { blockSendUntilConversationsAreVerified } from '../../util/blockSendUnti
import { deleteStoryForEveryone as doDeleteStoryForEveryone } from '../../util/deleteStoryForEveryone';
import { deleteGroupStoryReplyForEveryone as doDeleteGroupStoryReplyForEveryone } from '../../util/deleteGroupStoryReplyForEveryone';
import { enqueueReactionForSend } from '../../reactions/enqueueReactionForSend';
import { getMessageById } from '../../messages/getMessageById';
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
import { markOnboardingStoryAsRead } from '../../util/markOnboardingStoryAsRead';
import { markViewed } from '../../services/MessageUpdater';
import { queueAttachmentDownloads } from '../../util/queueAttachmentDownloads';
@ -387,7 +387,7 @@ function markStoryRead(
return;
}
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
log.warn(`markStoryRead: no message found ${messageId}`);
@ -520,7 +520,7 @@ function queueStoryDownload(
return;
}
const message = await getMessageById(storyId);
const message = await __DEPRECATED$getMessageById(storyId);
if (message) {
// We want to ensure that we re-hydrate the story reply context with the
@ -1395,7 +1395,7 @@ function removeAllContactStories(
const messages = (
await Promise.all(
messageIds.map(async messageId => {
const message = await getMessageById(messageId);
const message = await __DEPRECATED$getMessageById(messageId);
if (!message) {
log.warn(`${logId}: no message found ${messageId}`);

View file

@ -0,0 +1,98 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { bindActionCreators } from 'redux';
import type { BadgesStateType } from './ducks/badges';
import type { CallHistoryDetails } from '../types/CallDisposition';
import type { MainWindowStatsType } from '../windows/context';
import type { MenuOptionsType } from '../types/menu';
import type { StoryDataType } from './ducks/stories';
import type { StoryDistributionListDataType } from './ducks/storyDistributionLists';
import { actionCreators } from './actions';
import { createStore } from './createStore';
import { getInitialState } from './getInitialState';
export function initializeRedux({
callsHistory,
initialBadgesState,
mainWindowStats,
menuOptions,
stories,
storyDistributionLists,
}: {
callsHistory: ReadonlyArray<CallHistoryDetails>;
initialBadgesState: BadgesStateType;
mainWindowStats: MainWindowStatsType;
menuOptions: MenuOptionsType;
stories: Array<StoryDataType>;
storyDistributionLists: Array<StoryDistributionListDataType>;
}): void {
const initialState = getInitialState({
badges: initialBadgesState,
callsHistory,
mainWindowStats,
menuOptions,
stories,
storyDistributionLists,
});
const store = createStore(initialState);
window.reduxStore = store;
// Binding these actions to our redux store and exposing them allows us to update
// redux when things change in the backbone world.
window.reduxActions = {
accounts: bindActionCreators(actionCreators.accounts, store.dispatch),
app: bindActionCreators(actionCreators.app, store.dispatch),
audioPlayer: bindActionCreators(actionCreators.audioPlayer, store.dispatch),
audioRecorder: bindActionCreators(
actionCreators.audioRecorder,
store.dispatch
),
badges: bindActionCreators(actionCreators.badges, store.dispatch),
callHistory: bindActionCreators(actionCreators.callHistory, store.dispatch),
calling: bindActionCreators(actionCreators.calling, store.dispatch),
composer: bindActionCreators(actionCreators.composer, store.dispatch),
conversations: bindActionCreators(
actionCreators.conversations,
store.dispatch
),
crashReports: bindActionCreators(
actionCreators.crashReports,
store.dispatch
),
inbox: bindActionCreators(actionCreators.inbox, store.dispatch),
emojis: bindActionCreators(actionCreators.emojis, store.dispatch),
expiration: bindActionCreators(actionCreators.expiration, store.dispatch),
globalModals: bindActionCreators(
actionCreators.globalModals,
store.dispatch
),
items: bindActionCreators(actionCreators.items, store.dispatch),
lightbox: bindActionCreators(actionCreators.lightbox, store.dispatch),
linkPreviews: bindActionCreators(
actionCreators.linkPreviews,
store.dispatch
),
mediaGallery: bindActionCreators(
actionCreators.mediaGallery,
store.dispatch
),
network: bindActionCreators(actionCreators.network, store.dispatch),
safetyNumber: bindActionCreators(
actionCreators.safetyNumber,
store.dispatch
),
search: bindActionCreators(actionCreators.search, store.dispatch),
stickers: bindActionCreators(actionCreators.stickers, store.dispatch),
stories: bindActionCreators(actionCreators.stories, store.dispatch),
storyDistributionLists: bindActionCreators(
actionCreators.storyDistributionLists,
store.dispatch
),
toast: bindActionCreators(actionCreators.toast, store.dispatch),
updates: bindActionCreators(actionCreators.updates, store.dispatch),
user: bindActionCreators(actionCreators.user, store.dispatch),
username: bindActionCreators(actionCreators.username, store.dispatch),
};
}

View file

@ -19,7 +19,6 @@ import {
} from '../selectors/conversations';
import { getIntl, getTheme, getRegionCode } from '../selectors/user';
import { getLinkPreview } from '../selectors/linkPreviews';
import { getMessageById } from '../../messages/getMessageById';
import { getPreferredBadgeSelector } from '../selectors/badges';
import type {
ForwardMessageData,
@ -36,6 +35,8 @@ import { SmartCompositionTextArea } from './CompositionTextArea';
import { useToastActions } from '../ducks/toast';
import { hydrateRanges } from '../../types/BodyRange';
import { isDownloaded } from '../../types/Attachment';
import { __DEPRECATED$getMessageById } from '../../messages/getMessageById';
import { strictAssert } from '../../util/assert';
function toMessageForwardDraft(
props: ForwardMessagePropsType,
@ -119,10 +120,10 @@ function SmartForwardMessagesModalInner({
try {
const messages = await Promise.all(
finalDrafts.map(async (draft): Promise<ForwardMessageData> => {
const message = await getMessageById(draft.originalMessageId);
if (message == null) {
throw new Error('No message found');
}
const message = await __DEPRECATED$getMessageById(
draft.originalMessageId
);
strictAssert(message, 'no message found');
return {
draft,
originalMessage: message.attributes,