Skip to content
This repository was archived by the owner on Oct 30, 2025. It is now read-only.

Commit 54676f5

Browse files
authored
Merge pull request #229 from RocketChat/improvement/createDirectMessage-package-location
[IMPROVEMENT][PROJECT] Puts the createDirectMessage on the proper package.
2 parents bea2945 + 88bca2d commit 54676f5

File tree

5 files changed

+46
-46
lines changed

5 files changed

+46
-46
lines changed

core/src/main/kotlin/chat/rocket/core/internal/rest/Channel.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package chat.rocket.core.internal.rest
33
import chat.rocket.common.model.RoomType
44
import chat.rocket.core.RocketChatClient
55
import chat.rocket.core.internal.RestResult
6+
import chat.rocket.core.internal.model.CreateDirectMessagePayload
67
import chat.rocket.core.internal.model.CreateNewChannelPayload
8+
import chat.rocket.core.model.DirectMessage
79
import chat.rocket.core.model.Room
810
import com.squareup.moshi.Types
911
import kotlinx.coroutines.experimental.CommonPool
@@ -35,4 +37,26 @@ suspend fun RocketChatClient.createChannel(
3537
val type = Types.newParameterizedType(RestResult::class.java, Room::class.java)
3638

3739
return@withContext handleRestCall<RestResult<Room>>(request, type).result()
38-
}
40+
}
41+
42+
/**
43+
* Create a direct message (DM) room with an user.
44+
*
45+
* @param username The username of the user to create a DM with.
46+
* @return A DirectMessage object.
47+
*/
48+
suspend fun RocketChatClient.createDirectMessage(username: String): DirectMessage =
49+
withContext(CommonPool) {
50+
val payload = CreateDirectMessagePayload(username = username)
51+
val adapter = moshi.adapter(CreateDirectMessagePayload::class.java)
52+
val payloadBody = adapter.toJson(payload)
53+
54+
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)
55+
56+
val url = requestUrl(restUrl, "im.create").build()
57+
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()
58+
59+
val type = Types.newParameterizedType(RestResult::class.java, DirectMessage::class.java)
60+
61+
return@withContext handleRestCall<RestResult<DirectMessage>>(request, type).result()
62+
}

core/src/main/kotlin/chat/rocket/core/internal/rest/Message.kt

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import chat.rocket.common.model.BaseResult
44
import chat.rocket.common.model.RoomType
55
import chat.rocket.core.RocketChatClient
66
import chat.rocket.core.internal.RestResult
7-
import chat.rocket.core.internal.model.CreateDirectMessagePayload
87
import chat.rocket.core.internal.model.DeletePayload
98
import chat.rocket.core.internal.model.MessageReportPayload
109
import chat.rocket.core.internal.model.PostMessagePayload
@@ -13,7 +12,6 @@ import chat.rocket.core.internal.model.SendMessageBody
1312
import chat.rocket.core.internal.model.SendMessagePayload
1413
import chat.rocket.core.model.DeleteResult
1514
import chat.rocket.core.model.Message
16-
import chat.rocket.core.model.NewDirectMessageResult
1715
import chat.rocket.core.model.PagedResult
1816
import chat.rocket.core.model.ReadReceipt
1917
import chat.rocket.core.model.attachment.Attachment
@@ -397,26 +395,4 @@ suspend fun RocketChatClient.reportMessage(
397395
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()
398396

399397
return@withContext handleRestCall<BaseResult>(request, BaseResult::class.java).success
400-
}
401-
402-
/**
403-
* Create a direct message session with another user.
404-
*
405-
* @param username The username of the user to create a session with.
406-
* @return The updated Message object.
407-
*/
408-
suspend fun RocketChatClient.createDirectMessage(username: String): NewDirectMessageResult =
409-
withContext(CommonPool) {
410-
val payload = CreateDirectMessagePayload(username = username)
411-
val adapter = moshi.adapter(CreateDirectMessagePayload::class.java)
412-
val payloadBody = adapter.toJson(payload)
413-
414-
val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)
415-
416-
val url = requestUrl(restUrl, "im.create").build()
417-
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()
418-
419-
val type = Types.newParameterizedType(RestResult::class.java, NewDirectMessageResult::class.java)
420-
421-
return@withContext handleRestCall<RestResult<NewDirectMessageResult>>(request, type).result()
422-
}
398+
}

