Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tagged events in Room Account Data (MSC2437) #4753

Merged
merged 6 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4753.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Support tagged events in Room Account Data (MSC2437)
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.taggedevents

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

/**
* Keys are event IDs, values are event information.
*/
typealias TaggedEvent = Map<String, TaggedEventInfo>

/**
* Keys are tagged event names (eg. m.favourite), values are the related events.
*/
typealias TaggedEvents = Map<String, TaggedEvent>

/**
* 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(
@Json(name = "tags")
var tags: TaggedEvents = emptyMap()
bmarty marked this conversation as resolved.
Show resolved Hide resolved
) {
val favouriteEvents
get() = tags[TAG_FAVOURITE].orEmpty()

val hiddenEvents
get() = tags[TAG_HIDDEN].orEmpty()

fun tagEvent(eventId: String, info: TaggedEventInfo, tag: String) {
val taggedEvents = tags[tag].orEmpty().plus(eventId to info)
tags = tags.plus(tag to taggedEvents)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to rewrite to

    fun tagEvent(eventId: String, tag: String, info: TaggedEventInfo) {
        val tagMap = tags[tag].orEmpty().toMutableMap()
                .also { it[eventId] = info }

        tags = tags.toMutableMap().also { it[tag] = tagMap }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a better optimization :)

Copy link
Member

@bmarty bmarty Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL the plus() and minus() operator extensions on Map. Note that it can be written using + and - but for code clarity I prefer what you have done. Thanks!


fun untagEvent(eventId: String, tag: String) {
val taggedEvents = tags[tag]?.minus(eventId).orEmpty()
tags = tags.plus(tag to taggedEvents)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to rewrite to:

    fun untagEvent(eventId: String, tag: String) {
        val tagMap = tags[tag]?.toMutableMap() ?: return
        tagMap.remove(eventId)

        tags = tags.toMutableMap().also { it[tag] = tagMap }
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


companion object {
const val TAG_FAVOURITE = "m.favourite"
const val TAG_HIDDEN = "m.hidden"
}
}

@JsonClass(generateAdapter = true)
data class TaggedEventInfo(
@Json(name = "keywords")
val keywords: List<String>? = null,

@Json(name = "origin_server_ts")
val originServerTs: Long? = null,

@Json(name = "tagged_at")
val taggedAt: Long? = null
)