Skip to content

Commit

Permalink
Add posting data model
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Kolmogortsev committed Aug 15, 2024
1 parent 4409685 commit a02650b
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 31 deletions.
24 changes: 20 additions & 4 deletions modules/core/src/main/scala/muffin/codec/CodecSupport.scala
Original file line number Diff line number Diff line change
Expand Up @@ -624,12 +624,28 @@ trait CodecSupportLow[To[_], From[_]] extends PrimitivesSupport[To, From] {

given PostFrom: From[Post] =
parsing
.field[Option[Props]]("props")
.fieldMap[Option[Props], Props]("props")(_.getOrElse(Props.empty))
.field[Option[List[FileId]]]("file_ids")
.field[ChannelId]("channel_id")
.field[UserId]("user_id")
.field[String]("message")
.field[MessageId]("id")
.build {
case id *: message *: props *: EmptyTuple => Post(id, message, props.getOrElse(Props.empty))
}
.build(Post.apply.tupled)

given PostedEventDataFrom: From[PostedEventData] =
parsing
.field[Post]("post")
.field[String]("sender_name")
.field[ChannelType]("channel_type")
.field[String]("channel_name")
.build(PostedEventData.apply.tupled)

given ChannelTypeFrom: From[ChannelType] =
parsing[String, ChannelType] {
case "O" => ChannelType.Channel
case "D" => ChannelType.Direct
case unknown => ChannelType.Unknown(unknown)
}

given EventFrom[A: From]: From[Event[A]] =
parsing
Expand Down
8 changes: 4 additions & 4 deletions modules/core/src/main/scala/muffin/model/Posts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ import muffin.codec.*

case class Post(
id: MessageId,
message: String,
// create_at: Long,
// update_at: Long,
// delete_at: Long,
// edit_at: Long,
// user_id: UserId,
// channel_id: ChannelId,
userId: UserId,
channelId: ChannelId,
fileIds: Option[List[FileId]],
// root_id: MessageId,
// original_id: MessageId,
message: String,
// `type`: String,
props: Props = Props.empty
// hashtag: Option[String],
// file_ids: List[String],
// pending_post_id: Option[String],
)

Expand Down
15 changes: 8 additions & 7 deletions modules/core/src/main/scala/muffin/model/websocket/domain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ object domain {

}

}
case class PostedEventData(channelName: String, channelType: ChannelType, senderName: String, post: Post)

enum ChannelType {
case Direct
case Channel
case Unknown(repr: String)
}

