Skip to content

Commit

Permalink
Merge pull request #545 from schlawg/lila-out-streamers-online
Browse files Browse the repository at this point in the history
Lila out streamers online
  • Loading branch information
ornicar authored Mar 11, 2024
2 parents 02dc12a + cf5604d commit d241c96
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/LilaHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ final class LilaHandler(
)
case ApiUserOnline(user, false) => users.tellOne(user, ClientCtrl.ApiDisconnect)

case Impersonate(user, by) => Impersonations(user, by)

case Pong(pingAt) => Monitor.ping.record("site", pingAt)
case Impersonate(user, by) => Impersonations(user, by)
case StreamersOnline(streamers: Iterable[(User.Id, String)]) => Streamer.set(streamers)
case Pong(pingAt) => Monitor.ping.record("site", pingAt)

case LilaStop(reqId) =>
logger.info("******************** LILA STOP ********************")
Expand Down
14 changes: 14 additions & 0 deletions src/main/scala/Streamer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lila.ws

// this set comes from lila/modules/streamer and is updated a few times per minute at most
object Streamer:
@volatile private var streams = Map.empty[User.Id, String]

def set(newStreams: Iterable[(User.Id, String)]): Unit =
streams = newStreams.toMap

def intersect(userIds: Set[User.Id]): Map[User.Id, String] =
streams
.collect:
case (id, name) if userIds.contains(id) => id -> name
.toMap
4 changes: 3 additions & 1 deletion src/main/scala/ipc/CrowdJson.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ final class CrowdJson(inquirers: Inquirers, mongo: Mongo, lightUserApi: LightUse
if crowd.users.isEmpty then Future.successful(Json.obj("nb" -> crowd.members))
else
Future.traverse(crowd.users.filterNot(inquirers.contains))(lightUserApi.get).map { names =>
Json.obj(
val base = Json.obj(
"nb" -> crowd.members,
"users" -> names.filterNot(isBotName),
"anons" -> crowd.anons
)
val streamers = Streamer.intersect(crowd.users.toSet)
if streamers.isEmpty then base else base ++ Json.obj("streams" -> Json.toJson(streamers))
}

private def isBotName(name: User.TitleName) = name.value.startsWith("BOT ")
Expand Down
8 changes: 8 additions & 0 deletions src/main/scala/ipc/LilaOut.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object LilaOut:
case class UnFollow(left: User.Id, right: User.Id) extends SiteOut
case class Pong(pingAt: UptimeMillis) extends SiteOut with RoundOut
case class LilaResponse(reqId: Int, body: String) extends SiteOut with RoundOut
case class StreamersOnline(streamers: Iterable[(User.Id, String)]) extends SiteOut

// lobby

Expand Down Expand Up @@ -326,6 +327,13 @@ object LilaOut:
Some(ApiUserOnline(User.Id(userId), boolean(online)))
}

case "streamers/online" =>
Some(StreamersOnline {
commas(args)
.map(_.split(':'))
.collect { case Array(userId, name) => (User.Id(userId), name) }
})

case "pong" => args.toLongOption.map(UptimeMillis.apply).map(Pong.apply)

case "boot" => Some(LilaBoot)
Expand Down

0 comments on commit d241c96

Please sign in to comment.