Expose global account data get and set on MatrixClient

MatrixClient had no way to read or write global account data although the
underlying client supports it.

Add getAccountData and setAccountData to the interface and implement them in
RustMatrixClient using the same withContext/runCatchingExceptions idiom as the
neighbouring methods. The content is the raw JSON string the SDK returns and
accepts: event types are open ended, so a typed wrapper belongs to whichever
caller needs one. Scope is limited to global account data; the room level
variant and the change notification APIs can follow when a feature needs them.
This commit is contained in:
hayaksi1
2026-08-09 15:10:00 +03:00
parent 044737a3a7
commit 33bdf968c5
5 changed files with 69 additions and 0 deletions
@@ -210,6 +210,16 @@ interface MatrixClient {
*/
suspend fun addRecentEmoji(emoji: String): Result<Unit>
/**
* Returns the raw JSON content of the global account data event of type [eventType], or null if it is not set.
*/
suspend fun getAccountData(eventType: String): Result<String?>
/**
* Sets the global account data event of type [eventType] to the raw JSON [content].
*/
suspend fun setAccountData(eventType: String, content: String): Result<Unit>
/**
* Marks the room with the provided [roomId] as read, sending a fully read receipt for [eventId].
*
@@ -806,6 +806,18 @@ class RustMatrixClient(
}
}
override suspend fun getAccountData(eventType: String): Result<String?> = withContext(sessionDispatcher) {
runCatchingExceptions {
innerClient.accountData(eventType)
}
}
override suspend fun setAccountData(eventType: String, content: String): Result<Unit> = withContext(sessionDispatcher) {
runCatchingExceptions {
innerClient.setAccountData(eventType, content)
}
}
override suspend fun canLinkNewDevice(): Result<Boolean> = withContext(sessionDispatcher) {
runCatchingExceptions {
innerClient.isLoginWithQrCodeSupported()
@@ -44,6 +44,9 @@ import org.matrix.rustcomponents.sdk.StoreSizes
import org.matrix.rustcomponents.sdk.UserProfile
import java.io.File
private const val AN_ACCOUNT_DATA_EVENT_TYPE = "org.example.custom"
private const val AN_ACCOUNT_DATA_CONTENT = """{"key":"value"}"""
class RustMatrixClientTest {
@Test
fun `ensure that sessionId and deviceId can be retrieved from the client`() = runTest {
@@ -138,6 +141,30 @@ class RustMatrixClientTest {
assertThat(createParameters?.historyVisibilityOverride).isEqualTo(RoomHistoryVisibility.Invited)
}
@Test
fun `getAccountData returns the raw content provided by the client`() = runTest {
val accountDataResult = lambdaRecorder<String, String?> { AN_ACCOUNT_DATA_CONTENT }
val client = createRustMatrixClient(
client = FakeFfiClient(accountDataResult = accountDataResult)
)
assertThat(client.getAccountData(AN_ACCOUNT_DATA_EVENT_TYPE).getOrThrow()).isEqualTo(AN_ACCOUNT_DATA_CONTENT)
accountDataResult.assertions().isCalledOnce().with(value(AN_ACCOUNT_DATA_EVENT_TYPE))
client.destroy()
}
@Test
fun `setAccountData forwards the event type and the raw content to the client`() = runTest {
val setAccountDataResult = lambdaRecorder<String, String, Unit> { _, _ -> }
val client = createRustMatrixClient(
client = FakeFfiClient(setAccountDataResult = setAccountDataResult)
)
assertThat(client.setAccountData(AN_ACCOUNT_DATA_EVENT_TYPE, AN_ACCOUNT_DATA_CONTENT).isSuccess).isTrue()
setAccountDataResult.assertions().isCalledOnce().with(value(AN_ACCOUNT_DATA_EVENT_TYPE), value(AN_ACCOUNT_DATA_CONTENT))
client.destroy()
}
private fun TestScope.createRustMatrixClient(
client: Client = FakeFfiClient(),
sessionStore: SessionStore = InMemorySessionStore(
@@ -55,6 +55,8 @@ class FakeFfiClient(
private val getStoreSizesResult: () -> StoreSizes = { lambdaError() },
private val createRoomResult: (CreateRoomParameters) -> String = { lambdaError() },
private val homeserverCapabilities: HomeserverCapabilities = FakeFfiHomeserverCapabilities(),
private val accountDataResult: (String) -> String? = { lambdaError() },
private val setAccountDataResult: (String, String) -> Unit = { _, _ -> lambdaError() },
private val closeResult: () -> Unit = {},
) : Client(NoHandle) {
override fun userId(): String = userId
@@ -114,5 +116,13 @@ class FakeFfiClient(
return homeserverCapabilities
}
override suspend fun accountData(eventType: String): String? = simulateLongTask {
accountDataResult(eventType)
}
override suspend fun setAccountData(eventType: String, content: String) = simulateLongTask {
setAccountDataResult(eventType, content)
}
override fun close() = closeResult()
}
@@ -124,6 +124,8 @@ class FakeMatrixClient(
private val getJoinedRoomIdsResult: () -> Result<Set<RoomId>> = { Result.success(emptySet()) },
private val getRecentEmojisLambda: () -> Result<List<String>> = { Result.success(emptyList()) },
private val addRecentEmojiLambda: (String) -> Result<Unit> = { Result.success(Unit) },
private val getAccountDataLambda: (String) -> Result<String?> = { lambdaError() },
private val setAccountDataLambda: (String, String) -> Result<Unit> = { _, _ -> lambdaError() },
private val markRoomAsFullyReadResult: (RoomId, EventId) -> Result<Unit> = { _, _ -> lambdaError() },
private val markAllRoomsAsReadResult: () -> Result<Unit> = { Result.success(Unit) },
private val performDatabaseVacuumLambda: () -> Result<Unit> = { lambdaError() },
@@ -407,6 +409,14 @@ class FakeMatrixClient(
return getRecentEmojisLambda()
}
override suspend fun getAccountData(eventType: String): Result<String?> {
return getAccountDataLambda(eventType)
}
override suspend fun setAccountData(eventType: String, content: String): Result<Unit> {
return setAccountDataLambda(eventType, content)
}
override suspend fun markRoomAsFullyRead(roomId: RoomId, eventId: EventId): Result<Unit> {
return markRoomAsFullyReadResult(roomId, eventId)
}