Remove roomCreatorUserId and use current userId by default

This commit is contained in:
Florian Renaud 2022-08-25 14:16:20 +02:00
parent 90d688c222
commit eab4ebc3b1
3 changed files with 22 additions and 27 deletions

View file

@ -48,6 +48,7 @@ import org.matrix.android.sdk.api.session.room.model.redactOrDefault
import org.matrix.android.sdk.api.session.room.model.stateDefaultOrDefault
import org.matrix.android.sdk.api.session.room.model.usersDefaultOrDefault
import org.matrix.android.sdk.api.session.user.UserService
import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.session.room.create.CreateLocalRoomStateEventsTask.Params
import org.matrix.android.sdk.internal.session.room.membership.threepid.toThreePid
import org.matrix.android.sdk.internal.task.Task
@ -62,29 +63,25 @@ import javax.inject.Inject
* Ref: https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3createroom
*/
internal interface CreateLocalRoomStateEventsTask : Task<Params, List<Event>> {
data class Params(
val roomCreatorUserId: String,
val createRoomBody: CreateRoomBody
)
data class Params(val createRoomBody: CreateRoomBody)
}
internal class DefaultCreateLocalRoomStateEventsTask @Inject constructor(
@UserId private val myUserId: String,
private val userService: UserService,
private val clock: Clock,
) : CreateLocalRoomStateEventsTask {
private lateinit var createRoomBody: CreateRoomBody
private lateinit var roomCreatorUserId: String
override suspend fun execute(params: Params): List<Event> {
createRoomBody = params.createRoomBody
roomCreatorUserId = params.roomCreatorUserId
// Build the list of the state events following the priorities from the matrix specification
// Changing the order of the events might break the correct display of the room on the client side
return buildList {
createRoomCreateEvent()
createRoomMemberEvents(listOf(roomCreatorUserId))
createRoomMemberEvents(listOf(myUserId))
createRoomPowerLevelsEvent()
createRoomAliasEvent()
createRoomPresetEvents()
@ -103,7 +100,7 @@ internal class DefaultCreateLocalRoomStateEventsTask @Inject constructor(
val roomCreateEvent = createLocalStateEvent(
type = EventType.STATE_ROOM_CREATE,
content = RoomCreateContent(
creator = roomCreatorUserId,
creator = myUserId,
roomVersion = createRoomBody.roomVersion,
type = (createRoomBody.creationContent as? Map<*, *>)?.get(CreateRoomParams.CREATION_CONTENT_KEY_ROOM_TYPE) as? String
@ -144,8 +141,8 @@ internal class DefaultCreateLocalRoomStateEventsTask @Inject constructor(
createLocalStateEvent(
type = EventType.STATE_ROOM_MEMBER,
content = RoomMemberContent(
isDirect = createRoomBody.isDirect.takeUnless { user.userId == roomCreatorUserId }.orFalse(),
membership = if (user.userId == roomCreatorUserId) Membership.JOIN else Membership.INVITE,
isDirect = createRoomBody.isDirect.takeUnless { user.userId == myUserId }.orFalse(),
membership = if (user.userId == myUserId) Membership.JOIN else Membership.INVITE,
displayName = user.displayName,
avatarUrl = user.avatarUrl
).toContent(),
@ -186,7 +183,7 @@ internal class DefaultCreateLocalRoomStateEventsTask @Inject constructor(
val canonicalAliasContent = createLocalStateEvent(
type = EventType.STATE_ROOM_CANONICAL_ALIAS,
content = RoomCanonicalAliasContent(
canonicalAlias = "${createRoomBody.roomAliasName}:${roomCreatorUserId.getServerName()}"
canonicalAlias = "${createRoomBody.roomAliasName}:${myUserId.getServerName()}"
).toContent(),
)
add(canonicalAliasContent)
@ -287,7 +284,7 @@ internal class DefaultCreateLocalRoomStateEventsTask @Inject constructor(
private fun createLocalStateEvent(type: String?, content: Content?, stateKey: String? = ""): Event {
return Event(
type = type,
senderId = roomCreatorUserId,
senderId = myUserId,
stateKey = stateKey,
content = content,
originServerTs = clock.epochMillis(),

View file

@ -46,7 +46,6 @@ import org.matrix.android.sdk.internal.database.query.copyToRealmOrIgnore
import org.matrix.android.sdk.internal.database.query.getOrCreate
import org.matrix.android.sdk.internal.database.query.getOrNull
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.session.events.getFixedRoomMemberContent
import org.matrix.android.sdk.internal.session.room.membership.RoomMemberEventHandler
import org.matrix.android.sdk.internal.session.room.summary.RoomSummaryUpdater
@ -60,7 +59,6 @@ import javax.inject.Inject
internal interface CreateLocalRoomTask : Task<CreateRoomParams, String>
internal class DefaultCreateLocalRoomTask @Inject constructor(
@UserId private val myUserId: String,
@SessionDatabase private val monarchy: Monarchy,
private val roomMemberEventHandler: RoomMemberEventHandler,
private val roomSummaryUpdater: RoomSummaryUpdater,
@ -156,7 +154,7 @@ internal class DefaultCreateLocalRoomTask @Inject constructor(
isLastForward = true
}
val eventList = createLocalRoomStateEventsTask.execute(CreateLocalRoomStateEventsTask.Params(myUserId, createRoomBody))
val eventList = createLocalRoomStateEventsTask.execute(CreateLocalRoomStateEventsTask.Params(createRoomBody))
val roomMemberContentsByUser = HashMap<String, RoomMemberContent?>()
for (event in eventList) {

View file

@ -71,6 +71,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
private val userService = mockk<UserService>()
private val defaultCreateLocalRoomStateEventsTask = DefaultCreateLocalRoomStateEventsTask(
myUserId = MY_USER_ID,
userService = userService,
clock = clock
)
@ -103,20 +104,19 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
@Test
fun `given a CreateRoomBody when execute then the resulting list of events contains the correct room create state event`() = runTest {
// Given
val aRoomCreator = MY_USER_ID
val aRoomVersion = "a_room_version"
every { createRoomBody.roomVersion } returns aRoomVersion
// When
val params = CreateLocalRoomStateEventsTask.Params(aRoomCreator, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
val roomCreateEvent = result.find { it.type == EventType.STATE_ROOM_CREATE }
val roomCreateContent = roomCreateEvent?.content.toModel<RoomCreateContent>()
roomCreateContent?.creator shouldBeEqualTo aRoomCreator
roomCreateContent?.creator shouldBeEqualTo MY_USER_ID
roomCreateContent?.roomVersion shouldBeEqualTo aRoomVersion
}
@ -130,7 +130,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
every { createRoomBody.topic } returns aRoomTopic
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -158,7 +158,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
}
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -195,7 +195,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
every { createRoomBody.powerLevelContentOverride } returns aPowerLevelsContent
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -212,7 +212,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
every { createRoomBody.roomAliasName } returns aRoomAlias
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -244,7 +244,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
every { createRoomBody.preset } returns case.preset
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -284,7 +284,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
every { createRoomBody.initialStates } returns aListOfInitialStateEvents
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -319,7 +319,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
every { createRoomBody.invite3pids } returns aListOf3pids
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -358,7 +358,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
}
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then
@ -447,7 +447,7 @@ internal class DefaultCreateLocalRoomStateEventsTaskTest {
)
// When
val params = CreateLocalRoomStateEventsTask.Params(MY_USER_ID, createRoomBody)
val params = CreateLocalRoomStateEventsTask.Params(createRoomBody)
val result = defaultCreateLocalRoomStateEventsTask.execute(params)
// Then