Skip to content
Closed
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
178 changes: 0 additions & 178 deletions .github/workspace-dep-closures.json

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions crates/macro_db_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ default = ["account_merge_request", "team"]
team = ["dep:models_team"]

[dependencies]
messages = { path = "../messages", features = ["outbound", "annotations"] }
agent = { path = "../agent" }
anyhow = { workspace = true }
rootcause = { workspace = true }
cached = { workspace = true }
chrono = { workspace = true }
document_sub_type = { path = "../document_sub_type" }
Expand Down Expand Up @@ -42,4 +40,4 @@ uuid = { workspace = true }
workspace-hack = { version = "0.1", path = "../workspace-hack" }

[dev-dependencies]
entity_access = { path = "../entity_access", default-features = false, features = ["outbound"] }
messages = { path = "../messages", features = ["outbound", "annotations"] }
5 changes: 0 additions & 5 deletions crates/macro_db_client/src/annotations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
pub mod create_anchor;
pub mod delete_anchor;
pub mod edit_anchor;
pub mod get;

/// Authorized PDF geometry persistence for the annotation domain.
pub mod repository;
2 changes: 2 additions & 0 deletions crates/messages/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ uuid = { workspace = true }
workspace-hack = { version = "0.1", path = "../workspace-hack" }

[dev-dependencies]
entity_access = { path = "../entity_access", default-features = false, features = ["outbound"] }
macro_db_client = { path = "../macro_db_client" }
initiative = { path = "../initiative", default-features = false, features = ["postgres"] }
macro_db_migrator = { path = "../macro_db_migrator" }
models_permissions = { path = "../models_permissions" }
Expand Down
3 changes: 3 additions & 0 deletions crates/messages/src/outbound/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub mod entity_access_audience;
/// Contextual notification transport.
#[cfg(feature = "delivery")]
pub mod notification_sender;
/// Postgres persistence for PDF annotation geometry.
#[cfg(all(feature = "outbound", feature = "annotations"))]
pub mod pg_annotation_repo;
/// Parent and thread audience facts.
#[cfg(feature = "delivery")]
pub mod pg_discussion_context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use messages::domain::{
use crate::domain::{
annotations::{AnnotationMutation, AnnotationRepository, AnnotationTarget, DeletedAnnotation},
ports::MessageError,
};
Expand All @@ -9,6 +9,10 @@ use model::annotations::{
use sqlx::PgPool;
use uuid::Uuid;

mod delete;
mod edit;

/// Authorized PDF geometry persistence for the annotation domain.
pub struct PgAnnotationRepository(pub PgPool);

impl AnnotationRepository for PgAnnotationRepository {
Expand All @@ -21,7 +25,7 @@ impl AnnotationRepository for PgAnnotationRepository {
SELECT a.uuid, a."documentId", a.owner, a.root_id, t.user_id
FROM "PdfPlaceableCommentAnchor" a JOIN comms_message_threads t ON t.root_id = a.root_id
WHERE NOT $2 AND a.uuid = $1 AND t.deleted_at IS NULL
"#, id, highlight).fetch_optional(&self.0).await.map_err(|e| MessageError::Repository(e.into()))?.ok_or(MessageError::NotFound)?;
"#, id, highlight).fetch_optional(&self.0).await.map_err(repository_error)?.ok_or(MessageError::NotFound)?;
Ok(AnnotationTarget {
id: row.uuid,
document_id: row.document_id,
Expand All @@ -35,17 +39,22 @@ impl AnnotationRepository for PgAnnotationRepository {
access: AnnotationMutation,
input: EditAnchorRequest,
) -> Result<EditAnchorResponse, MessageError> {
super::edit_anchor::edit_document_anchor(&self.0, access, input)
.await
.map_err(|e| MessageError::Repository(rootcause::report!("{e}")))
edit::edit_document_anchor(&self.0, access, input).await
}
async fn delete(
&self,
access: AnnotationMutation,
input: DeleteUnthreadedAnchorRequest,
) -> Result<DeletedAnnotation, MessageError> {
super::delete_anchor::delete_document_anchor(&self.0, access, input)
.await
.map_err(|e| MessageError::Repository(rootcause::report!("{e}")))
delete::delete_document_anchor(&self.0, access, input).await
}
}

fn repository_error(error: sqlx::Error) -> MessageError {
MessageError::Repository(rootcause::Report::new(error).into())
}

/// A capability that no longer matches the stored annotation.
fn stale_capability(reason: &'static str) -> MessageError {
MessageError::Repository(rootcause::report!("{reason}"))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use anyhow::{Result, ensure};
use messages::domain::annotations::{AnnotationMutation, DeletedAnnotation};
use super::{repository_error, stale_capability};
use crate::domain::{
annotations::{AnnotationMutation, DeletedAnnotation},
models::MessageParent,
ports::MessageError,
};
use crate::outbound::pg_message_repo::PgMessageRepository;
use model::annotations::{
AnchorId, PdfAnchorId,
delete::{
Expand All @@ -16,10 +21,10 @@ pub async fn delete_document_anchor(
db: &Pool<Postgres>,
access: AnnotationMutation,
req: DeleteUnthreadedAnchorRequest,
) -> Result<DeletedAnnotation> {
) -> Result<DeletedAnnotation, MessageError> {
let thread_id: Option<Uuid>;
let document_id: String;
let mut transaction = db.begin().await?;
let mut transaction = db.begin().await.map_err(repository_error)?;

let anchor_info: AnchorId;
match req {
Expand All @@ -31,20 +36,14 @@ pub async fn delete_document_anchor(
}

let thread = if let Some(thread_id) = thread_id {
let parent = messages::domain::models::MessageParent::parse("document", &document_id)?;
Some(
messages::outbound::pg_message_repo::PgMessageRepository::delete_thread_in(
&mut transaction,
&parent,
thread_id,
)
.await?,
)
let parent = MessageParent::parse("document", &document_id)
.map_err(|e| MessageError::Repository(rootcause::Report::new(e).into()))?;
Some(PgMessageRepository::delete_thread_in(&mut transaction, &parent, thread_id).await?)
} else {
None
};

transaction.commit().await?;
transaction.commit().await.map_err(repository_error)?;

Ok(DeletedAnnotation {
response: DeleteUnthreadedAnchorResponse {
Expand All @@ -60,22 +59,26 @@ async fn delete_pdf_highlight_anchor(
transaction: &mut Transaction<'_, Postgres>,
access: AnnotationMutation,
uuid: Uuid,
) -> Result<(Option<Uuid>, String)> {
ensure!(access.target().id == uuid, "annotation capability mismatch");
) -> Result<(Option<Uuid>, String), MessageError> {
if access.target().id != uuid {
return Err(stale_capability("annotation capability mismatch"));
}
let row = sqlx::query!(r#"SELECT root_id AS thread_id, "documentId" AS document_id
FROM "PdfHighlightAnchor" WHERE uuid = $1 AND "documentId" = $2 AND "deletedAt" IS NULL FOR UPDATE"#,
uuid, access.target().document_id).fetch_one(transaction.as_mut()).await?;
ensure!(
row.thread_id == access.target().thread_id,
"annotation discussion changed; retry authorization"
);
uuid, access.target().document_id).fetch_one(transaction.as_mut()).await.map_err(repository_error)?;
if row.thread_id != access.target().thread_id {
return Err(stale_capability(
"annotation discussion changed; retry authorization",
));
}
sqlx::query!(
r#"UPDATE "PdfHighlightAnchor" SET "deletedAt" = NOW()
WHERE uuid = $1 AND "deletedAt" IS NULL"#,
uuid
)
.execute(transaction.as_mut())
.await?;
.await
.map_err(repository_error)?;

Ok((row.thread_id, row.document_id))
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::*;
use messages::domain::ports::{MessageChange, MessageError, MessageEvent, MessageEventPublisher};
use crate::domain::ports::{MessageChange, MessageError, MessageEvent, MessageEventPublisher};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use sqlx::PgPool;
use std::sync::{Arc, Mutex};

Expand All @@ -19,7 +20,13 @@ fn request() -> DeleteUnthreadedAnchorRequest {
))
}

#[sqlx::test(fixtures(path = "../../../fixtures", scripts("message_annotations")))]
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(
path = "../../../../../macro_db_client/fixtures",
scripts("message_annotations")
)
)]
async fn another_user_cannot_delete_annotation_or_its_discussion(pool: PgPool) {
let events = RecordedEvents::default();
let result =
Expand All @@ -35,7 +42,13 @@ async fn another_user_cannot_delete_annotation_or_its_discussion(pool: PgPool) {
assert_eq!(roots, 4);
}

#[sqlx::test(fixtures(path = "../../../fixtures", scripts("message_annotations")))]
#[sqlx::test(
migrator = "MACRO_DB_MIGRATIONS",
fixtures(
path = "../../../../../macro_db_client/fixtures",
scripts("message_annotations")
)
)]
async fn deleting_threaded_annotation_uses_shared_thread_tombstones(pool: PgPool) {
let events = RecordedEvents::default();
let response = delete_document_anchor(&pool, "macro|user@user.com", request(), events.clone())
Expand Down Expand Up @@ -71,10 +84,12 @@ async fn deleting_threaded_annotation_uses_shared_thread_tombstones(pool: PgPool
assert!(state.deleted_at.is_some());
assert!(state.anchor.is_none());
drop(emitted);
let highlights =
crate::annotations::get::fetch_pdf_highlight_anchors(&pool, "document-with-comments")
.await
.unwrap();
let highlights = macro_db_client::annotations::get::fetch_pdf_highlight_anchors(
&pool,
"document-with-comments",
)
.await
.unwrap();
assert_eq!(highlights.len(), 1);
assert_eq!(highlights[0].text, "Standalone underline");
}
Expand All @@ -85,8 +100,8 @@ async fn delete_document_anchor(
request: DeleteUnthreadedAnchorRequest,
events: impl MessageEventPublisher,
) -> std::result::Result<DeleteUnthreadedAnchorResponse, MessageError> {
messages::domain::annotations::AnnotationService::new(
crate::annotations::repository::PgAnnotationRepository(pool.clone()),
crate::domain::annotations::AnnotationService::new(
super::super::PgAnnotationRepository(pool.clone()),
entity_access::domain::service::EntityAccessServiceImpl::new(
entity_access::outbound::PgAccessRepository::new(pool.clone()),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Result, ensure};
use messages::domain::annotations::AnnotationMutation;
use super::{repository_error, stale_capability};
use crate::domain::{annotations::AnnotationMutation, ports::MessageError};
use model::annotations::{
Anchor, PdfAnchor, PdfPlaceableCommentAnchor,
edit::{
Expand All @@ -13,7 +13,7 @@ pub async fn edit_document_anchor(
db: &Pool<Postgres>,
access: AnnotationMutation,
req: EditAnchorRequest,
) -> Result<EditAnchorResponse> {
) -> Result<EditAnchorResponse, MessageError> {
match req {
EditAnchorRequest::Pdf(EditPdfAnchorRequest::FreeComment(request)) => {
let anchor = edit_pdf_free_comment_anchor(db, access, request).await?;
Expand All @@ -29,11 +29,10 @@ async fn edit_pdf_free_comment_anchor(
db: &Pool<Postgres>,
access: AnnotationMutation,
request: EditPdfPlaceableCommentAnchorRequest,
) -> Result<PdfPlaceableCommentAnchor> {
ensure!(
access.target().id == request.uuid,
"annotation capability mismatch"
);
) -> Result<PdfPlaceableCommentAnchor, MessageError> {
if access.target().id != request.uuid {
return Err(stale_capability("annotation capability mismatch"));
}
let anchor = sqlx::query_as!(
PdfPlaceableCommentAnchor,
r#"
Expand Down Expand Up @@ -85,7 +84,8 @@ async fn edit_pdf_free_comment_anchor(
access.target().document_id,
)
.fetch_one(db)
.await?;
.await
.map_err(repository_error)?;

Ok(anchor)
}
Expand Down
Loading
Loading