diff --git a/Source/Plugins/Core/com.equella.core/scalasrc/com/tle/web/api/search/SearchHelper.scala b/Source/Plugins/Core/com.equella.core/scalasrc/com/tle/web/api/search/SearchHelper.scala index 0caf793317..99b0d3b9e3 100644 --- a/Source/Plugins/Core/com.equella.core/scalasrc/com/tle/web/api/search/SearchHelper.scala +++ b/Source/Plugins/Core/com.equella.core/scalasrc/com/tle/web/api/search/SearchHelper.scala @@ -295,6 +295,7 @@ object SearchHelper { beans.asScala // Filter out restricted attachments if the user does not have permissions to view them .filter(a => !a.isRestricted || hasRestrictedAttachmentPrivileges) + .map(sanitiseAttachmentBean) .map(att => { val broken = recurseBrokenAttachmentCheck(itemKey, att.getUuid) def ifNotBroken[T](f: () => Option[T]) = if (!broken) f() else None @@ -355,6 +356,21 @@ object SearchHelper { } } + /** + * Find out the latest version of the Item which a Custom Attachment points to. + * + * @param version Version of a linked Item. It is either 0 or 1 where 0 means using the latest version + * and 1 means always using version 1. + * @param uuid UUID of the linked Item. + */ + def getLatestVersionForCustomAttachment(version: Int, uuid: String): Int = { + version match { + // If version of is 0, find the real latest version of this Item. + case 0 => LegacyGuice.itemService.getLatestVersion(uuid) + case realVersion => realVersion + } + } + /** * Determines if a given customAttachment is invalid. Required as these attachments can be recursive. * @param customAttachment The attachment to check. @@ -362,14 +378,11 @@ object SearchHelper { */ def getBrokenAttachmentStatusForResourceAttachment( customAttachment: CustomAttachment): Boolean = { - val uuid = customAttachment.getData("uuid").asInstanceOf[String] - // If version of the linked Item is 0, find the latest version of this Item. - val version = customAttachment.getData("version").asInstanceOf[Int] match { - case 0 => LegacyGuice.itemService.getLatestVersion(uuid) - case realVersion => realVersion - } + val uuid = customAttachment.getData("uuid").asInstanceOf[String] + val version = customAttachment.getData("version").asInstanceOf[Int] + + val key = new ItemId(uuid, getLatestVersionForCustomAttachment(version, uuid)) - val key = new ItemId(uuid, version) if (customAttachment.getType != "resource") { return false; } @@ -488,4 +501,23 @@ object SearchHelper { */ def getAttachmentDescription(itemKey: ItemKey, attachmentUuid: String): Option[String] = Option(LegacyGuice.itemService.getAttachmentForUuid(itemKey, attachmentUuid).getDescription) + + /** + * When an AttachmentBean is converted to SearchResultAttachment, it may require some extra sanitising + * to complete the conversion. The sanitising work includes tasks listed below. + * + * 1. Help ResourceAttachmentBean check the version of its linked resource. + * + * @param att An AttachmentBean to be sanitised. + */ + def sanitiseAttachmentBean(att: AttachmentBean): AttachmentBean = { + att match { + case bean: ResourceAttachmentBean => + val latestVersion = + getLatestVersionForCustomAttachment(bean.getItemVersion, bean.getItemUuid) + bean.setItemVersion(latestVersion) + case _ => + } + att + } } diff --git a/build.sbt b/build.sbt index d6207b92ec..052c9d7625 100644 --- a/build.sbt +++ b/build.sbt @@ -115,7 +115,7 @@ name := "Equella" (ThisBuild / equellaMajor) := 2021 (ThisBuild / equellaMinor) := 2 -(ThisBuild / equellaPatch) := 1 +(ThisBuild / equellaPatch) := 2 (ThisBuild / equellaStream) := "Stable" (ThisBuild / equellaBuild) := buildConfig.value.getString("build.buildname") (ThisBuild / buildTimestamp) := Instant.now().getEpochSecond