From 30287101229bd5022753f4dd4e87d1610c43d7a3 Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 7 Nov 2018 10:39:10 +0100 Subject: [PATCH] Add group users API --- .../api/session/group/model/GroupSummary.kt | 3 ++- .../database/mapper/GroupSummaryMapper.kt | 3 ++- .../database/model/GroupSummaryEntity.kt | 3 ++- .../session/group/GetGroupDataRequest.kt | 22 +++++++++++++++---- .../internal/session/group/GroupAPI.kt | 10 +++++++++ .../internal/session/group/model/GroupRoom.kt | 4 ++-- .../internal/session/group/model/GroupUser.kt | 13 +++++++++++ .../session/group/model/GroupUsers.kt | 10 +++++++++ 8 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUser.kt create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUsers.kt diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/group/model/GroupSummary.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/group/model/GroupSummary.kt index 2ae1d2fe..6a18d75c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/group/model/GroupSummary.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/group/model/GroupSummary.kt @@ -5,5 +5,6 @@ data class GroupSummary( val displayName: String = "", val shortDescription: String = "", val avatarUrl: String = "", - val roomIds: List = emptyList() + val roomIds: List = emptyList(), + val userIds: List = emptyList() ) \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/GroupSummaryMapper.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/GroupSummaryMapper.kt index 0daa9415..a08fd810 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/GroupSummaryMapper.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/mapper/GroupSummaryMapper.kt @@ -12,7 +12,8 @@ object GroupSummaryMapper { roomSummaryEntity.displayName, roomSummaryEntity.shortDescription, roomSummaryEntity.avatarUrl, - roomSummaryEntity.roomIds.toList() + roomSummaryEntity.roomIds.toList(), + roomSummaryEntity.userIds.toList() ) } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/GroupSummaryEntity.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/GroupSummaryEntity.kt index 4c69616d..4e903d53 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/GroupSummaryEntity.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/model/GroupSummaryEntity.kt @@ -8,7 +8,8 @@ open class GroupSummaryEntity(@PrimaryKey var groupId: String = "", var displayName: String = "", var shortDescription: String = "", var avatarUrl: String = "", - var roomIds: RealmList = RealmList() + var roomIds: RealmList = RealmList(), + var userIds: RealmList = RealmList() ) : RealmObject() { companion object diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataRequest.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataRequest.kt index 8651b309..c3ce919a 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataRequest.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GetGroupDataRequest.kt @@ -12,6 +12,7 @@ import im.vector.matrix.android.internal.database.query.where import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.group.model.GroupRooms import im.vector.matrix.android.internal.session.group.model.GroupSummaryResponse +import im.vector.matrix.android.internal.session.group.model.GroupUsers import im.vector.matrix.android.internal.util.CancelableCoroutine import im.vector.matrix.android.internal.util.MatrixCoroutineDispatchers import im.vector.matrix.android.internal.util.tryTransactionSync @@ -45,24 +46,37 @@ class GetGroupDataRequest( val groupRooms = executeRequest { apiCall = groupAPI.getRooms(groupId) }.bind() - insertInDb(groupSummary, groupRooms, groupId).bind() + + val groupUsers = executeRequest { + apiCall = groupAPI.getUsers(groupId) + }.bind() + insertInDb(groupSummary, groupRooms, groupUsers, groupId).bind() }.fix() } - private fun insertInDb(groupSummary: GroupSummaryResponse, groupRooms: GroupRooms, groupId: String): Try { + private fun insertInDb(groupSummary: GroupSummaryResponse, + groupRooms: GroupRooms, + groupUsers: GroupUsers, + groupId: String): Try { return monarchy .tryTransactionSync { realm -> val groupSummaryEntity = GroupSummaryEntity.where(realm, groupId).findFirst() - ?: realm.createObject(groupId) + ?: realm.createObject(groupId) groupSummaryEntity.avatarUrl = groupSummary.profile?.avatarUrl ?: "" val name = groupSummary.profile?.name groupSummaryEntity.displayName = if (name.isNullOrEmpty()) groupId else name groupSummaryEntity.shortDescription = groupSummary.profile?.shortDescription ?: "" - val roomIds = groupRooms.rooms.mapNotNull { it.roomId } + val roomIds = groupRooms.rooms.map { it.roomId } groupSummaryEntity.roomIds.clear() groupSummaryEntity.roomIds.addAll(roomIds) + + val userIds = groupUsers.users.map { it.userId } + groupSummaryEntity.userIds.clear() + groupSummaryEntity.userIds.addAll(userIds) + + } } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupAPI.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupAPI.kt index a3a6978f..b7c90821 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupAPI.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/GroupAPI.kt @@ -3,6 +3,7 @@ package im.vector.matrix.android.internal.session.group import im.vector.matrix.android.internal.network.NetworkConstants import im.vector.matrix.android.internal.session.group.model.GroupRooms import im.vector.matrix.android.internal.session.group.model.GroupSummaryResponse +import im.vector.matrix.android.internal.session.group.model.GroupUsers import retrofit2.Call import retrofit2.http.GET import retrofit2.http.Path @@ -26,4 +27,13 @@ interface GroupAPI { fun getRooms(@Path("groupId") groupId: String): Call + /** + * Request the users list. + * + * @param groupId the group id + */ + @GET(NetworkConstants.URI_API_PREFIX_PATH_R0 + "groups/{groupId}/users") + fun getUsers(@Path("groupId") groupId: String): Call + + } \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupRoom.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupRoom.kt index fd3590b2..d40573f3 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupRoom.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupRoom.kt @@ -6,11 +6,11 @@ import com.squareup.moshi.JsonClass @JsonClass(generateAdapter = true) data class GroupRoom( - @Json(name = "aliases") val aliases: List? = null, + @Json(name = "aliases") val aliases: List = emptyList(), @Json(name = "canonical_alias") val canonicalAlias: String? = null, @Json(name = "name") val name: String? = null, @Json(name = "num_joined_members") val numJoinedMembers: Int = 0, - @Json(name = "room_id") val roomId: String? = null, + @Json(name = "room_id") val roomId: String, @Json(name = "topic") val topic: String? = null, @Json(name = "world_readable") val worldReadable: Boolean = false, @Json(name = "guest_can_join") val guestCanJoin: Boolean = false, diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUser.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUser.kt new file mode 100644 index 00000000..b4c87e4c --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUser.kt @@ -0,0 +1,13 @@ +package im.vector.matrix.android.internal.session.group.model + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class GroupUser( + @Json(name = "display_name") val displayName: String = "", + @Json(name = "user_id") val userId: String, + @Json(name = "is_privileged") val isPrivileged: Boolean = false, + @Json(name = "avatar_url") val avatarUrl: String? = "", + @Json(name = "is_public") val isPublic: Boolean = false +) \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUsers.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUsers.kt new file mode 100644 index 00000000..b4e39fc0 --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/group/model/GroupUsers.kt @@ -0,0 +1,10 @@ +package im.vector.matrix.android.internal.session.group.model + +import com.squareup.moshi.Json +import com.squareup.moshi.JsonClass + +@JsonClass(generateAdapter = true) +data class GroupUsers( + @Json(name = "total_user_count_estimate") val totalUserCountEstimate: Int, + @Json(name = "chunk") val users: List = emptyList() +) \ No newline at end of file