From f9b69fff9ebb24c4f8d3c08c723fe3295d066b4e Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Thu, 16 Dec 2021 17:17:29 +0100 Subject: [PATCH 1/5] Add tagged event entry in room account data types --- .../sdk/api/session/room/accountdata/RoomAccountDataTypes.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/accountdata/RoomAccountDataTypes.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/accountdata/RoomAccountDataTypes.kt index 96eb86c0d6..312fb7e164 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/accountdata/RoomAccountDataTypes.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/accountdata/RoomAccountDataTypes.kt @@ -21,4 +21,5 @@ object RoomAccountDataTypes { const val EVENT_TYPE_TAG = "m.tag" const val EVENT_TYPE_FULLY_READ = "m.fully_read" const val EVENT_TYPE_SPACE_ORDER = "org.matrix.msc3230.space_order" // m.space_order + const val EVENT_TYPE_TAGGED_EVENTS = "m.tagged_events" } From 706c5b2b32ee123564e850e108529842b46f9e0d Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Thu, 16 Dec 2021 17:47:43 +0100 Subject: [PATCH 2/5] Add tagged events model --- changelog.d/4753.feature | 1 + .../room/tagged_events/TaggedEventsContent.kt | 85 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 changelog.d/4753.feature create mode 100644 matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/tagged_events/TaggedEventsContent.kt diff --git a/changelog.d/4753.feature b/changelog.d/4753.feature new file mode 100644 index 0000000000..1a474f0f5f --- /dev/null +++ b/changelog.d/4753.feature @@ -0,0 +1 @@ +Support tagged events in Room Account Data (MSC2437) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/tagged_events/TaggedEventsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/tagged_events/TaggedEventsContent.kt new file mode 100644 index 0000000000..fdfa28c00a --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/tagged_events/TaggedEventsContent.kt @@ -0,0 +1,85 @@ +/* + * Copyright 2021 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.internal.session.room.tagged_events + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +/** + * Keys are event IDs, values are event information. + */ +typealias TaggedEvent = Map + +/** + * Keys are tagged event names (eg. m.favourite), values are the related events. + */ +typealias TaggedEvents = Map + +/** + * Class used to parse the content of a m.tagged_events type event. + * This kind of event defines the tagged events in a room. + * + * The content of this event is a tags key whose value is an object mapping the name of each tag + * to another object. The JSON object associated with each tag is an object where the keys are the + * event IDs and values give information about the events. + * + * Ref: https://github.com/matrix-org/matrix-doc/pull/2437 + */ +@JsonClass(generateAdapter = true) +data class TaggedEventsContent( + var tags: TaggedEvents = emptyMap() +) { + val favouriteEvents + get() = tags[TAG_FAVOURITE].orEmpty() + + val hiddenEvents + get() = tags[TAG_HIDDEN].orEmpty() + + fun tagEvent(eventId: String, info: TaggedEventInfo, tag: String) { + val tagMap = HashMap(tags[tag] ?: emptyMap()) + tagMap[eventId] = info + + val updatedTags = HashMap>(tags) + updatedTags[tag] = tagMap + tags = updatedTags + } + + fun untagEvent(eventId: String, tag: String) { + val tagMap = HashMap(tags[tag] ?: emptyMap()) + tagMap.remove(eventId) + + val updatedTags = HashMap>(tags) + updatedTags[tag] = tagMap + tags = updatedTags + } + + companion object { + const val TAGS_KEY = "tags" + const val TAG_FAVOURITE = "m.favourite" + const val TAG_HIDDEN = "m.hidden" + } +} + +data class TaggedEventInfo( + var keywords: List? = null, + + @Json(name = "origin_server_ts") + val originServerTs: Long? = null, + + @Json(name = "tagged_at") + var taggedAt: Long? = null +) From 3bd2b77af35380b5587047dd5a4696617b6ad524 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Fri, 17 Dec 2021 15:55:26 +0100 Subject: [PATCH 3/5] Fix package name --- .../room/{tagged_events => taggedevents}/TaggedEventsContent.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/{tagged_events => taggedevents}/TaggedEventsContent.kt (97%) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/tagged_events/TaggedEventsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt similarity index 97% rename from matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/tagged_events/TaggedEventsContent.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt index fdfa28c00a..318e8f8ea2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/tagged_events/TaggedEventsContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.matrix.android.sdk.internal.session.room.tagged_events +package org.matrix.android.sdk.internal.session.room.taggedevents import com.squareup.moshi.Json import com.squareup.moshi.JsonClass From daadf72ffedbc99378f6235d33532c9f83e63076 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Tue, 4 Jan 2022 17:55:51 +0100 Subject: [PATCH 4/5] Code review --- .../room/taggedevents/TaggedEventsContent.kt | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt index 318e8f8ea2..58ca25f1e2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt @@ -41,6 +41,7 @@ typealias TaggedEvents = Map */ @JsonClass(generateAdapter = true) data class TaggedEventsContent( + @Json(name = "tags") var tags: TaggedEvents = emptyMap() ) { val favouriteEvents @@ -50,36 +51,28 @@ data class TaggedEventsContent( get() = tags[TAG_HIDDEN].orEmpty() fun tagEvent(eventId: String, info: TaggedEventInfo, tag: String) { - val tagMap = HashMap(tags[tag] ?: emptyMap()) - tagMap[eventId] = info - - val updatedTags = HashMap>(tags) - updatedTags[tag] = tagMap - tags = updatedTags + val taggedEvents = tags[tag].orEmpty().plus(eventId to info) + tags = tags.plus(tag to taggedEvents) } fun untagEvent(eventId: String, tag: String) { - val tagMap = HashMap(tags[tag] ?: emptyMap()) - tagMap.remove(eventId) - - val updatedTags = HashMap>(tags) - updatedTags[tag] = tagMap - tags = updatedTags + val taggedEvents = tags[tag]?.minus(eventId).orEmpty() + tags = tags.plus(tag to taggedEvents) } companion object { - const val TAGS_KEY = "tags" const val TAG_FAVOURITE = "m.favourite" const val TAG_HIDDEN = "m.hidden" } } data class TaggedEventInfo( - var keywords: List? = null, + @Json(name = "keywords") + val keywords: List? = null, @Json(name = "origin_server_ts") val originServerTs: Long? = null, @Json(name = "tagged_at") - var taggedAt: Long? = null + val taggedAt: Long? = null ) From 8d7b2ab5d8c9856e0ef52da8639df08c39b23a44 Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Thu, 6 Jan 2022 15:32:14 +0100 Subject: [PATCH 5/5] Add missing json class annotation --- .../internal/session/room/taggedevents/TaggedEventsContent.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt index 58ca25f1e2..1b19d27e1d 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/taggedevents/TaggedEventsContent.kt @@ -66,6 +66,7 @@ data class TaggedEventsContent( } } +@JsonClass(generateAdapter = true) data class TaggedEventInfo( @Json(name = "keywords") val keywords: List? = null,