From 692b0ae18978357ab14c1f2bd7b8fa227f7716d3 Mon Sep 17 00:00:00 2001 From: trevor-signal <131492920+trevor-signal@users.noreply.github.com> Date: Fri, 15 Nov 2024 12:06:57 -0500 Subject: [PATCH] Trim group name and description from group state --- ts/groups.ts | 27 +++++++++++++++++---------- ts/services/backups/export.ts | 4 ++-- ts/services/backups/import.ts | 4 ++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/ts/groups.ts b/ts/groups.ts index fc919051b048..7bf2265ee5a4 100644 --- a/ts/groups.ts +++ b/ts/groups.ts @@ -4513,6 +4513,10 @@ async function integrateGroupChange({ }; } +function normalizeTextField(text: string | null | undefined): string { + return text?.trim() ?? ''; +} + function extractDiffs({ current, dropInitialJoinMessage, @@ -4617,11 +4621,12 @@ function extractDiffs({ } // name - - if (old.name !== current.name) { + const oldName = normalizeTextField(old.name); + const newName = normalizeTextField(current.name); + if (oldName !== newName) { details.push({ type: 'title', - newTitle: current.name, + newTitle: newName, }); } @@ -4640,11 +4645,13 @@ function extractDiffs({ } // description - if (old.description !== current.description) { + const oldDescription = normalizeTextField(old.description); + const newDescription = normalizeTextField(current.description); + if (oldDescription !== newDescription) { details.push({ type: 'description', - removed: !current.description, - description: current.description, + removed: !newDescription, + description: newDescription, }); } @@ -5373,7 +5380,7 @@ async function applyGroupChange({ if (actions.modifyTitle) { const { title } = actions.modifyTitle; if (title && title.content === 'title') { - result.name = dropNull(title.title); + result.name = dropNull(title.title)?.trim(); } else { log.warn( `applyGroupChange/${logId}: Clearing group title due to missing data.` @@ -5575,7 +5582,7 @@ async function applyGroupChange({ if (actions.modifyDescription) { const { descriptionBytes } = actions.modifyDescription; if (descriptionBytes && descriptionBytes.content === 'descriptionText') { - result.description = dropNull(descriptionBytes.descriptionText); + result.description = dropNull(descriptionBytes.descriptionText)?.trim(); } else { log.warn( `applyGroupChange/${logId}: Clearing group description due to missing data.` @@ -5809,7 +5816,7 @@ async function applyGroupState({ // Note: During decryption, title becomes a GroupAttributeBlob const { title } = groupState; if (title && title.content === 'title') { - result.name = dropNull(title.title); + result.name = dropNull(title.title)?.trim(); } else { result.name = undefined; } @@ -6019,7 +6026,7 @@ async function applyGroupState({ // descriptionBytes const { descriptionBytes } = groupState; if (descriptionBytes && descriptionBytes.content === 'descriptionText') { - result.description = dropNull(descriptionBytes.descriptionText); + result.description = dropNull(descriptionBytes.descriptionText)?.trim(); } else { result.description = undefined; } diff --git a/ts/services/backups/export.ts b/ts/services/backups/export.ts index e8e5f7c13ce3..6f90fd9f439d 100644 --- a/ts/services/backups/export.ts +++ b/ts/services/backups/export.ts @@ -890,12 +890,12 @@ export class BackupExportStream extends Readable { storySendMode, snapshot: { title: { - title: convo.name ?? '', + title: convo.name?.trim() ?? '', }, description: convo.description != null ? { - descriptionText: convo.description, + descriptionText: convo.description.trim(), } : null, avatarUrl: convo.avatar?.url, diff --git a/ts/services/backups/import.ts b/ts/services/backups/import.ts index 55af2e12e75d..83e7a193f8b4 100644 --- a/ts/services/backups/import.ts +++ b/ts/services/backups/import.ts @@ -927,8 +927,8 @@ export class BackupImportStream extends Writable { : undefined, // Snapshot - name: dropNull(title?.title), - description: dropNull(description?.descriptionText), + name: dropNull(title?.title)?.trim(), + description: dropNull(description?.descriptionText)?.trim(), expireTimer: expirationTimerS ? DurationInSeconds.fromSeconds(expirationTimerS) : undefined,