fix(postgres): accept_note returns ThoughtId via SELECT after INSERT
This commit is contained in:
@@ -220,7 +220,7 @@ impl ActivityPubRepository for PgActivityPubRepository {
|
|||||||
content_warning: Option<String>,
|
content_warning: Option<String>,
|
||||||
visibility: &str,
|
visibility: &str,
|
||||||
in_reply_to: Option<&str>,
|
in_reply_to: Option<&str>,
|
||||||
) -> Result<(), DomainError> {
|
) -> Result<ThoughtId, DomainError> {
|
||||||
let capped: String = content.chars().take(MAX_REMOTE_CONTENT_CHARS).collect();
|
let capped: String = content.chars().take(MAX_REMOTE_CONTENT_CHARS).collect();
|
||||||
let (in_reply_to_id, in_reply_to_url) = match in_reply_to {
|
let (in_reply_to_id, in_reply_to_url) = match in_reply_to {
|
||||||
Some(url) => {
|
Some(url) => {
|
||||||
@@ -251,8 +251,16 @@ impl ActivityPubRepository for PgActivityPubRepository {
|
|||||||
.bind(&in_reply_to_url)
|
.bind(&in_reply_to_url)
|
||||||
.execute(&self.pool)
|
.execute(&self.pool)
|
||||||
.await
|
.await
|
||||||
.into_domain()
|
.into_domain()?;
|
||||||
.map(|_| ())
|
|
||||||
|
// SELECT the id — works whether the INSERT was a no-op or not (idempotent).
|
||||||
|
let row: (uuid::Uuid,) =
|
||||||
|
sqlx::query_as("SELECT id FROM thoughts WHERE ap_id=$1")
|
||||||
|
.bind(ap_id)
|
||||||
|
.fetch_one(&self.pool)
|
||||||
|
.await
|
||||||
|
.into_domain()?;
|
||||||
|
Ok(ThoughtId::from_uuid(row.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn apply_note_update(&self, ap_id: &str, new_content: &str) -> Result<(), DomainError> {
|
async fn apply_note_update(&self, ap_id: &str, new_content: &str) -> Result<(), DomainError> {
|
||||||
@@ -365,4 +373,34 @@ mod tests {
|
|||||||
let repo = PgActivityPubRepository::new(pool);
|
let repo = PgActivityPubRepository::new(pool);
|
||||||
assert_eq!(repo.count_local_notes().await.unwrap(), 0);
|
assert_eq!(repo.count_local_notes().await.unwrap(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[sqlx::test(migrations = "./migrations")]
|
||||||
|
async fn accept_note_returns_thought_id(pool: sqlx::PgPool) {
|
||||||
|
let repo = PgActivityPubRepository::new(pool.clone());
|
||||||
|
let actor_user_id = repo
|
||||||
|
.intern_remote_actor("https://remote.example/users/alice")
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let thought_id = repo
|
||||||
|
.accept_note(
|
||||||
|
"https://remote.example/notes/1",
|
||||||
|
&actor_user_id,
|
||||||
|
"Hello #rust world",
|
||||||
|
chrono::Utc::now(),
|
||||||
|
false,
|
||||||
|
None,
|
||||||
|
"public",
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let row: (uuid::Uuid,) = sqlx::query_as("SELECT id FROM thoughts WHERE ap_id=$1")
|
||||||
|
.bind("https://remote.example/notes/1")
|
||||||
|
.fetch_one(&pool)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(thought_id.as_uuid(), row.0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,8 +103,8 @@ impl ActivityPubRepository for TestApRepo {
|
|||||||
_content_warning: Option<String>,
|
_content_warning: Option<String>,
|
||||||
_visibility: &str,
|
_visibility: &str,
|
||||||
_in_reply_to: Option<&str>,
|
_in_reply_to: Option<&str>,
|
||||||
) -> Result<(), DomainError> {
|
) -> Result<ThoughtId, DomainError> {
|
||||||
Ok(())
|
Ok(ThoughtId::from_uuid(uuid::Uuid::new_v4()))
|
||||||
}
|
}
|
||||||
async fn apply_note_update(
|
async fn apply_note_update(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
@@ -76,8 +76,8 @@ impl ActivityPubRepository for NoOpApRepo {
|
|||||||
_content_warning: Option<String>,
|
_content_warning: Option<String>,
|
||||||
_visibility: &str,
|
_visibility: &str,
|
||||||
_in_reply_to: Option<&str>,
|
_in_reply_to: Option<&str>,
|
||||||
) -> Result<(), DomainError> {
|
) -> Result<ThoughtId, DomainError> {
|
||||||
Ok(())
|
Ok(ThoughtId::from_uuid(uuid::Uuid::new_v4()))
|
||||||
}
|
}
|
||||||
async fn apply_note_update(
|
async fn apply_note_update(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
Reference in New Issue
Block a user