diff --git a/js/background.js b/js/background.js index a4112e1a5..b040c8276 100644 --- a/js/background.js +++ b/js/background.js @@ -630,6 +630,7 @@ conversation.setLokiProfile({ displayName: newName, }); + // might be good to not trigger a sync if the name did not change await conversation.commit(); await window.libsession.Utils.SyncUtils.forceSyncConfigurationNowIfNeeded( true diff --git a/ts/session/utils/Messages.ts b/ts/session/utils/Messages.ts index 1957fea66..cd1051727 100644 --- a/ts/session/utils/Messages.ts +++ b/ts/session/utils/Messages.ts @@ -6,16 +6,7 @@ import { import { EncryptionType, PubKey } from '../types'; import { ClosedGroupMessage } from '../messages/outgoing/content/data/group/ClosedGroupMessage'; import { ClosedGroupNewMessage } from '../messages/outgoing/content/data/group/ClosedGroupNewMessage'; -import { ConversationModel } from '../../../js/models/conversations'; -import { - ConfigurationMessage, - ConfigurationMessageClosedGroup, - ConfigurationMessageContact, -} from '../messages/outgoing/content/ConfigurationMessage'; -import uuid from 'uuid'; -import { getLatestClosedGroupEncryptionKeyPair } from '../../../js/modules/data'; -import { UserUtils } from '.'; -import { ECKeyPair } from '../../receiver/keypairs'; + import _ from 'lodash'; import { ClosedGroupEncryptionPairReplyMessage } from '../messages/outgoing/content/data/group/ClosedGroupEncryptionPairReplyMessage'; @@ -65,94 +56,3 @@ export async function toRawMessage( return rawMessage; } - -export const getCurrentConfigurationMessage = async ( - convos: Array -) => { - const ourPubKey = (await UserUtils.getOurNumber()).key; - const ourConvo = convos.find(convo => convo.id === ourPubKey); - - // Filter open groups - const openGroupsIds = convos - .filter(c => !!c.get('active_at') && c.isPublic() && !c.get('left')) - .map(c => c.id.substring((c.id as string).lastIndexOf('@') + 1)) as Array< - string - >; - - // Filter Closed/Medium groups - const closedGroupModels = convos.filter( - c => - !!c.get('active_at') && - c.isMediumGroup() && - c.get('members').includes(ourPubKey) && - !c.get('left') && - !c.get('isKickedFromGroup') && - !c.isBlocked() - ); - - const closedGroups = await Promise.all( - closedGroupModels.map(async c => { - const groupPubKey = c.get('id'); - const fetchEncryptionKeyPair = await getLatestClosedGroupEncryptionKeyPair( - groupPubKey - ); - if (!fetchEncryptionKeyPair) { - return null; - } - - return new ConfigurationMessageClosedGroup({ - publicKey: groupPubKey, - name: c.get('name'), - members: c.get('members') || [], - admins: c.get('groupAdmins') || [], - encryptionKeyPair: ECKeyPair.fromHexKeyPair(fetchEncryptionKeyPair), - }); - }) - ); - - const onlyValidClosedGroup = closedGroups.filter(m => m !== null) as Array< - ConfigurationMessageClosedGroup - >; - - // Filter contacts - const contactsModels = convos.filter( - c => - !!c.get('active_at') && - c.getLokiProfile()?.displayName && - c.isPrivate() && - !c.isBlocked() - ); - - const contacts = contactsModels.map(c => { - return new ConfigurationMessageContact({ - publicKey: c.id, - displayName: c.getLokiProfile()?.displayName, - profilePictureURL: c.get('avatarPointer'), - profileKey: c.get('profileKey'), - }); - }); - - if (!ourConvo) { - window.log.error( - 'Could not find our convo while building a configuration message.' - ); - } - const profileKeyFromStorage = window.storage.get('profileKey'); - const profileKey = profileKeyFromStorage - ? new Uint8Array(profileKeyFromStorage) - : undefined; - - const profilePicture = ourConvo?.get('avatarPointer') || undefined; - const displayName = ourConvo?.getLokiProfile()?.displayName || undefined; - - return new ConfigurationMessage({ - identifier: uuid(), - timestamp: Date.now(), - activeOpenGroups: openGroupsIds, - activeClosedGroups: onlyValidClosedGroup, - displayName, - profilePicture, - profileKey, - contacts, - }); -}; diff --git a/ts/session/utils/syncUtils.ts b/ts/session/utils/syncUtils.ts index 15c5723db..5438dc86c 100644 --- a/ts/session/utils/syncUtils.ts +++ b/ts/session/utils/syncUtils.ts @@ -1,9 +1,20 @@ -import { createOrUpdateItem, getItemById } from '../../../js/modules/data'; +import { + createOrUpdateItem, + getItemById, + getLatestClosedGroupEncryptionKeyPair, +} from '../../../js/modules/data'; import { getMessageQueue } from '..'; import { ConversationController } from '../conversations'; -import { getCurrentConfigurationMessage } from './Messages'; -import { RawMessage } from '../types'; import { DAYS } from './Number'; +import uuid from 'uuid'; +import { UserUtils } from '.'; +import { ConversationModel } from '../../../js/models/conversations'; +import { ECKeyPair } from '../../receiver/keypairs'; +import { + ConfigurationMessage, + ConfigurationMessageClosedGroup, + ConfigurationMessageContact, +} from '../messages/outgoing/content/ConfigurationMessage'; const ITEM_ID_LAST_SYNC_TIMESTAMP = 'lastSyncedTimestamp'; @@ -47,7 +58,7 @@ export const forceSyncConfigurationNowIfNeeded = async ( const allConvos = ConversationController.getInstance().getConversations(); void getCurrentConfigurationMessage(allConvos).then(configMessage => { - window.log.info('forceSyncConfigurationNowIfNeeded with', configMessage); + // console.warn('forceSyncConfigurationNowIfNeeded with', configMessage); try { // this just adds the message to the sending queue. @@ -55,8 +66,8 @@ export const forceSyncConfigurationNowIfNeeded = async ( // tslint:disable-next-line: no-void-expression const callback = waitForMessageSent ? () => { - resolve(true); - } + resolve(true); + } : undefined; void getMessageQueue().sendSyncMessage(configMessage, callback as any); // either we resolve from the callback if we need to wait for it, @@ -73,3 +84,94 @@ export const forceSyncConfigurationNowIfNeeded = async ( } }); }); + +export const getCurrentConfigurationMessage = async ( + convos: Array +) => { + const ourPubKey = (await UserUtils.getOurNumber()).key; + const ourConvo = convos.find(convo => convo.id === ourPubKey); + + // Filter open groups + const openGroupsIds = convos + .filter(c => !!c.get('active_at') && c.isPublic() && !c.get('left')) + .map(c => c.id.substring((c.id as string).lastIndexOf('@') + 1)) as Array< + string + >; + + // Filter Closed/Medium groups + const closedGroupModels = convos.filter( + c => + !!c.get('active_at') && + c.isMediumGroup() && + c.get('members').includes(ourPubKey) && + !c.get('left') && + !c.get('isKickedFromGroup') && + !c.isBlocked() + ); + + const closedGroups = await Promise.all( + closedGroupModels.map(async c => { + const groupPubKey = c.get('id'); + const fetchEncryptionKeyPair = await getLatestClosedGroupEncryptionKeyPair( + groupPubKey + ); + if (!fetchEncryptionKeyPair) { + return null; + } + + return new ConfigurationMessageClosedGroup({ + publicKey: groupPubKey, + name: c.get('name'), + members: c.get('members') || [], + admins: c.get('groupAdmins') || [], + encryptionKeyPair: ECKeyPair.fromHexKeyPair(fetchEncryptionKeyPair), + }); + }) + ); + + const onlyValidClosedGroup = closedGroups.filter(m => m !== null) as Array< + ConfigurationMessageClosedGroup + >; + + // Filter contacts + const contactsModels = convos.filter( + c => + !!c.get('active_at') && + c.getLokiProfile()?.displayName && + c.isPrivate() && + !c.isBlocked() + ); + + const contacts = contactsModels.map(c => { + return new ConfigurationMessageContact({ + publicKey: c.id, + displayName: c.getLokiProfile()?.displayName, + profilePictureURL: c.get('avatarPointer'), + profileKey: c.get('profileKey'), + }); + }); + + if (!ourConvo) { + window.log.error( + 'Could not find our convo while building a configuration message.' + ); + } + const profileKeyFromStorage = window.storage.get('profileKey'); + const profileKey = profileKeyFromStorage + ? new Uint8Array(profileKeyFromStorage) + : undefined; + + const profilePicture = ourConvo?.get('avatarPointer') || undefined; + const displayName = ourConvo?.getLokiProfile()?.displayName || undefined; + + return new ConfigurationMessage({ + identifier: uuid(), + timestamp: Date.now(), + activeOpenGroups: openGroupsIds, + activeClosedGroups: onlyValidClosedGroup, + displayName, + profilePicture, + profileKey, + contacts, + }); +};