signal-desktop/ts/util/findAndDeleteOnboardingStoryIfExists.ts

60 lines
1.6 KiB
TypeScript
Raw Normal View History

2022-11-09 02:38:19 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import * as log from '../logging/log';
import { calculateExpirationTimestamp } from './expirationTimer';
2023-01-10 01:44:20 +00:00
import { DAY } from './durations';
2022-11-09 02:38:19 +00:00
export async function findAndDeleteOnboardingStoryIfExists(): Promise<void> {
const existingOnboardingStoryMessageIds = window.storage.get(
'existingOnboardingStoryMessageIds'
);
if (!existingOnboardingStoryMessageIds) {
return;
}
const hasExpired = await (async () => {
const [storyId] = existingOnboardingStoryMessageIds;
try {
const messageAttributes = await window.MessageCache.resolveAttributes(
'findAndDeleteOnboardingStoryIfExists',
storyId
);
2022-11-09 02:38:19 +00:00
const expires = calculateExpirationTimestamp(messageAttributes) ?? 0;
2022-11-09 02:38:19 +00:00
2023-01-10 01:44:20 +00:00
const now = Date.now();
const isExpired = expires < now;
const needsRepair = expires > now + 2 * DAY;
return isExpired || needsRepair;
} catch {
return true;
2022-11-09 02:38:19 +00:00
}
})();
if (!hasExpired) {
log.info(
'findAndDeleteOnboardingStoryIfExists: current msg has not expired'
);
return;
}
log.info('findAndDeleteOnboardingStoryIfExists: removing onboarding stories');
await window.Signal.Data.removeMessages(existingOnboardingStoryMessageIds);
await window.storage.put('existingOnboardingStoryMessageIds', undefined);
2022-12-05 17:34:26 +00:00
const signalConversation =
await window.ConversationController.getOrCreateSignalConversation();
existingOnboardingStoryMessageIds.forEach(messageId =>
window.reduxActions.conversations.messageDeleted(
messageId,
signalConversation.id
)
);
2022-11-09 02:38:19 +00:00
}