Skip to content

Commit

Permalink
Handle events of type "m.room.server_acl" (#890)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Nov 12, 2020
1 parent 812b1f7 commit 413a556
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Features ✨:

Improvements 🙌:
- Open an existing DM instead of creating a new one (#2319)
- Handle events of type "m.room.server_acl" (#890)

Bugfix 🐛:
- Fix issue when restoring draft after sharing (#2287)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ fun CharSequence.ensurePrefix(prefix: CharSequence): CharSequence {
else -> "$prefix$this"
}
}

/**
* Append a new line and then the provided string
*/
fun StringBuilder.appendNl(str: String) = append("\n").append(str)
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ object EventType {
const val STATE_ROOM_RELATED_GROUPS = "m.room.related_groups"
const val STATE_ROOM_PINNED_EVENT = "m.room.pinned_events"
const val STATE_ROOM_ENCRYPTION = "m.room.encryption"
const val STATE_ROOM_SERVER_ACL = "m.room.server_acl"

// Call Events
const val CALL_INVITE = "m.call.invite"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2020 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.api.session.room.model

import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass

/**
* Class representing the EventType.STATE_ROOM_SERVER_ACL state event content
* Ref: https://matrix.org/docs/spec/client_server/r0.6.1#m-room-server-acl
*/
@JsonClass(generateAdapter = true)
data class RoomServerAclContent(
/**
* True to allow server names that are IP address literals. False to deny.
* Defaults to true if missing or otherwise not a boolean.
* This is strongly recommended to be set to false as servers running with IP literal names are strongly
* discouraged in order to require legitimate homeservers to be backed by a valid registered domain name.
*/
@Json(name = "allow_ip_literals")
val allowIpLiterals: Boolean = true,

/**
* The server names to allow in the room, excluding any port information. Wildcards may be used to cover
* a wider range of hosts, where * matches zero or more characters and ? matches exactly one character.
*
* This defaults to an empty list when not provided, effectively disallowing every server.
*/
@Json(name = "allow")
val allowList: List<String> = emptyList(),

/**
* The server names to disallow in the room, excluding any port information. Wildcards may be used to cover
* a wider range of hosts, where * matches zero or more characters and ? matches exactly one character.
*
* This defaults to an empty list when not provided.
*/
@Json(name = "deny")
val denyList: List<String> = emptyList()

) {
companion object {
const val ALL = "*"
}
}
17 changes: 17 additions & 0 deletions matrix-sdk-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@
<string name="notice_room_update_by_you">You upgraded this room.</string>
<string name="notice_direct_room_update">%s upgraded here.</string>
<string name="notice_direct_room_update_by_you">You upgraded here.</string>
<string name="notice_room_server_acl_set_title">%s set the server ACLs for this room:</string>
<string name="notice_room_server_acl_set_title_by_you">You set the server ACLs for this room:</string>
<string name="notice_room_server_acl_set_banned">• Server matching %s are banned.</string>
<string name="notice_room_server_acl_set_allowed">• Server matching %s are allowed.</string>
<string name="notice_room_server_acl_set_ip_literals_allowed">• Server matching IP literals are allowed.</string>
<string name="notice_room_server_acl_set_ip_literals_not_allowed">• Server matching IP literals are banned.</string>

<string name="notice_room_server_acl_updated_title">%s changed the server ACLs for this room:</string>
<string name="notice_room_server_acl_updated_title_by_you">You changed the server ACLs for this room:</string>
<string name="notice_room_server_acl_updated_banned">• Server matching %s are now banned.</string>
<string name="notice_room_server_acl_updated_was_banned">• Server matching %s were removed from the ban list.</string>
<string name="notice_room_server_acl_updated_allowed">• Server matching %s are now allowed.</string>
<string name="notice_room_server_acl_updated_was_allowed">• Server matching %s were removed from the allowed list.</string>
<string name="notice_room_server_acl_updated_ip_literals_allowed">• Server matching IP literals are now allowed.</string>
<string name="notice_room_server_acl_updated_ip_literals_not_allowed">• Server matching IP literals are now banned.</string>
<string name="notice_room_server_acl_updated_no_change">No change.</string>
<string name="notice_room_server_acl_allow_is_empty">🎉 All servers are banned from participating! This room can no longer be used.</string>

<string name="notice_requested_voip_conference">%1$s requested a VoIP conference</string>
<string name="notice_requested_voip_conference_by_you">You requested a VoIP conference</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class MessageActionsViewModel @AssistedInject constructor(@Assisted
EventType.STATE_ROOM_ALIASES,
EventType.STATE_ROOM_CANONICAL_ALIAS,
EventType.STATE_ROOM_HISTORY_VISIBILITY,
EventType.STATE_ROOM_SERVER_ACL,
EventType.CALL_INVITE,
EventType.CALL_CANDIDATES,
EventType.CALL_HANGUP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class TimelineItemFactory @Inject constructor(private val messageItemFactory: Me
EventType.STATE_ROOM_CANONICAL_ALIAS,
EventType.STATE_ROOM_JOIN_RULES,
EventType.STATE_ROOM_HISTORY_VISIBILITY,
EventType.STATE_ROOM_SERVER_ACL,
EventType.STATE_ROOM_GUEST_ACCESS,
EventType.STATE_ROOM_WIDGET_LEGACY,
EventType.STATE_ROOM_WIDGET,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package im.vector.app.features.home.room.detail.timeline.format
import im.vector.app.ActiveSessionDataSource
import im.vector.app.R
import im.vector.app.core.resources.StringProvider
import org.matrix.android.sdk.api.extensions.appendNl
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType
Expand All @@ -35,6 +36,7 @@ import org.matrix.android.sdk.api.session.room.model.RoomJoinRules
import org.matrix.android.sdk.api.session.room.model.RoomJoinRulesContent
import org.matrix.android.sdk.api.session.room.model.RoomMemberContent
import org.matrix.android.sdk.api.session.room.model.RoomNameContent
import org.matrix.android.sdk.api.session.room.model.RoomServerAclContent
import org.matrix.android.sdk.api.session.room.model.RoomSummary
import org.matrix.android.sdk.api.session.room.model.RoomThirdPartyInviteContent
import org.matrix.android.sdk.api.session.room.model.RoomTopicContent
Expand Down Expand Up @@ -72,6 +74,7 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
EventType.STATE_ROOM_CANONICAL_ALIAS -> formatRoomCanonicalAliasEvent(timelineEvent.root, timelineEvent.senderInfo.disambiguatedDisplayName)
EventType.STATE_ROOM_HISTORY_VISIBILITY ->
formatRoomHistoryVisibilityEvent(timelineEvent.root, timelineEvent.senderInfo.disambiguatedDisplayName, rs)
EventType.STATE_ROOM_SERVER_ACL -> formatRoomServerAclEvent(timelineEvent.root, timelineEvent.senderInfo.disambiguatedDisplayName)
EventType.STATE_ROOM_GUEST_ACCESS -> formatRoomGuestAccessEvent(timelineEvent.root, timelineEvent.senderInfo.disambiguatedDisplayName, rs)
EventType.STATE_ROOM_ENCRYPTION -> formatRoomEncryptionEvent(timelineEvent.root, timelineEvent.senderInfo.disambiguatedDisplayName)
EventType.STATE_ROOM_WIDGET,
Expand Down Expand Up @@ -383,6 +386,77 @@ class NoticeEventFormatter @Inject constructor(private val activeSessionDataSour
}
}

private fun formatRoomServerAclEvent(event: Event, senderName: String?): String? {
val eventContent = event.getClearContent().toModel<RoomServerAclContent>() ?: return null
val prevEventContent = event.resolvedPrevContent()?.toModel<RoomServerAclContent>()

return buildString {
// Title
append(if (prevEventContent == null) {
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_server_acl_set_title_by_you)
} else {
sp.getString(R.string.notice_room_server_acl_set_title, senderName)
}
} else {
if (event.isSentByCurrentUser()) {
sp.getString(R.string.notice_room_server_acl_updated_title_by_you)
} else {
sp.getString(R.string.notice_room_server_acl_updated_title, senderName)
}
})
// Details
if (eventContent.allowList.isEmpty()) {
// Special case for stuck room
append("\n")
append(sp.getString(R.string.notice_room_server_acl_allow_is_empty))
} else {
if (prevEventContent == null) {
eventContent.allowList.forEach { appendNl(sp.getString(R.string.notice_room_server_acl_set_allowed, it)) }
eventContent.denyList.forEach { appendNl(sp.getString(R.string.notice_room_server_acl_set_banned, it)) }
if (eventContent.allowIpLiterals) {
appendNl(sp.getString(R.string.notice_room_server_acl_set_ip_literals_allowed))
} else {
appendNl(sp.getString(R.string.notice_room_server_acl_set_ip_literals_not_allowed))
}
} else {
// Display only diff
var hasChanged = false
// New allowed servers
(eventContent.allowList - prevEventContent.allowList)
.also { hasChanged = hasChanged || it.isNotEmpty() }
.forEach { appendNl(sp.getString(R.string.notice_room_server_acl_updated_allowed, it)) }
// Removed allowed servers
(prevEventContent.allowList - eventContent.allowList)
.also { hasChanged = hasChanged || it.isNotEmpty() }
.forEach { appendNl(sp.getString(R.string.notice_room_server_acl_updated_was_allowed, it)) }
// New denied servers
(eventContent.denyList - prevEventContent.denyList)
.also { hasChanged = hasChanged || it.isNotEmpty() }
.forEach { appendNl(sp.getString(R.string.notice_room_server_acl_updated_banned, it)) }
// Removed denied servers
(prevEventContent.denyList - eventContent.denyList)
.also { hasChanged = hasChanged || it.isNotEmpty() }
.forEach { appendNl(sp.getString(R.string.notice_room_server_acl_updated_was_banned, it)) }


if (prevEventContent.allowIpLiterals != eventContent.allowIpLiterals) {
hasChanged = true
if (eventContent.allowIpLiterals) {
appendNl(sp.getString(R.string.notice_room_server_acl_updated_ip_literals_allowed))
} else {
appendNl(sp.getString(R.string.notice_room_server_acl_updated_ip_literals_not_allowed))
}
}

if (!hasChanged) {
appendNl(sp.getString(R.string.notice_room_server_acl_updated_no_change))
}
}
}
}
}

private fun formatRoomCanonicalAliasEvent(event: Event, senderName: String?): String? {
val eventContent: RoomCanonicalAliasContent? = event.getClearContent().toModel()
val canonicalAlias = eventContent?.canonicalAlias
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object TimelineDisplayableEvents {
EventType.STATE_ROOM_ALIASES,
EventType.STATE_ROOM_CANONICAL_ALIAS,
EventType.STATE_ROOM_HISTORY_VISIBILITY,
EventType.STATE_ROOM_SERVER_ACL,
EventType.STATE_ROOM_POWER_LEVELS,
EventType.CALL_INVITE,
EventType.CALL_HANGUP,
Expand Down

0 comments on commit 413a556

Please sign in to comment.