Skip to content

Commit

Permalink
runfix: don't try to migrate 1:1 conversation with deleted user (#16820
Browse files Browse the repository at this point in the history
…) (#16825)

* runfix: don't try to migrate 1:1 conversation with deleted user

* docs: improve comments

* test: add scenario that covers deleted users
  • Loading branch information
PatrykBuniX authored Feb 13, 2024
1 parent 9789ebb commit 0354e2d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/script/conversation/ConversationRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,41 @@ describe('ConversationRepository', () => {
const conversationEntity = await conversationRepository.init1to1Conversation(conversation, true);
expect(conversationEntity).toEqual(conversation);
});

it('returns a proteus conversation even if both users support mls but the user was deleted on backend', async () => {
const conversationRepository = testFactory.conversation_repository!;
const userRepository = testFactory.user_repository!;

const otherUserId = {id: 'f718410c-3833-479d-bd80-af03f38416', domain: 'test-domain'};
const otherUser = new User(otherUserId.id, otherUserId.domain);
otherUser.isDeleted = true;

otherUser.supportedProtocols([ConversationProtocol.PROTEUS, ConversationProtocol.MLS]);

userRepository['userState'].users.push(otherUser);

const selfUserId = {id: '109da9ca-a495-47a870-9ffbe924b2d1', domain: 'test-domain'};
const selfUser = new User(selfUserId.id, selfUserId.domain);
selfUser.supportedProtocols([ConversationProtocol.PROTEUS, ConversationProtocol.MLS]);
jest.spyOn(conversationRepository['userState'], 'self').mockReturnValueOnce(selfUser);

const proteus1to1Conversation = _generateConversation({
type: CONVERSATION_TYPE.ONE_TO_ONE,
protocol: ConversationProtocol.PROTEUS,
});

const connection = new ConnectionEntity();
proteus1to1Conversation.connection(connection);
connection.conversationId = proteus1to1Conversation.qualifiedId;
connection.userId = otherUserId;
otherUser.connection(connection);

jest.spyOn(conversationRepository['conversationService'], 'getMLS1to1Conversation');

const conversationEntity = await conversationRepository.init1to1Conversation(proteus1to1Conversation, true);
expect(conversationEntity).toEqual(proteus1to1Conversation);
expect(conversationRepository['conversationService'].getMLS1to1Conversation).not.toHaveBeenCalled();
});
});

describe('getInitialised1To1Conversation', () => {
Expand Down
18 changes: 16 additions & 2 deletions src/script/conversation/ConversationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1865,10 +1865,24 @@ export class ConversationRepository {
// If there's local mls conversation, we want to use it
const localMLSConversation = this.conversationState.findMLS1to1Conversation(otherUserId);

// If both users support mls or mls conversation is already known, we use it
// If mls conversation is already known, we use it
// we never go back to proteus conversation, even if one of the users do not support mls anymore
// (e.g. due to the change of supported protocols in team configuration)
if (protocol === ConversationProtocol.MLS || localMLSConversation) {
if (localMLSConversation) {
return this.initMLS1to1Conversation(otherUserId, isMLSSupportedByTheOtherUser);
}

// If both users support mls, we will initialise mls 1:1 conversation (unless it's a proteus 1:1 conversation with a user that has been deleted)
if (protocol === ConversationProtocol.MLS) {
// If the other user is deleted on backend, just open a proteus conversation and do not try to migrate it to mls.
if (isProteusConversation(conversation)) {
const otherUser = await this.userRepository.getUserById(otherUserId);
if (otherUser.isDeleted) {
this.logger.warn(`User ${otherUserId.id} is deleted, opening proteus conversation`);
return conversation;
}
}

return this.initMLS1to1Conversation(otherUserId, isMLSSupportedByTheOtherUser);
}

Expand Down

0 comments on commit 0354e2d

Please sign in to comment.