Skip to content

Commit

Permalink
refactor: Streamline leaving and clearing conversation (#15965)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc authored Oct 10, 2023
1 parent 508971b commit 58c0f59
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 45 deletions.
46 changes: 11 additions & 35 deletions src/script/conversation/ConversationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export class ConversationRepository {
);

conversationsToLeave.forEach(async conversation => {
await this.leaveConversation(conversation, {clearContent: false, localOnly: true});
await this.leaveConversation(conversation, {localOnly: true});
await this.insertFederationStopSystemMessage(conversation, [deletedDomain]);
});

Expand Down Expand Up @@ -1673,27 +1673,15 @@ export class ConversationRepository {
* @note According to spec we archive a conversation when we clear it.
* It will be unarchived once it is opened through search. We use the archive flag to distinguish states.
*
* @param conversationEntity Conversation to clear
* @param conversation Conversation to clear
* @param leaveConversation Should we leave the conversation before clearing the content?
*/
public async clearConversation(conversationEntity: Conversation, leaveConversation = false) {
const isActiveConversation = this.conversationState.isActiveConversation(conversationEntity);
const nextConversationEntity = this.getNextConversation(conversationEntity);

if (leaveConversation) {
conversationEntity.status(ConversationStatus.PAST_MEMBER);
this.callingRepository.leaveCall(
conversationEntity.qualifiedId,
LEAVE_CALL_REASON.USER_MANUALY_LEFT_CONVERSATION,
);
}
public async clearConversation(conversation: Conversation) {
const isActiveConversation = this.conversationState.isActiveConversation(conversation);
const nextConversationEntity = this.getNextConversation(conversation);

await this.messageRepository.updateClearedTimestamp(conversationEntity);
await this._clearConversation(conversationEntity);

if (leaveConversation) {
await this.removeMember(conversationEntity, this.userState.self().qualifiedId);
}
await this.messageRepository.updateClearedTimestamp(conversation);
await this._clearConversation(conversation);

if (isActiveConversation) {
amplify.publish(WebAppEvents.CONVERSATION.SHOW, nextConversationEntity, {});
Expand Down Expand Up @@ -1764,16 +1752,13 @@ export class ConversationRepository {
/**
* Remove the current user from a conversation.
*
* @param conversationEntity Conversation to remove user from
* @param conversation Conversation to remove user from
* @param clearContent Should we clear the conversation content from the database?
* @returns Resolves when user was removed from the conversation
*/
private async leaveConversation(conversationEntity: Conversation, {clearContent = false, localOnly = false} = {}) {
public async leaveConversation(conversation: Conversation, {localOnly = false} = {}) {
const userQualifiedId = this.userState.self().qualifiedId;

return clearContent
? this.clearConversation(conversationEntity, true)
: this.removeMemberFromConversation(conversationEntity, userQualifiedId, {localOnly});
return this.removeMember(conversation, userQualifiedId, {localOnly});
}

/**
Expand All @@ -1784,18 +1769,9 @@ export class ConversationRepository {
* @param clearContent Should we clear the conversation content from the database?
* @returns Resolves when member was removed from the conversation
*/
public async removeMember(
conversationEntity: Conversation,
userId: QualifiedId,
{clearContent = false, localOnly = false} = {},
) {
const isUserLeaving = this.userState.self().qualifiedId.id === userId.id;
public async removeMember(conversationEntity: Conversation, userId: QualifiedId, {localOnly = false} = {}) {
const isMLSConversation = conversationEntity.isUsingMLSProtocol;

if (isUserLeaving) {
return this.leaveConversation(conversationEntity, {clearContent});
}

return isMLSConversation
? this.removeMemberFromMLSConversation(conversationEntity, userId, {localOnly})
: this.removeMemberFromConversation(conversationEntity, userId, {localOnly});
Expand Down
29 changes: 19 additions & 10 deletions src/script/view_model/ActionsViewModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,26 @@ export class ActionsViewModel {
});
};

private readonly leaveOrClearConversation = async (
conversation: Conversation,
{leave, clear}: {leave: boolean; clear: boolean},
): Promise<void> => {
if (leave) {
await this.conversationRepository.leaveConversation(conversation);
}
if (clear) {
await this.conversationRepository.clearConversation(conversation);
}
};

readonly clearConversation = (conversationEntity: Conversation): void => {
if (conversationEntity) {
const modalType = conversationEntity.isLeavable() ? PrimaryModal.type.OPTION : PrimaryModal.type.CONFIRM;

PrimaryModal.show(modalType, {
primaryAction: {
action: (leaveConversation = false) => {
this.conversationRepository.clearConversation(conversationEntity, leaveConversation);
action: async (leave = false) => {
await this.leaveOrClearConversation(conversationEntity, {clear: true, leave: leave});
},
text: t('modalConversationClearAction'),
},
Expand Down Expand Up @@ -261,28 +273,25 @@ export class ActionsViewModel {
return this.connectionRepository.ignoreRequest(userEntity);
};

readonly leaveConversation = (conversationEntity: Conversation): Promise<void> => {
if (!conversationEntity) {
readonly leaveConversation = (conversation: Conversation): Promise<void> => {
if (!conversation) {
return Promise.reject();
}

return new Promise(resolve => {
PrimaryModal.show(PrimaryModal.type.OPTION, {
primaryAction: {
action: async (clearContent = false) => {
await this.conversationRepository.removeMember(conversationEntity, this.userState.self().qualifiedId, {
clearContent,
});

await this.leaveOrClearConversation(conversation, {clear: clearContent, leave: true});
resolve();
},
text: t('modalConversationLeaveAction'),
},
text: {
closeBtnLabel: t('modalConversationLeaveMessageCloseBtn', conversationEntity.display_name()),
closeBtnLabel: t('modalConversationLeaveMessageCloseBtn', conversation.display_name()),
message: t('modalConversationLeaveMessage'),
option: t('modalConversationLeaveOption'),
title: t('modalConversationLeaveHeadline', conversationEntity.display_name()),
title: t('modalConversationLeaveHeadline', conversation.display_name()),
},
});
});
Expand Down

0 comments on commit 58c0f59

Please sign in to comment.