49 lines
1.3 KiB
Rust
49 lines
1.3 KiB
Rust
use axum::Json;
|
|
use axum::extract::State;
|
|
use axum::response::IntoResponse;
|
|
use serde::Serialize;
|
|
|
|
use crate::entity::channel;
|
|
use crate::state::AppState;
|
|
use crate::web;
|
|
use crate::web::context::UserContext;
|
|
use crate::web::route::user::PartialUser;
|
|
|
|
#[derive(Debug, sqlx::FromRow, Serialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct RecipientChannel {
|
|
#[serde(flatten)]
|
|
pub channel: channel::Channel,
|
|
pub recipients: Vec<PartialUser>,
|
|
}
|
|
|
|
pub async fn list(
|
|
State(state): State<AppState>,
|
|
context: UserContext,
|
|
) -> web::Result<impl IntoResponse> {
|
|
let channels = state.database.select_user_channels(context.user_id).await?;
|
|
|
|
let mut recipient_channels = Vec::new();
|
|
for channel in channels {
|
|
let recipients = state.database.select_channel_recipients(channel.id).await?;
|
|
|
|
let recipients = match recipients {
|
|
Some(recipients) => recipients
|
|
.into_iter()
|
|
.filter(|user| user.id != context.user_id)
|
|
.map(PartialUser::from)
|
|
.collect(),
|
|
None => {
|
|
continue;
|
|
},
|
|
};
|
|
|
|
recipient_channels.push(RecipientChannel {
|
|
channel,
|
|
recipients,
|
|
});
|
|
}
|
|
|
|
Ok(Json(recipient_channels))
|
|
}
|