Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add system notices endpoint #1077

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/org/maproulette/Config.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ class Config @Inject() (implicit val configuration: Configuration) {
this.config.getOptional[Int](Config.KEY_TASK_RESET).getOrElse(Config.DEFAULT_TASK_RESET)
lazy val signIn: Boolean =
this.config.getOptional[Boolean](Config.KEY_SIGNIN).getOrElse(Config.DEFAULT_SIGNIN)
lazy val systemNoticesUrl: String = this.config
.getOptional[String](Config.KEY_SYSTEM_NOTICES_URL)
.getOrElse(Config.DEFAULT_SYSTEM_NOTICES_URL)

//caching properties
lazy val cacheType: String =
Expand Down Expand Up @@ -282,6 +285,7 @@ object Config {
val KEY_TASK_BASE_COMPLETED_TIME_SPENT = s"$GROUP_MAPROULETTE.task.base_completed_time_spent"
val KEY_TASK_MAX_TASKS_PER_CHALLENGE = s"$GROUP_MAPROULETTE.task.max_tasks_per_challenge"
val KEY_REVIEW_NEEDED_DEFAULT = s"$GROUP_MAPROULETTE.review.default"
val KEY_SYSTEM_NOTICES_URL = s"$GROUP_MAPROULETTE.systemNoticesUrl"

val SUB_GROUP_SCHEDULER = s"$GROUP_MAPROULETTE.scheduler"

Expand Down Expand Up @@ -400,6 +404,7 @@ object Config {
val DEFAULT_MAX_TASKS_PER_CHALLENGE = 50000
val DEFAULT_ARCHIVE_STALE_TIME_IN_MONTHS = 6
val DEFAULT_TASK_BASE_COMPLETED_TIME_SPENT = 14000
val DEFAULT_SYSTEM_NOTICES_URL = ""

// Redis Cache id's for different caches
val CACHE_ID_TAGS = "1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ package org.maproulette.framework.controller

import javax.inject.Inject
import org.maproulette.exception.StatusMessage
import org.maproulette.framework.service.{NotificationService}
import org.maproulette.framework.model.{NotificationSubscriptions}
import org.maproulette.framework.service.NotificationService
import org.maproulette.framework.model.NotificationSubscriptions
import org.maproulette.framework.psql.{Order, OrderField, Paging}
import org.maproulette.session.SessionManager
import org.maproulette.utils.Crypto
import org.maproulette.Config
import play.api.libs.json._
import play.api.mvc._
import play.api.libs.ws.WSClient
import scala.concurrent.ExecutionContext
import scala.concurrent.Future

/**
* @author nrotstan
Expand All @@ -22,7 +26,9 @@ class NotificationController @Inject() (
sessionManager: SessionManager,
components: ControllerComponents,
bodyParsers: PlayBodyParsers,
crypto: Crypto
crypto: Crypto,
wsClient: WSClient,
config: Config
) extends AbstractController(components)
with DefaultWrites {

Expand Down Expand Up @@ -107,4 +113,41 @@ class NotificationController @Inject() (
Ok(Json.toJson(StatusMessage("OK", JsString(s"Subscriptions updated"))))
}
}

implicit val ec: ExecutionContext = scala.concurrent.ExecutionContext.global
def getAnnouncements(): Action[AnyContent] = Action.async { implicit request =>
val url = config.systemNoticesUrl

if (!url.isEmpty) {
wsClient
.url(url)
.withHttpHeaders(
"Accept" -> "application/json"
)
.get()
.map { response =>
if (response.status == 200) {
try {
val json = response.json
Ok(Json.toJson(StatusMessage("OK", json)))
} catch {
case _: Throwable =>
InternalServerError(Json.toJson("An error occurred: Invalid JSON response"))
}
} else {
InternalServerError(Json.toJson(s"An error occurred: ${response.status}"))
}
}
.recover {
case e: Exception =>
InternalServerError(Json.toJson(s"An error occurred: ${e.getMessage}"))
}
} else {
Future.successful(
InternalServerError(
Json.toJson("An error occurred: System notices not set up on this server")
)
)
}
}
}
6 changes: 6 additions & 0 deletions conf/v2_route/notification.api
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,9 @@ GET /user/:userId/notificationSubscriptions @org.maproulette.framework.c
# $ref: '#/components/schemas/org.maproulette.framework.model.NotificationSubscriptions'
###
PUT /user/:userId/notificationSubscriptions @org.maproulette.framework.controller.NotificationController.updateNotificationSubscriptions(userId:Long)
###
# tags: [ Notification ]
# summary: Retrieves System Notices
# description: Retrieves system notices set up from a third party
###
GET /user/announcements @org.maproulette.framework.controller.NotificationController.getAnnouncements()