Skip to content

Commit 3345194

Browse files
committed
Adding participant count to chat room
1 parent c7360c3 commit 3345194

2 files changed

Lines changed: 61 additions & 19 deletions

File tree

obp-api/src/main/scala/code/api/v6_0_0/APIMethods600.scala

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12870,7 +12870,7 @@ trait APIMethods600 {
1287012870
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot add creator as participant", 400)
1287112871
}
1287212872
} yield {
12873-
(JSONFactory600.createChatRoomJson(room), HttpCode.`201`(callContext))
12873+
(JSONFactory600.createChatRoomJson(room, participantCount = computeParticipantCount(room.chatRoomId)), HttpCode.`201`(callContext))
1287412874
}
1287512875
}
1287612876
}
@@ -12941,7 +12941,7 @@ trait APIMethods600 {
1294112941
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot add creator as participant", 400)
1294212942
}
1294312943
} yield {
12944-
(JSONFactory600.createChatRoomJson(room), HttpCode.`201`(callContext))
12944+
(JSONFactory600.createChatRoomJson(room, participantCount = computeParticipantCount(room.chatRoomId)), HttpCode.`201`(callContext))
1294512945
}
1294612946
}
1294712947
}
@@ -12999,8 +12999,11 @@ trait APIMethods600 {
1299912999
unreadCounts <- Future {
1300013000
computeUnreadCounts(rooms, u.userId)
1300113001
}
13002+
participantCounts <- Future {
13003+
computeParticipantCounts(rooms)
13004+
}
1300213005
} yield {
13003-
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts), HttpCode.`200`(callContext))
13006+
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts, participantCounts), HttpCode.`200`(callContext))
1300413007
}
1300513008
}
1300613009
}
@@ -13058,8 +13061,11 @@ trait APIMethods600 {
1305813061
unreadCounts <- Future {
1305913062
computeUnreadCounts(rooms, u.userId)
1306013063
}
13064+
participantCounts <- Future {
13065+
computeParticipantCounts(rooms)
13066+
}
1306113067
} yield {
13062-
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts), HttpCode.`200`(callContext))
13068+
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts, participantCounts), HttpCode.`200`(callContext))
1306313069
}
1306413070
}
1306513071
}
@@ -13122,7 +13128,7 @@ trait APIMethods600 {
1312213128
x => unboxFullOrFail(x, callContext, NotChatRoomParticipant, 403)
1312313129
}
1312413130
} yield {
13125-
(JSONFactory600.createChatRoomJson(room), HttpCode.`200`(callContext))
13131+
(JSONFactory600.createChatRoomJson(room, participantCount = computeParticipantCount(room.chatRoomId)), HttpCode.`200`(callContext))
1312613132
}
1312713133
}
1312813134
}
@@ -13185,7 +13191,7 @@ trait APIMethods600 {
1318513191
x => unboxFullOrFail(x, callContext, NotChatRoomParticipant, 403)
1318613192
}
1318713193
} yield {
13188-
(JSONFactory600.createChatRoomJson(room), HttpCode.`200`(callContext))
13194+
(JSONFactory600.createChatRoomJson(room, participantCount = computeParticipantCount(room.chatRoomId)), HttpCode.`200`(callContext))
1318913195
}
1319013196
}
1319113197
}
@@ -13258,7 +13264,7 @@ trait APIMethods600 {
1325813264
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot update chat room", 400)
1325913265
}
1326013266
} yield {
13261-
(JSONFactory600.createChatRoomJson(updatedRoom), HttpCode.`200`(callContext))
13267+
(JSONFactory600.createChatRoomJson(updatedRoom, participantCount = computeParticipantCount(updatedRoom.chatRoomId)), HttpCode.`200`(callContext))
1326213268
}
1326313269
}
1326413270
}
@@ -13331,7 +13337,7 @@ trait APIMethods600 {
1333113337
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot update chat room", 400)
1333213338
}
1333313339
} yield {
13334-
(JSONFactory600.createChatRoomJson(updatedRoom), HttpCode.`200`(callContext))
13340+
(JSONFactory600.createChatRoomJson(updatedRoom, participantCount = computeParticipantCount(updatedRoom.chatRoomId)), HttpCode.`200`(callContext))
1333513341
}
1333613342
}
1333713343
}
@@ -13490,7 +13496,7 @@ trait APIMethods600 {
1349013496
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot archive chat room", 400)
1349113497
}
1349213498
} yield {
13493-
(JSONFactory600.createChatRoomJson(archivedRoom), HttpCode.`200`(callContext))
13499+
(JSONFactory600.createChatRoomJson(archivedRoom, participantCount = computeParticipantCount(archivedRoom.chatRoomId)), HttpCode.`200`(callContext))
1349413500
}
1349513501
}
1349613502
}
@@ -13555,7 +13561,7 @@ trait APIMethods600 {
1355513561
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot archive chat room", 400)
1355613562
}
1355713563
} yield {
13558-
(JSONFactory600.createChatRoomJson(archivedRoom), HttpCode.`200`(callContext))
13564+
(JSONFactory600.createChatRoomJson(archivedRoom, participantCount = computeParticipantCount(archivedRoom.chatRoomId)), HttpCode.`200`(callContext))
1355913565
}
1356013566
}
1356113567
}
@@ -13624,7 +13630,7 @@ trait APIMethods600 {
1362413630
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot update chat room", 400)
1362513631
}
1362613632
} yield {
13627-
(JSONFactory600.createChatRoomJson(updatedRoom), HttpCode.`200`(callContext))
13633+
(JSONFactory600.createChatRoomJson(updatedRoom, participantCount = computeParticipantCount(updatedRoom.chatRoomId)), HttpCode.`200`(callContext))
1362813634
}
1362913635
}
1363013636
}
@@ -13693,7 +13699,7 @@ trait APIMethods600 {
1369313699
x => unboxFullOrFail(x, callContext, s"$UnknownError Cannot update chat room", 400)
1369413700
}
1369513701
} yield {
13696-
(JSONFactory600.createChatRoomJson(updatedRoom), HttpCode.`200`(callContext))
13702+
(JSONFactory600.createChatRoomJson(updatedRoom, participantCount = computeParticipantCount(updatedRoom.chatRoomId)), HttpCode.`200`(callContext))
1369713703
}
1369813704
}
1369913705
}
@@ -16343,10 +16349,13 @@ trait APIMethods600 {
1634316349
}
1634416350
}
1634516351
}
16352+
participantCounts <- Future {
16353+
computeParticipantCounts(roomsAndCounts.map(_._1))
16354+
}
1634616355
} yield {
1634716356
val rooms = roomsAndCounts.map(_._1)
1634816357
val unreadCounts = roomsAndCounts.map { case (room, count) => room.chatRoomId -> count }.toMap
16349-
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts), HttpCode.`200`(callContext))
16358+
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts, participantCounts), HttpCode.`200`(callContext))
1635016359
}
1635116360
}
1635216361
}
@@ -16428,8 +16437,11 @@ trait APIMethods600 {
1642816437
unreadCounts <- Future {
1642916438
computeUnreadCounts(rooms, u.userId)
1643016439
}
16440+
participantCounts <- Future {
16441+
computeParticipantCounts(rooms)
16442+
}
1643116443
} yield {
16432-
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts), HttpCode.`200`(callContext))
16444+
(JSONFactory600.createChatRoomsJson(rooms, unreadCounts, participantCounts), HttpCode.`200`(callContext))
1643316445
}
1643416446
}
1643516447
}
@@ -16672,6 +16684,24 @@ trait APIMethods600 {
1667216684
}
1667316685
}
1667416686

