mdk-storage-traits 0.8.0

Storage abstraction for MDK that wraps OpenMLS storage backends
Documentation
//! Welcomes module
//!
//! This module is responsible for storing and retrieving welcomes
//! It also handles the parsing of welcome content
//!
//! The welcomes are stored in the database and can be retrieved by event ID
//!
//! Here we also define the storage traits that are used to store and retrieve welcomes

use nostr::EventId;

pub mod error;
pub mod types;
pub mod validation;

use self::error::WelcomeError;
use self::types::*;

/// Default limit for pending welcomes queries to prevent unbounded memory usage
pub const DEFAULT_PENDING_WELCOMES_LIMIT: usize = 1000;

/// Maximum allowed limit for pending welcomes queries to prevent resource exhaustion
pub const MAX_PENDING_WELCOMES_LIMIT: usize = 10000;

bounded_pagination! {
    /// Pagination parameters for querying pending welcomes
    default_limit: DEFAULT_PENDING_WELCOMES_LIMIT,
    max_limit: MAX_PENDING_WELCOMES_LIMIT,
    error_type: WelcomeError,
    validate_fn: validate_pending_welcomes_limit
}

/// Storage traits for the welcomes module
pub trait WelcomeStorage {
    /// Save a welcome
    fn save_welcome(&self, welcome: Welcome) -> Result<(), WelcomeError>;

    /// Find a welcome by event ID
    fn find_welcome_by_event_id(&self, event_id: &EventId)
    -> Result<Option<Welcome>, WelcomeError>;

    /// Get pending welcomes with optional pagination
    ///
    /// # Arguments
    ///
    /// * `pagination` - Optional pagination parameters. If `None`, uses default limit and offset.
    ///
    /// # Returns
    ///
    /// Returns a vector of pending welcomes ordered by ID (descending)
    ///
    /// # Errors
    ///
    /// Returns [`WelcomeError::InvalidParameters`] if:
    /// - `limit` is 0
    /// - `limit` exceeds [`MAX_PENDING_WELCOMES_LIMIT`]
    ///
    /// # Examples
    ///
    /// ```ignore
    /// // Get pending welcomes with default pagination
    /// let welcomes = storage.pending_welcomes(None)?;
    ///
    /// // Get first 10 pending welcomes
    /// let welcomes = storage.pending_welcomes(Some(Pagination::new(Some(10), Some(0))))?;
    ///
    /// // Get next 10 pending welcomes
    /// let welcomes = storage.pending_welcomes(Some(Pagination::new(Some(10), Some(10))))?;
    /// ```
    fn pending_welcomes(
        &self,
        pagination: Option<Pagination>,
    ) -> Result<Vec<Welcome>, WelcomeError>;

    /// Save a processed welcome
    fn save_processed_welcome(
        &self,
        processed_welcome: ProcessedWelcome,
    ) -> Result<(), WelcomeError>;

    /// Find a processed welcome by event ID
    fn find_processed_welcome_by_event_id(
        &self,
        event_id: &EventId,
    ) -> Result<Option<ProcessedWelcome>, WelcomeError>;
}