case class PostedEventData(
channelName: String,
teamId: String,
senderName: String,
post: Post
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
{
"response": {
"id": "34b52dfdd69f485bb0e70d1879",
"user_id": "34b52dfdd69f485bb0e70d1879",
"channel_id": "34b52dfdd69f485bb0e70d1879",
"message": "text",
"props": {
"attachments": [
Expand Down
2 changes: 2 additions & 0 deletions modules/core/src/test/resources/http/test/posts/Post.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
},
"response": {
"id": "34b52dfdd69f485bb0e70d1879",
"user_id": "34b52dfdd69f485bb0e70d1879",
"channel_id": "34b52dfdd69f485bb0e70d1879",
"message": "message",
"props": {
"attachments": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"channel_name": "awif8o9dxidst8suxf47gwc3gw__hpgdxehuti8huqei4yoyfcdcoc",
"channel_type": "D",
"sender_name": "@danil",
"post": {
"id": "6pzso8hr77gnirkntn1qe9hxso",
"create_at": 1723751876786,
"update_at": 1723751876786,
"edit_at": 0,
"delete_at": 0,
"is_pinned": false,
"user_id": "hpgdxehuti8huqei4yoyfcdcoc",
"channel_id": "rawjhctgq3ytpmgyfcyn3zowjc",
"root_id": "",
"original_id": "",
"message": "",
"type": "",
"props": {
"disable_group_highlight": true
},
"hashtags": "",
"file_ids": [
"a7frxuzgrp8ftkifpaxdemaphr"
],
"pending_post_id": "hpgdxehuti8huqei4yoyfcdcoc:1723751876740",
"reply_count": 0,
"last_reply_at": 0,
"participants": null,
"metadata": {
"files": [
{
"id": "a7frxuzgrp8ftkifpaxdemaphr",
"user_id": "hpgdxehuti8huqei4yoyfcdcoc",
"post_id": "6pzso8hr77gnirkntn1qe9hxso",
"channel_id": "rawjhctgq3ytpmgyfcyn3zowjc",
"create_at": 1723751875886,
"update_at": 1723751875886,
"delete_at": 0,
"name": "docs.yaml",
"extension": "yaml",
"size": 7850,
"mime_type": "",
"mini_preview": null,
"remote_id": "",
"archived": false
}
]
}
},
"channel_display_name": "@danil",
"mentions": "[\"awif8o9dxidst8suxf47gwc3gw\"]",
"otherFile": "true",
"set_online": true,
"team_id": ""
}
28 changes: 26 additions & 2 deletions modules/core/src/test/scala/muffin/api/ApiTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ trait ApiTest[To[_], From[_]](integration: String, codecSupport: CodecSupport[To
.build[domain.TestObject] {
case field *: EmptyTuple => domain.TestObject.apply(field)
}

//
Scenario(s"process websocket event $integration") {
for {
listenedEvent <- Deferred[IO, domain.TestObject]
Expand All @@ -301,7 +301,7 @@ trait ApiTest[To[_], From[_]](integration: String, codecSupport: CodecSupport[To
_ <- websocketFiber.join
} yield assert(event == domain.TestObject.default)
}

//
Scenario(s"Different connections work independent $integration") {
val badEvent = domain.TestObject("broken")
for {
Expand Down Expand Up @@ -339,6 +339,30 @@ trait ApiTest[To[_], From[_]](integration: String, codecSupport: CodecSupport[To
events.contains(badEvent)
)
}

Scenario(s"process posting event with files $integration") {
for {
listenedEvent <- Deferred[IO, PostedEventData]
websocketFiber <- apiClient
.websocket()
.flatMap(
_.addListener[PostedEventData](
EventType.Posted,
event => listenedEvent.complete(event).void
)
.connect()
.start
)
expected <- loadResource(
"websockets/posting/postingWithFileIds.json"
)
.flatMap(raw =>
IO.fromEither(Decode[PostedEventData].apply(raw))
)
event <- listenedEvent.get.timeout(2.seconds)
_ <- websocketFiber.join
} yield assert(event == expected)
}
}

Feature("file api") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import org.scalatest.featurespec.AsyncFeatureSpec

import muffin.api.*
import muffin.http.{Body, EventListener, HttpClient, Method, Params}
import muffin.model.websocket.domain.{Event, EventType}
import muffin.model.websocket.domain.RawJson
import muffin.model.websocket.domain.{Event, EventType, RawJson}

class CirceApiTest extends ApiTest[Encoder, Decoder]("circe", codec) {

Expand Down Expand Up @@ -54,15 +53,21 @@ class CirceApiTest extends ApiTest[Encoder, Decoder]("circe", codec) {
headers: Map[String, String],
backoffSettings: BackoffSettings,
listeners: List[EventListener[IO]]
): IO[Unit] = events.traverse_(event => listeners.traverse_(_.onEvent(event)))
): IO[Unit] = events.flatMap(_.traverse_(event => listeners.traverse_(_.onEvent(event))))

}

private val events = List(
Event(
EventType.Hello,
RawJson.from(Encoder[domain.TestObject].apply(domain.TestObject.default).toString)
)
private val events = loadResource(
"websockets/posting/postingWithFileIds.json"
)
.map(postingEvent =>
List(
Event(
EventType.Hello,
RawJson.from(Encoder[domain.TestObject].apply(domain.TestObject.default).toString)
),
Event(EventType.Posted, RawJson.from(postingEvent))
)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ class ZioApiTest extends ApiTest[JsonEncoder, JsonDecoder]("zio", codec) {
headers: Map[String, String],
backoffSettings: BackoffSettings,
listeners: List[EventListener[IO]]
): IO[Unit] = events.traverse_(event => listeners.traverse_(_.onEvent(event)))
): IO[Unit] = events.flatMap(_.traverse_(event => listeners.traverse_(_.onEvent(event))))

private val events = List(
Event(
EventType.Hello,
RawJson.from(domain.TestObject.default.toJson)
private val events = loadResource("websockets/posting/postingWithFileIds.json").map(postingEvent =>
List(
Event(
EventType.Hello,
RawJson.from(domain.TestObject.default.toJson)
),
Event(EventType.Posted, RawJson.from(postingEvent))
)
)

}
}

Expand Down

0 comments on commit a02650b

Please sign in to comment.