Update unread count when creating important local notifications

This commit is contained in:
Scott Nonnenberg 2022-05-11 19:45:20 -07:00 committed by GitHub
parent ddde85cdd8
commit 105508c50f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 521 additions and 6 deletions

View file

@ -0,0 +1,132 @@
// Copyright 2021-2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { Database } from 'better-sqlite3';
import { ReadStatus } from '../../messages/MessageReadStatus';
import { SeenStatus } from '../../MessageSeenStatus';
import type { LoggerType } from '../../types/Logging';
export default function updateToSchemaVersion58(
currentVersion: number,
db: Database,
logger: LoggerType
): void {
if (currentVersion >= 58) {
return;
}
db.transaction(() => {
db.exec(
`
--- Promote unread status in JSON to SQL column
UPDATE messages
SET
readStatus = ${ReadStatus.Unread},
seenStatus = ${SeenStatus.Unseen}
WHERE
json_extract(json, '$.unread') IS true OR
json_extract(json, '$.unread') IS 1;
--- Clean up all old messages that still have a null read status
--- Note: we don't need to update seenStatus, because that was defaulted to zero
UPDATE messages
SET
readStatus = ${ReadStatus.Read}
WHERE
readStatus IS NULL;
--- Re-run unseen/unread queries from migration 56
UPDATE messages
SET
seenStatus = ${SeenStatus.Unseen}
WHERE
readStatus = ${ReadStatus.Unread} AND
(
type IS NULL
OR
type IN (
'call-history',
'change-number-notification',
'chat-session-refreshed',
'delivery-issue',
'group',
'incoming',
'keychange',
'timer-notification',
'verified-change'
)
);
UPDATE messages
SET
readStatus = ${ReadStatus.Read}
WHERE
readStatus = ${ReadStatus.Unread} AND
type IS NOT NULL AND
type NOT IN (
'call-history',
'change-number-notification',
'chat-session-refreshed',
'delivery-issue',
'group',
'incoming',
'keychange',
'timer-notification',
'verified-change'
);
--- (new) Ensure these message types are not unread, just unseen
UPDATE messages
SET
readStatus = ${ReadStatus.Read}
WHERE
readStatus = ${ReadStatus.Unread} AND
(
type IN (
'change-number-notification',
'keychange'
)
);
--- (new) Ensure that these message types are neither unseen nor unread
UPDATE messages
SET
readStatus = ${ReadStatus.Read},
seenStatus = ${SeenStatus.Seen}
WHERE
type IN (
'group-v1-migration',
'message-history-unsynced',
'outgoing',
'profile-change',
'universal-timer-notification'
);
--- Make sure JSON reflects SQL columns
UPDATE messages
SET
json = json_patch(
json,
json_object(
'readStatus', readStatus,
'seenStatus', seenStatus
)
)
WHERE
readStatus IS NOT NULL OR
seenStatus IS NOT 0;
`
);
db.pragma('user_version = 58');
})();
logger.info('updateToSchemaVersion58: success!');
}

View file

@ -33,6 +33,7 @@ import updateToSchemaVersion54 from './54-unprocessed-received-at-counter';
import updateToSchemaVersion55 from './55-report-message-aci';
import updateToSchemaVersion56 from './56-add-unseen-to-message';
import updateToSchemaVersion57 from './57-rm-message-history-unsynced';
import updateToSchemaVersion58 from './58-update-unread';
function updateToSchemaVersion1(
currentVersion: number,
@ -1929,6 +1930,7 @@ export const SCHEMA_VERSIONS = [
updateToSchemaVersion55,
updateToSchemaVersion56,
updateToSchemaVersion57,
updateToSchemaVersion58,
];
export function updateSchema(db: Database, logger: LoggerType): void {