wsb 0.155.49

Workspace - A tool suite for file operations, version management, and development workflow automation
Documentation
use axum::{
    extract::{Path, State},
    http::StatusCode,
    Json,
};
use serde::Deserialize;

use super::sessions::ErrorResponse;
use crate::web::state::WebState;

pub async fn list_all_pins(
    State(state): State<WebState>,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
    let pins = state.db.list_all_pins().await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: e.to_string(),
            }),
        )
    })?;
    Ok(Json(serde_json::json!({ "pins": pins })))
}

pub async fn list_session_pins(
    State(state): State<WebState>,
    Path(id): Path<String>,
) -> Result<Json<serde_json::Value>, (StatusCode, Json<ErrorResponse>)> {
    let pins = state.db.list_session_pins(&id).await.map_err(|e| {
        (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(ErrorResponse {
                error: e.to_string(),
            }),
        )
    })?;
    Ok(Json(serde_json::json!({ "pins": pins })))
}

#[derive(Deserialize)]
pub struct PinRequest {
    pub note: Option<String>,
}

pub async fn pin_message(
    State(state): State<WebState>,
    Path((session_id, message_id)): Path<(String, i64)>,
    Json(body): Json<PinRequest>,
) -> Result<(StatusCode, Json<serde_json::Value>), (StatusCode, Json<ErrorResponse>)> {
    let pin_id = state
        .db
        .pin_message(&session_id, message_id, body.note.as_deref())
        .await
        .map_err(|e| {
            (
                StatusCode::BAD_REQUEST,
                Json(ErrorResponse {
                    error: e.to_string(),
                }),
            )
        })?;

    Ok((
        StatusCode::CREATED,
        Json(serde_json::json!({ "id": pin_id })),
    ))
}

pub async fn unpin_message(
    State(state): State<WebState>,
    Path((session_id, message_id)): Path<(String, i64)>,
) -> Result<StatusCode, (StatusCode, Json<ErrorResponse>)> {
    state
        .db
        .unpin_message(&session_id, message_id)
        .await
        .map_err(|e| {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(ErrorResponse {
                    error: e.to_string(),
                }),
            )
        })?;
    Ok(StatusCode::NO_CONTENT)
}