Skip to content

Commit

Permalink
code golf and type safety
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Feb 18, 2025
1 parent c90bc95 commit 36c128c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
5 changes: 1 addition & 4 deletions app/mashup/UserInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ object UserInfo:
postApi.nbByUser(user.id).mon(_.user.segment("nbForumPosts"))
},
withUblog.so(ublogApi.userBlogPreviewFor(user, 3)),
studyRepo
.countByOwner(user.id, ctx.me.map(_.userId))
.recoverDefault
.mon(_.user.segment("nbStudies")),
studyRepo.countByOwner(user.id).recoverDefault.mon(_.user.segment("nbStudies")),
simulApi.countHostedByUser.get(user.id).mon(_.user.segment("nbSimuls")),
relayApi.countOwnedByUser.get(user.id).mon(_.user.segment("nbBroadcasts")),
teamApi.joinedTeamIdsOfUserAsSeenBy(user).mon(_.user.segment("teamIds")),
Expand Down
10 changes: 4 additions & 6 deletions modules/study/src/main/StudyRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,11 @@ final class StudyRepo(private[study] val coll: AsyncColl)(using
def selectBroadcast = selectTopic(StudyTopic.broadcast)
private[study] def selectNotBroadcast = $doc(F.topics.$ne(StudyTopic.broadcast))

private[study] def hasMemberOrIsPublic(userId: Option[UserId]) = userId match
case None => selectPublic
case Some(u) => $or($doc(s"members.${u}".$exists(true)), selectPublic)
private def hasMemberOrIsPublic(using as: Option[MyId]) = as.fold(selectPublic): me =>
$or($doc(s"members.$me".$exists(true)), selectPublic)

def countByOwner(ownerId: UserId, viewerId: Option[UserId]) = coll(
_.countSel(selectOwnerId(ownerId) ++ hasMemberOrIsPublic(viewerId))
)
def countByOwner(ownerId: UserId)(using Option[MyId]) = coll:
_.countSel(selectOwnerId(ownerId) ++ hasMemberOrIsPublic)

def sourceByOwner(ownerId: UserId, isMe: Boolean): Source[Study, ?] =
Source.futureSource:
Expand Down

0 comments on commit 36c128c

Please sign in to comment.