Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(email): scope message read/starred writes to the owning inbox (#5183)

Evan Hutnike97906f78ead
diff --git a/.sqlx/query-0df754458a7a7e50fd7cdc9a66f170bd25959079efaf0bb0c916acae2234f90d.json b/.sqlx/query-0df754458a7a7e50fd7cdc9a66f170bd25959079efaf0bb0c916acae2234f90d.json
deleted file mode 100644
index 793dff68d7..0000000000
--- a/.sqlx/query-0df754458a7a7e50fd7cdc9a66f170bd25959079efaf0bb0c916acae2234f90d.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE email_messages m\n SET\n is_read = $1,\n updated_at = NOW()\n FROM email_links l\n WHERE\n m.id = $2\n AND m.link_id = l.id\n AND l.fusionauth_user_id = $3\n RETURNING m.id\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Bool",
- "Uuid",
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "0df754458a7a7e50fd7cdc9a66f170bd25959079efaf0bb0c916acae2234f90d"
-}
diff --git a/.sqlx/query-714f0e8d34476dcc43f7ee64ea1a429f9420fe1403f56b6c472640efd43451ce.json b/.sqlx/query-714f0e8d34476dcc43f7ee64ea1a429f9420fe1403f56b6c472640efd43451ce.json
deleted file mode 100644
index 5fa3db5f43..0000000000
--- a/.sqlx/query-714f0e8d34476dcc43f7ee64ea1a429f9420fe1403f56b6c472640efd43451ce.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE email_messages m\n SET\n is_starred = $1,\n updated_at = NOW()\n FROM email_links l\n WHERE\n m.id = ANY($2)\n AND m.link_id = l.id\n AND l.fusionauth_user_id = $3\n RETURNING m.id\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Bool",
- "UuidArray",
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "714f0e8d34476dcc43f7ee64ea1a429f9420fe1403f56b6c472640efd43451ce"
-}
diff --git a/.sqlx/query-b4dee63c4a43a3451a781ef80fcc67300c2f673a0f7a7f6497690bb3adc8f06c.json b/.sqlx/query-b4dee63c4a43a3451a781ef80fcc67300c2f673a0f7a7f6497690bb3adc8f06c.json
deleted file mode 100644
index 3bd6ad86d0..0000000000
--- a/.sqlx/query-b4dee63c4a43a3451a781ef80fcc67300c2f673a0f7a7f6497690bb3adc8f06c.json
+++ /dev/null
@@ -1,24 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n UPDATE email_messages m\n SET\n is_read = $1,\n updated_at = NOW()\n FROM email_links l\n WHERE\n m.id = ANY($2)\n AND m.link_id = l.id\n AND l.fusionauth_user_id = $3\n RETURNING m.id\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "id",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Bool",
- "UuidArray",
- "Text"
- ]
- },
- "nullable": [
- false
- ]
- },
- "hash": "b4dee63c4a43a3451a781ef80fcc67300c2f673a0f7a7f6497690bb3adc8f06c"
-}
diff --git a/.sqlx/query-b953e348e90811fb7c465644244fbbe6e85359ff6537ee78f3cd995c4f2d0d5d.json b/.sqlx/query-b953e348e90811fb7c465644244fbbe6e85359ff6537ee78f3cd995c4f2d0d5d.json
new file mode 100644
index 0000000000..5fe7830b5d
--- /dev/null
+++ b/.sqlx/query-b953e348e90811fb7c465644244fbbe6e85359ff6537ee78f3cd995c4f2d0d5d.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_messages\n SET\n is_starred = $1,\n updated_at = NOW()\n WHERE\n id = ANY($2)\n AND link_id = $3\n RETURNING id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Bool",
+ "UuidArray",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "b953e348e90811fb7c465644244fbbe6e85359ff6537ee78f3cd995c4f2d0d5d"
+}
diff --git a/.sqlx/query-e2100096a00a83acde37202e2bf4671f9cb011e78f79828f204b770f56202164.json b/.sqlx/query-e2100096a00a83acde37202e2bf4671f9cb011e78f79828f204b770f56202164.json
new file mode 100644
index 0000000000..584c5cc2eb
--- /dev/null
+++ b/.sqlx/query-e2100096a00a83acde37202e2bf4671f9cb011e78f79828f204b770f56202164.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_messages\n SET\n is_read = $1,\n updated_at = NOW()\n WHERE\n id = ANY($2)\n AND link_id = $3\n RETURNING id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Bool",
+ "UuidArray",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "e2100096a00a83acde37202e2bf4671f9cb011e78f79828f204b770f56202164"
+}
diff --git a/.sqlx/query-ff775ebda62e79f83385d44e4cf96680f8a5d43da78cd53fbd0b19eb56bb10ce.json b/.sqlx/query-ff775ebda62e79f83385d44e4cf96680f8a5d43da78cd53fbd0b19eb56bb10ce.json
new file mode 100644
index 0000000000..e892ca39b4
--- /dev/null
+++ b/.sqlx/query-ff775ebda62e79f83385d44e4cf96680f8a5d43da78cd53fbd0b19eb56bb10ce.json
@@ -0,0 +1,24 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n UPDATE email_messages\n SET\n is_read = $1,\n updated_at = NOW()\n WHERE\n id = $2\n AND link_id = $3\n RETURNING id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Bool",
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false
+ ]
+ },
+ "hash": "ff775ebda62e79f83385d44e4cf96680f8a5d43da78cd53fbd0b19eb56bb10ce"
+}
diff --git a/crates/email_db_client/fixtures/message_read_status.sql b/crates/email_db_client/fixtures/message_read_status.sql
new file mode 100644
index 0000000000..7f54959508
--- /dev/null
+++ b/crates/email_db_client/fixtures/message_read_status.sql
@@ -0,0 +1,88 @@
+-- SQL fixture for message read-status update tests
+-- Two links so cross-inbox scoping can be asserted. The second link deliberately
+-- carries a fusionauth_user_id that differs from its macro_id, mirroring a shared
+-- or delegated inbox.
+
+------------------------------------------------------------
+-- User Links
+------------------------------------------------------------
+
+INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-000000000e11', 'macro|[email protected]', '00000000-0000-0000-0000-000000000e11',
+ '[email protected]', 'GMAIL', true, NOW(), NOW());
+
+-- Second link, owned by a different fusion user than its macro_id implies
+INSERT INTO email_links (id, macro_id, fusionauth_user_id, email_address, provider, is_sync_active, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-000000000e12', 'macro|[email protected]', '00000000-0000-0000-0000-0000000000ff',
+ '[email protected]', 'GMAIL', true, NOW(), NOW());
+
+------------------------------------------------------------
+-- Contacts
+------------------------------------------------------------
+
+INSERT INTO email_contacts (id, link_id, email_address, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-0000000ce011',
+ '00000000-0000-0000-0000-000000000e11',
+ NOW(), NOW());
+
+INSERT INTO email_contacts (id, link_id, email_address, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-0000000ce012',
+ '00000000-0000-0000-0000-000000000e12',
+ NOW(), NOW());
+
+------------------------------------------------------------
+-- Threads
+------------------------------------------------------------
+
+INSERT INTO email_threads (id, link_id, inbox_visible, is_read, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000e211',
+ '00000000-0000-0000-0000-000000000e11',
+ true, false, NOW(), NOW());
+
+INSERT INTO email_threads (id, link_id, inbox_visible, is_read, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000e212',
+ '00000000-0000-0000-0000-000000000e12',
+ true, false, NOW(), NOW());
+
+------------------------------------------------------------
+-- Messages on link e11, both unread
+------------------------------------------------------------
+
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ has_attachments, is_read, is_starred, is_draft, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000e611',
+ '00000000-0000-0000-0000-00000000e211',
+ '00000000-0000-0000-0000-000000000e11',
+ 'provider-msg-e611',
+ FALSE,
+ '00000000-0000-0000-0000-0000000ce011',
+ '2025-01-05 10:00:00 +00:00',
+ false, false, false, false, NOW(), NOW());
+
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ has_attachments, is_read, is_starred, is_draft, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000e612',
+ '00000000-0000-0000-0000-00000000e211',
+ '00000000-0000-0000-0000-000000000e11',
+ 'provider-msg-e612',
+ FALSE,
+ '00000000-0000-0000-0000-0000000ce011',
+ '2025-01-05 11:00:00 +00:00',
+ false, false, false, false, NOW(), NOW());
+
+------------------------------------------------------------
+-- Message on link e12, unread
+------------------------------------------------------------
+
+INSERT INTO email_messages (id, thread_id, link_id, provider_id, is_sent, from_contact_id, internal_date_ts,
+ has_attachments, is_read, is_starred, is_draft, created_at, updated_at)
+VALUES ('00000000-0000-0000-0000-00000000e613',
+ '00000000-0000-0000-0000-00000000e212',
+ '00000000-0000-0000-0000-000000000e12',
+ 'provider-msg-e613',
+ FALSE,
+ '00000000-0000-0000-0000-0000000ce012',
+ '2025-01-05 12:00:00 +00:00',
+ false, false, false, false, NOW(), NOW());
diff --git a/crates/email_db_client/src/messages/test.rs b/crates/email_db_client/src/messages/test.rs
index d5e0877679..8af79539d5 100644
--- a/crates/email_db_client/src/messages/test.rs
+++ b/crates/email_db_client/src/messages/test.rs
@@ -342,3 +342,124 @@ async fn delete_message_clears_calendar_flag_when_last_ics_message_removed(
assert!(!fetch_calendar_flag(&pool, thread_id).await?);
Ok(())
}
+
+const READ_STATUS_LINK: &str = "00000000-0000-0000-0000-000000000e11";
+const READ_STATUS_OTHER_LINK: &str = "00000000-0000-0000-0000-000000000e12";
+const READ_STATUS_MSG_A: &str = "00000000-0000-0000-0000-00000000e611";
+const READ_STATUS_MSG_B: &str = "00000000-0000-0000-0000-00000000e612";
+const READ_STATUS_OTHER_MSG: &str = "00000000-0000-0000-0000-00000000e613";
+
+async fn fetch_is_read(pool: &Pool<Postgres>, message_id: Uuid) -> anyhow::Result<bool> {
+ Ok(sqlx::query_scalar!(
+ "SELECT is_read FROM email_messages WHERE id = $1",
+ message_id
+ )
+ .fetch_one(pool)
+ .await?)
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../fixtures", scripts("message_read_status"))
+)]
+async fn update_message_read_status_batch_marks_messages_read(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(READ_STATUS_LINK)?;
+ let message_ids = vec![
+ Uuid::parse_str(READ_STATUS_MSG_A)?,
+ Uuid::parse_str(READ_STATUS_MSG_B)?,
+ ];
+
+ let updated = crate::messages::update::update_message_read_status_batch(
+ &pool,
+ message_ids.clone(),
+ link_id,
+ true,
+ )
+ .await?;
+
+ assert_eq!(updated, 2);
+ for id in message_ids {
+ assert!(fetch_is_read(&pool, id).await?);
+ }
+
+ Ok(())
+}
+
+/// The inbox that owns a thread is not always owned by the macro user acting on it:
+/// shared and delegated inboxes are owned by a separate fusion user. Scoping this
+/// update by anything other than the link silently matched zero rows, leaving
+/// `is_read` diverged from the labels and the denormalized thread flag.
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../fixtures", scripts("message_read_status"))
+)]
+async fn update_message_read_status_batch_scopes_to_owning_link(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let other_link_id = Uuid::parse_str(READ_STATUS_OTHER_LINK)?;
+ let message_id = Uuid::parse_str(READ_STATUS_MSG_A)?;
+
+ // Ids that belong to a different link than the one supplied must not be updated,
+ // and the mismatch is loud rather than silently absorbed.
+ let result = crate::messages::update::update_message_read_status_batch(
+ &pool,
+ vec![message_id],
+ other_link_id,
+ true,
+ )
+ .await;
+
+ assert!(result.is_err());
+ assert!(!fetch_is_read(&pool, message_id).await?);
+
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../fixtures", scripts("message_read_status"))
+)]
+async fn update_message_read_status_batch_leaves_other_inboxes_untouched(
+ pool: Pool<Postgres>,
+) -> anyhow::Result<()> {
+ let link_id = Uuid::parse_str(READ_STATUS_LINK)?;
+ let other_message_id = Uuid::parse_str(READ_STATUS_OTHER_MSG)?;
+
+ let updated = crate::messages::update::update_message_read_status_batch(
+ &pool,
+ vec![Uuid::parse_str(READ_STATUS_MSG_A)?],
+ link_id,
+ true,
+ )
+ .await?;
+
+ assert_eq!(updated, 1);
+ assert!(!fetch_is_read(&pool, other_message_id).await?);
+
+ Ok(())
+}
+
+#[sqlx::test(
+ migrator = "MACRO_DB_MIGRATIONS",
+ fixtures(path = "../../fixtures", scripts("message_read_status"))
+)]
+async fn update_message_read_status_ignores_wrong_link(pool: Pool<Postgres>) -> anyhow::Result<()> {
+ let other_link_id = Uuid::parse_str(READ_STATUS_OTHER_LINK)?;
+ let message_id = Uuid::parse_str(READ_STATUS_MSG_A)?;
+
+ let mut conn = pool.acquire().await?;
+ let updated = crate::messages::update::update_message_read_status(
+ &mut conn,
+ message_id,
+ other_link_id,
+ true,
+ )
+ .await?;
+
+ assert!(updated.is_none());
+ assert!(!fetch_is_read(&pool, message_id).await?);
+
+ Ok(())
+}
diff --git a/crates/email_db_client/src/messages/update.rs b/crates/email_db_client/src/messages/update.rs
index 4b52c0faa5..b88f15267d 100644
--- a/crates/email_db_client/src/messages/update.rs
+++ b/crates/email_db_client/src/messages/update.rs
@@ -1,28 +1,27 @@
use sqlx::types::Uuid;
+/// Update the read status of a single message, scoped to the inbox that owns it.
#[tracing::instrument(skip(tx), err)]
pub async fn update_message_read_status(
tx: &mut sqlx::PgConnection,
message_id: Uuid,
- fusionauth_user_id: &str,
+ link_id: Uuid,
is_read: bool,
) -> anyhow::Result<Option<Uuid>> {
let result = sqlx::query!(
r#"
- UPDATE email_messages m
+ UPDATE email_messages
SET
is_read = $1,
updated_at = NOW()
- FROM email_links l
WHERE
- m.id = $2
- AND m.link_id = l.id
- AND l.fusionauth_user_id = $3
- RETURNING m.id
+ id = $2
+ AND link_id = $3
+ RETURNING id
"#,
is_read,
message_id,
- fusionauth_user_id
+ link_id
)
.fetch_optional(tx)
.await?;
@@ -30,8 +29,8 @@ pub async fn update_message_read_status(
if result.is_none() {
tracing::warn!(
message_id = %message_id,
- user_id = %fusionauth_user_id,
- "No message was updated - message may not exist or doesn't belong to the user"
+ link_id = %link_id,
+ "No message was updated - message may not exist or doesn't belong to the inbox"
);
}
@@ -39,13 +38,18 @@ pub async fn update_message_read_status(
Ok(result.map(|r| r.id))
}
-/// Update the read status of multiple messages at once
-/// Returns the count of messages that were successfully updated
+/// Update the read status of multiple messages at once.
+///
+/// Scoped by `link_id` — the inbox that owns the messages — not by the calling user.
+/// A delegated or shared inbox is owned by a different macro/fusion user than the
+/// caller acting on it, so scoping by the caller would match no rows.
+///
+/// Returns the count of messages that were successfully updated.
#[tracing::instrument(skip(executor), err)]
pub async fn update_message_read_status_batch<'e, E>(
executor: E,
message_ids: Vec<Uuid>,
- fusionauth_user_id: &str,
+ link_id: Uuid,
is_read: bool,
) -> anyhow::Result<usize>
where
@@ -57,43 +61,52 @@ where
let result = sqlx::query!(
r#"
- UPDATE email_messages m
+ UPDATE email_messages
SET
is_read = $1,
updated_at = NOW()
- FROM email_links l
WHERE
- m.id = ANY($2)
- AND m.link_id = l.id
- AND l.fusionauth_user_id = $3
- RETURNING m.id
+ id = ANY($2)
+ AND link_id = $3
+ RETURNING id
"#,
is_read,
&message_ids,
- fusionauth_user_id
+ link_id
)
.fetch_all(executor)
.await?;
let updated_count = result.len();
+ // Matching nothing means the ids and the link disagree, which is a scoping bug
+ // rather than a race. Fail loudly instead of leaving is_read silently diverged.
+ if updated_count == 0 {
+ anyhow::bail!(
+ "no messages updated for link {link_id}: none of the {} requested ids belong to it",
+ message_ids.len()
+ );
+ }
+
if updated_count < message_ids.len() {
tracing::warn!(
requested_count = message_ids.len(),
updated_count = updated_count,
- user_id = %fusionauth_user_id,
- "Some messages were not updated - they may not exist or don't belong to the user"
+ link_id = %link_id,
+ "Some messages were not updated - they may not exist or don't belong to the inbox"
);
}
Ok(updated_count)
}
+/// Update the starred status of multiple messages at once, scoped by owning inbox.
+/// See [`update_message_read_status_batch`] for why this is not scoped by caller.
#[tracing::instrument(skip(executor), err)]
pub async fn update_message_starred_status_batch<'e, E>(
executor: E,
message_ids: Vec<Uuid>,
- fusionauth_user_id: &str,
+ link_id: Uuid,
is_starred: bool,
) -> anyhow::Result<usize>
where
@@ -105,20 +118,18 @@ where
let result = sqlx::query!(
r#"
- UPDATE email_messages m
+ UPDATE email_messages
SET
is_starred = $1,
updated_at = NOW()
- FROM email_links l
WHERE
- m.id = ANY($2)
- AND m.link_id = l.id
- AND l.fusionauth_user_id = $3
- RETURNING m.id
+ id = ANY($2)
+ AND link_id = $3
+ RETURNING id
"#,
is_starred,
&message_ids,
- fusionauth_user_id
+ link_id
)
.fetch_all(executor)
.await?;
@@ -129,8 +140,8 @@ where
tracing::warn!(
requested_count = message_ids.len(),
updated_count = updated_count,
- user_id = %fusionauth_user_id,
- "Some messages were not updated - they may not exist or don't belong to the user"
+ link_id = %link_id,
+ "Some messages were not updated - they may not exist or don't belong to the inbox"
);
}
diff --git a/services/email_service/src/api/email/messages/labels.rs b/services/email_service/src/api/email/messages/labels.rs
index 19d6179e15..68d235437d 100644
--- a/services/email_service/src/api/email/messages/labels.rs
+++ b/services/email_service/src/api/email/messages/labels.rs
@@ -186,7 +186,7 @@ pub async fn handler(
email_db_client::messages::update::update_message_read_status_batch(
&mut *tx,
message_db_ids.clone(),
- &authorization.authorization.user.user_context.fusion_user_id,
+ link.id,
!is_adding,
)
.await
@@ -195,7 +195,7 @@ pub async fn handler(
email_db_client::messages::update::update_message_starred_status_batch(
&mut *tx,
message_db_ids.clone(),
- &authorization.authorization.user.user_context.fusion_user_id,
+ link.id,
is_adding,
)
.await
diff --git a/services/email_service/src/api/email/threads/seen.rs b/services/email_service/src/api/email/threads/seen.rs
index 6c344bf46a..8e7cae3098 100644
--- a/services/email_service/src/api/email/threads/seen.rs
+++ b/services/email_service/src/api/email/threads/seen.rs
@@ -108,42 +108,42 @@ pub async fn seen_handler(
})
.collect();
- if unread_messages.is_empty() {
- // Nothing to mark as read
- return Ok((StatusCode::OK, Json(EmptyResponse::default())).into_response());
- }
-
let message_db_ids: Vec<Uuid> = unread_messages.iter().map(|m| m.db_id).collect();
let mut tx = ctx.db.begin().await?;
let transaction_result = async {
- // Update thread read status
+ // Write the denormalized thread flag even when no message carries the UNREAD
+ // label. Soup reads this flag, so a thread whose labels were already stripped
+ // while the flag stayed false would otherwise never recover: it reads unread
+ // forever and every later call to this endpoint short-circuits.
email_db_client::threads::update::update_thread_read_status(
&mut *tx, thread_id, link.id, true,
)
.await
.context("Failed to update thread read status")?;
- // Update messages read status
- email_db_client::messages::update::update_message_read_status_batch(
- &mut *tx,
- message_db_ids.clone(),
- &authorization.authorization.user.user_context.fusion_user_id,
- true,
- )
- .await
- .context("Failed to update message read status")?;
-
- // Remove UNREAD label from messages in DB
- email_db_client::labels::delete::delete_message_labels_batch(
- &mut *tx,
- &message_db_ids,
- system_labels::UNREAD,
- link.id,
- )
- .await
- .context("Failed to remove 'UNREAD' label from messages")?;
+ if !message_db_ids.is_empty() {
+ // Update messages read status
+ email_db_client::messages::update::update_message_read_status_batch(
+ &mut *tx,
+ message_db_ids.clone(),
+ link.id,
+ true,
+ )
+ .await
+ .context("Failed to update message read status")?;
+
+ // Remove UNREAD label from messages in DB
+ email_db_client::labels::delete::delete_message_labels_batch(
+ &mut *tx,
+ &message_db_ids,
+ system_labels::UNREAD,
+ link.id,
+ )
+ .await
+ .context("Failed to remove 'UNREAD' label from messages")?;
+ }
anyhow::Ok(())
}
@@ -162,6 +162,11 @@ pub async fn seen_handler(
}
}
+ if unread_messages.is_empty() {
+ // No message changed state, so there is nothing to announce or sync upstream.
+ return Ok((StatusCode::OK, Json(EmptyResponse::default())).into_response());
+ }
+
publish_email_event(
ctx.macro_event_broker.as_ref(),
&EmailMacroEvent::thread_read(ThreadReadMetadata {
diff --git a/services/email_service/src/pubsub/gmail_ops/operations/modify_message_labels.rs b/services/email_service/src/pubsub/gmail_ops/operations/modify_message_labels.rs
index 98e4d57f23..a18bf6f251 100644
--- a/services/email_service/src/pubsub/gmail_ops/operations/modify_message_labels.rs
+++ b/services/email_service/src/pubsub/gmail_ops/operations/modify_message_labels.rs
@@ -124,17 +124,14 @@ async fn revert_db_changes(
email_db_client::messages::update::update_message_read_status_batch(
&mut *tx,
failed_ids.clone(),
- &link.fusionauth_user_id,
+ link.id,
is_adding,
)
.await
.context("Failed to revert message read status")?;
} else if *provider_label_id == service::label::system_labels::STARRED {
email_db_client::messages::update::update_message_starred_status_batch(
- &mut *tx,
- failed_ids,
- &link.fusionauth_user_id,
- !is_adding,
+ &mut *tx, failed_ids, link.id, !is_adding,
)
.await
.context("Failed to revert message starred status")?;
diff --git a/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs b/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs
index ab075957ad..94eedaa6a2 100644
--- a/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs
+++ b/services/email_service/src/pubsub/inbox_sync/operations/update_labels.rs
@@ -380,7 +380,7 @@ pub async fn add_message_labels(
email_db_client::messages::update::update_message_read_status(
&mut tx,
message_db_id,
- &link.fusionauth_user_id,
+ link.id,
false,
)
.await
@@ -442,7 +442,7 @@ pub async fn remove_message_labels(
email_db_client::messages::update::update_message_read_status(
&mut tx,
message_db_id,
- &link.fusionauth_user_id,
+ link.id,
true,
)
.await