Skip to content

Commit

Permalink
Conversation: Prevent getProps errors on initial link
Browse files Browse the repository at this point in the history
Co-authored-by: Scott Nonnenberg <scott@signal.org>
Co-authored-by: Evan Hahn <69474926+EvanHahn-Signal@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 8, 2021
1 parent 3b13d57 commit a9b980d
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 36 deletions.
8 changes: 5 additions & 3 deletions ts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DataMessageClass } from './textsecure.d';
import { MessageAttributesType } from './model-types.d';
import { WhatIsThis } from './window.d';
import { getTitleBarVisibility, TitleBarVisibility } from './types/Settings';
import { DEFAULT_CONVERSATION_COLOR } from './types/Colors';
import { ChallengeHandler } from './challenge';
import { isWindowDragElement } from './util/isWindowDragElement';
import { assert } from './util/assert';
Expand Down Expand Up @@ -81,9 +82,10 @@ export async function startApp(): Promise<void> {

window.storage.onready(() => {
if (!window.storage.get('defaultConversationColor')) {
window.storage.put('defaultConversationColor', {
color: 'ultramarine',
});
window.storage.put(
'defaultConversationColor',
DEFAULT_CONVERSATION_COLOR
);
}
});

Expand Down
3 changes: 2 additions & 1 deletion ts/model-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ConversationModel } from './models/conversations';
import { ProfileNameChangeType } from './util/getStringForProfileChange';
import { CapabilitiesType } from './textsecure/WebAPI';
import { GroupNameCollisionsWithIdsByTitle } from './util/groupMemberNameCollisions';
import { ConversationColorType } from './types/Colors';

export type WhatIsThis = any;

Expand Down Expand Up @@ -196,7 +197,7 @@ export type ConversationAttributesType = {
addedBy?: string;
capabilities?: CapabilitiesType;
color?: string;
conversationColor?: string;
conversationColor?: ConversationColorType;
customColor?: CustomColorType;
customColorId?: string;
discoveredUnregisteredAt?: number;
Expand Down
28 changes: 17 additions & 11 deletions ts/models/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
AvatarColors,
ConversationColorType,
CustomColorType,
DEFAULT_CONVERSATION_COLOR,
} from '../types/Colors';
import { MessageModel } from './messages';
import { isMuted } from '../util/isMuted';
Expand Down Expand Up @@ -1379,23 +1380,26 @@ export class ConversationModel extends window.Backbone
// We don't want to crash or have an infinite loop if we loop back into this function
// again. We'll log a warning and returned old cached props or throw an error.
this.format = () => {
const { stack } = new Error('for stack');
window.log.warn(
`Conversation.format()/${this.idForLogging()} reentrant call! ${stack}`
);
if (!this.oldCachedProps) {
throw new Error(
`Conversation.format()/${this.idForLogging()} reentrant call, no old cached props!`
);
}
return this.oldCachedProps;
};

this.cachedProps = this.getProps();
const { stack } = new Error('for stack');
window.log.warn(
`Conversation.format()/${this.idForLogging()} reentrant call! ${stack}`
);

this.format = oldFormat;
return this.oldCachedProps;
};

return this.cachedProps;
try {
this.cachedProps = this.getProps();
return this.cachedProps;
} finally {
this.format = oldFormat;
}
}

// Note: this should never be called directly. Use conversation.format() instead, which
Expand Down Expand Up @@ -4879,7 +4883,8 @@ export class ConversationModel extends window.Backbone

