Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runfix: leave mls conversation #15972

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/__mocks__/@wireapp/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class Account extends EventEmitter {
messageTimer: {
setConversationLevelTimer: jest.fn(),
},
removeUsersFromMLSConversation: jest.fn(),
removeUserFromConversation: jest.fn(),
},

client: {
Expand Down
108 changes: 107 additions & 1 deletion src/script/conversation/ConversationRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import {
} from '@wireapp/api-client/lib/conversation';
import {RECEIPT_MODE} from '@wireapp/api-client/lib/conversation/data';
import {ConversationProtocol} from '@wireapp/api-client/lib/conversation/NewConversation';
import {ConversationCreateEvent, ConversationMemberJoinEvent, CONVERSATION_EVENT} from '@wireapp/api-client/lib/event/';
import {
ConversationMemberLeaveEvent,
ConversationCreateEvent,
ConversationMemberJoinEvent,
CONVERSATION_EVENT,
} from '@wireapp/api-client/lib/event/';
import {QualifiedId} from '@wireapp/api-client/lib/user';
import {amplify} from 'amplify';
import {StatusCodes as HTTP_STATUS} from 'http-status-codes';
Expand Down Expand Up @@ -1540,4 +1545,105 @@ describe('ConversationRepository', () => {
expect(conversationRepo['refreshAllConversationsUnavailableParticipants']).toHaveBeenCalled();
});
});

describe('removeMembers', () => {
it.each([ConversationProtocol.PROTEUS, ConversationProtocol.MIXED])(
'should remove member from %s conversation',
async protocol => {
const conversationRepository = await testFactory.exposeConversationActors();

const conversation = _generateConversation({protocol});

const selfUser = generateUser();
conversation.selfUser(selfUser);

const user1 = generateUser();
const user2 = generateUser();

conversation.participating_user_ets([user1, user2]);

const coreConversationService = container.resolve(Core).service!.conversation;

jest.spyOn(conversationRepository['eventRepository'], 'injectEvent').mockImplementation(jest.fn());

await conversationRepository.removeMembers(conversation, [user1.qualifiedId]);

expect(coreConversationService.removeUserFromConversation).toHaveBeenCalledWith(
conversation.qualifiedId,
user1.qualifiedId,
);
},
);

it('should remove member from mls conversation', async () => {
const conversationRepository = await testFactory.exposeConversationActors();

const conversation = _generateConversation({protocol: ConversationProtocol.MLS});

const selfUser = generateUser();
conversation.selfUser(selfUser);

const user1 = generateUser();
const user2 = generateUser();

conversation.participating_user_ets([user1, user2]);

const coreConversationService = container.resolve(Core).service!.conversation;

jest.spyOn(conversationRepository['eventRepository'], 'injectEvent').mockImplementation(jest.fn());

const mockedMemberLeaveEvent: ConversationMemberLeaveEvent = {
conversation: conversation.id,
data: {qualified_user_ids: [], user_ids: []},
from: '',
time: '',
type: CONVERSATION_EVENT.MEMBER_LEAVE,
};
jest
.spyOn(coreConversationService, 'removeUsersFromMLSConversation')
.mockResolvedValueOnce({events: [mockedMemberLeaveEvent], conversation: {} as BackendConversation});
await conversationRepository.removeMembers(conversation, [user1.qualifiedId]);

expect(coreConversationService.removeUsersFromMLSConversation).toHaveBeenCalledWith({
conversationId: conversation.qualifiedId,
qualifiedUserIds: [user1.qualifiedId],
groupId: conversation.groupId,
});
expect(conversationRepository['eventRepository'].injectEvent).toHaveBeenCalled();
});
});

describe('leaveConversation', () => {
afterEach(() => {
jest.clearAllMocks();
});

it.each([ConversationProtocol.PROTEUS, ConversationProtocol.MIXED, ConversationProtocol.MLS])(
'should leave %s conversation',
async protocol => {
const conversationRepository = await testFactory.exposeConversationActors();

const conversation = _generateConversation({protocol});

const selfUser = generateUser();
conversation.selfUser(selfUser);

spyOn(conversationRepository['userState'], 'self').and.returnValue(selfUser);

conversation.participating_user_ets([generateUser(), generateUser()]);

const coreConversationService = container.resolve(Core).service!.conversation;

jest.spyOn(conversationRepository['eventRepository'], 'injectEvent').mockImplementation(jest.fn());

await conversationRepository.leaveConversation(conversation);

expect(coreConversationService.removeUserFromConversation).toHaveBeenCalledWith(
conversation.qualifiedId,
selfUser.qualifiedId,
);
expect(conversationRepository['eventRepository'].injectEvent).toHaveBeenCalled();
},
);
});
});
10 changes: 7 additions & 3 deletions src/script/conversation/ConversationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1742,9 +1742,13 @@ export class ConversationRepository {
*/
public async leaveConversation(conversation: Conversation, {localOnly = false} = {}) {
const userQualifiedId = this.userState.self().qualifiedId;
return localOnly
? this.removeMembersLocally(conversation, [userQualifiedId])
: this.removeMembers(conversation, [userQualifiedId]);

if (localOnly) {
return this.removeMembersLocally(conversation, [userQualifiedId]);
}

const events = await this.removeMembersFromConversation(conversation, [userQualifiedId]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot call .removeMembers method. We want to always delete the user "the proteus way", no need to check for conversation protocol.

await this.eventRepository.injectEvents(events, EventRepository.SOURCE.BACKEND_RESPONSE);
}

/**
Expand Down