2021-08-12 18:15:55 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-disable max-classes-per-file */
|
|
|
|
|
|
|
|
import { Collection, Model } from 'backbone';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { MessageModel } from '../models/messages';
|
2021-08-12 18:15:55 +00:00
|
|
|
import { ReadStatus } from '../messages/MessageReadStatus';
|
|
|
|
import { markViewed } from '../services/MessageUpdater';
|
2022-07-12 16:39:18 +00:00
|
|
|
import { isIncoming } from '../state/selectors/message';
|
2021-09-23 18:16:09 +00:00
|
|
|
import { notificationService } from '../services/notifications';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../logging/log';
|
2022-05-11 20:59:58 +00:00
|
|
|
import { GiftBadgeStates } from '../components/conversation/Message';
|
2021-08-12 18:15:55 +00:00
|
|
|
|
2022-01-04 15:27:16 +00:00
|
|
|
export type ViewSyncAttributesType = {
|
2021-08-12 18:15:55 +00:00
|
|
|
senderId: string;
|
2022-01-04 15:27:16 +00:00
|
|
|
senderE164?: string;
|
2021-08-12 18:15:55 +00:00
|
|
|
senderUuid: string;
|
|
|
|
timestamp: number;
|
|
|
|
viewedAt: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ViewSyncModel extends Model<ViewSyncAttributesType> {}
|
|
|
|
|
|
|
|
let singleton: ViewSyncs | undefined;
|
|
|
|
|
|
|
|
export class ViewSyncs extends Collection {
|
|
|
|
static getSingleton(): ViewSyncs {
|
|
|
|
if (!singleton) {
|
|
|
|
singleton = new ViewSyncs();
|
|
|
|
}
|
|
|
|
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
forMessage(message: MessageModel): Array<ViewSyncModel> {
|
|
|
|
const senderId = window.ConversationController.ensureContactIds({
|
|
|
|
e164: message.get('source'),
|
|
|
|
uuid: message.get('sourceUuid'),
|
|
|
|
});
|
|
|
|
const syncs = this.filter(item => {
|
|
|
|
return (
|
|
|
|
item.get('senderId') === senderId &&
|
|
|
|
item.get('timestamp') === message.get('sent_at')
|
|
|
|
);
|
|
|
|
});
|
|
|
|
if (syncs.length) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-12 18:15:55 +00:00
|
|
|
`Found ${syncs.length} early view sync(s) for message ${message.get(
|
|
|
|
'sent_at'
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
this.remove(syncs);
|
|
|
|
}
|
|
|
|
return syncs;
|
|
|
|
}
|
|
|
|
|
|
|
|
async onSync(sync: ViewSyncModel): Promise<void> {
|
|
|
|
try {
|
|
|
|
const messages = await window.Signal.Data.getMessagesBySentAt(
|
2021-12-10 22:51:54 +00:00
|
|
|
sync.get('timestamp')
|
2021-08-12 18:15:55 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const found = messages.find(item => {
|
|
|
|
const senderId = window.ConversationController.ensureContactIds({
|
2021-12-10 22:51:54 +00:00
|
|
|
e164: item.source,
|
|
|
|
uuid: item.sourceUuid,
|
2021-08-12 18:15:55 +00:00
|
|
|
});
|
|
|
|
|
2022-07-12 16:39:18 +00:00
|
|
|
return senderId === sync.get('senderId');
|
2021-08-12 18:15:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!found) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(
|
2021-08-12 18:15:55 +00:00
|
|
|
'Nothing found for view sync',
|
|
|
|
sync.get('senderId'),
|
|
|
|
sync.get('senderE164'),
|
|
|
|
sync.get('senderUuid'),
|
|
|
|
sync.get('timestamp')
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-23 18:16:09 +00:00
|
|
|
notificationService.removeBy({ messageId: found.id });
|
2021-08-12 18:15:55 +00:00
|
|
|
|
|
|
|
const message = window.MessageController.register(found.id, found);
|
|
|
|
|
|
|
|
if (message.get('readStatus') !== ReadStatus.Viewed) {
|
|
|
|
message.set(markViewed(message.attributes, sync.get('viewedAt')));
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:59:58 +00:00
|
|
|
const giftBadge = message.get('giftBadge');
|
|
|
|
if (giftBadge) {
|
|
|
|
message.set({
|
|
|
|
giftBadge: {
|
|
|
|
...giftBadge,
|
2022-05-16 19:54:38 +00:00
|
|
|
state: isIncoming(message.attributes)
|
|
|
|
? GiftBadgeStates.Redeemed
|
|
|
|
: GiftBadgeStates.Opened,
|
2022-05-11 20:59:58 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-12 18:15:55 +00:00
|
|
|
this.remove(sync);
|
|
|
|
} catch (error) {
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(
|
2021-08-12 18:15:55 +00:00
|
|
|
'ViewSyncs.onSync error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|