Skip to content

Commit

Permalink
simplify filters
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinBeczak committed Jan 2, 2024
1 parent bc07d27 commit 7d3f912
Showing 1 changed file with 82 additions and 86 deletions.
168 changes: 82 additions & 86 deletions app/org/maproulette/framework/service/ChallengeListingService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ChallengeListingService @Inject() (
def retrieve(parentId: Long): Option[ChallengeListing] =
this.query(Query.simple(List(BaseParameter(Challenge.FIELD_PARENT_ID, parentId)))).headOption

def emptyFilterGroup: FilterGroup = FilterGroup(List.empty)

/**
* Returns a list of challenges that have reviews/review requests.
*
Expand Down Expand Up @@ -57,9 +59,7 @@ class ChallengeListingService @Inject() (
table = Some(Challenge.TABLE)
)
)
} else {
None
}
} else None

val projectNameParameter =
if (projectSearchQuery.nonEmpty) {
Expand All @@ -71,98 +71,94 @@ class ChallengeListingService @Inject() (
table = Some(Project.TABLE)
)
)
} else {
None
}
} else None

val parameters = List(challengeNameParameter, projectNameParameter).flatten

val filterGroup =
if (parameters.nonEmpty) {
FilterGroup(parameters)
} else {
FilterGroup(List())
}
if (parameters.nonEmpty) FilterGroup(parameters)
else emptyFilterGroup

val taskReviewFilterGroup = FilterGroup(
List(
// Has a task review
BaseParameter(
TaskReview.FIELD_ID,
"",
Operator.NULL,
negate = true,
table = Some(TaskReview.TABLE)
),
// Exclude unnecessary review status
BaseParameter(
"review_status",
Task.REVIEW_STATUS_UNNECESSARY,
Operator.EQ,
negate = true,
table = Some(TaskReview.TABLE)
),
// Task Status in list if given a list of task statuses
FilterParameter.conditional(
"status",
taskStatus.getOrElse(List.empty),
Operator.IN,
includeOnlyIfTrue = taskStatus.getOrElse(List.empty).nonEmpty,
table = Some("tasks")
),
// review_requested_by != user.id unless a super user
// to be reviewed tasks (review type = 1)
FilterParameter.conditional(
TaskReview.FIELD_REVIEW_REQUESTED_BY,
user.id,
negate = true,
includeOnlyIfTrue = (reviewTasksType == 1) && !permission.isSuperUser(user),
table = Some(TaskReview.TABLE)
),
// reviewed_by == user.id for 'tasks reviewed by me' (review type = 3)
FilterParameter.conditional(
TaskReview.FIELD_REVIEWED_BY,
user.id,
includeOnlyIfTrue = reviewTasksType == 2,
table = Some(TaskReview.TABLE)
),
// reviewed_by == user.id for 'my reviewed tasks' (review type = 2)
FilterParameter.conditional(
TaskReview.FIELD_REVIEW_REQUESTED_BY,
user.id,
includeOnlyIfTrue = reviewTasksType == 3,
table = Some(TaskReview.TABLE)
),
// review status = requested or disputed if reviewTasksType = 1
FilterParameter.conditional(
TaskReview.FIELD_REVIEW_STATUS,
List(Task.REVIEW_STATUS_REQUESTED, Task.REVIEW_STATUS_DISPUTED),
Operator.IN,
includeOnlyIfTrue = reviewTasksType == 1,
table = Some(TaskReview.TABLE)
)
)
)

val excludeOtherReviewersFilterGroup = FilterGroup(
List(
BaseParameter(
TaskReview.FIELD_REVIEWED_BY,
"",
Operator.NULL,
table = Some(TaskReview.TABLE)
),
BaseParameter(TaskReview.FIELD_REVIEWED_BY, user.id, table = Some(TaskReview.TABLE))
),
OR(),
excludeOtherReviewers && reviewTasksType == 1
)

val filter =
Filter(
List(
filterGroup,
FilterGroup(
List(
// Has a task review
BaseParameter(
TaskReview.FIELD_ID,
"",
Operator.NULL,
negate = true,
table = Some(TaskReview.TABLE)
),
// Exclude unnecessary review status
BaseParameter(
"review_status",
Task.REVIEW_STATUS_UNNECESSARY,
Operator.EQ,
negate = true,
table = Some(TaskReview.TABLE)
),
// Task Status in list if given a list of task statuses
FilterParameter.conditional(
"status",
taskStatus.getOrElse(List.empty),
Operator.IN,
includeOnlyIfTrue = taskStatus.nonEmpty,
table = Some("tasks")
),
// review_requested_by != user.id unless a super user
// to be reviewed tasks (review type = 1)
FilterParameter.conditional(
TaskReview.FIELD_REVIEW_REQUESTED_BY,
user.id,
negate = true,
includeOnlyIfTrue = (reviewTasksType == 1) && !permission.isSuperUser(user),
table = Some(TaskReview.TABLE)
),
// reviewed_by == user.id for 'tasks reviewed by me' (review type = 3)
FilterParameter.conditional(
TaskReview.FIELD_REVIEWED_BY,
user.id,
includeOnlyIfTrue = reviewTasksType == 2,
table = Some(TaskReview.TABLE)
),
// reviewed_by == user.id for 'my reviewed tasks' (review type = 2)
FilterParameter.conditional(
TaskReview.FIELD_REVIEW_REQUESTED_BY,
user.id,
includeOnlyIfTrue = reviewTasksType == 3,
table = Some(TaskReview.TABLE)
),
// review status = requested or disputed if reviewTasksType = 1
FilterParameter.conditional(
TaskReview.FIELD_REVIEW_STATUS,
List(Task.REVIEW_STATUS_REQUESTED, Task.REVIEW_STATUS_DISPUTED),
Operator.IN,
includeOnlyIfTrue = reviewTasksType == 1,
table = Some(TaskReview.TABLE)
)
)
),
// Check project/challenge visibility
taskReviewFilterGroup,
challengeService.challengeVisibilityFilter(user),
// reviewed_by is empty or user.id if excludeOtherReviewers
FilterGroup(
List(
BaseParameter(
TaskReview.FIELD_REVIEWED_BY,
"",
Operator.NULL,
table = Some(TaskReview.TABLE)
),
BaseParameter(TaskReview.FIELD_REVIEWED_BY, user.id, table = Some(TaskReview.TABLE))
),
OR(),
excludeOtherReviewers && reviewTasksType == 1
)
excludeOtherReviewersFilterGroup
)
)
this.query(
Expand Down

0 comments on commit 7d3f912

Please sign in to comment.