diff --git a/src/main/scala/service/RequestCache.scala b/src/main/scala/service/RequestCache.scala index 0f0b0f0..3747e85 100644 --- a/src/main/scala/service/RequestCache.scala +++ b/src/main/scala/service/RequestCache.scala @@ -1,7 +1,6 @@ package service import model._ -import service.SystemSettingsService.SystemSettings /** * This service is used for a view helper mainly. @@ -9,28 +8,23 @@ * It may be called many times in one request, so each method stores * its result into the cache which available during a request. */ -trait RequestCache { - - def getSystemSettings()(implicit context: app.Context): SystemSettings = - context.cache("system_settings"){ - new SystemSettingsService {}.loadSystemSettings() - } +trait RequestCache extends SystemSettingsService with AccountService with IssuesService { def getIssue(userName: String, repositoryName: String, issueId: String)(implicit context: app.Context): Option[Issue] = { context.cache(s"issue.${userName}/${repositoryName}#${issueId}"){ - new IssuesService {}.getIssue(userName, repositoryName, issueId) + super.getIssue(userName, repositoryName, issueId) } } def getAccountByUserName(userName: String)(implicit context: app.Context): Option[Account] = { context.cache(s"account.${userName}"){ - new AccountService {}.getAccountByUserName(userName) + super.getAccountByUserName(userName) } } def getAccountByMailAddress(mailAddress: String)(implicit context: app.Context): Option[Account] = { context.cache(s"account.${mailAddress}"){ - new AccountService {}.getAccountByMailAddress(mailAddress) + super.getAccountByMailAddress(mailAddress) } } } diff --git a/src/main/scala/view/AvatarImageProvider.scala b/src/main/scala/view/AvatarImageProvider.scala index bcc2d6b..1dff9ab 100644 --- a/src/main/scala/view/AvatarImageProvider.scala +++ b/src/main/scala/view/AvatarImageProvider.scala @@ -16,7 +16,7 @@ val src = if(mailAddress.isEmpty){ // by user name getAccountByUserName(userName).map { account => - if(account.image.isEmpty && getSystemSettings().gravatar){ + if(account.image.isEmpty && context.settings.gravatar){ s"""https://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress.toLowerCase)}?s=${size}""" } else { s"""${context.path}/${account.userName}/_avatar""" @@ -27,13 +27,13 @@ } else { // by mail address getAccountByMailAddress(mailAddress).map { account => - if(account.image.isEmpty && getSystemSettings().gravatar){ + if(account.image.isEmpty && context.settings.gravatar){ s"""https://www.gravatar.com/avatar/${StringUtil.md5(account.mailAddress.toLowerCase)}?s=${size}""" } else { s"""${context.path}/${account.userName}/_avatar""" } } getOrElse { - if(getSystemSettings().gravatar){ + if(context.settings.gravatar){ s"""https://www.gravatar.com/avatar/${StringUtil.md5(mailAddress.toLowerCase)}?s=${size}""" } else { s"""${context.path}/_unknown/_avatar""" diff --git a/src/test/scala/view/AvatarImageProviderSpec.scala b/src/test/scala/view/AvatarImageProviderSpec.scala index 003c00f..4a00c4f 100644 --- a/src/test/scala/view/AvatarImageProviderSpec.scala +++ b/src/test/scala/view/AvatarImageProviderSpec.scala @@ -10,53 +10,58 @@ class AvatarImageProviderSpec extends Specification { - implicit val context = app.Context("", None, null) - "getAvatarImageHtml" should { "show Gravatar image for no image account if gravatar integration is enabled" in { - val provider = new AvatarImageProviderImpl(Some(createAccount(None)), createSystemSettings(true)) + implicit val context = app.Context(createSystemSettings(true), None, null) + val provider = new AvatarImageProviderImpl(Some(createAccount(None))) provider.toHtml("user", 20).toString mustEqual "" } "show uploaded image even if gravatar integration is enabled" in { - val provider = new AvatarImageProviderImpl(Some(createAccount(Some("icon.png"))), createSystemSettings(true)) + implicit val context = app.Context(createSystemSettings(true), None, null) + val provider = new AvatarImageProviderImpl(Some(createAccount(Some("icon.png")))) provider.toHtml("user", 20).toString mustEqual "" } "show local image for no image account if gravatar integration is disabled" in { - val provider = new AvatarImageProviderImpl(Some(createAccount(None)), createSystemSettings(false)) + implicit val context = app.Context(createSystemSettings(false), None, null) + val provider = new AvatarImageProviderImpl(Some(createAccount(None))) provider.toHtml("user", 20).toString mustEqual "" } "show Gravatar image for specified mail address if gravatar integration is enabled" in { - val provider = new AvatarImageProviderImpl(None, createSystemSettings(true)) + implicit val context = app.Context(createSystemSettings(true), None, null) + val provider = new AvatarImageProviderImpl(None) provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual "" } "show unknown image for unknown user if gravatar integration is enabled" in { - val provider = new AvatarImageProviderImpl(None, createSystemSettings(true)) + implicit val context = app.Context(createSystemSettings(true), None, null) + val provider = new AvatarImageProviderImpl(None) provider.toHtml("user", 20).toString mustEqual "" } "show unknown image for specified mail address if gravatar integration is disabled" in { - val provider = new AvatarImageProviderImpl(None, createSystemSettings(false)) + implicit val context = app.Context(createSystemSettings(false), None, null) + val provider = new AvatarImageProviderImpl(None) provider.toHtml("user", 20, "hoge@hoge.com").toString mustEqual "" } "add tooltip if it's enabled" in { - val provider = new AvatarImageProviderImpl(None, createSystemSettings(false)) + implicit val context = app.Context(createSystemSettings(false), None, null) + val provider = new AvatarImageProviderImpl(None) provider.toHtml("user", 20, "hoge@hoge.com", true).toString mustEqual "" @@ -80,7 +85,7 @@ private def createSystemSettings(useGravatar: Boolean) = SystemSettings( - baseUrl = None, + baseUrl = Some(""), allowAccountRegistration = false, gravatar = useGravatar, notification = false, @@ -93,15 +98,13 @@ /** * Adapter to test AvatarImageProviderImpl. */ - class AvatarImageProviderImpl(account: Option[Account], settings: SystemSettings) - extends AvatarImageProvider with RequestCache { + class AvatarImageProviderImpl(account: Option[Account]) extends AvatarImageProvider with RequestCache { def toHtml(userName: String, size: Int, mailAddress: String = "", tooltip: Boolean = false) (implicit context: app.Context): Html = getAvatarImageHtml(userName, size, mailAddress, tooltip) override def getAccountByMailAddress(mailAddress: String)(implicit context: app.Context): Option[Account] = account override def getAccountByUserName(userName: String)(implicit context: app.Context): Option[Account] = account - override def getSystemSettings()(implicit context: app.Context): SystemSettings = settings } }