Skip to content

Use ObjectId in TracingStore where applicable #8674

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

Merged
merged 11 commits into from
Jun 23, 2025
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
2 changes: 1 addition & 1 deletion app/controllers/AuthenticationController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class AuthenticationController @Inject()(
} yield result

def accessibleBySwitching(datasetId: Option[ObjectId],
annotationId: Option[String],
annotationId: Option[ObjectId],
workflowHash: Option[String]): Action[AnyContent] = sil.SecuredAction.async {
implicit request =>
for {
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/WKRemoteTracingStoreController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ class WKRemoteTracingStoreController @Inject()(tracingStoreService: TracingStore
tracingStoreService.validateAccess(name, key) { _ =>
val report = request.body
for {
annotationId <- ObjectId.fromString(report.annotationId)
annotation <- annotationDAO.findOne(annotationId)
annotation <- annotationDAO.findOne(report.annotationId)
_ <- ensureAnnotationNotFinished(annotation)
_ <- annotationDAO.updateModified(annotation._id, Instant.now)
_ = report.statistics.map(statistics => annotationService.updateStatistics(annotation._id, statistics))
Expand Down
18 changes: 9 additions & 9 deletions app/models/annotation/AnnotationLayerPrecedence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ case class RedundantTracingProperties(
zoomLevel: Double,
userBoundingBoxes: Seq[NamedBoundingBoxProto],
editPositionAdditionalCoordinates: Seq[AdditionalCoordinateProto],
userStateBoundingBoxVisibilities: Map[String, Seq[Id32WithBool]] // UserId → Seq(bboxId, bboxIsVisible)
userStateBoundingBoxVisibilities: Map[ObjectId, Seq[Id32WithBool]] // UserId → Seq(bboxId, bboxIsVisible)
)

trait AnnotationLayerPrecedence extends FoxImplicits {
Expand Down Expand Up @@ -79,7 +79,7 @@ trait AnnotationLayerPrecedence extends FoxImplicits {
userStates: Seq[SkeletonUserStateProto],
oldPrecedenceLayerProperties: RedundantTracingProperties): Seq[SkeletonUserStateProto] = {
val adaptedExistingUserStates = userStates.map { userState =>
val userId = userState.userId
val userId = ObjectId(userState.userId)
oldPrecedenceLayerProperties.userStateBoundingBoxVisibilities.get(userId) match {
case None => userState
case Some(precedenceBboxVisibilities) =>
Expand All @@ -88,9 +88,9 @@ trait AnnotationLayerPrecedence extends FoxImplicits {
}
// We also have to create new user states for the users the old precedence layer has, but the new precedence layer is missing.
val newUserPrecedenceProperties = oldPrecedenceLayerProperties.userStateBoundingBoxVisibilities.filter(tuple =>
!userStates.exists(_.userId == tuple._1))
!userStates.exists(_.userId == tuple._1.toString))
val newUserStates = newUserPrecedenceProperties.map {
case (userId: String, boundingBoxVisibilities: Seq[Id32WithBool]) =>
case (userId: ObjectId, boundingBoxVisibilities: Seq[Id32WithBool]) =>
SkeletonTracingDefaults
.emptyUserState(userId)
.copy(
Expand All @@ -104,7 +104,7 @@ trait AnnotationLayerPrecedence extends FoxImplicits {
userStates: Seq[VolumeUserStateProto],
oldPrecedenceLayerProperties: RedundantTracingProperties): Seq[VolumeUserStateProto] = {
val adaptedExistingUserStates = userStates.map { userState =>
val userId = userState.userId
val userId = ObjectId(userState.userId)
oldPrecedenceLayerProperties.userStateBoundingBoxVisibilities.get(userId) match {
case None => userState
case Some(precedenceBboxVisibilities) =>
Expand All @@ -113,9 +113,9 @@ trait AnnotationLayerPrecedence extends FoxImplicits {
}
// We also have to create new user states for the users the old precedence layer has, but the new precedence layer is missing.
val newUserPrecedenceProperties = oldPrecedenceLayerProperties.userStateBoundingBoxVisibilities.filter(tuple =>
!userStates.exists(_.userId == tuple._1))
!userStates.exists(_.userId == tuple._1.toString))
val newUserStates = newUserPrecedenceProperties.map {
case (userId: String, boundingBoxVisibilities: Seq[Id32WithBool]) =>
case (userId: ObjectId, boundingBoxVisibilities: Seq[Id32WithBool]) =>
VolumeTracingDefaults
.emptyUserState(userId)
.copy(
Expand Down Expand Up @@ -194,7 +194,7 @@ trait AnnotationLayerPrecedence extends FoxImplicits {
s.userBoundingBoxes ++ s.userBoundingBox.map(
com.scalableminds.webknossos.datastore.geometry.NamedBoundingBoxProto(0, None, None, None, _)),
s.editPositionAdditionalCoordinates,
s.userStates.map(userState => (userState.userId, userState.boundingBoxVisibilities)).toMap
s.userStates.map(userState => (ObjectId(userState.userId), userState.boundingBoxVisibilities)).toMap
)
case Right(v) =>
RedundantTracingProperties(
Expand All @@ -204,7 +204,7 @@ trait AnnotationLayerPrecedence extends FoxImplicits {
v.userBoundingBoxes ++ v.userBoundingBox.map(
com.scalableminds.webknossos.datastore.geometry.NamedBoundingBoxProto(0, None, None, None, _)),
v.editPositionAdditionalCoordinates,
v.userStates.map(userState => (userState.userId, userState.boundingBoxVisibilities)).toMap
v.userStates.map(userState => (ObjectId(userState.userId), userState.boundingBoxVisibilities)).toMap
)
}
}
2 changes: 1 addition & 1 deletion app/models/annotation/AnnotationMerger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class AnnotationMerger @Inject()(datasetDAO: DatasetDAO, tracingStoreService: Tr
for {
dataset <- datasetDAO.findOne(datasetId)
tracingStoreClient: WKRemoteTracingStoreClient <- tracingStoreService.clientFor(dataset)
mergedAnnotationProto <- tracingStoreClient.mergeAnnotationsByIds(annotations.map(_.id),
mergedAnnotationProto <- tracingStoreClient.mergeAnnotationsByIds(annotations.map(_._id),
annotations.map(_._user),
newAnnotationId,
toTemporaryStore,
Expand Down
2 changes: 1 addition & 1 deletion app/models/annotation/AnnotationService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ class AnnotationService @Inject()(
dataStore <- dataStoreDAO.findOneByName(dataset._dataStore.trim) ?~> "datastore.notFound"
tracingStore <- tracingStoreDAO.findFirst
annotationSource = AnnotationSource(
id = annotation.id,
id = annotation._id,
annotationLayers = annotation.annotationLayers,
datasetDirectoryName = dataset.directoryName,
organizationId = organization._id,
Expand Down
6 changes: 3 additions & 3 deletions app/models/annotation/WKRemoteTracingStoreClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class WKRemoteTracingStoreClient(
.postEmpty()
}

def mergeAnnotationsByIds(annotationIds: List[String],
def mergeAnnotationsByIds(annotationIds: List[ObjectId],
ownerIds: List[ObjectId],
newAnnotationId: ObjectId,
toTemporaryStore: Boolean,
Expand All @@ -183,8 +183,8 @@ class WKRemoteTracingStoreClient(
.addQueryString("toTemporaryStore" -> toTemporaryStore.toString)
.addQueryString("newAnnotationId" -> newAnnotationId.toString)
.addQueryString("requestingUserId" -> requestingUserId.toString)
.postJsonWithProtoResponse[MergedFromIdsRequest, AnnotationProto](
MergedFromIdsRequest(annotationIds, ownerIds.map(_.toString)))(AnnotationProto)
.postJsonWithProtoResponse[MergedFromIdsRequest, AnnotationProto](MergedFromIdsRequest(annotationIds, ownerIds))(
AnnotationProto)
}

def mergeSkeletonTracingsByContents(newTracingId: String, tracings: SkeletonTracings): Fox[Unit] = {
Expand Down
18 changes: 8 additions & 10 deletions app/security/AccessibleBySwitchingService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AccessibleBySwitchingService @Inject()(

def getOrganizationToSwitchTo(user: User,
datasetId: Option[ObjectId],
annotationId: Option[String],
annotationId: Option[ObjectId],
workflowHash: Option[String])(implicit ctx: DBAccessContext): Fox[Organization] =
for {
isSuperUser <- multiUserDAO.findOne(user._multiUser).map(_.isSuperUser)
Expand All @@ -42,7 +42,7 @@ class AccessibleBySwitchingService @Inject()(
} yield selectedOrganization

private def accessibleBySwitchingForSuperUser(datasetIdOpt: Option[ObjectId],
annotationIdOpt: Option[String],
annotationIdOpt: Option[ObjectId],
workflowHashOpt: Option[String]): Fox[Organization] = {
implicit val ctx: DBAccessContext = GlobalAccessContext
(datasetIdOpt, annotationIdOpt, workflowHashOpt) match {
Expand All @@ -53,8 +53,7 @@ class AccessibleBySwitchingService @Inject()(
} yield organization
case (None, Some(annotationId), None) =>
for {
annotationObjectId <- ObjectId.fromString(annotationId)
annotation <- annotationDAO.findOne(annotationObjectId) // Note: this does not work for compound annotations.
annotation <- annotationDAO.findOne(annotationId) // Note: this does not work for compound annotations.
user <- userDAO.findOne(annotation._user)
organization <- organizationDAO.findOne(user._organization)
} yield organization
Expand All @@ -69,7 +68,7 @@ class AccessibleBySwitchingService @Inject()(

private def accessibleBySwitchingForMultiUser(multiUserId: ObjectId,
datasetIdOpt: Option[ObjectId],
annotationIdOpt: Option[String],
annotationIdOpt: Option[ObjectId],
workflowHashOpt: Option[String]): Fox[Organization] =
for {
identities <- userDAO.findAllByMultiUser(multiUserId)
Expand All @@ -80,7 +79,7 @@ class AccessibleBySwitchingService @Inject()(

private def canAccessDatasetOrAnnotationOrWorkflow(user: User,
datasetIdOpt: Option[ObjectId],
annotationIdOpt: Option[String],
annotationIdOpt: Option[ObjectId],
workflowHashOpt: Option[String]): Fox[Boolean] = {
val ctx = AuthorizedAccessContext(user)
(datasetIdOpt, annotationIdOpt, workflowHashOpt) match {
Expand All @@ -99,12 +98,11 @@ class AccessibleBySwitchingService @Inject()(
foundFox.shiftBox.map(_.isDefined)
}

private def canAccessAnnotation(user: User, ctx: DBAccessContext, annotationId: String): Fox[Boolean] = {
private def canAccessAnnotation(user: User, ctx: DBAccessContext, annotationId: ObjectId): Fox[Boolean] = {
val foundFox = for {
annotationIdParsed <- ObjectId.fromString(annotationId)
annotation <- annotationDAO.findOne(annotationIdParsed)(GlobalAccessContext)
annotation <- annotationDAO.findOne(annotationId)(GlobalAccessContext)
_ <- Fox.fromBool(annotation.state != Cancelled)
restrictions <- annotationProvider.restrictionsFor(AnnotationIdentifier(annotation.typ, annotationIdParsed))(ctx)
restrictions <- annotationProvider.restrictionsFor(AnnotationIdentifier(annotation.typ, annotationId))(ctx)
_ <- restrictions.allowAccess(user)
} yield ()
foundFox.shiftBox.map(_.isDefined)
Expand Down
10 changes: 5 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ Compile / console / scalacOptions -= "-Xlint:unused"
scapegoatIgnoredFiles := Seq(".*/Tables.scala", ".*/Routes.scala", ".*/.*mail.*template\\.scala")
scapegoatDisabledInspections := Seq("FinalModifierOnCaseClass", "UnusedMethodParameter", "UnsafeTraversableMethods")

// Allow path binding for ObjectId
routesImport += "com.scalableminds.util.objectid.ObjectId"

lazy val commonSettings = Seq(
resolvers ++= DependencyResolvers.dependencyResolvers,
Compile / doc / sources := Seq.empty,
Compile / packageDoc / publishArtifact := false
Compile / packageDoc / publishArtifact := false,
)

lazy val protocolBufferSettings = Seq(
Expand Down Expand Up @@ -92,6 +89,7 @@ lazy val webknossosDatastore = (project in file("webknossos-datastore"))
}
((libs +++ subs +++ targets) ** "*.jar").classpath
},
routesImport += "com.scalableminds.util.objectid.ObjectId",
copyMessagesFilesSetting
)

Expand All @@ -102,11 +100,12 @@ lazy val webknossosTracingstore = (project in file("webknossos-tracingstore"))
.settings(
name := "webknossos-tracingstore",
commonSettings,
routesImport += "com.scalableminds.util.objectid.ObjectId",
generateReverseRouter := false,
BuildInfoSettings.webknossosTracingstoreBuildInfoSettings,
libraryDependencies ++= Dependencies.webknossosTracingstoreDependencies,
dependencyOverrides ++= Dependencies.dependencyOverrides,
copyMessagesFilesSetting
copyMessagesFilesSetting,
)

lazy val webknossos = (project in file("."))
Expand All @@ -116,6 +115,7 @@ lazy val webknossos = (project in file("."))
.settings(
name := "webknossos",
commonSettings,
routesImport += "com.scalableminds.util.objectid.ObjectId",
generateReverseRouter := false,
AssetCompilation.settings,
BuildInfoSettings.webknossosBuildInfoSettings,
Expand Down
2 changes: 1 addition & 1 deletion conf/webknossos.latest.routes
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ GET /auth/token
DELETE /auth/token controllers.AuthenticationController.deleteToken()
GET /auth/switch controllers.AuthenticationController.switchMultiUser(to: String)
POST /auth/switchOrganization/:organizationId controllers.AuthenticationController.switchOrganization(organizationId: String)
GET /auth/accessibleBySwitching controllers.AuthenticationController.accessibleBySwitching(datasetId: Option[ObjectId], annotationId: Option[String], workflowHash: Option[String])
GET /auth/accessibleBySwitching controllers.AuthenticationController.accessibleBySwitching(datasetId: Option[ObjectId], annotationId: Option[ObjectId], workflowHash: Option[String])
POST /auth/sendInvites controllers.AuthenticationController.sendInvites()
POST /auth/startResetPassword controllers.AuthenticationController.handleStartResetPassword()
POST /auth/changePassword controllers.AuthenticationController.changePassword()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ describe("Annotation API (E2E)", () => {
[UpdateActions.updateCameraAnnotation([2, 3, 4], null, [1, 2, 3], 2)],
],
123456789,
null,
true,
),
0,
);
Expand Down Expand Up @@ -246,6 +248,8 @@ describe("Annotation API (E2E)", () => {
createSaveQueueFromUpdateActions(
[createTreesUpdateActions, [updateTreeGroupsUpdateAction]],
123456789,
null,
true,
),
0,
);
Expand Down Expand Up @@ -284,7 +288,12 @@ describe("Annotation API (E2E)", () => {

const updateTreeAction = UpdateActions.updateTree(trees.getOrThrow(1), tracingId);
const [saveQueue] = addVersionNumbers(
createSaveQueueFromUpdateActions([createTreesUpdateActions, [updateTreeAction]], 123456789),
createSaveQueueFromUpdateActions(
[createTreesUpdateActions, [updateTreeAction]],
123456789,
null,
true,
),
0,
);

Expand All @@ -301,6 +310,8 @@ describe("Annotation API (E2E)", () => {
createSaveQueueFromUpdateActions(
[[UpdateActions.updateMetadataOfAnnotation(newDescription)]],
123456789,
null,
true,
),
0,
);
Expand Down
3 changes: 3 additions & 0 deletions frontend/javascripts/test/e2e-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const tokenUserFInOrgaX =

let currentUserAuthToken = tokenUserA;

const idUserA = "570b9f4d2a7c0e4d008da6ef";

function setUserAuthToken(token: string) {
currentUserAuthToken = token;
}
Expand Down Expand Up @@ -163,4 +165,5 @@ export {
tokenUserFInOrgaX,
tokenUserFInOrgaY,
setUserAuthToken,
idUserA,
};
4 changes: 3 additions & 1 deletion frontend/javascripts/test/helpers/saveHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { TracingStats } from "viewer/model/accessors/annotation_accessor";
import type { UpdateActionWithoutIsolationRequirement } from "viewer/model/sagas/update_actions";
import type { SaveQueueEntry } from "viewer/store";
import { idUserA } from "test/e2e-setup";
import dummyUser from "test/fixtures/dummy_user";

export function createSaveQueueFromUpdateActions(
updateActions: UpdateActionWithoutIsolationRequirement[][],
timestamp: number,
stats: TracingStats | null = null,
useE2eAuthorId: boolean = false,
): SaveQueueEntry[] {
return updateActions.map((ua) => ({
version: -1,
Expand All @@ -15,7 +17,7 @@ export function createSaveQueueFromUpdateActions(
actions: ua,
info: "[]",
transactionGroupCount: 1,
authorId: dummyUser.id,
authorId: useE2eAuthorId ? idUserA : dummyUser.id,
transactionGroupIndex: 0,
transactionId: "dummyRequestId",
}));
Expand Down
Loading