getConversationColor(): ConversationColorType {
const defaultConversationColor = window.storage.get(
'defaultConversationColor'
'defaultConversationColor',
DEFAULT_CONVERSATION_COLOR
);

return this.get('conversationColor') || defaultConversationColor.color;
Expand All @@ -4890,7 +4895,8 @@ export class ConversationModel extends window.Backbone
customColorId?: string;
} {
const defaultConversationColor = window.storage.get(
'defaultConversationColor'
'defaultConversationColor',
DEFAULT_CONVERSATION_COLOR
);

if (this.getConversationColor() !== 'custom') {
Expand Down
16 changes: 7 additions & 9 deletions ts/state/ducks/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
AvatarColorType,
ConversationColorType,
CustomColorType,
DefaultConversationColorType,
DEFAULT_CONVERSATION_COLOR,
} from '../../types/Colors';
import { ConversationAttributesType } from '../../model-types.d';
import { BodyRangeType } from '../../types/Util';
Expand Down Expand Up @@ -394,13 +396,7 @@ type CustomColorRemovedActionType = {
type: typeof CUSTOM_COLOR_REMOVED;
payload: {
colorId: string;
defaultConversationColor: {
color: ConversationColorType;
customColorData?: {
id: string;
value: CustomColorType;
};
};
defaultConversationColor: DefaultConversationColorType;
};
};
type SetPreJoinConversationActionType = {
Expand Down Expand Up @@ -765,7 +761,8 @@ function removeCustomColorOnConversations(
}

const defaultConversationColor = window.storage.get(
'defaultConversationColor'
'defaultConversationColor',
DEFAULT_CONVERSATION_COLOR
);

dispatch({
Expand Down Expand Up @@ -800,7 +797,8 @@ function resetAllChatColors(): ThunkAction<
});

const defaultConversationColor = window.storage.get(
'defaultConversationColor'
'defaultConversationColor',
DEFAULT_CONVERSATION_COLOR
);

dispatch({
Expand Down
11 changes: 3 additions & 8 deletions ts/state/ducks/items.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Signal Messenger, LLC
// Copyright 2019-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { omit } from 'lodash';
Expand All @@ -11,6 +11,7 @@ import {
ConversationColors,
ConversationColorType,
CustomColorType,
DefaultConversationColorType,
} from '../../types/Colors';
import { reloadSelectedConversation } from '../../shims/reloadSelectedConversation';

Expand All @@ -22,13 +23,7 @@ export type ItemsStateType = {
readonly [key: string]: unknown;

// This property should always be set and this is ensured in background.ts
readonly defaultConversationColor?: {
color: ConversationColorType;
customColorData?: {
id: string;
value: CustomColorType;
};
};
readonly defaultConversationColor?: DefaultConversationColorType;

readonly customColors?: {
readonly colors: Record<string, CustomColorType>;
Expand Down
6 changes: 3 additions & 3 deletions ts/state/selectors/items.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2019-2020 Signal Messenger, LLC
// Copyright 2019-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { createSelector } from 'reselect';
Expand All @@ -8,9 +8,9 @@ import { ITEM_NAME as UNIVERSAL_EXPIRE_TIMER_ITEM } from '../../util/universalEx
import { StateType } from '../reducer';
import { ItemsStateType } from '../ducks/items';
import {
ConversationColors,
ConversationColorType,
CustomColorType,
DEFAULT_CONVERSATION_COLOR,
} from '../../types/Colors';

export const getItems = (state: StateType): ItemsStateType => state.items;
Expand Down Expand Up @@ -41,5 +41,5 @@ export const getDefaultConversationColor = createSelector(
id: string;
value: CustomColorType;
};
} => state.defaultConversationColor ?? { color: ConversationColors[0] }
} => state.defaultConversationColor ?? DEFAULT_CONVERSATION_COLOR
);
14 changes: 13 additions & 1 deletion ts/types/Colors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Signal Messenger, LLC
// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

export const AvatarColors = [
Expand Down Expand Up @@ -93,3 +93,15 @@ export type AvatarColorType = typeof AvatarColors[number];
export type ConversationColorType =
| typeof ConversationColors[number]
| 'custom';

export type DefaultConversationColorType = {
color: ConversationColorType;
customColorData?: {
id: string;
value: CustomColorType;
};
};

export const DEFAULT_CONVERSATION_COLOR: DefaultConversationColorType = {
color: 'ultramarine',
};
1 change: 1 addition & 0 deletions ts/window.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ import { SignalProtocolStore } from './SignalProtocolStore';
import { StartupQueue } from './util/StartupQueue';
import * as synchronousCrypto from './util/synchronousCrypto';
import SyncRequest from './textsecure/SyncRequest';
import { ConversationColorType, CustomColorType } from './types/Colors';

export { Long } from 'long';

Expand Down

0 comments on commit a9b980d

Please sign in to comment.