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.

feat(email-service): Signature support, Settings support (#203)

Evan Hutnik36480e71af42
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-26ab5fa9630526b99a3937baff648f056a532178864f1f62ce55d107d1702f7f.json b/rust/cloud-storage/email_db_client/.sqlx/query-26ab5fa9630526b99a3937baff648f056a532178864f1f62ce55d107d1702f7f.json
new file mode 100644
index 0000000000..204ec15fdc
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/.sqlx/query-26ab5fa9630526b99a3937baff648f056a532178864f1f62ce55d107d1702f7f.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT link_id, signature_on_replies_forwards\n FROM email_settings\n WHERE link_id = $1\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "link_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "signature_on_replies_forwards",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "26ab5fa9630526b99a3937baff648f056a532178864f1f62ce55d107d1702f7f"
+}
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-d84f480cd90b929670bdbe24b839126ad17975d9f7490e8a5d521e2c1eb752c5.json b/rust/cloud-storage/email_db_client/.sqlx/query-d84f480cd90b929670bdbe24b839126ad17975d9f7490e8a5d521e2c1eb752c5.json
new file mode 100644
index 0000000000..b146805915
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/.sqlx/query-d84f480cd90b929670bdbe24b839126ad17975d9f7490e8a5d521e2c1eb752c5.json
@@ -0,0 +1,29 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_settings (link_id, signature_on_replies_forwards)\n VALUES ($1, $2)\n ON CONFLICT (link_id)\n DO UPDATE SET\n signature_on_replies_forwards = EXCLUDED.signature_on_replies_forwards,\n updated_at = NOW()\n RETURNING link_id, signature_on_replies_forwards\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "link_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "signature_on_replies_forwards",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Bool"
+ ]
+ },
+ "nullable": [
+ false,
+ false
+ ]
+ },
+ "hash": "d84f480cd90b929670bdbe24b839126ad17975d9f7490e8a5d521e2c1eb752c5"
+}
diff --git a/rust/cloud-storage/email_db_client/.sqlx/query-de68d0cec65bf1c6779ac453af4ca4185ce3ebe294d432408c876b8a0b0a6b63.json b/rust/cloud-storage/email_db_client/.sqlx/query-de68d0cec65bf1c6779ac453af4ca4185ce3ebe294d432408c876b8a0b0a6b63.json
new file mode 100644
index 0000000000..55078c8d47
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/.sqlx/query-de68d0cec65bf1c6779ac453af4ca4185ce3ebe294d432408c876b8a0b0a6b63.json
@@ -0,0 +1,14 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n INSERT INTO email_settings (link_id) -- default settings for new links\n VALUES ($1)\n ON CONFLICT (link_id)\n DO NOTHING\n ",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "de68d0cec65bf1c6779ac453af4ca4185ce3ebe294d432408c876b8a0b0a6b63"
+}
diff --git a/rust/cloud-storage/email_db_client/src/lib.rs b/rust/cloud-storage/email_db_client/src/lib.rs
index 07d4af39f6..3866799b46 100644
--- a/rust/cloud-storage/email_db_client/src/lib.rs
+++ b/rust/cloud-storage/email_db_client/src/lib.rs
@@ -7,6 +7,7 @@ pub mod links;
pub mod messages;
pub mod parse;
pub mod previews;
+pub mod settings;
pub mod sfs_mappings;
pub mod sync_tokens;
pub mod threads;
diff --git a/rust/cloud-storage/email_db_client/src/links/insert.rs b/rust/cloud-storage/email_db_client/src/links/insert.rs
index 26078ea8d4..126c24e8a0 100644
--- a/rust/cloud-storage/email_db_client/src/links/insert.rs
+++ b/rust/cloud-storage/email_db_client/src/links/insert.rs
@@ -1,4 +1,4 @@
-use anyhow::{Context, anyhow};
+use anyhow::anyhow;
use models_email::email::{db, service};
use sqlx::PgPool;
use sqlx::types::Uuid;
@@ -11,7 +11,7 @@ struct LinkId {
/// If a record with matching fusionauth_user_id, email_address, and provider already exists,
/// updates the existing record with values from the provided Link.
/// Returns the ID of the inserted or updated link and a boolean indicating if a new record was created.
-#[tracing::instrument(skip(pool), level = "info")]
+#[tracing::instrument(skip(pool), level = "info", err)]
pub async fn upsert_link(
pool: &PgPool,
service_link: service::link::Link,
@@ -24,7 +24,6 @@ pub async fn upsert_link(
}
let db_link: db::link::Link = service_link.into();
- let provider_display = format!("{:?}", db_link.provider);
let result = sqlx::query_as!(
LinkId,
@@ -45,16 +44,22 @@ pub async fn upsert_link(
db_link.is_sync_active
)
.fetch_one(pool)
- .await
- .with_context(|| {
- format!(
- "Failed to upsert link for fusionauth_user_id {}, email {}, provider {}",
- db_link.fusionauth_user_id, db_link.email_address, provider_display
- )
- })?;
+ .await?;
let mut service_link: service::link::Link = db_link.into();
service_link.id = result.id;
+ let _ = sqlx::query!(
+ r#"
+ INSERT INTO email_settings (link_id) -- default settings for new links
+ VALUES ($1)
+ ON CONFLICT (link_id)
+ DO NOTHING
+ "#,
+ service_link.id,
+ )
+ .execute(pool)
+ .await?;
+
Ok(service_link)
}
diff --git a/rust/cloud-storage/email_db_client/src/settings/mod.rs b/rust/cloud-storage/email_db_client/src/settings/mod.rs
new file mode 100644
index 0000000000..ec87db6226
--- /dev/null
+++ b/rust/cloud-storage/email_db_client/src/settings/mod.rs
@@ -0,0 +1,52 @@
+use models_email::{db, service};
+use sqlx::PgPool;
+use sqlx::types::Uuid;
+
+/// Updates a user's settings.
+#[tracing::instrument(skip(pool), err)]
+pub async fn patch_settings(
+ pool: &PgPool,
+ service_settings: service::settings::Settings,
+) -> anyhow::Result<service::settings::Settings> {
+ let db_settings = db::settings::Settings::from(service_settings);
+
+ let result = sqlx::query_as!(
+ db::settings::Settings,
+ r#"
+ INSERT INTO email_settings (link_id, signature_on_replies_forwards)
+ VALUES ($1, $2)
+ ON CONFLICT (link_id)
+ DO UPDATE SET
+ signature_on_replies_forwards = EXCLUDED.signature_on_replies_forwards,
+ updated_at = NOW()
+ RETURNING link_id, signature_on_replies_forwards
+ "#,
+ db_settings.link_id,
+ db_settings.signature_on_replies_forwards,
+ )
+ .fetch_one(pool)
+ .await?;
+
+ Ok(service::settings::Settings::from(result))
+}
+
+/// Fetches a user's settings by link ID.
+#[tracing::instrument(skip(pool), level = "info", err)]
+pub async fn fetch_settings(
+ pool: &PgPool,
+ link_id: Uuid,
+) -> anyhow::Result<service::settings::Settings> {
+ let result = sqlx::query_as!(
+ db::settings::Settings,
+ r#"
+ SELECT link_id, signature_on_replies_forwards
+ FROM email_settings
+ WHERE link_id = $1
+ "#,
+ link_id
+ )
+ .fetch_one(pool)
+ .await?;
+
+ Ok(service::settings::Settings::from(result))
+}
diff --git a/rust/cloud-storage/email_service/src/api/email/links/list.rs b/rust/cloud-storage/email_service/src/api/email/links/list.rs
index 23252018e5..c11b925ed8 100644
--- a/rust/cloud-storage/email_service/src/api/email/links/list.rs
+++ b/rust/cloud-storage/email_service/src/api/email/links/list.rs
@@ -1,18 +1,44 @@
use crate::api::context::ApiContext;
+use crate::util::gmail::auth::fetch_gmail_access_token_from_link;
use axum::extract::State;
use axum::http::StatusCode;
use axum::response::{IntoResponse, Response};
use axum::{Extension, Json};
+use futures::future::join_all;
use model::response::{EmptyResponse, ErrorResponse};
use model::user::UserContext;
-use models_email::email::service::link::Link;
+use models_email::api;
use utoipa::ToSchema;
+use strum_macros::AsRefStr;
+use thiserror::Error;
+
+#[derive(Debug, Error, AsRefStr)]
+pub enum ListLinksError {
+ #[error("Database error")]
+ DatabaseError(anyhow::Error),
+
+ #[error("Failed to fetch Gmail access token")]
+ AuthError(anyhow::Error),
+}
+
+impl IntoResponse for ListLinksError {
+ fn into_response(self) -> Response {
+ let status_code = match &self {
+ ListLinksError::DatabaseError(_) | ListLinksError::AuthError(_) => {
+ StatusCode::INTERNAL_SERVER_ERROR
+ }
+ };
+
+ (status_code, self.to_string()).into_response()
+ }
+}
+
/// The response returned from the list links endpoint
#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
pub struct ListLinksResponse {
/// the thread, with messages inside
- pub links: Vec<Link>,
+ pub links: Vec<api::link::Link>,
}
/// List all links belonging to the user.
@@ -32,22 +58,47 @@ pub struct ListLinksResponse {
pub async fn list_links_handler(
State(ctx): State<ApiContext>,
user_context: Extension<UserContext>,
-) -> Result<Response, Response> {
+) -> Result<Response, ListLinksError> {
let links = email_db_client::links::get::fetch_links_by_fusionauth_user_id(
&ctx.db,
&user_context.fusion_user_id,
)
.await
- .map_err(|e| {
- tracing::error!(error=?e, "unable to fetch links");
- (
- StatusCode::INTERNAL_SERVER_ERROR,
- Json(ErrorResponse {
- message: "unable to fetch links",
- }),
- )
- .into_response()
- })?;
-
- Ok((StatusCode::OK, Json(ListLinksResponse { links })).into_response())
+ .map_err(ListLinksError::DatabaseError)?;
+
+ let tasks = links.into_iter().map(|link| {
+ let ctx = ctx.clone();
+ async move {
+ let settings = email_db_client::settings::fetch_settings(&ctx.db, link.id)
+ .await
+ .map_err(ListLinksError::DatabaseError)?;
+
+ let access_token = fetch_gmail_access_token_from_link(
+ &link,
+ &ctx.redis_client,
+ &ctx.auth_service_client,
+ )
+ .await
+ .map_err(ListLinksError::AuthError)?;
+
+ let signature = ctx
+ .gmail_client
+ .get_email_signature(&access_token, &link.email_address)
+ .await
+ .unwrap_or(None);
+
+ Ok(api::link::Link::new(
+ link,
+ signature,
+ api::settings::Settings::from(settings),
+ ))
+ }
+ });
+
+ let results = join_all(tasks).await;
+
+ let api_links: Result<Vec<_>, _> = results.into_iter().collect();
+ let api_links = api_links?;
+
+ Ok((StatusCode::OK, Json(ListLinksResponse { links: api_links })).into_response())
}
diff --git a/rust/cloud-storage/email_service/src/api/email/mod.rs b/rust/cloud-storage/email_service/src/api/email/mod.rs
index 6c6fb28da7..dcdd897ee0 100644
--- a/rust/cloud-storage/email_service/src/api/email/mod.rs
+++ b/rust/cloud-storage/email_service/src/api/email/mod.rs
@@ -11,6 +11,7 @@ pub(crate) mod init;
pub(crate) mod labels;
pub(crate) mod links;
pub(crate) mod messages;
+pub(crate) mod settings;
pub(crate) mod sync;
pub(crate) mod threads;
pub(crate) mod validation;
@@ -25,6 +26,7 @@ pub fn router(state: ApiContext) -> Router<ApiContext> {
.nest("/links", links::router())
.nest("/contacts", contacts::router())
.nest("/backfill", backfill::router(state.clone()))
+ .nest("/settings", settings::router(state.clone()))
.nest("/sync", sync::router(state.clone()))
// deleting all user info from the db can take a long time - prevent connection from dropping
.layer(axum::middleware::from_fn(
diff --git a/rust/cloud-storage/email_service/src/api/email/settings/mod.rs b/rust/cloud-storage/email_service/src/api/email/settings/mod.rs
new file mode 100644
index 0000000000..39e38d01d2
--- /dev/null
+++ b/rust/cloud-storage/email_service/src/api/email/settings/mod.rs
@@ -0,0 +1,15 @@
+use crate::api::context::ApiContext;
+use crate::api::email::settings::patch::patch_settings_handler;
+use axum::Router;
+use axum::routing::patch;
+
+pub(crate) mod patch;
+
+pub fn router(state: ApiContext) -> Router<ApiContext> {
+ Router::new()
+ .route("/", patch(patch_settings_handler))
+ .layer(axum::middleware::from_fn_with_state(
+ state,
+ crate::api::middleware::link::attach_link_context,
+ ))
+}
diff --git a/rust/cloud-storage/email_service/src/api/email/settings/patch.rs b/rust/cloud-storage/email_service/src/api/email/settings/patch.rs
new file mode 100644
index 0000000000..1be91c758c
--- /dev/null
+++ b/rust/cloud-storage/email_service/src/api/email/settings/patch.rs
@@ -0,0 +1,69 @@
+use crate::api::context::ApiContext;
+use axum::extract::State;
+use axum::http::StatusCode;
+use axum::response::{IntoResponse, Response};
+use axum::{Extension, Json};
+use model::response::ErrorResponse;
+use models_email::service::link::Link;
+use models_email::{api, service};
+use strum_macros::AsRefStr;
+use thiserror::Error;
+use utoipa::ToSchema;
+
+#[derive(Debug, Error, AsRefStr)]
+pub enum PatchSettingsError {
+ #[error("Failed to update settings")]
+ DatabaseError(#[from] anyhow::Error),
+}
+
+impl IntoResponse for PatchSettingsError {
+ fn into_response(self) -> Response {
+ let status_code = match &self {
+ PatchSettingsError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
+ };
+
+ (status_code, self.to_string()).into_response()
+ }
+}
+
+#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
+pub struct PatchSettingsRequest {
+ pub settings: api::settings::Settings,
+}
+
+#[derive(Debug, serde::Serialize, serde::Deserialize, ToSchema)]
+pub struct PatchSettingsResponse {
+ pub settings: api::settings::Settings,
+}
+
+/// Patch user settings.
+#[utoipa::path(
+ patch,
+ tag = "Settings",
+ path = "/email/settings",
+ operation_id = "patch_settings",
+ request_body = PatchSettingsRequest,
+ responses(
+ (status = 200, body = PatchSettingsResponse),
+ (status = 400, body = ErrorResponse),
+ (status = 401, body = ErrorResponse),
+ (status = 500, body = ErrorResponse),
+ )
+)]
+#[tracing::instrument(skip(ctx, api_settings))]
+pub async fn patch_settings_handler(
+ State(ctx): State<ApiContext>,
+ link: Extension<Link>,
+ Json(api_settings): Json<PatchSettingsRequest>,
+) -> Result<Json<PatchSettingsResponse>, PatchSettingsError> {
+ let service_settings = service::settings::Settings::new(api_settings.settings, link.id);
+
+ let updated_settings =
+ email_db_client::settings::patch_settings(&ctx.db, service_settings).await?;
+
+ let response_settings = api::settings::Settings::from(updated_settings);
+
+ Ok(Json(PatchSettingsResponse {
+ settings: response_settings,
+ }))
+}
diff --git a/rust/cloud-storage/email_service/src/api/email/threads/get.rs b/rust/cloud-storage/email_service/src/api/email/threads/get.rs
index 893aa50800..ae54b6c5f6 100644
--- a/rust/cloud-storage/email_service/src/api/email/threads/get.rs
+++ b/rust/cloud-storage/email_service/src/api/email/threads/get.rs
@@ -159,6 +159,7 @@ pub async fn get_thread_handler(
// TODO: deduplicate with internal api
#[utoipa::path(
get,
+ tag = "Threads",
path = "/email/threads/{id}/messages",
params(
("id" = String, Path, description = "Thread ID"),
diff --git a/rust/cloud-storage/email_service/src/api/internal/delete_user.rs b/rust/cloud-storage/email_service/src/api/internal/delete_user.rs
index 99156498c6..b10bf2d41d 100644
--- a/rust/cloud-storage/email_service/src/api/internal/delete_user.rs
+++ b/rust/cloud-storage/email_service/src/api/internal/delete_user.rs
@@ -40,7 +40,7 @@ pub async fn handler(
};
let gmail_access_token = match crate::util::gmail::auth::fetch_gmail_access_token_from_link(
- link.clone(),
+ &link,
&ctx.redis_client,
&ctx.auth_service_client,
)
diff --git a/rust/cloud-storage/email_service/src/api/swagger.rs b/rust/cloud-storage/email_service/src/api/swagger.rs
index c4af7cb844..3983364335 100644
--- a/rust/cloud-storage/email_service/src/api/swagger.rs
+++ b/rust/cloud-storage/email_service/src/api/swagger.rs
@@ -10,6 +10,7 @@ use crate::api::email::labels::list::ListLabelsResponse;
use crate::api::email::links::list::ListLinksResponse;
use crate::api::email::messages::labels::{UpdateLabelBatchRequest, UpdateLabelBatchResponse};
use crate::api::email::messages::send::{SendMessageRequest, SendMessageResponse};
+use crate::api::email::settings::patch::{PatchSettingsRequest, PatchSettingsResponse};
use crate::api::email::threads::archived::ArchiveThreadRequest;
use crate::api::email::threads::get::GetThreadResponse;
use crate::api::email::threads::previews::cursor::{
@@ -59,6 +60,7 @@ use utoipa::OpenApi;
email::contacts::list::list_contacts_handler,
email::sync::enable::enable_handler,
email::sync::disable::disable_handler,
+ email::settings::patch::patch_settings_handler,
),
components(
schemas(
@@ -114,7 +116,9 @@ use utoipa::OpenApi;
service::label::LabelInfo,
service::attachment::Attachment,
service::thread::Thread,
- service::message::Message
+ service::message::Message,
+ PatchSettingsRequest,
+ PatchSettingsResponse
),
),
tags(
diff --git a/rust/cloud-storage/email_service/src/pubsub/backfill/increment_counters.rs b/rust/cloud-storage/email_service/src/pubsub/backfill/increment_counters.rs
index 315710fdd6..9d6c02ebd7 100644
--- a/rust/cloud-storage/email_service/src/pubsub/backfill/increment_counters.rs
+++ b/rust/cloud-storage/email_service/src/pubsub/backfill/increment_counters.rs
@@ -109,7 +109,8 @@ async fn handle_job_completed(
})
})?;
- if link.macro_id.ends_with("@macro.com") {
+ // temporarily only enabling for macro emails for testing
+ if link.macro_id.ends_with("@macro.com") && !cfg!(feature = "disable_attachment_upload") {
let attachments =
email_db_client::attachments::provider::upload::fetch_job_attachments_for_backfill(
&ctx.db, link.id,
@@ -164,7 +165,8 @@ async fn handle_thread_completed(
})
})?;
- if link.macro_id.ends_with("@macro.com") {
+ // temporarily only for macro emails, for testing
+ if link.macro_id.ends_with("@macro.com") && !cfg!(feature = "disable_attachment_upload") {
let attachments =
email_db_client::attachments::provider::upload::fetch_thread_attachments_for_backfill(
&ctx.db,
diff --git a/rust/cloud-storage/email_service/src/pubsub/backfill/process.rs b/rust/cloud-storage/email_service/src/pubsub/backfill/process.rs
index d7de3e4315..46b9509ad2 100644
--- a/rust/cloud-storage/email_service/src/pubsub/backfill/process.rs
+++ b/rust/cloud-storage/email_service/src/pubsub/backfill/process.rs
@@ -92,18 +92,15 @@ async fn inner_process_message(
}
};
- let access_token = fetch_gmail_access_token_from_link(
- link.clone(),
- &ctx.redis_client,
- &ctx.auth_service_client,
- )
- .await
- .map_err(|e| {
- ProcessingError::NonRetryable(DetailedError {
- reason: FailureReason::AccessTokenFetchFailed,
- source: e.context("Failed to fetch access token from link"),
- })
- })?;
+ let access_token =
+ fetch_gmail_access_token_from_link(&link, &ctx.redis_client, &ctx.auth_service_client)
+ .await
+ .map_err(|e| {
+ ProcessingError::NonRetryable(DetailedError {
+ reason: FailureReason::AccessTokenFetchFailed,
+ source: e.context("Failed to fetch access token from link"),
+ })
+ })?;
match &data.backfill_operation {
BackfillOperation::Init => {
diff --git a/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs b/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs
index c10dcaa6c6..e1a2e36f6d 100644
--- a/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs
+++ b/rust/cloud-storage/email_service/src/pubsub/webhook/operations/upsert_message.rs
@@ -129,8 +129,8 @@ pub async fn upsert_message(
// notify downstream services of new messages
notify_for_new_messages(ctx, link, new_message_provider_ids).await?;
- // internal only, for now
- if link.macro_id.ends_with("@macro.com") {
+ // temporarily only for macro emails, for testing purposes
+ if link.macro_id.ends_with("@macro.com") && !cfg!(feature = "disable_attachment_upload") {
// upload attachments to Macro
let attachments = email_db_client::attachments::provider::upload::fetch_insertable_attachments_for_new_email(
&ctx.db,
diff --git a/rust/cloud-storage/email_service/src/util/gmail/auth.rs b/rust/cloud-storage/email_service/src/util/gmail/auth.rs
index 97b386fd90..9efe31515b 100644
--- a/rust/cloud-storage/email_service/src/util/gmail/auth.rs
+++ b/rust/cloud-storage/email_service/src/util/gmail/auth.rs
@@ -41,7 +41,7 @@ pub async fn fetch_gmail_token_usercontext_response(
/// Creates a cache key using a link, then fetches access token
pub async fn fetch_gmail_access_token_from_link(
- link: Link,
+ link: &Link,
redis_client: &RedisClient,
auth_service_client: &AuthServiceClient,
) -> anyhow::Result<String> {
diff --git a/rust/cloud-storage/gmail_client/src/lib.rs b/rust/cloud-storage/gmail_client/src/lib.rs
index 766ca79a07..73f6d107f5 100644
--- a/rust/cloud-storage/gmail_client/src/lib.rs
+++ b/rust/cloud-storage/gmail_client/src/lib.rs
@@ -7,6 +7,7 @@ pub(crate) mod messages;
pub(crate) mod parse;
pub(crate) mod profile;
pub(crate) mod sanitizer;
+mod settings;
pub(crate) mod threads;
pub(crate) mod watch;
@@ -317,4 +318,14 @@ impl GmailClient {
) -> anyhow::Result<ContactList> {
contacts::list_other_contacts(self, access_token, link_id, sync_token).await
}
+
+ /// Gets the email signature for a specific email address
+ #[tracing::instrument(skip(self, access_token))]
+ pub async fn get_email_signature(
+ &self,
+ access_token: &str,
+ email_address: &str,
+ ) -> Result<Option<String>, GmailError> {
+ settings::get_email_signature(self, access_token, email_address).await
+ }
}
diff --git a/rust/cloud-storage/gmail_client/src/settings.rs b/rust/cloud-storage/gmail_client/src/settings.rs
new file mode 100644
index 0000000000..da2034bd9f
--- /dev/null
+++ b/rust/cloud-storage/gmail_client/src/settings.rs
@@ -0,0 +1,41 @@
+use crate::GmailClient;
+use models_email::gmail::EmailSignature;
+use models_email::gmail::error::GmailError;
+
+#[tracing::instrument(skip(client, access_token), err)]
+pub(crate) async fn get_email_signature(
+ client: &GmailClient,
+ access_token: &str,
+ email_address: &str,
+) -> Result<Option<String>, GmailError> {
+ let url = format!(
+ "{}/users/me/settings/sendAs/{}",
+ client.base_url, email_address
+ );
+
+ let http_client = client.inner.clone();
+
+ let response = http_client
+ .get(&url)
+ .bearer_auth(access_token)
+ .send()
+ .await
+ .map_err(|e| GmailError::HttpRequest(e.to_string()))?;
+
+ let response = response.error_for_status().map_err(|e| match e.status() {
+ Some(status) if status.as_u16() == 401 => GmailError::Unauthorized,
+ Some(status) if status.as_u16() == 429 => GmailError::RateLimitExceeded,
+ Some(status) if status.as_u16() == 404 => GmailError::NotFound(format!(
+ "Gmail API returned 404 for email_address: {}",
+ email_address
+ )),
+ _ => GmailError::ApiError(e.to_string()),
+ })?;
+
+ let email_signature = response
+ .json::<EmailSignature>()
+ .await
+ .map_err(|e| GmailError::BodyReadError(e.to_string()))?;
+
+ Ok((!email_signature.signature.is_empty()).then_some(email_signature.signature))
+}
diff --git a/rust/cloud-storage/macro_db_client/migrations/20251117204659_email_settings.sql b/rust/cloud-storage/macro_db_client/migrations/20251117204659_email_settings.sql
new file mode 100644
index 0000000000..aa716c56e3
--- /dev/null
+++ b/rust/cloud-storage/macro_db_client/migrations/20251117204659_email_settings.sql
@@ -0,0 +1,9 @@
+-- table for storing user settings for email
+CREATE TABLE "email_settings"
+(
+ link_id UUID NOT NULL PRIMARY KEY REFERENCES email_links (id) ON DELETE CASCADE,
+ signature_on_replies_forwards BOOLEAN NOT NULL DEFAULT FALSE,
+ created_at timestamp DEFAULT now() NOT NULL,
+ updated_at timestamp DEFAULT now() NOT NULL
+);
+
diff --git a/rust/cloud-storage/models_email/src/email/api/link.rs b/rust/cloud-storage/models_email/src/email/api/link.rs
new file mode 100644
index 0000000000..e4cec4f5b4
--- /dev/null
+++ b/rust/cloud-storage/models_email/src/email/api/link.rs
@@ -0,0 +1,65 @@
+use crate::api::settings::Settings;
+use crate::service;
+use chrono::{DateTime, Utc};
+use serde::{Deserialize, Serialize};
+use utoipa::ToSchema;
+use uuid::Uuid;
+
+#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, ToSchema)]
+#[serde(rename_all = "UPPERCASE")]
+pub enum UserProvider {
+ Gmail,
+}
+
+impl UserProvider {
+ pub fn as_str(&self) -> &'static str {
+ match self {
+ UserProvider::Gmail => "GMAIL",
+ }
+ }
+}
+
+impl std::fmt::Display for UserProvider {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.write_str(self.as_str())
+ }
+}
+
+impl From<crate::email::db::link::UserProvider> for UserProvider {
+ fn from(provider: crate::email::db::link::UserProvider) -> Self {
+ match provider {
+ crate::email::db::link::UserProvider::Gmail => UserProvider::Gmail,
+ }
+ }
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
+pub struct Link {
+ pub id: Uuid,
+ pub macro_id: String,
+ pub fusionauth_user_id: String,
+ pub email_address: String,
+ pub provider: UserProvider,
+ pub is_sync_active: bool,
+ pub signature: Option<String>,
+ pub settings: Settings,
+ pub created_at: DateTime<Utc>,
+ pub updated_at: DateTime<Utc>,
+}
+
+impl Link {
+ pub fn new(source: service::link::Link, signature: Option<String>, settings: Settings) -> Self {
+ Link {
+ id: source.id,
+ macro_id: source.macro_id,
+ fusionauth_user_id: source.fusionauth_user_id,
+ email_address: source.email_address,
+ provider: UserProvider::from(source.provider),
+ is_sync_active: source.is_sync_active,
+ signature,
+ settings,
+ created_at: source.created_at,
+ updated_at: source.updated_at,
+ }
+ }
+}
diff --git a/rust/cloud-storage/models_email/src/email/api/mod.rs b/rust/cloud-storage/models_email/src/email/api/mod.rs
new file mode 100644
index 0000000000..9156906270
--- /dev/null
+++ b/rust/cloud-storage/models_email/src/email/api/mod.rs
@@ -0,0 +1,2 @@
+pub mod link;
+pub mod settings;
diff --git a/rust/cloud-storage/models_email/src/email/api/settings.rs b/rust/cloud-storage/models_email/src/email/api/settings.rs
new file mode 100644
index 0000000000..a7f06dc450
--- /dev/null
+++ b/rust/cloud-storage/models_email/src/email/api/settings.rs
@@ -0,0 +1,15 @@
+use serde::{Deserialize, Serialize};
+use utoipa::ToSchema;
+
+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
+pub struct Settings {
+ pub signature_on_replies_forwards: Option<bool>,
+}
+
+impl From<crate::email::service::settings::Settings> for Settings {
+ fn from(service_settings: crate::email::service::settings::Settings) -> Self {
+ Settings {
+ signature_on_replies_forwards: Some(service_settings.signature_on_replies_forwards),
+ }
+ }
+}
diff --git a/rust/cloud-storage/models_email/src/email/db/mod.rs b/rust/cloud-storage/models_email/src/email/db/mod.rs
index 82bc701ecb..634091cb4d 100644
--- a/rust/cloud-storage/models_email/src/email/db/mod.rs
+++ b/rust/cloud-storage/models_email/src/email/db/mod.rs
@@ -6,5 +6,7 @@ pub mod contact;
pub mod label;
pub mod link;
pub mod message;
+
+pub mod settings;
pub mod sync_token;
pub mod thread;
diff --git a/rust/cloud-storage/models_email/src/email/db/settings.rs b/rust/cloud-storage/models_email/src/email/db/settings.rs
new file mode 100644
index 0000000000..6986ba3f13
--- /dev/null
+++ b/rust/cloud-storage/models_email/src/email/db/settings.rs
@@ -0,0 +1,18 @@
+use serde::{Deserialize, Serialize};
+use utoipa::ToSchema;
+use uuid::Uuid;
+
+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
+pub struct Settings {
+ pub link_id: Uuid,
+ pub signature_on_replies_forwards: bool,
+}
+
+impl From<crate::email::service::settings::Settings> for Settings {
+ fn from(service_settings: crate::email::service::settings::Settings) -> Self {
+ Settings {
+ link_id: service_settings.link_id,
+ signature_on_replies_forwards: service_settings.signature_on_replies_forwards,
+ }
+ }
+}
diff --git a/rust/cloud-storage/models_email/src/email/mod.rs b/rust/cloud-storage/models_email/src/email/mod.rs
index bfd6c0dd1a..9a703e7de3 100644
--- a/rust/cloud-storage/models_email/src/email/mod.rs
+++ b/rust/cloud-storage/models_email/src/email/mod.rs
@@ -1,2 +1,3 @@
+pub mod api;
pub mod db;
pub mod service;
diff --git a/rust/cloud-storage/models_email/src/email/service/mod.rs b/rust/cloud-storage/models_email/src/email/service/mod.rs
index 36b3fc1351..7b6098f65e 100644
--- a/rust/cloud-storage/models_email/src/email/service/mod.rs
+++ b/rust/cloud-storage/models_email/src/email/service/mod.rs
@@ -8,5 +8,6 @@ pub mod label;
pub mod link;
pub mod message;
pub mod pubsub;
+pub mod settings;
pub mod sync_token;
pub mod thread;
diff --git a/rust/cloud-storage/models_email/src/email/service/settings.rs b/rust/cloud-storage/models_email/src/email/service/settings.rs
new file mode 100644
index 0000000000..9d2ad54a37
--- /dev/null
+++ b/rust/cloud-storage/models_email/src/email/service/settings.rs
@@ -0,0 +1,29 @@
+use serde::{Deserialize, Serialize};
+use utoipa::ToSchema;
+use uuid::Uuid;
+
+#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
+pub struct Settings {
+ pub link_id: Uuid,
+ pub signature_on_replies_forwards: bool,
+}
+
+impl Settings {
+ pub fn new(api_settings: crate::email::api::settings::Settings, link_id: Uuid) -> Self {
+ Settings {
+ link_id,
+ signature_on_replies_forwards: api_settings
+ .signature_on_replies_forwards
+ .unwrap_or(false),
+ }
+ }
+}
+
+impl From<crate::email::db::settings::Settings> for Settings {
+ fn from(db_settings: crate::email::db::settings::Settings) -> Self {
+ Settings {
+ link_id: db_settings.link_id,
+ signature_on_replies_forwards: db_settings.signature_on_replies_forwards,
+ }
+ }
+}
diff --git a/rust/cloud-storage/models_email/src/gmail/mod.rs b/rust/cloud-storage/models_email/src/gmail/mod.rs
index cb1e41717d..b6007709c8 100644
--- a/rust/cloud-storage/models_email/src/gmail/mod.rs
+++ b/rust/cloud-storage/models_email/src/gmail/mod.rs
@@ -224,3 +224,10 @@ pub struct GmailUserProfile {
pub threads_total: i32,
pub history_id: String,
}
+
+/// Response structure for Gmail send-as settings
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct EmailSignature {
+ pub signature: String,
+}