Skip to content

Commit

Permalink
feat: federation stopped system messages [WPB-1815] (#2040)
Browse files Browse the repository at this point in the history
Co-authored-by: Yamil Medina <[email protected]>
  • Loading branch information
Garzas and yamilmedina authored Aug 16, 2023
1 parent 34ca0ab commit 1138c26
Show file tree
Hide file tree
Showing 18 changed files with 325 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,32 @@ fun MessagePreview.uiLastMessageContent(): UILastMessageContent {
}
}

is MessagePreviewContent.FederatedMembersRemoved -> {
val membersRemovedContent = (content as MessagePreviewContent.FederatedMembersRemoved)
val isSelfRemoved = membersRemovedContent.isSelfUserRemoved
val otherUsersSize = membersRemovedContent.otherUserIdList.size

val previewMessageContent = when {
isSelfRemoved -> {
if (otherUsersSize == 0) {
UIText.StringResource(R.string.last_message_other_removed_only_self_user)
} else {
UIText.PluralResource(
R.plurals.last_message_other_removed_self_user_and_others,
otherUsersSize,
otherUsersSize
)
}
}

else -> {
UIText.PluralResource(R.plurals.last_message_other_removed_other_users, otherUsersSize, otherUsersSize)
}
}

UILastMessageContent.TextMessage(MessageBody(previewMessageContent))
}

