Skip to content

[NEW] Add subscriptions.unread endpoint #228

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import se.ansman.kotshi.JsonSerializable
@JsonSerializable
data class ChatRoomPayload(@Json(name = "rid") val roomId: String)

@JsonSerializable
data class ChatRoomUnreadPayload(val roomId: String)

@JsonSerializable
data class ChatRoomNamePayload(val roomId: String, val name: String?)

Expand Down
21 changes: 21 additions & 0 deletions core/src/main/kotlin/chat/rocket/core/internal/rest/ChatRoom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import chat.rocket.core.internal.model.ChatRoomJoinCodePayload
import chat.rocket.core.internal.model.ChatRoomNamePayload
import chat.rocket.core.internal.model.ChatRoomPayload
import chat.rocket.core.internal.model.ChatRoomReadOnlyPayload
import chat.rocket.core.internal.model.ChatRoomUnreadPayload
import chat.rocket.core.internal.model.ChatRoomTopicPayload
import chat.rocket.core.internal.model.ChatRoomTypePayload
import chat.rocket.core.internal.model.ChatRoomFavoritePayload
Expand Down Expand Up @@ -232,6 +233,26 @@ suspend fun RocketChatClient.markAsRead(roomId: String) {
}
}

/**
* Marks a room as unread.
*
* @param roomId The ID of the room.
*/
suspend fun RocketChatClient.markAsUnread(roomId: String) {
withContext(CommonPool) {
val payload = ChatRoomUnreadPayload(roomId)
val adapter = moshi.adapter(ChatRoomUnreadPayload::class.java)
val payloadBody = adapter.toJson(payload)

val body = RequestBody.create(MEDIA_TYPE_JSON, payloadBody)

val url = requestUrl(restUrl, "subscriptions.unread").build()
val request = requestBuilderForAuthenticatedMethods(url).post(body).build()

handleRestCall<Any>(request, Any::class.java)
}
}

// TODO: Add doc.
suspend fun RocketChatClient.joinChat(roomId: String): Boolean = withContext(CommonPool) {
val payload = RoomIdPayload(roomId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ class ChatRoomTest {
}
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding the tests! 💯

fun `markAsUnread() should succeed without throwing`() {
mockServer.expect()
.post()
.withPath("/api/v1/subscriptions.unread")
.andReturn(200, SUCCESS)
.once()

runBlocking {
sut.markAsUnread(roomId = "GENERAL")
}
}

@Test(expected = RocketChatException::class)
fun `markAsUnread() should fail with RocketChatAuthException if not logged in`() {
mockServer.expect()
.post()
.withPath("/api/v1/subscriptions.unread")
.andReturn(401, MUST_BE_LOGGED_ERROR)
.once()

runBlocking {
sut.markAsUnread(roomId = "GENERAL")
}
}

@Test
fun `getMembers() should succeed without throwing`() {
mockServer.expect()
Expand Down