Skip to content

Commit

Permalink
improve srp
Browse files Browse the repository at this point in the history
  • Loading branch information
atomrc committed Oct 10, 2023
1 parent 032a495 commit 9cc7250
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/script/conversation/ConversationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ export class ConversationRepository {

try {
for (const user of usersToRemove) {
await this.removeMembers(conversation, [user.qualifiedId], {localOnly: true});
await this.removeMembersLocally(conversation, [user.qualifiedId]);
}
} catch (error) {
console.warn('failed to remove users', error);
Expand Down Expand Up @@ -1746,9 +1746,16 @@ export class ConversationRepository {
*/
public async leaveConversation(conversation: Conversation, {localOnly = false} = {}) {
const userQualifiedId = this.userState.self().qualifiedId;
return this.removeMembers(conversation, [userQualifiedId], {localOnly});
return localOnly
? this.removeMembersLocally(conversation, [userQualifiedId])
: this.removeMembers(conversation, [userQualifiedId]);
}

private async removeMembersLocally(conversation: Conversation, userIds: QualifiedId[]) {
const currentTimestamp = this.serverTimeHandler.toServerTimestamp();
const events = userIds.map(userId => EventBuilder.buildMemberLeave(conversation, userId, true, currentTimestamp));
await this.eventRepository.injectEvents(events, EventRepository.SOURCE.INJECTED);
}
/**
* Umbrella function to remove a member from a conversation, no matter the protocol or type.
*
Expand All @@ -1757,16 +1764,7 @@ 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 removeMembers(conversationEntity: Conversation, userIds: QualifiedId[], {localOnly = false} = {}) {
if (localOnly) {
const currentTimestamp = this.serverTimeHandler.toServerTimestamp();
const events = userIds.map(userId =>
EventBuilder.buildMemberLeave(conversationEntity, userId, true, currentTimestamp),
);
await this.eventRepository.injectEvents(events, EventRepository.SOURCE.INJECTED);
return;
}

public async removeMembers(conversationEntity: Conversation, userIds: QualifiedId[]) {
const events = isMLSConversation(conversationEntity)
? await this.removeMemberFromMLSConversation(conversationEntity, userIds)
: await this.removeMemberFromConversation(conversationEntity, userIds);
Expand Down

0 comments on commit 9cc7250

Please sign in to comment.