Skip to content

Commit

Permalink
Require user for leaderboard and topChallenges
Browse files Browse the repository at this point in the history
The below routes were updated to raise an http 404 when the :id does not exist, and corrections were made to the api docs.

- /data/user/:id/leaderboard
- /data/user/:id/topChallenges
  • Loading branch information
ljdelight committed Jan 8, 2024
1 parent 8406fc9 commit 1cf167a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
18 changes: 15 additions & 3 deletions app/org/maproulette/framework/service/LeaderboardService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ package org.maproulette.framework.service
import javax.inject.{Inject, Singleton}
import org.joda.time.DateTime
import org.maproulette.Config
import org.maproulette.framework.model.{User, LeaderboardUser, LeaderboardChallenge, Task}
import org.maproulette.exception.NotFoundException
import org.maproulette.framework.model.{LeaderboardChallenge, LeaderboardUser, Task, User}
import org.maproulette.framework.mixins.LeaderboardMixin
import org.maproulette.framework.repository.{LeaderboardRepository, ChallengeRepository}
import org.maproulette.framework.repository.{ChallengeRepository, LeaderboardRepository}
import org.maproulette.framework.psql._
import org.maproulette.framework.psql.filter._
import org.maproulette.session.SearchLeaderboardParameters
Expand All @@ -26,7 +27,8 @@ class LeaderboardService @Inject() (
repository: LeaderboardRepository,
challengeRepository: ChallengeRepository,
config: Config,
boundingBoxFinder: BoundingBoxFinder
boundingBoxFinder: BoundingBoxFinder,
userService: UserService
) extends LeaderboardMixin {

/**
Expand Down Expand Up @@ -192,6 +194,11 @@ class LeaderboardService @Inject() (
params: SearchLeaderboardParameters,
bracket: Int = 0
): List[LeaderboardUser] = {
// The userId must exist and must not be a system user, otherwise return NotFound (http 404).
if (userId <= 0 || this.userService.retrieve(userId).isEmpty) {
throw new NotFoundException(s"No user found with id $userId")
}

// We can attempt to use the pre-built user_leaderboard table if we have no user,
// no project and no challenge filters (and have a specified monthDuration).
if (params.projectFilter == None && params.challengeFilter == None && params.onlyEnabled && params.monthDuration != None &&
Expand Down Expand Up @@ -259,6 +266,11 @@ class LeaderboardService @Inject() (
limit: Int = Config.DEFAULT_LIST_SIZE,
offset: Int = 0
): List[LeaderboardChallenge] = {
// The userId must exist and must not be a system user, otherwise return NotFound (http 404).
if (userId <= 0 || this.userService.retrieve(userId).isEmpty) {
throw new NotFoundException(s"No user found with id $userId")
}

// We can attempt to use the pre-built top challenges table if we have no user,
// no project and no challenge filters (and have a specified monthDuration).
if (params.projectFilter == None && params.challengeFilter == None && params.onlyEnabled && params.monthDuration != None &&
Expand Down
16 changes: 14 additions & 2 deletions conf/v2_route/leaderboard.api
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,15 @@ GET /data/user/leaderboard @org.maproulette.framework.c
# responses:
# '200':
# description: List of leaderboard stats
# content:
# application/json:
# schema:
# $ref: '#/components/schemas/org.maproulette.framework.model.LeaderboardUser'
# '404':
# description: User not found
# parameters:
# - name: userId
# in: query
# in: path
# description: User id to fetch ranking for.
# schema:
# type: integer
Expand Down Expand Up @@ -134,9 +140,15 @@ GET /data/user/:userId/leaderboard @org.maproulette.framework.c
# responses:
# '200':
# description: Brief list of challenges
# content:
# application/json:
# schema:
# $ref: '#/components/schemas/org.maproulette.framework.model.LeaderboardChallenge'
# '404':
# description: User not found
# parameters:
# - name: userId
# in: query
# in: path
# description: User id to fetch challenges for.
# schema:
# type: integer
Expand Down

0 comments on commit 1cf167a

Please sign in to comment.