Skip to content

Commit

Permalink
room membership change states will use displayName
Browse files Browse the repository at this point in the history
changelog

code improvement
  • Loading branch information
Velin92 authored and stefanceriu committed Jun 11, 2024
1 parent 7ba1e49 commit 61e00cf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ struct RoomEventStringBuilder {
return stateEventStringBuilder
.buildString(for: state, sender: sender, isOutgoing: isOutgoing)
.map(AttributedString.init)
case .roomMembership(let userID, _, let change):
case .roomMembership(let userID, let displayName, let change):
return stateEventStringBuilder
.buildString(for: change, member: userID, sender: sender, isOutgoing: isOutgoing)
.buildString(for: change, memberUserID: userID, memberDisplayName: displayName, sender: sender, isOutgoing: isOutgoing)
.map(AttributedString.init)
case .profileChange(let displayName, let prevDisplayName, let avatarUrl, let prevAvatarUrl):
return stateEventStringBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ struct RoomStateEventStringBuilder {
var shouldDisambiguateDisplayNames = true

func buildString(for change: MembershipChange?,
member: String,
memberUserID: String,
memberDisplayName: String?,
sender: TimelineItemSender,
isOutgoing: Bool) -> String? {
guard let change else {
Expand All @@ -31,51 +32,52 @@ struct RoomStateEventStringBuilder {
}

let senderIsYou = isOutgoing
let memberIsYou = member == userID
let displayName = if shouldDisambiguateDisplayNames {
let memberIsYou = memberUserID == userID
let member = memberDisplayName ?? memberUserID
let senderDisplayName = if shouldDisambiguateDisplayNames {
sender.disambiguatedDisplayName ?? sender.id
} else {
sender.displayName ?? sender.id
}

switch change {
case .joined:
return memberIsYou ? L10n.stateEventRoomJoinByYou : L10n.stateEventRoomJoin(displayName)
return memberIsYou ? L10n.stateEventRoomJoinByYou : L10n.stateEventRoomJoin(senderDisplayName)
case .left:
return memberIsYou ? L10n.stateEventRoomLeaveByYou : L10n.stateEventRoomLeave(displayName)
return memberIsYou ? L10n.stateEventRoomLeaveByYou : L10n.stateEventRoomLeave(senderDisplayName)
case .banned, .kickedAndBanned:
return senderIsYou ? L10n.stateEventRoomBanByYou(member) : L10n.stateEventRoomBan(displayName, member)
return senderIsYou ? L10n.stateEventRoomBanByYou(member) : L10n.stateEventRoomBan(senderDisplayName, member)
case .unbanned:
return senderIsYou ? L10n.stateEventRoomUnbanByYou(member) : L10n.stateEventRoomUnban(displayName, member)
return senderIsYou ? L10n.stateEventRoomUnbanByYou(member) : L10n.stateEventRoomUnban(senderDisplayName, member)
case .kicked:
return senderIsYou ? L10n.stateEventRoomRemoveByYou(member) : L10n.stateEventRoomRemove(displayName, member)
return senderIsYou ? L10n.stateEventRoomRemoveByYou(member) : L10n.stateEventRoomRemove(senderDisplayName, member)
case .invited:
if senderIsYou {
return L10n.stateEventRoomInviteByYou(member)
} else if memberIsYou {
return L10n.stateEventRoomInviteYou(displayName)
return L10n.stateEventRoomInviteYou(senderDisplayName)
} else {
return L10n.stateEventRoomInvite(displayName, member)
return L10n.stateEventRoomInvite(senderDisplayName, member)
}
case .invitationAccepted:
return memberIsYou ? L10n.stateEventRoomInviteAcceptedByYou : L10n.stateEventRoomInviteAccepted(displayName)
return memberIsYou ? L10n.stateEventRoomInviteAcceptedByYou : L10n.stateEventRoomInviteAccepted(senderDisplayName)
case .invitationRejected:
return memberIsYou ? L10n.stateEventRoomRejectByYou : L10n.stateEventRoomReject(displayName)
return memberIsYou ? L10n.stateEventRoomRejectByYou : L10n.stateEventRoomReject(senderDisplayName)
case .invitationRevoked:
return senderIsYou ? L10n.stateEventRoomThirdPartyRevokedInviteByYou(member) : L10n.stateEventRoomThirdPartyRevokedInvite(displayName, member)
return senderIsYou ? L10n.stateEventRoomThirdPartyRevokedInviteByYou(member) : L10n.stateEventRoomThirdPartyRevokedInvite(senderDisplayName, member)
case .knocked:
return memberIsYou ? L10n.stateEventRoomKnockByYou : L10n.stateEventRoomKnock(member)
case .knockAccepted:
return senderIsYou ? L10n.stateEventRoomKnockAcceptedByYou(displayName) : L10n.stateEventRoomKnockAccepted(displayName, member)
return senderIsYou ? L10n.stateEventRoomKnockAcceptedByYou(senderDisplayName) : L10n.stateEventRoomKnockAccepted(senderDisplayName, member)
case .knockRetracted:
return memberIsYou ? L10n.stateEventRoomKnockRetractedByYou : L10n.stateEventRoomKnockRetracted(member)
case .knockDenied:
if senderIsYou {
return L10n.stateEventRoomKnockDeniedByYou(member)
} else if memberIsYou {
return L10n.stateEventRoomKnockDeniedYou(displayName)
return L10n.stateEventRoomKnockDeniedYou(senderDisplayName)
} else {
return L10n.stateEventRoomKnockDenied(displayName, member)
return L10n.stateEventRoomKnockDenied(senderDisplayName, member)
}
case .none, .error, .notImplemented: // Not useful information for the user.
MXLog.verbose("Filtering timeline item for membership change: \(change)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
return nil
}
return buildStateTimelineItem(for: eventItemProxy, state: content, isOutgoing: isOutgoing)
case .roomMembership(userId: let userID, _, change: let change):
case .roomMembership(userId: let userID, let displayName, change: let change):
if isDM, change == .joined, userID == self.userID {
return nil
}
return buildStateMembershipChangeTimelineItem(for: eventItemProxy, member: userID, membershipChange: change, isOutgoing: isOutgoing)
return buildStateMembershipChangeTimelineItem(for: eventItemProxy, memberUserID: userID, memberDisplayName: displayName, membershipChange: change, isOutgoing: isOutgoing)
case .profileChange(let displayName, let prevDisplayName, let avatarUrl, let prevAvatarUrl):
return buildStateProfileChangeTimelineItem(for: eventItemProxy,
displayName: displayName,
Expand Down Expand Up @@ -609,10 +609,11 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
}

private func buildStateMembershipChangeTimelineItem(for eventItemProxy: EventTimelineItemProxy,
member: String,
memberUserID: String,
memberDisplayName: String?,
membershipChange: MembershipChange?,
isOutgoing: Bool) -> RoomTimelineItemProtocol? {
guard let text = stateEventStringBuilder.buildString(for: membershipChange, member: member, sender: eventItemProxy.sender, isOutgoing: isOutgoing) else { return nil }
guard let text = stateEventStringBuilder.buildString(for: membershipChange, memberUserID: memberUserID, memberDisplayName: memberDisplayName, sender: eventItemProxy.sender, isOutgoing: isOutgoing) else { return nil }
return buildStateTimelineItem(for: eventItemProxy, text: text, isOutgoing: isOutgoing)
}

Expand Down
1 change: 1 addition & 0 deletions changelog.d/2846.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Room membership state changes will use the display name whenever possible.

0 comments on commit 61e00cf

Please sign in to comment.