Add group users API

This commit is contained in:
ganfra 2018-11-07 10:39:10 +01:00
parent 49715a30b6
commit 3028710122
8 changed files with 59 additions and 9 deletions

View File

@ -5,5 +5,6 @@ data class GroupSummary(
val displayName: String = "",
val shortDescription: String = "",
val avatarUrl: String = "",
val roomIds: List<String> = emptyList()
val roomIds: List<String> = emptyList(),
val userIds: List<String> = emptyList()
)

View File

@ -12,7 +12,8 @@ object GroupSummaryMapper {
roomSummaryEntity.displayName,
roomSummaryEntity.shortDescription,
roomSummaryEntity.avatarUrl,
roomSummaryEntity.roomIds.toList()
roomSummaryEntity.roomIds.toList(),
roomSummaryEntity.userIds.toList()
)
}
}

View File

@ -8,7 +8,8 @@ open class GroupSummaryEntity(@PrimaryKey var groupId: String = "",
var displayName: String = "",
var shortDescription: String = "",
var avatarUrl: String = "",
var roomIds: RealmList<String> = RealmList()
var roomIds: RealmList<String> = RealmList(),
var userIds: RealmList<String> = RealmList()
) : RealmObject() {

companion object

View File

@ -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<GroupRooms> {
apiCall = groupAPI.getRooms(groupId)
}.bind()
insertInDb(groupSummary, groupRooms, groupId).bind()

val groupUsers = executeRequest<GroupUsers> {
apiCall = groupAPI.getUsers(groupId)
}.bind()
insertInDb(groupSummary, groupRooms, groupUsers, groupId).bind()
}.fix()
}

private fun insertInDb(groupSummary: GroupSummaryResponse, groupRooms: GroupRooms, groupId: String): Try<Unit> {
private fun insertInDb(groupSummary: GroupSummaryResponse,
groupRooms: GroupRooms,
groupUsers: GroupUsers,
groupId: String): Try<Unit> {
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)


}
}


View File

@ -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<GroupRooms>


/**
* 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<GroupUsers>


}

View File

@ -6,11 +6,11 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class GroupRoom(

@Json(name = "aliases") val aliases: List<String>? = null,
@Json(name = "aliases") val aliases: List<String> = 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,

View File

@ -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
)

View File

@ -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<GroupUser> = emptyList()
)