signal-desktop/ts/util/markOnboardingStoryAsRead.ts

58 lines
1.5 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';
2024-07-22 18:16:33 +00:00
import { DataWriter } from '../sql/Client';
import { __DEPRECATED$getMessageById } from '../messages/getMessageById';
2022-11-09 02:38:19 +00:00
import { isNotNil } from './isNotNil';
2022-11-16 20:18:02 +00:00
import { DurationInSeconds } from './durations';
2022-11-09 02:38:19 +00:00
import { markViewed } from '../services/MessageUpdater';
import { storageServiceUploadJob } from '../services/storage';
export async function markOnboardingStoryAsRead(): Promise<boolean> {
2022-11-09 02:38:19 +00:00
const existingOnboardingStoryMessageIds = window.storage.get(
'existingOnboardingStoryMessageIds'
);
if (!existingOnboardingStoryMessageIds) {
log.warn('markOnboardingStoryAsRead: no existing messages');
return false;
2022-11-09 02:38:19 +00:00
}
const messages = await Promise.all(
existingOnboardingStoryMessageIds.map(__DEPRECATED$getMessageById)
2022-11-09 02:38:19 +00:00
);
const storyReadDate = Date.now();
const messageAttributes = messages
.map(message => {
if (!message) {
return;
}
message.set({
2022-11-16 20:18:02 +00:00
expireTimer: DurationInSeconds.DAY,
2022-11-09 02:38:19 +00:00
});
message.set(markViewed(message.attributes, storyReadDate));
return message.attributes;
})
.filter(isNotNil);
log.info(
`markOnboardingStoryAsRead: marked ${messageAttributes.length} viewed`
);
2024-07-22 18:16:33 +00:00
await DataWriter.saveMessages(messageAttributes, {
ourAci: window.textsecure.storage.user.getCheckedAci(),
2022-11-09 02:38:19 +00:00
});
await window.storage.put('hasViewedOnboardingStory', true);
2022-11-09 02:38:19 +00:00
storageServiceUploadJob({ reason: 'markOnboardingStoryAsRead' });
return true;
2022-11-09 02:38:19 +00:00
}