allow closedgroup new message to be sent to our other devices

also, do not drop it on the receiving side
pull/1498/head
Audric Ackermann 5 years ago
parent 01f834ae98
commit a31c457c08
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -152,9 +152,9 @@ export async function handleNewClosedGroup(
await removeFromCache(envelope); await removeFromCache(envelope);
return; return;
} }
const ourPrimary = await UserUtils.getOurNumber(); const ourNumber = await UserUtils.getOurNumber();
if (envelope.senderIdentity === ourPrimary.key) { if (envelope.senderIdentity === ourNumber.key) {
window.log.warn( window.log.warn(
'Dropping new closed group updatemessage from our other device.' 'Dropping new closed group updatemessage from our other device.'
); );
@ -173,7 +173,7 @@ export async function handleNewClosedGroup(
const members = membersAsData.map(toHex); const members = membersAsData.map(toHex);
const admins = adminsAsData.map(toHex); const admins = adminsAsData.map(toHex);
if (!members.includes(ourPrimary.key)) { if (!members.includes(ourNumber.key)) {
log.info( log.info(
'Got a new group message but apparently we are not a member of it. Dropping it.' 'Got a new group message but apparently we are not a member of it. Dropping it.'
); );
@ -694,15 +694,13 @@ async function handleClosedGroupMemberLeft(
const oldMembers = convo.get('members') || []; const oldMembers = convo.get('members') || [];
const leftMemberWasPresent = oldMembers.includes(sender); const leftMemberWasPresent = oldMembers.includes(sender);
const members = didAdminLeave ? [] : oldMembers.filter(s => s !== sender); const members = didAdminLeave ? [] : oldMembers.filter(s => s !== sender);
// Guard against self-sends // Show log if we sent this message ourself (from another device or not)
const ourPubkey = await UserUtils.getCurrentDevicePubKey(); const ourPubkey = await UserUtils.getCurrentDevicePubKey();
if (!ourPubkey) { if (!ourPubkey) {
throw new Error('Could not get user pubkey'); throw new Error('Could not get user pubkey');
} }
if (sender === ourPubkey) { if (await UserUtils.isUs(sender)) {
window.log.info('self send group update ignored'); window.log.info('Got self-sent group update member left...');
await removeFromCache(envelope);
return;
} }
// Generate and distribute a new encryption key pair if needed // Generate and distribute a new encryption key pair if needed

@ -531,8 +531,7 @@ export async function handleConfigurationMessage(
if (!ourPubkey) { if (!ourPubkey) {
return; return;
} }
console.warn('ourPubkey', ourPubkey);
console.warn('envelope.source', envelope.source);
if (envelope.source !== ourPubkey) { if (envelope.source !== ourPubkey) {
window?.log?.info( window?.log?.info(
'Dropping configuration change from someone else than us.' 'Dropping configuration change from someone else than us.'

@ -6,6 +6,7 @@ import {
} from './MessageQueueInterface'; } from './MessageQueueInterface';
import { import {
ChatMessage, ChatMessage,
ClosedGroupNewMessage,
ContentMessage, ContentMessage,
DataMessage, DataMessage,
ExpirationTimerUpdateMessage, ExpirationTimerUpdateMessage,
@ -40,8 +41,7 @@ export class MessageQueue implements MessageQueueInterface {
) { ) {
throw new Error('SyncMessage needs to be sent with sendSyncMessage'); throw new Error('SyncMessage needs to be sent with sendSyncMessage');
} }
await this.process(user, message, sentCb);
await this.sendMessageToDevices([user], message);
} }
public async send( public async send(
@ -55,7 +55,7 @@ export class MessageQueue implements MessageQueueInterface {
) { ) {
throw new Error('SyncMessage needs to be sent with sendSyncMessage'); throw new Error('SyncMessage needs to be sent with sendSyncMessage');
} }
await this.sendMessageToDevices([device], message, sentCb); await this.process(device, message, sentCb);
} }
/** /**
@ -136,7 +136,7 @@ export class MessageQueue implements MessageQueueInterface {
throw new Error('ourNumber is not set'); throw new Error('ourNumber is not set');
} }
await this.sendMessageToDevices([PubKey.cast(ourPubKey)], message, sentCb); await this.process(PubKey.cast(ourPubKey), message, sentCb);
} }
public async processPending(device: PubKey) { public async processPending(device: PubKey) {
@ -172,18 +172,6 @@ export class MessageQueue implements MessageQueueInterface {
}); });
} }
public async sendMessageToDevices(
devices: Array<PubKey>,
message: ContentMessage,
sentCb?: (message: RawMessage) => Promise<void>
) {
const promises = devices.map(async device => {
await this.process(device, message, sentCb);
});
return Promise.all(promises);
}
private async processAllPending() { private async processAllPending() {
const devices = await this.pendingMessageCache.getDevices(); const devices = await this.pendingMessageCache.getDevices();
const promises = devices.map(async device => this.processPending(device)); const promises = devices.map(async device => this.processPending(device));
@ -191,6 +179,9 @@ export class MessageQueue implements MessageQueueInterface {
return Promise.all(promises); return Promise.all(promises);
} }
/**
* This method should not be called directly. Only through sendToPubKey.
*/
private async process( private async process(
device: PubKey, device: PubKey,
message: ContentMessage, message: ContentMessage,
@ -199,9 +190,11 @@ export class MessageQueue implements MessageQueueInterface {
// Don't send to ourselves // Don't send to ourselves
const currentDevice = await UserUtils.getCurrentDevicePubKey(); const currentDevice = await UserUtils.getCurrentDevicePubKey();
if (currentDevice && device.isEqual(currentDevice)) { if (currentDevice && device.isEqual(currentDevice)) {
// We allow a message for ourselve only if it's a ConfigurationMessage or a message with a syncTarget set // We allow a message for ourselve only if it's a ConfigurationMessage, a ClosedGroupNewMessage,
// or a message with a syncTarget set.
if ( if (
message instanceof ConfigurationMessage || message instanceof ConfigurationMessage ||
message instanceof ClosedGroupNewMessage ||
(message as any).syncTarget?.length > 0 (message as any).syncTarget?.length > 0
) { ) {
window.log.warn('Processing sync message'); window.log.warn('Processing sync message');

@ -141,30 +141,18 @@ describe('MessageQueue', () => {
describe('sendToPubKey', () => { describe('sendToPubKey', () => {
it('should send the message to the device', async () => { it('should send the message to the device', async () => {
const devices = TestUtils.generateFakePubKeys(1); const device = TestUtils.generateFakePubKey();
const stub = sandbox const stub = sandbox.stub(messageQueueStub as any, 'process').resolves();
.stub(messageQueueStub, 'sendMessageToDevices')
.resolves();
const message = TestUtils.generateChatMessage(); const message = TestUtils.generateChatMessage();
await messageQueueStub.sendToPubKey(devices[0], message); await messageQueueStub.sendToPubKey(device, message);
const args = stub.lastCall.args as [Array<PubKey>, ContentMessage]; const args = stub.lastCall.args as [Array<PubKey>, ContentMessage];
expect(args[0]).to.have.same.members(devices); expect(args[0]).to.be.equal(device);
expect(args[1]).to.equal(message); expect(args[1]).to.equal(message);
}); });
}); });
describe('sendMessageToDevices', () => {
it('can send to many devices', async () => {
const devices = TestUtils.generateFakePubKeys(5);
const message = TestUtils.generateChatMessage();
await messageQueueStub.sendMessageToDevices(devices, message);
expect(pendingMessageCache.getCache()).to.have.length(devices.length);
});
});
describe('sendToGroup', () => { describe('sendToGroup', () => {
it('should throw an error if invalid non-group message was passed', async () => { it('should throw an error if invalid non-group message was passed', async () => {
// const chatMessage = TestUtils.generateChatMessage(); // const chatMessage = TestUtils.generateChatMessage();

Loading…
Cancel
Save