Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
//! HTTP handlers for notification type preferences (enable/disable).
use axum::{
Json,
extract::{OriginalUri, Path, Query, State},
http::{HeaderMap, StatusCode, header, uri::PathAndQuery},
response::Html,
};
use macro_authorization::{MacroAuthorizationExtractor, MacroAuthorizationService, UserOrInternal};
use macro_service_urls::NotificationServiceUrl;
use macro_user_id::user_id::MacroUserIdStr;
use model_error_response::ErrorResponse;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use crate::domain::{
models::signing::{self, SignedUrl},
service::NotificationReader,
};
use super::NotificationRouterState;
/// Path parameter for a notification event type.
#[derive(Deserialize)]
pub struct NotificationEventTypePath {
/// The notification event type (e.g. "channel_message_send").
pub notification_event_type: String,
}
/// Response for listing disabled notification types.
#[derive(Debug, Serialize, ToSchema)]
pub struct GetNotificationTypePreferencesResponse {
/// The notification types that the user has disabled.
pub disabled_types: Vec<String>,
}
/// Get the notification types that the user has disabled.
#[utoipa::path(
get,
operation_id = "get_notification_type_preferences",
path = "/v1/user_notifications/preferences",
responses(
(status = 200, body = GetNotificationTypePreferencesResponse),
(status = 401, body = ErrorResponse),
(status = 500, body = ErrorResponse),
)
)]
pub async fn get_notification_type_preferences<
S: NotificationReader,
Auth: MacroAuthorizationService,
>(
State(state): State<NotificationRouterState<S, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
) -> Result<Json<GetNotificationTypePreferencesResponse>, (StatusCode, Json<ErrorResponse<'static>>)>
{
let disabled = state
.inner
.get_disabled_notification_types(user.authorization.user.macro_user_id)
.await
.map_err(|e| {
tracing::error!(error=?e, "failed to get notification type preferences");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
message: "failed to get notification type preferences".into(),
}),
)
})?;
Ok(Json(GetNotificationTypePreferencesResponse {
disabled_types: disabled
.into_iter()
.map(|d| d.notification_event_type)
.collect(),
}))
}
/// Disable a notification type for the authenticated user.
#[utoipa::path(
put,
operation_id = "disable_notification_type",
path = "/v1/user_notifications/preferences/{notification_event_type}/disable",
params(
("notification_event_type" = String, Path, description = "The notification event type to disable"),
),
responses(
(status = 200),
(status = 400, body = ErrorResponse),
(status = 401, body = ErrorResponse),
(status = 500, body = ErrorResponse),
)
)]
pub async fn disable_notification_type<S: NotificationReader, Auth: MacroAuthorizationService>(
State(state): State<NotificationRouterState<S, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
Path(NotificationEventTypePath {
notification_event_type,
}): Path<NotificationEventTypePath>,
) -> Result<Json<()>, (StatusCode, Json<ErrorResponse<'static>>)> {
disable_notification_type_inner(
&state,
user.authorization.user.macro_user_id,
notification_event_type.as_str(),
)
.await
.map(Json)
}
/// The query param value to extract the macro user id
#[derive(Deserialize)]
pub struct PresignedQueryParams {
id: MacroUserIdStr<'static>,
}
/// Disable a notification type for the authenticated user via a GET request with a presigned url.
/// This guarantees the signed url was produced in a trusted environment
pub async fn presigned_disable_notification_type<
S: NotificationReader,
Auth: MacroAuthorizationService,
>(
State(state): State<NotificationRouterState<S, Auth>>,
Path(NotificationEventTypePath {
notification_event_type,
}): Path<NotificationEventTypePath>,
Query(params): Query<PresignedQueryParams>,
headers: HeaderMap,
original_uri: OriginalUri,
) -> Result<Html<String>, (StatusCode, Html<String>)> {
let notification_service_url = NotificationServiceUrl::new().map_err(|err| {
tracing::error!(error=?err, "failed to resolve notification service URL");
(
StatusCode::INTERNAL_SERVER_ERROR,
Html("Invalid link".to_string()),
)
})?;
let notification_service_url = notification_service_url.parse_url().map_err(|err| {
tracing::error!(error=?err, "failed to parse notification service URL");
(
StatusCode::INTERNAL_SERVER_ERROR,
Html("Invalid link".to_string()),
)
})?;
let host = headers
.get(header::HOST)
.and_then(|value| value.to_str().ok())
.or_else(|| notification_service_url.host_str())
.ok_or_else(|| (StatusCode::BAD_REQUEST, Html("Invalid link".to_string())))?;
let to_verify = signing::public_request_url(
notification_service_url.scheme(),
host,
original_uri
.path_and_query()
.map(PathAndQuery::as_str)
.unwrap_or("/"),
)
.map_err(|_| (StatusCode::BAD_REQUEST, Html("Invalid link".to_string())))?;
let Some(_verified) = SignedUrl::verify(to_verify, state.hmac_signing_key.clone()) else {
return Err((
StatusCode::BAD_REQUEST,
Html("Invalid signature".to_string()),
));
};
disable_notification_type_inner(&state, params.id, notification_event_type.as_str())
.await
.map(|()| {
Html(format!(
"You have been unsubscribed from {notification_event_type}"
))
})
.map_err(|(status, Json(ErrorResponse { message }))| (status, Html(message.to_string())))
}
/// internal implementation of the GET/PUT methods to DRY up the code
async fn disable_notification_type_inner<S: NotificationReader, Auth: MacroAuthorizationService>(
state: &NotificationRouterState<S, Auth>,
calling_user: MacroUserIdStr<'_>,
notification_event_type: &str,
) -> Result<(), (StatusCode, Json<ErrorResponse<'static>>)> {
// make sure the notification to block is one that matches the list
let true = state
.blockable_notification_typenames
.contains(notification_event_type)
else {
return Err((
StatusCode::BAD_REQUEST,
Json(ErrorResponse {
message: format!(
"Cannot block notification type {notification_event_type}. Expected one of {:?}",
state.blockable_notification_typenames
)
.into(),
}),
));
};
state
.inner
.disable_notification_type(calling_user, notification_event_type)
.await
.map_err(|e| {
tracing::error!(error=?e, "failed to disable notification type");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
message: "failed to disable notification type".into(),
}),
)
})?;
Ok(())
}
/// Re-enable a notification type for the authenticated user.
#[utoipa::path(
put,
operation_id = "enable_notification_type",
path = "/v1/user_notifications/preferences/{notification_event_type}/enable",
params(
("notification_event_type" = String, Path, description = "The notification event type to enable"),
),
responses(
(status = 200),
(status = 401, body = ErrorResponse),
(status = 500, body = ErrorResponse),
)
)]
pub async fn enable_notification_type<S: NotificationReader, Auth: MacroAuthorizationService>(
State(state): State<NotificationRouterState<S, Auth>>,
user: MacroAuthorizationExtractor<Auth, UserOrInternal>,
Path(NotificationEventTypePath {
notification_event_type,
}): Path<NotificationEventTypePath>,
) -> Result<Json<()>, (StatusCode, Json<ErrorResponse<'static>>)> {
state
.inner
.enable_notification_type(
user.authorization.user.macro_user_id,
¬ification_event_type,
)
.await
.map_err(|e| {
tracing::error!(error=?e, "failed to enable notification type");
(
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
message: "failed to enable notification type".into(),
}),
)
})?;
Ok(Json(()))
}