|
| 1 | +/* |
| 2 | + * Copyright 2021 The Matrix.org Foundation C.I.C. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.matrix.android.sdk.internal.session.room.tagged_events |
| 18 | + |
| 19 | +import com.squareup.moshi.Json |
| 20 | +import com.squareup.moshi.JsonClass |
| 21 | + |
| 22 | +/** |
| 23 | + * Keys are event IDs, values are event information. |
| 24 | + */ |
| 25 | +typealias TagEvent = Map<String, TaggedEventInfo> |
| 26 | + |
| 27 | +/** |
| 28 | + * Keys are tag names (eg. m.favourite), values are the related events. |
| 29 | + */ |
| 30 | +typealias TagEvents = Map<String, TagEvent> |
| 31 | + |
| 32 | +/** |
| 33 | + * Class used to parse the content of a m.tagged_events type event. |
| 34 | + * This kind of event defines the tagged events in a room. |
| 35 | + * |
| 36 | + * The content of this event is a tags key whose value is an object mapping the name of each tag |
| 37 | + * to another object. The JSON object associated with each tag is an object where the keys are the |
| 38 | + * event IDs and values give information about the events. |
| 39 | + * |
| 40 | + * Ref: https://github.com./matrix-org/matrix-doc/pull/2437 |
| 41 | + */ |
| 42 | +@JsonClass(generateAdapter = true) |
| 43 | +data class TaggedEventsContent( |
| 44 | + var tags: TagEvents = emptyMap() |
| 45 | +) { |
| 46 | + val favouriteEvents |
| 47 | + get() = tags[TAG_FAVOURITE].orEmpty() |
| 48 | + |
| 49 | + val hiddenEvents |
| 50 | + get() = tags[TAG_HIDDEN].orEmpty() |
| 51 | + |
| 52 | + fun tagEvent(eventId: String, info: TaggedEventInfo, tag: String) { |
| 53 | + val tagMap = HashMap<String, TaggedEventInfo>(tags[tag] ?: emptyMap()) |
| 54 | + tagMap[eventId] = info |
| 55 | + |
| 56 | + val updatedTags = HashMap<String, Map<String, TaggedEventInfo>>(tags) |
| 57 | + updatedTags[tag] = tagMap |
| 58 | + tags = updatedTags |
| 59 | + } |
| 60 | + |
| 61 | + fun untagEvent(eventId: String, tag: String) { |
| 62 | + val tagMap = HashMap<String, TaggedEventInfo>(tags[tag] ?: emptyMap()) |
| 63 | + tagMap.remove(eventId) |
| 64 | + |
| 65 | + val updatedTags = HashMap<String, Map<String, TaggedEventInfo>>(tags) |
| 66 | + updatedTags[tag] = tagMap |
| 67 | + tags = updatedTags |
| 68 | + } |
| 69 | + |
| 70 | + companion object { |
| 71 | + const val TAGS_KEY = "tags" |
| 72 | + const val TAG_FAVOURITE = "m.favourite" |
| 73 | + const val TAG_HIDDEN = "m.hidden" |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +data class TaggedEventInfo( |
| 78 | + var keywords: List<String>? = null, |
| 79 | + |
| 80 | + @Json(name = "origin_server_ts") |
| 81 | + val originServerTs: Long? = null, |
| 82 | + |
| 83 | + @Json(name = "tagged_at") |
| 84 | + var taggedAt: Long? = null |
| 85 | +) |
0 commit comments