Skip to content

Commit 45f6bfc

Browse files
committed
Added lab feature to pin/unpin messages
1 parent dba8aae commit 45f6bfc

40 files changed

+590
-22
lines changed

library/ui-strings/src/main/res/values/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@
373373
<string name="action_sign_out_confirmation_simple">Are you sure you want to sign out?</string>
374374
<string name="action_voice_call">Voice Call</string>
375375
<string name="action_video_call">Video Call</string>
376+
<string name="action_open_pinned_messages">Open Pinned Messages</string>
376377
<string name="action_view_threads">View Threads</string>
377378
<string name="action_mark_all_as_read">Mark all as read</string>
378379
<string name="action_quick_reply">Quick reply</string>
@@ -801,6 +802,12 @@
801802
<string name="threads_labs_enable_notice_title">Threads Beta</string>
802803
<string name="threads_labs_enable_notice_message">Your homeserver does not currently support threads, so this feature may be unreliable. Some threaded messages may not be reliably available. %sDo you want to enable threads anyway?</string>
803804

805+
<!-- Pinning -->
806+
<string name="pinning_message">Pin</string>
807+
<string name="unpinning_message">Unpin</string>
808+
<string name="pinned_messages_timeline_title">Pinned Messages</string>
809+
<string name="user_pinned_message">%1$s pinned a message.</string>
810+
<string name="user_unpinned_message">%1$s unpinned a message.</string>
804811

805812
<!-- Search -->
806813
<string name="search_hint">Search</string>
@@ -3032,6 +3039,7 @@
30323039

30333040
<string name="labs_auto_report_uisi">Auto Report Decryption Errors.</string>
30343041
<string name="labs_auto_report_uisi_desc">Your system will automatically send logs when an unable to decrypt error occurs</string>
3042+
<string name="labs_enable_pinned_messages">Enable Pinned Messages</string>
30353043
<string name="labs_enable_thread_messages">Enable Thread Messages</string>
30363044
<string name="labs_enable_thread_messages_desc">Note: app will be restarted</string>
30373045
<string name="settings_show_latest_profile">Show latest user info</string>

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/Event.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageContent
3030
import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent
3131
import org.matrix.android.sdk.api.session.room.model.message.MessageType
3232
import org.matrix.android.sdk.api.session.room.model.message.asMessageAudioEvent
33+
import org.matrix.android.sdk.api.session.room.model.pinnedmessages.PinnedEventsStateContent
3334
import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent
3435
import org.matrix.android.sdk.api.session.room.model.relation.isReply
3536
import org.matrix.android.sdk.api.session.room.model.relation.shouldRenderInThread
@@ -447,3 +448,11 @@ fun Event.supportsNotification() =
447448