is MessagePreviewContent.Ephemeral -> {
val ephemeralContent = (content as MessagePreviewContent.Ephemeral)
if (ephemeralContent.isGroupConversation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ class SystemMessageContentMapper @Inject constructor(
private val messageResourceProvider: MessageResourceProvider
) {

@Suppress("ComplexMethod")
fun mapMessage(
message: Message.System,
members: List<User>
) = when (val content = message.content) {
): UIMessageContent = when (val content = message.content) {
is MemberChange -> mapMemberChangeMessage(content, message.senderUserId, members)
is MessageContent.MissedCall -> mapMissedCallMessage(message.senderUserId, members)
is MessageContent.ConversationRenamed -> mapConversationRenamedMessage(message.senderUserId, content, members)
Expand All @@ -64,6 +65,7 @@ class SystemMessageContentMapper @Inject constructor(
is MessageContent.MLSWrongEpochWarning -> mapMLSWrongEpochWarning()
is MessageContent.ConversationDegradedMLS -> mapConversationDegraded(Conversation.Protocol.MLS)
is MessageContent.ConversationDegradedProteus -> mapConversationDegraded(Conversation.Protocol.PROTEUS)
is MessageContent.FederationStopped -> mapFederationMessage(content)
}

private fun mapConversationCreated(senderUserId: UserId, date: String, userList: List<User>): UIMessageContent.SystemMessage {
Expand Down Expand Up @@ -181,7 +183,7 @@ class SystemMessageContentMapper @Inject constructor(
content: MemberChange,
senderUserId: UserId,
userList: List<User>
): UIMessageContent.SystemMessage? {
): UIMessageContent.SystemMessage {
val sender = userList.findUser(userId = senderUserId)
val isAuthorSelfAction = content.members.size == 1 && senderUserId == content.members.first()
val isSelfTriggered = sender is SelfUser
Expand Down Expand Up @@ -221,9 +223,18 @@ class SystemMessageContentMapper @Inject constructor(

is FailedToAdd ->
UIMessageContent.SystemMessage.MemberFailedToAdd(mapFailedToAddUsersByDomain(content.members, userList))

is MemberChange.FederationRemoved -> UIMessageContent.SystemMessage.FederationMemberRemoved(
memberNames = memberNameList
)
}
}

private fun mapFederationMessage(content: MessageContent.FederationStopped) = when (content) {
is MessageContent.FederationStopped.ConnectionRemoved -> UIMessageContent.SystemMessage.FederationStopped(content.domainList)
is MessageContent.FederationStopped.Removed -> UIMessageContent.SystemMessage.FederationStopped(listOf(content.domain))
}

private fun mapFailedToAddUsersByDomain(members: List<UserId>, userList: List<User>): Map<String, List<UIText>> {
val memberNameList = members.groupBy { it.domain }.mapValues {
it.value.map { userId ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class UIParticipantMapper @Inject constructor(
connectionState = connectionState,
unavailable = unavailable,
isDeleted = (user is OtherUser && user.deleted),
botService = (user as? OtherUser)?.botService
botService = (user as? OtherUser)?.botService,
isDefederated = (user is OtherUser && user.defederated)
)
}

Expand All @@ -66,7 +67,8 @@ class UIParticipantMapper @Inject constructor(
membership = userTypeMapper.toMembership(userSummary.userType),
unavailable = !userSummary.isUserDeleted && userSummary.userName.orEmpty().isEmpty(),
isDeleted = userSummary.isUserDeleted,
isSelf = isSelfUser
isSelf = isSelfUser,
isDefederated = false
)
}

Expand All @@ -80,7 +82,8 @@ class UIParticipantMapper @Inject constructor(
unavailable = !userSummary.isUserDeleted && userSummary.userName.orEmpty().isEmpty(),
isDeleted = userSummary.isUserDeleted,
isSelf = false,
readReceiptDate = date
readReceiptDate = date,
isDefederated = false
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ class MigrationMapper @Inject constructor() {
userType = userType,
availabilityStatus = mapUserAvailabilityStatus(scalaUserData.availability),
botService = botService,
deleted = scalaUserData.deleted
deleted = scalaUserData.deleted,
defederated = false
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -56,7 +57,10 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextDecoration
import com.wire.android.R
import com.wire.android.ui.common.button.WireSecondaryButton
import com.wire.android.ui.common.colorsScheme
Expand All @@ -69,11 +73,13 @@ import com.wire.android.ui.home.conversations.model.UIMessageContent.SystemMessa
import com.wire.android.ui.theme.WireTheme
import com.wire.android.ui.theme.wireColorScheme
import com.wire.android.ui.theme.wireTypography
import com.wire.android.util.CustomTabsHelper
import com.wire.android.util.ui.PreviewMultipleThemes
import com.wire.android.util.ui.UIText
import com.wire.android.util.ui.annotatedText
import com.wire.android.util.ui.toUIText

@Suppress("ComplexMethod")
@Composable
fun SystemMessageItem(
message: UIMessage.System,
Expand Down Expand Up @@ -189,6 +195,31 @@ fun SystemMessageItem(
onCancelClick = remember { { onFailedMessageCancelClicked(message.header.messageId) } }
)
}
if (message.messageContent.learnMoreResId != null) {
val learnMoreLink = stringResource(id = message.messageContent.learnMoreResId)
val learnMoreText = stringResource(id = R.string.label_learn_more)
val annotatedString = buildAnnotatedString {
append(learnMoreText)
addStyle(
style = SpanStyle(
color = MaterialTheme.colorScheme.primary,
textDecoration = TextDecoration.Underline
),
start = 0,
end = learnMoreText.length
)
}
ClickableText(
text = annotatedString,
onClick = {
CustomTabsHelper.launchUrl(
context,
learnMoreLink
)
},
style = MaterialTheme.wireTypography.body01,
)
}
}
}
if (message.messageContent is SystemMessage.ConversationMessageCreated) {
Expand Down Expand Up @@ -232,6 +263,8 @@ private fun getColorFilter(message: SystemMessage): ColorFilter? {
is SystemMessage.ConversationMessageCreated,
is SystemMessage.ConversationStartedWithMembers,
is SystemMessage.ConversationMessageTimerDeactivated,
is SystemMessage.FederationMemberRemoved,
is SystemMessage.FederationStopped,
is SystemMessage.MLSWrongEpochWarning -> ColorFilter.tint(colorsScheme().onBackground)
}
}
Expand Down Expand Up @@ -368,10 +401,81 @@ fun PreviewSystemMessageFailedToAddMultiple() {
}
}

@PreviewMultipleThemes
@Composable
fun PreviewSystemMessageFederationMemberRemoved() {
WireTheme {
SystemMessageItem(
message = mockMessageWithKnock.copy(
messageContent = SystemMessage.FederationMemberRemoved(
listOf(
"Barbara Cotolina".toUIText(),
"Albert Lewis".toUIText()
)
)
)
)
}
}

@PreviewMultipleThemes
@Composable
fun PreviewSystemMessageFederationMemberRemoved7Users() {
WireTheme {
SystemMessageItem(
message = mockMessageWithKnock.copy(
messageContent = SystemMessage.FederationMemberRemoved(
listOf(
"Albert Lewis".toUIText(),
"Bert Strunk".toUIText(),
"Claudia Schiffer".toUIText(),
"Dorothee Friedrich".toUIText(),
"Erich Weinert".toUIText(),
"Frieda Kahlo".toUIText(),
"Gudrun Gut".toUIText()
)
)
)
)
}
}

@PreviewMultipleThemes
@Composable
fun PreviewSystemMessageFederationStopped() {
WireTheme {
SystemMessageItem(
message = mockMessageWithKnock.copy(
messageContent = SystemMessage.FederationStopped(
listOf(
"bella.wire.link",
"foma.wire.link"
)
)
)
)
}
}

@PreviewMultipleThemes
@Composable
fun PreviewSystemMessageFederationStoppedSelf() {
WireTheme {
SystemMessageItem(
message = mockMessageWithKnock.copy(
messageContent = SystemMessage.FederationStopped(
listOf("foma.wire.link")
)
)
)
}
}

private val SystemMessage.expandable
get() = when (this) {
is SystemMessage.MemberAdded -> this.memberNames.size > EXPANDABLE_THRESHOLD
is SystemMessage.MemberRemoved -> this.memberNames.size > EXPANDABLE_THRESHOLD
is SystemMessage.FederationMemberRemoved -> this.memberNames.size > EXPANDABLE_THRESHOLD
is SystemMessage.MemberJoined -> false
is SystemMessage.MemberLeft -> false
is SystemMessage.MissedCall -> false
Expand All @@ -389,6 +493,7 @@ private val SystemMessage.expandable
is SystemMessage.ConversationStartedWithMembers -> this.memberNames.size > EXPANDABLE_THRESHOLD
is SystemMessage.MemberFailedToAdd -> this.usersCount > SINGLE_EXPANDABLE_THRESHOLD
is SystemMessage.ConversationDegraded -> false
is SystemMessage.FederationStopped -> false
}

private fun List<String>.toUserNamesListString(res: Resources): String = when {
Expand Down Expand Up @@ -435,6 +540,11 @@ fun SystemMessage.annotatedString(
memberNames.limitUserNamesList(res, if (expanded) memberNames.size else EXPANDABLE_THRESHOLD).toUserNamesListString(res)
)

is SystemMessage.FederationMemberRemoved ->
arrayOf(
memberNames.limitUserNamesList(res, if (expanded) memberNames.size else EXPANDABLE_THRESHOLD).toUserNamesListString(res)
)

is SystemMessage.MemberJoined -> arrayOf(author.asString(res))
is SystemMessage.MemberLeft -> arrayOf(author.asString(res))
is SystemMessage.MissedCall -> arrayOf(author.asString(res))
Expand Down Expand Up @@ -465,6 +575,8 @@ fun SystemMessage.annotatedString(
res, normalStyle, boldStyle, normalColor, boldColor, errorColor, isErrorString,
if (usersCount > SINGLE_EXPANDABLE_THRESHOLD) expanded else true
)

is SystemMessage.FederationStopped -> domainList.toTypedArray()
}

return res.annotatedText(stringResId, normalStyle, boldStyle, normalColor, boldColor, errorColor, isErrorString, *args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ data class UIParticipant(
val unavailable: Boolean = false,
val isDeleted: Boolean = false,
val readReceiptDate: Instant? = null,
val botService: BotService? = null
val botService: BotService? = null,
val isDefederated: Boolean = false
)
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ sealed class UIMessageContent {
@DrawableRes val iconResId: Int?,
@StringRes open val stringResId: Int,
val isSmallIcon: Boolean = true,
val additionalContent: String = ""
val additionalContent: String = "",
@StringRes val learnMoreResId: Int? = null
) : UIMessageContent() {

data class Knock(val author: UIText) : SystemMessage(
Expand Down Expand Up @@ -319,6 +320,29 @@ sealed class UIMessageContent {
}
)

data class FederationMemberRemoved(
val memberNames: List<UIText>
) : SystemMessage(
R.drawable.ic_minus,
if (memberNames.size > 1) {
R.string.label_system_message_federation_many_member_removed
} else {
R.string.label_system_message_federation_one_member_removed
}
)

data class FederationStopped(
val domainList: List<String>
) : SystemMessage(
R.drawable.ic_info,
if (domainList.size > 1) {
R.string.label_system_message_federation_conection_removed
} else {
R.string.label_system_message_federation_removed
},
learnMoreResId = R.string.url_federation_support
)

sealed class MissedCall(
open val author: UIText,
@StringRes override val stringResId: Int
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<string name="url_message_details_offline_backends_learn_more" translatable="false">https://support.wire.com/hc/articles/9357718008093-Backend</string>
<string name="url_self_client_verification_learn_more" translatable="false">https://support.wire.com/hc/en-us/articles/207693005-How-can-I-verify-my-own-devices-</string>
<string name="url_self_client_fingerprint_learn_more" translatable="false">https://support.wire.com/hc/en-us/articles/207692235-How-can-I-compare-key-fingerprints-</string>
<string name="url_federation_support" translatable="false">https://support.wire.com/hc/categories/4719917054365-Federation</string>
<!-- Navigation -->
<string name="vault_screen_title">Vault</string>
<string name="archive_screen_title">Archive</string>
Expand Down Expand Up @@ -554,6 +555,10 @@
<string name="label_system_message_removed_by_self">%1$s removed %2$s from the conversation</string>
<string name="label_system_message_added_by_other">%1$s added %2$s to the conversation</string>
<string name="label_system_message_removed_by_other">%1$s removed %2$s from the conversation</string>
<string name="label_system_message_federation_one_member_removed">%1$s was removed from the conversation</string>
<string name="label_system_message_federation_many_member_removed">%1$s were removed from the conversation</string>
<string name="label_system_message_federation_removed">**Your backend** stopped federating with %1$s.</string>
<string name="label_system_message_federation_conection_removed">The backends %1$s and %2$s stopped federating.</string>
<string name="label_system_message_left_the_conversation_by_self">%1$s left the conversation</string>
<string name="label_system_message_left_the_conversation_by_other">%1$s left the conversation</string>
<string name="label_system_message_other_called">%1$s called</string>
Expand Down
3 changes: 2 additions & 1 deletion app/src/test/kotlin/com/wire/android/framework/TestUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ object TestUser {
availabilityStatus = UserAvailabilityStatus.AVAILABLE,
userType = UserType.INTERNAL,
botService = null,
deleted = false
deleted = false,
defederated = false
)
val MEMBER_SELF = MemberDetails(SELF_USER, Member.Role.Admin)
val MEMBER_OTHER = MemberDetails(OTHER_USER, Member.Role.Member)
Expand Down
Loading

0 comments on commit 1138c26

Please sign in to comment.