feat: init

This commit is contained in:
zhenyi
2026-06-07 11:30:56 +08:00
commit 563381c1ca
361 changed files with 41327 additions and 0 deletions
+127
View File
@@ -0,0 +1,127 @@
use crate::error::AppError;
use crate::models::common::{DeliveryChannel, NotificationType, TargetType};
use crate::models::notifications::NotificationBlock;
use crate::service::NotificationService;
use crate::session::Session;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::util::clamp_limit_offset;
#[derive(Debug, Deserialize, Serialize)]
pub struct CreateBlockParams {
pub workspace_id: Option<Uuid>,
pub repo_id: Option<Uuid>,
pub target_type: TargetType,
pub target_id: Option<Uuid>,
pub notification_type: Option<NotificationType>,
pub channel: Option<DeliveryChannel>,
pub reason: Option<String>,
pub expires_at: Option<DateTime<Utc>>,
}
impl NotificationBlock {
pub async fn find_by_id(
pool: &sqlx::PgPool,
id: Uuid,
user_id: Uuid,
) -> Result<Option<Self>, AppError> {
sqlx::query_as::<_, NotificationBlock>(
"SELECT id, user_id, workspace_id, repo_id, target_type, target_id, notification_type, \
channel, reason, expires_at, created_at, updated_at \
FROM notification_block WHERE id = $1 AND user_id = $2",
)
.bind(id)
.bind(user_id)
.fetch_optional(pool)
.await
.map_err(AppError::Database)
}
pub async fn list_for_user(
pool: &sqlx::PgPool,
user_id: Uuid,
limit: i64,
offset: i64,
) -> Result<Vec<Self>, AppError> {
sqlx::query_as::<_, NotificationBlock>(
"SELECT id, user_id, workspace_id, repo_id, target_type, target_id, notification_type, \
channel, reason, expires_at, created_at, updated_at \
FROM notification_block WHERE user_id = $1 \
ORDER BY created_at DESC LIMIT $2 OFFSET $3",
)
.bind(user_id)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await
.map_err(AppError::Database)
}
}
impl NotificationService {
pub async fn list_blocks(
&self,
session: &Session,
limit: i64,
offset: i64,
) -> Result<Vec<NotificationBlock>, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let (limit, offset) = clamp_limit_offset(limit, offset);
NotificationBlock::list_for_user(self.ctx.db.reader(), user_id, limit, offset).await
}
pub async fn create_block(
&self,
session: &Session,
params: CreateBlockParams,
) -> Result<NotificationBlock, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let id = Uuid::now_v7();
let now = Utc::now();
sqlx::query(
"INSERT INTO notification_block \
(id, user_id, workspace_id, repo_id, target_type, target_id, notification_type, channel, reason, expires_at, created_at, updated_at) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $11)",
)
.bind(id)
.bind(user_id)
.bind(params.workspace_id)
.bind(params.repo_id)
.bind(params.target_type.as_str())
.bind(params.target_id)
.bind(params.notification_type.map(|t| t.as_str()))
.bind(params.channel.map(|c| c.as_str()))
.bind(params.reason)
.bind(params.expires_at)
.bind(now)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
NotificationBlock::find_by_id(self.ctx.db.reader(), id, user_id)
.await?
.ok_or(AppError::InternalServerError(
"failed to fetch created block".into(),
))
}
pub async fn delete_block(&self, session: &Session, block_id: Uuid) -> Result<(), AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let result = sqlx::query("DELETE FROM notification_block WHERE id = $1 AND user_id = $2")
.bind(block_id)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
if result.rows_affected() == 0 {
return Err(AppError::NotFound("block not found".into()));
}
Ok(())
}
}
+141
View File
@@ -0,0 +1,141 @@
use crate::error::AppError;
use crate::models::notifications::Notification;
use crate::service::NotificationService;
use crate::session::Session;
use chrono::Utc;
use uuid::Uuid;
use super::util::clamp_limit_offset;
impl NotificationService {
pub async fn list_notifications(
&self,
session: &Session,
unread_only: bool,
limit: i64,
offset: i64,
) -> Result<Vec<Notification>, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let (limit, offset) = clamp_limit_offset(limit, offset);
Notification::list_for_user(self.ctx.db.reader(), user_id, unread_only, limit, offset).await
}
pub async fn count_unread(&self, session: &Session) -> Result<i64, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
Notification::count_unread(self.ctx.db.reader(), user_id).await
}
pub async fn mark_as_read(
&self,
session: &Session,
notification_id: Uuid,
) -> Result<Notification, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let now = Utc::now();
sqlx::query(
"UPDATE notification SET read_at = $1, updated_at = $2 \
WHERE id = $3 AND user_id = $4 AND deleted_at IS NULL",
)
.bind(now)
.bind(now)
.bind(notification_id)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
Notification::find_by_id(self.ctx.db.reader(), notification_id, user_id)
.await?
.ok_or(AppError::NotFound("notification not found".into()))
}
pub async fn mark_all_as_read(&self, session: &Session) -> Result<i64, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let now = Utc::now();
let result = sqlx::query(
"UPDATE notification SET read_at = $1, updated_at = $2 \
WHERE user_id = $3 AND deleted_at IS NULL AND read_at IS NULL",
)
.bind(now)
.bind(now)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
Ok(result.rows_affected() as i64)
}
pub async fn dismiss_notification(
&self,
session: &Session,
notification_id: Uuid,
) -> Result<Notification, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let now = Utc::now();
sqlx::query(
"UPDATE notification SET dismissed_at = $1, updated_at = $2 \
WHERE id = $3 AND user_id = $4 AND deleted_at IS NULL",
)
.bind(now)
.bind(now)
.bind(notification_id)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
Notification::find_by_id(self.ctx.db.reader(), notification_id, user_id)
.await?
.ok_or(AppError::NotFound("notification not found".into()))
}
pub async fn delete_notification(
&self,
session: &Session,
notification_id: Uuid,
) -> Result<(), AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let now = Utc::now();
let result = sqlx::query(
"UPDATE notification SET deleted_at = $1, updated_at = $2 \
WHERE id = $3 AND user_id = $4 AND deleted_at IS NULL",
)
.bind(now)
.bind(now)
.bind(notification_id)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
if result.rows_affected() == 0 {
return Err(AppError::NotFound("notification not found".into()));
}
Ok(())
}
pub async fn clear_all_notifications(&self, session: &Session) -> Result<i64, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let now = Utc::now();
let result = sqlx::query(
"UPDATE notification SET dismissed_at = $1, updated_at = $2 \
WHERE user_id = $3 AND deleted_at IS NULL AND dismissed_at IS NULL",
)
.bind(now)
.bind(now)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
Ok(result.rows_affected() as i64)
}
}
+85
View File
@@ -0,0 +1,85 @@
use crate::error::AppError;
use crate::models::notifications::NotificationDelivery;
use crate::service::NotificationService;
use crate::session::Session;
use uuid::Uuid;
use super::util::clamp_limit_offset;
impl NotificationDelivery {
pub async fn list_for_user(
pool: &sqlx::PgPool,
user_id: Uuid,
limit: i64,
offset: i64,
) -> Result<Vec<Self>, AppError> {
sqlx::query_as::<_, NotificationDelivery>(
"SELECT id, notification_id, user_id, channel, destination, status, provider, \
provider_message_id, attempts, last_error, scheduled_at, sent_at, delivered_at, failed_at, created_at, updated_at \
FROM notification_delivery WHERE user_id = $1 \
ORDER BY created_at DESC LIMIT $2 OFFSET $3",
)
.bind(user_id)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await
.map_err(AppError::Database)
}
pub async fn list_for_notification(
pool: &sqlx::PgPool,
notification_id: Uuid,
user_id: Uuid,
limit: i64,
offset: i64,
) -> Result<Vec<Self>, AppError> {
sqlx::query_as::<_, NotificationDelivery>(
"SELECT d.id, d.notification_id, d.user_id, d.channel, d.destination, d.status, d.provider, \
d.provider_message_id, d.attempts, d.last_error, d.scheduled_at, d.sent_at, d.delivered_at, d.failed_at, d.created_at, d.updated_at \
FROM notification_delivery d \
JOIN notification n ON d.notification_id = n.id \
WHERE d.notification_id = $1 AND n.user_id = $2 \
ORDER BY d.created_at DESC LIMIT $3 OFFSET $4",
)
.bind(notification_id)
.bind(user_id)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await
.map_err(AppError::Database)
}
}
impl NotificationService {
pub async fn list_deliveries(
&self,
session: &Session,
limit: i64,
offset: i64,
) -> Result<Vec<NotificationDelivery>, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let (limit, offset) = clamp_limit_offset(limit, offset);
NotificationDelivery::list_for_user(self.ctx.db.reader(), user_id, limit, offset).await
}
pub async fn list_deliveries_for_notification(
&self,
session: &Session,
notification_id: Uuid,
limit: i64,
offset: i64,
) -> Result<Vec<NotificationDelivery>, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let (limit, offset) = clamp_limit_offset(limit, offset);
NotificationDelivery::list_for_notification(
self.ctx.db.reader(),
notification_id,
user_id,
limit,
offset,
)
.await
}
}
+6
View File
@@ -0,0 +1,6 @@
pub mod blocks;
pub mod core;
pub mod deliveries;
pub mod subscriptions;
pub mod templates;
pub mod util;
+183
View File
@@ -0,0 +1,183 @@
use crate::error::AppError;
use crate::models::common::{EventType, SubscriptionLevel, TargetType};
use crate::models::notifications::NotificationSubscription;
use crate::service::NotificationService;
use crate::session::Session;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::util::clamp_limit_offset;
#[derive(Debug, Deserialize, Serialize)]
pub struct CreateSubscriptionParams {
pub workspace_id: Option<Uuid>,
pub repo_id: Option<Uuid>,
pub target_type: TargetType,
pub target_id: Option<Uuid>,
pub event_types: Vec<EventType>,
pub channels: Vec<String>,
pub level: SubscriptionLevel,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct UpdateSubscriptionParams {
pub event_types: Option<Vec<EventType>>,
pub channels: Option<Vec<String>>,
pub level: Option<SubscriptionLevel>,
pub muted: Option<bool>,
pub muted_until: Option<DateTime<Utc>>,
}
impl NotificationSubscription {
pub async fn find_by_id(
pool: &sqlx::PgPool,
id: Uuid,
user_id: Uuid,
) -> Result<Option<Self>, AppError> {
sqlx::query_as::<_, NotificationSubscription>(
"SELECT id, user_id, workspace_id, repo_id, target_type, target_id, event_types, \
channels, level, muted, muted_until, created_at, updated_at \
FROM notification_subscription WHERE id = $1 AND user_id = $2",
)
.bind(id)
.bind(user_id)
.fetch_optional(pool)
.await
.map_err(AppError::Database)
}
pub async fn list_for_user(
pool: &sqlx::PgPool,
user_id: Uuid,
limit: i64,
offset: i64,
) -> Result<Vec<Self>, AppError> {
sqlx::query_as::<_, NotificationSubscription>(
"SELECT id, user_id, workspace_id, repo_id, target_type, target_id, event_types, \
channels, level, muted, muted_until, created_at, updated_at \
FROM notification_subscription WHERE user_id = $1 \
ORDER BY created_at DESC LIMIT $2 OFFSET $3",
)
.bind(user_id)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await
.map_err(AppError::Database)
}
}
impl NotificationService {
pub async fn list_subscriptions(
&self,
session: &Session,
limit: i64,
offset: i64,
) -> Result<Vec<NotificationSubscription>, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let (limit, offset) = clamp_limit_offset(limit, offset);
NotificationSubscription::list_for_user(self.ctx.db.reader(), user_id, limit, offset).await
}
pub async fn create_subscription(
&self,
session: &Session,
params: CreateSubscriptionParams,
) -> Result<NotificationSubscription, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let id = Uuid::now_v7();
let now = Utc::now();
sqlx::query(
"INSERT INTO notification_subscription \
(id, user_id, workspace_id, repo_id, target_type, target_id, event_types, channels, level, muted, muted_until, created_at, updated_at) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, false, NULL, $10, $10)",
)
.bind(id)
.bind(user_id)
.bind(params.workspace_id)
.bind(params.repo_id)
.bind(params.target_type.as_str())
.bind(params.target_id)
.bind(&params.event_types)
.bind(&params.channels)
.bind(params.level.as_str())
.bind(now)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
NotificationSubscription::find_by_id(self.ctx.db.reader(), id, user_id)
.await?
.ok_or(AppError::InternalServerError(
"failed to fetch created subscription".into(),
))
}
pub async fn update_subscription(
&self,
session: &Session,
subscription_id: Uuid,
params: UpdateSubscriptionParams,
) -> Result<NotificationSubscription, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let now = Utc::now();
let existing =
NotificationSubscription::find_by_id(self.ctx.db.reader(), subscription_id, user_id)
.await?
.ok_or(AppError::NotFound("subscription not found".into()))?;
let event_types = params.event_types.unwrap_or(existing.event_types);
let channels = params.channels.unwrap_or(existing.channels);
let level = params.level.unwrap_or(existing.level);
let muted = params.muted.unwrap_or(existing.muted);
let muted_until = params.muted_until.or(existing.muted_until);
sqlx::query(
"UPDATE notification_subscription \
SET event_types = $1, channels = $2, level = $3, muted = $4, muted_until = $5, updated_at = $6 \
WHERE id = $7 AND user_id = $8",
)
.bind(&event_types)
.bind(&channels)
.bind(level.as_str())
.bind(muted)
.bind(muted_until)
.bind(now)
.bind(subscription_id)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
NotificationSubscription::find_by_id(self.ctx.db.reader(), subscription_id, user_id)
.await?
.ok_or(AppError::InternalServerError(
"failed to fetch updated subscription".into(),
))
}
pub async fn delete_subscription(
&self,
session: &Session,
subscription_id: Uuid,
) -> Result<(), AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let result =
sqlx::query("DELETE FROM notification_subscription WHERE id = $1 AND user_id = $2")
.bind(subscription_id)
.bind(user_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
if result.rows_affected() == 0 {
return Err(AppError::NotFound("subscription not found".into()));
}
Ok(())
}
}
+210
View File
@@ -0,0 +1,210 @@
use crate::error::AppError;
use crate::models::common::{DeliveryChannel, NotificationType, Role};
use crate::models::notifications::NotificationTemplate;
use crate::models::users::User;
use crate::service::NotificationService;
use crate::session::Session;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use super::util::clamp_limit_offset;
#[derive(Debug, Deserialize, Serialize)]
pub struct CreateTemplateParams {
pub key: String,
pub notification_type: NotificationType,
pub channel: DeliveryChannel,
pub locale: String,
pub subject_template: Option<String>,
pub title_template: String,
pub body_template: String,
pub action_text_template: Option<String>,
pub enabled: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct UpdateTemplateParams {
pub subject_template: Option<String>,
pub title_template: Option<String>,
pub body_template: Option<String>,
pub action_text_template: Option<String>,
pub enabled: Option<bool>,
}
impl NotificationTemplate {
pub async fn find_by_id(pool: &sqlx::PgPool, id: Uuid) -> Result<Option<Self>, AppError> {
sqlx::query_as::<_, NotificationTemplate>(
"SELECT id, key, notification_type, channel, locale, subject_template, title_template, \
body_template, action_text_template, enabled, created_by, created_at, updated_at \
FROM notification_template WHERE id = $1",
)
.bind(id)
.fetch_optional(pool)
.await
.map_err(AppError::Database)
}
pub async fn list_all(
pool: &sqlx::PgPool,
limit: i64,
offset: i64,
) -> Result<Vec<Self>, AppError> {
sqlx::query_as::<_, NotificationTemplate>(
"SELECT id, key, notification_type, channel, locale, subject_template, title_template, \
body_template, action_text_template, enabled, created_by, created_at, updated_at \
FROM notification_template ORDER BY key, channel, locale LIMIT $1 OFFSET $2",
)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await
.map_err(AppError::Database)
}
}
impl NotificationService {
/// Check if user is system admin
async fn ensure_system_admin(&self, session: &Session) -> Result<Uuid, AppError> {
let user_id = session.user().ok_or(AppError::Unauthorized)?;
let user: User = sqlx::query_as(
"SELECT id, username, display_name, avatar_url, bio, status, role, visibility, \
is_active, is_bot, last_login_at, created_at, updated_at, deleted_at \
FROM \"user\" WHERE id = $1 AND deleted_at IS NULL",
)
.bind(user_id)
.fetch_optional(self.ctx.db.reader())
.await
.map_err(AppError::Database)?
.ok_or(AppError::NotFound("User not found".into()))?;
if user.role != Role::System && user.role != Role::Admin {
return Err(AppError::Forbidden("System admin access required".into()));
}
Ok(user_id)
}
pub async fn list_templates(
&self,
session: &Session,
limit: i64,
offset: i64,
) -> Result<Vec<NotificationTemplate>, AppError> {
self.ensure_system_admin(session).await?;
let (limit, offset) = clamp_limit_offset(limit, offset);
NotificationTemplate::list_all(self.ctx.db.reader(), limit, offset).await
}
pub async fn get_template(
&self,
session: &Session,
template_id: Uuid,
) -> Result<NotificationTemplate, AppError> {
self.ensure_system_admin(session).await?;
NotificationTemplate::find_by_id(self.ctx.db.reader(), template_id)
.await?
.ok_or(AppError::NotFound("template not found".into()))
}
pub async fn create_template(
&self,
session: &Session,
params: CreateTemplateParams,
) -> Result<NotificationTemplate, AppError> {
let user_id = self.ensure_system_admin(session).await?;
let id = Uuid::now_v7();
let now = chrono::Utc::now();
sqlx::query(
"INSERT INTO notification_template \
(id, key, notification_type, channel, locale, subject_template, title_template, \
body_template, action_text_template, enabled, created_by, created_at, updated_at) \
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $12)",
)
.bind(id)
.bind(&params.key)
.bind(params.notification_type.as_str())
.bind(params.channel.as_str())
.bind(&params.locale)
.bind(params.subject_template)
.bind(&params.title_template)
.bind(&params.body_template)
.bind(params.action_text_template)
.bind(params.enabled)
.bind(user_id)
.bind(now)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
NotificationTemplate::find_by_id(self.ctx.db.reader(), id)
.await?
.ok_or(AppError::InternalServerError(
"failed to fetch created template".into(),
))
}
pub async fn update_template(
&self,
session: &Session,
template_id: Uuid,
params: UpdateTemplateParams,
) -> Result<NotificationTemplate, AppError> {
self.ensure_system_admin(session).await?;
let now = chrono::Utc::now();
let existing = NotificationTemplate::find_by_id(self.ctx.db.reader(), template_id)
.await?
.ok_or(AppError::NotFound("template not found".into()))?;
let subject_template = params.subject_template.or(existing.subject_template);
let title_template = params.title_template.unwrap_or(existing.title_template);
let body_template = params.body_template.unwrap_or(existing.body_template);
let action_text_template = params
.action_text_template
.or(existing.action_text_template);
let enabled = params.enabled.unwrap_or(existing.enabled);
sqlx::query(
"UPDATE notification_template \
SET subject_template = $1, title_template = $2, body_template = $3, \
action_text_template = $4, enabled = $5, updated_at = $6 \
WHERE id = $7",
)
.bind(subject_template)
.bind(&title_template)
.bind(&body_template)
.bind(action_text_template)
.bind(enabled)
.bind(now)
.bind(template_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
NotificationTemplate::find_by_id(self.ctx.db.reader(), template_id)
.await?
.ok_or(AppError::InternalServerError(
"failed to fetch updated template".into(),
))
}
pub async fn delete_template(
&self,
session: &Session,
template_id: Uuid,
) -> Result<(), AppError> {
self.ensure_system_admin(session).await?;
let result = sqlx::query("DELETE FROM notification_template WHERE id = $1")
.bind(template_id)
.execute(self.ctx.db.writer())
.await
.map_err(AppError::Database)?;
if result.rows_affected() == 0 {
return Err(AppError::NotFound("template not found".into()));
}
Ok(())
}
}
+69
View File
@@ -0,0 +1,69 @@
pub use crate::service::util::clamp_limit_offset;
use crate::error::AppError;
use crate::models::notifications::Notification;
use sqlx::PgPool;
use uuid::Uuid;
impl Notification {
pub async fn find_by_id(
pool: &PgPool,
id: Uuid,
user_id: Uuid,
) -> Result<Option<Self>, AppError> {
sqlx::query_as::<_, Notification>(
"SELECT id, user_id, actor_id, workspace_id, repo_id, issue_id, pull_request_id, \
channel_id, message_id, notification_type, title, body, target_type, target_id, \
action_url, priority, read_at, dismissed_at, metadata, created_at, updated_at, deleted_at \
FROM notification WHERE id = $1 AND user_id = $2 AND deleted_at IS NULL",
)
.bind(id)
.bind(user_id)
.fetch_optional(pool)
.await
.map_err(AppError::Database)
}
pub async fn list_for_user(
pool: &PgPool,
user_id: Uuid,
unread_only: bool,
limit: i64,
offset: i64,
) -> Result<Vec<Self>, AppError> {
let sql = if unread_only {
"SELECT id, user_id, actor_id, workspace_id, repo_id, issue_id, pull_request_id, \
channel_id, message_id, notification_type, title, body, target_type, target_id, \
action_url, priority, read_at, dismissed_at, metadata, created_at, updated_at, deleted_at \
FROM notification \
WHERE user_id = $1 AND deleted_at IS NULL AND read_at IS NULL AND dismissed_at IS NULL \
ORDER BY created_at DESC LIMIT $2 OFFSET $3"
} else {
"SELECT id, user_id, actor_id, workspace_id, repo_id, issue_id, pull_request_id, \
channel_id, message_id, notification_type, title, body, target_type, target_id, \
action_url, priority, read_at, dismissed_at, metadata, created_at, updated_at, deleted_at \
FROM notification \
WHERE user_id = $1 AND deleted_at IS NULL AND dismissed_at IS NULL \
ORDER BY created_at DESC LIMIT $2 OFFSET $3"
};
sqlx::query_as::<_, Notification>(sql)
.bind(user_id)
.bind(limit)
.bind(offset)
.fetch_all(pool)
.await
.map_err(AppError::Database)
}
pub async fn count_unread(pool: &PgPool, user_id: Uuid) -> Result<i64, AppError> {
sqlx::query_scalar(
"SELECT COUNT(*) FROM notification \
WHERE user_id = $1 AND deleted_at IS NULL AND read_at IS NULL AND dismissed_at IS NULL",
)
.bind(user_id)
.fetch_one(pool)
.await
.map_err(AppError::Database)
}
}