Merge pull request #7373 from hayaksi1/fix/5794-account-data-api

Expose global account data get and set on MatrixClient
This commit is contained in:
Benoit Marty
2026-08-11 14:51:57 +02:00
committed by GitHub
5 changed files with 69 additions and 0 deletions
@@ -211,6 +211,16 @@ interface MatrixClient : UrlContentFetcher {
*/
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].
*
@@ -825,6 +825,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 {
@@ -139,6 +142,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(
@@ -56,6 +56,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 isUserStatusSupportedResult: () -> Boolean = { false },
private val subscribeToOwnProfileResult: (ProfileListener) -> Unit = {},
private val closeResult: () -> Unit = {},
@@ -124,5 +126,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() },
@@ -413,6 +415,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)
}