448449
fun Event.isContentReportable() =
449450
this.getClearType() in EventType.MESSAGE + EventType.STATE_ROOM_BEACON_INFO.values
451+
452+
fun Event.getIdsOfPinnedEvents(): MutableList<String>? {
453+
return getClearContent()?.toModel<PinnedEventsStateContent>()?.eventIds
454+
}
455+
456+
fun Event.getPreviousIdsOfPinnedEvents(): MutableList<String>? {
457+
return resolvedPrevContent()?.toModel<PinnedEventsStateContent>()?.eventIds
458+
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/events/model/EventType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ object EventType {
4545
const val STATE_ROOM_NAME = "m.room.name"
4646
const val STATE_ROOM_TOPIC = "m.room.topic"
4747
const val STATE_ROOM_AVATAR = "m.room.avatar"
48+
const val STATE_ROOM_PINNED_EVENT = "m.room.pinned_events"
4849
const val STATE_ROOM_MEMBER = "m.room.member"
4950
const val STATE_ROOM_THIRD_PARTY_INVITE = "m.room.third_party_invite"
5051
const val STATE_ROOM_CREATE = "m.room.create"
@@ -67,7 +68,6 @@ object EventType {
6768
const val STATE_ROOM_CANONICAL_ALIAS = "m.room.canonical_alias"
6869
const val STATE_ROOM_HISTORY_VISIBILITY = "m.room.history_visibility"
6970
const val STATE_ROOM_RELATED_GROUPS = "m.room.related_groups"
70-
const val STATE_ROOM_PINNED_EVENT = "m.room.pinned_events"
7171
const val STATE_ROOM_ENCRYPTION = "m.room.encryption"
7272
const val STATE_ROOM_SERVER_ACL = "m.room.server_acl"
7373

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.api.session.room.model.pinnedmessages
18+
19+
import com.squareup.moshi.Json
20+
import com.squareup.moshi.JsonClass
21+
22+
/**
23+
* Class representing a pinned event content.
24+
*/
25+
@JsonClass(generateAdapter = true)
26+
data class PinnedEventsStateContent(
27+
@Json(name = "pinned") val eventIds: MutableList<String>
28+
)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/state/StateService.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ interface StateService {
6666
*/
6767
suspend fun deleteAvatar()
6868

69+
/**
70+
* Pin a message of the room.
71+
*/
72+
suspend fun pinMessage(eventIds: MutableList<String>)
73+
6974
/**
7075
* Send a state event to the room.
7176
* @param eventType The type of event to send.
@@ -103,6 +108,16 @@ interface StateService {
103108
*/
104109
fun getStateEventsLive(eventTypes: Set<String>, stateKey: QueryStateEventValue): LiveData<List<Event>>
105110

111+
/**
112+
* Get state event containing the IDs of pinned events of the room
113+
*/
114+
fun getPinnedEventsState(): Event?
115+
116+
/**
117+
* Tells if an event is a pinned message
118+
*/
119+
fun isPinned(eventId: String): Boolean?
120+
106121
suspend fun setJoinRulePublic()
107122
suspend fun setJoinRuleInviteOnly()
108123
suspend fun setJoinRuleRestricted(allowList: List<String>)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/Timeline.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ interface Timeline {
4343
/**
4444
* This must be called before any other method after creating the timeline. It ensures the underlying database is open
4545
*/
46-
fun start(rootThreadEventId: String? = null)
46+
fun start(rootThreadEventId: String? = null, rootPinnedMessageEventId: String? = null)
4747

4848
/**
4949
* This must be called when you don't need the timeline. It ensures the underlying database get closed.

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/timeline/TimelineSettings.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ data class TimelineSettings(
3232
* The root thread eventId if this is a thread timeline, or null if this is NOT a thread timeline.
3333
*/
3434
val rootThreadEventId: String? = null,
35+
/**
36+
* The root pinned message eventId if this is a pinned messages timeline, or null if this is NOT a pinned messages timeline.
37+
*/
38+
val rootPinnedMessageEventId: String? = null,
3539
/**
3640
* If true Sender Info shown in room will get the latest data information (avatar + displayName).
3741
*/
@@ -42,4 +46,9 @@ data class TimelineSettings(
4246
* Returns true if this is a thread timeline or false otherwise.
4347
*/
4448
fun isThreadTimeline() = rootThreadEventId != null
49+
50+
/**
51+
* Returns true if this is a pinned messages timeline or false otherwise.
52+
*/
53+
fun isPinnedMessagesTimeline() = rootPinnedMessageEventId != null
4554
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomAPI.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import org.matrix.android.sdk.internal.session.room.membership.RoomMembersRespon
3333
import org.matrix.android.sdk.internal.session.room.membership.admin.UserIdAndReason
3434
import org.matrix.android.sdk.internal.session.room.membership.joining.InviteBody
3535
import org.matrix.android.sdk.internal.session.room.membership.threepid.ThreePidInviteBody
36+
import org.matrix.android.sdk.internal.session.room.pinnedmessages.PinnedEventsStateResponse
3637
import org.matrix.android.sdk.internal.session.room.read.ReadBody
3738
import org.matrix.android.sdk.internal.session.room.relation.RelationsResponse
3839
import org.matrix.android.sdk.internal.session.room.reporting.ReportContentBody
@@ -233,11 +234,22 @@ internal interface RoomAPI {
233234
): SendResponse
234235

235236
/**
236-
* Get state events of a room
237+
* Get all state events of a room
237238
* Ref: https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-rooms-roomid-state
238239
*/
239240
@GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "rooms/{roomId}/state")
240-
suspend fun getRoomState(@Path("roomId") roomId: String): List<Event>
241+
suspend fun getAllRoomStates(@Path("roomId") roomId: String): List<Event>
242+
243+
/**
244+
* Get specific state event of a room
245+
* Ref: https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-rooms-roomid-state-eventtype-statekey
246+
*/
247+
@GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "rooms/{roomId}/state/{eventType}/{state_key}")
248+
suspend fun getRoomState(
249+
@Path("roomId") roomId: String,
250+
@Path("eventType") eventType: String,
251+
@Path("state_key") stateKey: String
252+
): PinnedEventsStateResponse
241253

242254
/**
243255
* Paginate relations for event based in normal topological order.

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/peeking/ResolveRoomStateTask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class DefaultResolveRoomStateTask @Inject constructor(
3636

3737
override suspend fun execute(params: ResolveRoomStateTask.Params): List<Event> {
3838
return executeRequest(globalErrorReceiver) {
39-
roomAPI.getRoomState(params.roomId)
39+
roomAPI.getAllRoomStates(params.roomId)
4040
}
4141
}
4242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2022 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.pinnedmessages
18+
19+
import com.squareup.moshi.Json
20+
import com.squareup.moshi.JsonClass
21+
22+
@JsonClass(generateAdapter = true)
23+
internal data class PinnedEventsStateResponse(
24+
/**
25+
* A unique identifier for the event.
26+
*/
27+
@Json(name = "pinned") val pinned: List<String>
28+
)

0 commit comments

Comments
 (0)