You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-desktop/ts/session/utils/SyncMessageUtils.ts

69 lines
1.9 KiB
TypeScript

5 years ago
import * as _ from 'lodash';
5 years ago
import * as UserUtils from '../../util/user';
import { getAllConversations } from '../../../js/modules/data';
5 years ago
import { ContentMessage, SyncMessage } from '../messages/outgoing';
import { MultiDeviceProtocol } from '../protocols';
5 years ago
5 years ago
export function from(message: ContentMessage): SyncMessage | undefined {
5 years ago
// const { timestamp, identifier } = message;
5 years ago
// Stubbed for now
5 years ago
return undefined;
5 years ago
}
export async function canSync(message: ContentMessage): Promise<boolean> {
// This function should be agnostic to the device; it shouldn't need
// to know about the recipient
5 years ago
5 years ago
// Stubbed for now
5 years ago
return Boolean(from(message));
5 years ago
}
5 years ago
export async function getSyncContacts(): Promise<Array<any> | undefined> {
const thisDevice = await UserUtils.getCurrentDevicePubKey();
if (!thisDevice) {
return [];
}
const primaryDevice = await MultiDeviceProtocol.getPrimaryDevice(thisDevice);
5 years ago
const conversations = await getAllConversations({
ConversationCollection: window.Whisper.ConversationCollection,
5 years ago
});
5 years ago
// We are building a set of all contacts
5 years ago
const primaryContacts =
conversations.filter(
c =>
c.isPrivate() &&
!c.isOurLocalDevice() &&
!c.isBlocked() &&
5 years ago
!c.attributes.secondaryStatus
) || [];
5 years ago
5 years ago
const secondaryContactsPartial = conversations.filter(
c =>
5 years ago
c.isPrivate() &&
!c.isOurLocalDevice() &&
!c.isBlocked() &&
5 years ago
c.attributes.secondaryStatus
);
const seondaryContactsPromise = secondaryContactsPartial.map(async c =>
window.ConversationController.getOrCreateAndWait(
5 years ago
c.getPrimaryDevicePubKey(),
'private'
)
);
const secondaryContacts = (await Promise.all(seondaryContactsPromise))
// Filter out our primary key if it was added here
.filter(c => c.id !== primaryDevice.key);
5 years ago
// Return unique contacts
5 years ago
return _.uniqBy(
[...primaryContacts, ...secondaryContacts],
device => !!device
);
5 years ago
}