16687+
/**
16688+
* Compute the participant count for a single chat room.
16689+
*/
16690+
private def computeParticipantCount(chatRoomId: String): Long = {
16691+
code.chat.ParticipantTrait.participantProvider.vend
16692+
.getParticipants(chatRoomId)
16693+
.map(_.length.toLong)
16694+
.openOr(0L)
16695+
}
16696+
16697+
/**
16698+
* Compute the participant count for each given room.
16699+
* One DB query per room — same N+1 pattern as `computeUnreadCounts`.
16700+
*/
16701+
private def computeParticipantCounts(rooms: List[code.chat.ChatRoomTrait]): Map[String, Long] = {
16702+
rooms.map(room => room.chatRoomId -> computeParticipantCount(room.chatRoomId)).toMap
16703+
}
16704+
1667516705
/**
1667616706
* Compute unread counts for a list of rooms for a given user.
1667716707
* For open rooms, counts only mentions. For private rooms, counts all unread messages.

obp-api/src/main/scala/code/api/v6_0_0/JSONFactory6.0.0.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,8 @@ case class ChatRoomJsonV600(
12011201
last_message_sender: Option[String],
12021202
unread_count: Option[Long],
12031203
created_at: java.util.Date,
1204-
updated_at: java.util.Date
1204+
updated_at: java.util.Date,
1205+
participant_count: Long = 0L
12051206
)
12061207
case class ChatRoomsJsonV600(chat_rooms: List[ChatRoomJsonV600])
12071208

@@ -2972,7 +2973,11 @@ object JSONFactory600 extends CustomJsonFormats with MdcLoggable {
29722973
}
29732974

29742975
// Chat / Messaging factory functions
2975-
def createChatRoomJson(room: code.chat.ChatRoomTrait, unreadCount: Option[Long] = None): ChatRoomJsonV600 = {
2976+
def createChatRoomJson(
2977+
room: code.chat.ChatRoomTrait,
2978+
unreadCount: Option[Long] = None,
2979+
participantCount: Long = 0L
2980+
): ChatRoomJsonV600 = {
29762981
val creator = code.users.Users.users.vend.getUserByUserId(room.createdBy)
29772982
val hasLastMessage = room.lastMessageAt.isDefined
29782983
ChatRoomJsonV600(
@@ -2991,11 +2996,18 @@ object JSONFactory600 extends CustomJsonFormats with MdcLoggable {
29912996
last_message_sender = if (hasLastMessage) Some(room.lastMessageSender) else None,
29922997
unread_count = unreadCount,
29932998
created_at = room.createdDate,
2994-
updated_at = room.updatedDate
2999+
updated_at = room.updatedDate,
3000+
participant_count = participantCount
29953001
)
29963002
}
2997-
def createChatRoomsJson(rooms: List[code.chat.ChatRoomTrait], unreadCounts: Map[String, Long] = Map.empty): ChatRoomsJsonV600 = {
2998-
ChatRoomsJsonV600(rooms.map(r => createChatRoomJson(r, unreadCounts.get(r.chatRoomId))))
3003+
def createChatRoomsJson(
3004+
rooms: List[code.chat.ChatRoomTrait],
3005+
unreadCounts: Map[String, Long] = Map.empty,
3006+
participantCounts: Map[String, Long] = Map.empty
3007+
): ChatRoomsJsonV600 = {
3008+
ChatRoomsJsonV600(rooms.map(r =>
3009+
createChatRoomJson(r, unreadCounts.get(r.chatRoomId), participantCounts.getOrElse(r.chatRoomId, 0L))
3010+
))
29993011
}
30003012

30013013
def createParticipantJson(p: code.chat.ParticipantTrait): ParticipantJsonV600 = {

0 commit comments

Comments
 (0)