core/src/main/kotlin/chat/rocket/core/model/NewDirectMessageResult.kt renamed to core/src/main/kotlin/chat/rocket/core/model/DirectMessage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.squareup.moshi.Json
66
import se.ansman.kotshi.JsonSerializable
77

88
@JsonSerializable
9-
data class NewDirectMessageResult(
9+
data class DirectMessage(
1010
@Json(name = "_id")
1111
val id: String,
1212
@Json(name = "_updatedAt")

core/src/test/kotlin/chat/rocket/core/internal/rest/ChannelTest.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import chat.rocket.common.RocketChatAuthException
44
import chat.rocket.common.RocketChatException
55
import chat.rocket.common.model.RoomType
66
import chat.rocket.common.model.Token
7+
import chat.rocket.common.model.roomTypeOf
78
import chat.rocket.common.util.PlatformLogger
89
import chat.rocket.core.RocketChatClient
910
import chat.rocket.core.TokenRepository
@@ -110,6 +111,24 @@ class ChannelTest {
110111
}
111112
}
112113

114+
@Test
115+
fun `createDirectMessage() should return true and yield no exceptions`() {
116+
mockServer.expect()
117+
.post()
118+
.withPath("/api/v1/im.create")
119+
.andReturn(200, CREATE_DM_OK)
120+
.once()
121+
122+
runBlocking {
123+
val result = sut.createDirectMessage("rocket.cat")
124+
assertThat(result.id, isEqualTo("Lymsiu4Mn6xjTAan4RtMDEYc28fQ5aHpf4"))
125+
assertThat(result.type, isEqualTo(roomTypeOf("d")))
126+
assertThat(result.usernames.size, isEqualTo(2))
127+
assertThat(result.usernames[0], isEqualTo("rocket.cat"))
128+
assertThat(result.usernames[1], isEqualTo("user.test"))
129+
}
130+
}
131+
113132
@After
114133
fun shutdown() {
115134
mockServer.shutdown()

core/src/test/kotlin/chat/rocket/core/internal/rest/MessagesTest.kt

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package chat.rocket.core.internal.rest
22

33
import chat.rocket.common.RocketChatException
44
import chat.rocket.common.model.Token
5-
import chat.rocket.common.model.roomTypeOf
65
import chat.rocket.common.util.PlatformLogger
76
import chat.rocket.core.RocketChatClient
87
import chat.rocket.core.TokenRepository
@@ -256,24 +255,6 @@ class MessagesTest {
256255
}
257256
}
258257

259-
@Test
260-
fun `createDirectMessage() should return true and yield no exceptions`() {
261-
mockServer.expect()
262-
.post()
263-
.withPath("/api/v1/im.create")
264-
.andReturn(200, CREATE_DM_OK)
265-
.once()
266-
267-
runBlocking {
268-
val result = sut.createDirectMessage(username = "rocket.cat")
269-
assertThat(result.id, isEqualTo("Lymsiu4Mn6xjTAan4RtMDEYc28fQ5aHpf4"))
270-
assertThat(result.type, isEqualTo(roomTypeOf("d")))
271-
assertThat(result.usernames.size, isEqualTo(2))
272-
assertThat(result.usernames[0], isEqualTo("rocket.cat"))
273-
assertThat(result.usernames[1], isEqualTo("user.test"))
274-
}
275-
}
276-
277258
@After
278259
fun shutdown() {
279260
mockServer.shutdown()

0 commit comments

Comments
 (0)