address reviews

pull/1515/head
Audric Ackermann 4 years ago
parent e0c2cf8e9a
commit 215cffdd75
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -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

@ -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<ConversationModel>
) => {
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,
});
};

@ -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<ConversationModel>
) => {
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,
});
};

Loading…
Cancel
Save