Normalize UUID in ConversationModel.initialize
This commit is contained in:
parent
f03cf1ba0e
commit
fdbb2bfb36
3 changed files with 22 additions and 6 deletions
|
@ -17,6 +17,7 @@ import type {
|
||||||
WhatIsThis,
|
WhatIsThis,
|
||||||
} from '../model-types.d';
|
} from '../model-types.d';
|
||||||
import { getInitials } from '../util/getInitials';
|
import { getInitials } from '../util/getInitials';
|
||||||
|
import { normalizeUuid } from '../util/normalizeUuid';
|
||||||
import type { AttachmentType } from '../types/Attachment';
|
import type { AttachmentType } from '../types/Attachment';
|
||||||
import { isGIF } from '../types/Attachment';
|
import { isGIF } from '../types/Attachment';
|
||||||
import type { CallHistoryDetailsType } from '../types/Calling';
|
import type { CallHistoryDetailsType } from '../types/Calling';
|
||||||
|
@ -259,6 +260,17 @@ export class ConversationModel extends window.Backbone
|
||||||
override initialize(
|
override initialize(
|
||||||
attributes: Partial<ConversationAttributesType> = {}
|
attributes: Partial<ConversationAttributesType> = {}
|
||||||
): void {
|
): void {
|
||||||
|
const uuid = this.get('uuid');
|
||||||
|
const normalizedUuid =
|
||||||
|
uuid && normalizeUuid(uuid, 'ConversationModel.initialize');
|
||||||
|
if (uuid && normalizedUuid !== uuid) {
|
||||||
|
log.warn(
|
||||||
|
'ConversationModel.initialize: normalizing uuid from ' +
|
||||||
|
`${uuid} to ${normalizedUuid}`
|
||||||
|
);
|
||||||
|
this.set('uuid', normalizedUuid);
|
||||||
|
}
|
||||||
|
|
||||||
if (isValidE164(attributes.id, false)) {
|
if (isValidE164(attributes.id, false)) {
|
||||||
this.set({ id: UUID.generate().toString(), e164: attributes.id });
|
this.set({ id: UUID.generate().toString(), e164: attributes.id });
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,9 @@ describe('normalizeUuid', () => {
|
||||||
|
|
||||||
it("throws if passed a string that's not a UUID", () => {
|
it("throws if passed a string that's not a UUID", () => {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
() => normalizeUuid('not-uuid-at-all', 'context 3'),
|
() => normalizeUuid('not-UUID-at-all', 'context 3'),
|
||||||
'Normalizing invalid uuid: not-uuid-at-all in context "context 3"'
|
'Normalizing invalid uuid: not-UUID-at-all to not-uuid-at-all in ' +
|
||||||
|
'context "context 3"'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
// Copyright 2021 Signal Messenger, LLC
|
// Copyright 2021 Signal Messenger, LLC
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
import type { UUIDStringType } from '../types/UUID';
|
||||||
import { isValidUuid } from '../types/UUID';
|
import { isValidUuid } from '../types/UUID';
|
||||||
import { assert } from './assert';
|
import { assert } from './assert';
|
||||||
|
|
||||||
export function normalizeUuid(uuid: string, context: string): string {
|
export function normalizeUuid(uuid: string, context: string): UUIDStringType {
|
||||||
|
const result = uuid.toLowerCase();
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
isValidUuid(uuid),
|
isValidUuid(uuid) && isValidUuid(result),
|
||||||
`Normalizing invalid uuid: ${uuid} in context "${context}"`
|
`Normalizing invalid uuid: ${uuid} to ${result} in context "${context}"`
|
||||||
);
|
);
|
||||||
|
|
||||||
return uuid.toLowerCase();
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue