Rended m.room.canonical_alias event in the timeline, considering alt_aliases (#2428)

This commit is contained in:
Benoit Marty 2020-11-20 15:29:35 +01:00 committed by Benoit Marty
parent 7c2fea8623
commit 0d93105bcd
3 changed files with 102 additions and 11 deletions

View file

@ -24,5 +24,14 @@ import com.squareup.moshi.JsonClass
*/
@JsonClass(generateAdapter = true)
data class RoomCanonicalAliasContent(
@Json(name = "alias") val canonicalAlias: String? = null
/**
* The canonical alias for the room. If not present, null, or empty the room should be considered to have no canonical alias.
*/
@Json(name = "alias") val canonicalAlias: String? = null,
/**
* Alternative aliases the room advertises.
* This list can have aliases despite the alias field being null, empty, or otherwise not present.
*/
@Json(name = "alt_aliases") val alternativeAliases: List<String>? = null
)

View file

@ -262,6 +262,33 @@
<string name="notice_room_canonical_alias_unset">"%1$s removed the main address for this room."</string>
<string name="notice_room_canonical_alias_unset_by_you">"You removed the main address for this room."</string>
<plurals name="notice_room_canonical_alias_alternative_added">
<item quantity="one">%1$s added the alternative address %2$s for this room.</item>
<item quantity="other">%1$s added the alternative addresses %2$s for this room.</item>
</plurals>
<plurals name="notice_room_canonical_alias_alternative_added_by_you">
<item quantity="one">You added the alternative address %1$s for this room.</item>
<item quantity="other">You added the alternative addresses %1$s for this room.</item>
</plurals>
<plurals name="notice_room_canonical_alias_alternative_removed">
<item quantity="one">%1$s removed the alternative address %2$s for this room.</item>
<item quantity="other">%1$s removed the alternative addresses %2$s for this room.</item>
</plurals>
<plurals name="notice_room_canonical_alias_alternative_removed_by_you">
<item quantity="one">You removed the alternative address %1$s for this room.</item>
<item quantity="other">You removed the alternative addresses %1$s for this room.</item>
</plurals>
<string name="notice_room_canonical_alias_alternative_changed">%1$s changed the alternative addresses for this room.</string>
<string name="notice_room_canonical_alias_alternative_changed_by_you">You changed the alternative addresses for this room.</string>
<string name="notice_room_canonical_alias_main_and_alternative_changed">%1$s changed the main and alternative addresses for this room.</string>
<string name="notice_room_canonical_alias_main_and_alternative_changed_by_you">You changed the main and alternative addresses for this room.</string>
<string name="notice_room_canonical_alias_no_change">%1$s changed the addresses for this room.</string>
<string name="notice_room_canonical_alias_no_change_by_you">You changed the addresses for this room.</string>
<string name="notice_room_guest_access_can_join">"%1$s has allowed guests to join the room."</string>
<string name="notice_room_guest_access_can_join_by_you">"You have allowed guests to join the room."</string>
<string name="notice_direct_room_guest_access_can_join">"%1$s has allowed guests to join here."</string>

View file

@ -465,22 +465,77 @@ class NoticeEventFormatter @Inject constructor(
private fun formatRoomCanonicalAliasEvent(event: Event, senderName: String?): String? {
val eventContent: RoomCanonicalAliasContent? = event.getClearContent().toModel()
val canonicalAlias = eventContent?.canonicalAlias
return canonicalAlias
?.takeIf { it.isNotBlank() }
?.let {
val prevContent: RoomCanonicalAliasContent? = event.resolvedPrevContent().toModel()
val canonicalAlias = eventContent?.canonicalAlias?.takeIf { it.isNotEmpty() }
val prevCanonicalAlias = prevContent?.canonicalAlias?.takeIf { it.isNotEmpty() }
val altAliases = eventContent?.alternativeAliases.orEmpty()
val prevAltAliases = prevContent?.alternativeAliases.orEmpty()
val added = altAliases - prevAltAliases
val removed = prevAltAliases - altAliases
return if (added.isEmpty() && removed.isEmpty() && canonicalAlias == prevCanonicalAlias) {
// in case there is no difference between the two events say something as we can't simply hide the event from here
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_canonical_alias_set_by_you, it)
sp.getString(R.string.notice_room_canonical_alias_no_change_by_you)
} else {
sp.getString(R.string.notice_room_canonical_alias_set, senderName, it)
sp.getString(R.string.notice_room_canonical_alias_no_change, senderName)
}
} else if (added.isEmpty() && removed.isEmpty()) {
// Canonical has changed
if (canonicalAlias != null) {
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_canonical_alias_set_by_you, canonicalAlias)
} else {
sp.getString(R.string.notice_room_canonical_alias_set, senderName, canonicalAlias)
}
?: if (event.isSentByCurrentUser()) {
} else {
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_canonical_alias_unset_by_you)
} else {
sp.getString(R.string.notice_room_canonical_alias_unset, senderName)
}
}
} else if (added.isEmpty()) {
if (canonicalAlias == prevCanonicalAlias) {
// Some alternative has been removed
if (event.isSentByCurrentUser()) {
sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_removed_by_you, removed.size, removed.joinToString())
} else {
sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_removed, removed.size, senderName, removed.joinToString())
}
} else {
// Main and removed
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed_by_you)
} else {
sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed, senderName)
}
}
} else if (removed.isEmpty()) {
if (canonicalAlias == prevCanonicalAlias) {
// Some alternative has been added
if (event.isSentByCurrentUser()) {
sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_added_by_you, added.size, added.joinToString())
} else {
sp.getQuantityString(R.plurals.notice_room_canonical_alias_alternative_added, added.size, senderName, added.joinToString())
}
} else {
// Main and added
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed_by_you)
} else {
sp.getString(R.string.notice_room_canonical_alias_main_and_alternative_changed, senderName)
}
}
} else {
// Alternative added and removed
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_canonical_alias_alternative_changed_by_you)
} else {
sp.getString(R.string.notice_room_canonical_alias_alternative_changed, senderName)
}
}
}
private fun formatRoomGuestAccessEvent(event: Event, senderName: String?, rs: RoomSummary?): String? {
val eventContent: RoomGuestAccessContent? = event.getClearContent().toModel()