init
This commit is contained in:
@@ -23,8 +23,12 @@ pub async fn cleanup_expired(state: &AppState) -> Result<(), HTTPError> {
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
for item in pending {
|
||||
let _ = tokio::fs::remove_file(PathBuf::from(&state.config.media.root).join(&item.file_path)).await;
|
||||
attachment::Entity::delete_by_id(item.id).exec(&state.db).await?;
|
||||
let _ =
|
||||
tokio::fs::remove_file(PathBuf::from(&state.config.media.root).join(&item.file_path))
|
||||
.await;
|
||||
attachment::Entity::delete_by_id(item.id)
|
||||
.exec(&state.db)
|
||||
.await?;
|
||||
}
|
||||
media::cleanup_temporary_files(
|
||||
PathBuf::from(&state.config.media.root).as_path(),
|
||||
@@ -58,13 +62,22 @@ pub async fn create(
|
||||
{
|
||||
let name = field.name().unwrap_or_default().to_string();
|
||||
if name == "channel_id" {
|
||||
let value = field.text().await.map_err(|error| HTTPError::BadRequest(error.to_string()))?;
|
||||
let value = field
|
||||
.text()
|
||||
.await
|
||||
.map_err(|error| HTTPError::BadRequest(error.to_string()))?;
|
||||
channel_id = Some(value.parse::<Uuid>().map_err(HTTPError::UuidError)?);
|
||||
} else if name == "files" || name == "file" {
|
||||
let channel = channel_id.ok_or_else(|| HTTPError::BadRequest("channel_id must precede files".into()))?;
|
||||
let channel = channel_id
|
||||
.ok_or_else(|| HTTPError::BadRequest("channel_id must precede files".into()))?;
|
||||
let filename = field.file_name().unwrap_or("unnamed").to_string();
|
||||
let mime_type = field.content_type().unwrap_or("application/octet-stream").to_string();
|
||||
if !can_access(&state, channel, user.id).await? { return Err(HTTPError::Forbidden); }
|
||||
let mime_type = field
|
||||
.content_type()
|
||||
.unwrap_or("application/octet-stream")
|
||||
.to_string();
|
||||
if !can_access(&state, channel, user.id).await? {
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
let id = Uuid::new_v4();
|
||||
let mut output = PendingMediaFile::begin(
|
||||
PathBuf::from(&state.config.media.root).as_path(),
|
||||
@@ -75,35 +88,84 @@ pub async fn create(
|
||||
.await
|
||||
.map_err(|error| HTTPError::InternalServerError(error.to_string()))?;
|
||||
let mut size = 0_i64;
|
||||
while let Some(chunk) = field.chunk().await.map_err(|error| HTTPError::BadRequest(error.to_string()))? {
|
||||
while let Some(chunk) = field
|
||||
.chunk()
|
||||
.await
|
||||
.map_err(|error| HTTPError::BadRequest(error.to_string()))?
|
||||
{
|
||||
size += chunk.len() as i64;
|
||||
output.write(&chunk).await.map_err(|error| HTTPError::InternalServerError(error.to_string()))?;
|
||||
output
|
||||
.write(&chunk)
|
||||
.await
|
||||
.map_err(|error| HTTPError::InternalServerError(error.to_string()))?;
|
||||
}
|
||||
let file_path = output.finish().await.map_err(|error| HTTPError::InternalServerError(error.to_string()))?;
|
||||
let file_path = output
|
||||
.finish()
|
||||
.await
|
||||
.map_err(|error| HTTPError::InternalServerError(error.to_string()))?;
|
||||
let model = attachment::ActiveModel {
|
||||
id: Set(id), message_id: Set(None), channel_id: Set(channel), user_id: Set(user.id),
|
||||
filename: Set(filename), file_size: Set(size), mime_type: Set(mime_type), file_path: Set(file_path.clone()), created_at: Set(Utc::now()),
|
||||
id: Set(id),
|
||||
message_id: Set(None),
|
||||
channel_id: Set(channel),
|
||||
user_id: Set(user.id),
|
||||
filename: Set(filename),
|
||||
file_size: Set(size),
|
||||
mime_type: Set(mime_type),
|
||||
file_path: Set(file_path.clone()),
|
||||
created_at: Set(Utc::now()),
|
||||
};
|
||||
match model.insert(&state.db).await {
|
||||
Ok(item) => created.push(item),
|
||||
Err(error) => { let _ = tokio::fs::remove_file(PathBuf::from(&state.config.media.root).join(file_path)).await; return Err(HTTPError::Database(error)); }
|
||||
Err(error) => {
|
||||
let _ = tokio::fs::remove_file(
|
||||
PathBuf::from(&state.config.media.root).join(file_path),
|
||||
)
|
||||
.await;
|
||||
return Err(HTTPError::Database(error));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let channel_id = channel_id.ok_or_else(|| HTTPError::BadRequest("channel_id is required".into()))?;
|
||||
if !can_access(&state, channel_id, user.id).await? { return Err(HTTPError::Forbidden); }
|
||||
if created.is_empty() { return Err(HTTPError::BadRequest("at least one file is required".into())); }
|
||||
Ok((StatusCode::CREATED, axum::Json(AttachmentUploadResponse { attachments: created.into_iter().map(mapper::to_response).collect() })))
|
||||
let channel_id =
|
||||
channel_id.ok_or_else(|| HTTPError::BadRequest("channel_id is required".into()))?;
|
||||
if !can_access(&state, channel_id, user.id).await? {
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
if created.is_empty() {
|
||||
return Err(HTTPError::BadRequest(
|
||||
"at least one file is required".into(),
|
||||
));
|
||||
}
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
axum::Json(AttachmentUploadResponse {
|
||||
attachments: created.into_iter().map(mapper::to_response).collect(),
|
||||
}),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn file(State(state): State<AppState>, Path(id): Path<Uuid>) -> Result<Response, HTTPError> {
|
||||
let item = attachment::Entity::find_by_id(id).one(&state.db).await?.ok_or(HTTPError::NotFound)?;
|
||||
let bytes = tokio::fs::read(PathBuf::from(&state.config.media.root).join(&item.file_path)).await.map_err(|_| HTTPError::NotFound)?;
|
||||
pub async fn file(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Response, HTTPError> {
|
||||
let item = attachment::Entity::find_by_id(id)
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
let bytes = tokio::fs::read(PathBuf::from(&state.config.media.root).join(&item.file_path))
|
||||
.await
|
||||
.map_err(|_| HTTPError::NotFound)?;
|
||||
let mut response = Response::new(Body::from(bytes));
|
||||
if let Ok(value) = item.mime_type.parse() { response.headers_mut().insert(header::CONTENT_TYPE, value); }
|
||||
if let Ok(value) = item.mime_type.parse() {
|
||||
response.headers_mut().insert(header::CONTENT_TYPE, value);
|
||||
}
|
||||
if !item.mime_type.starts_with("image/") {
|
||||
let safe_name = item.filename.replace(['\"', '\r', '\n'], "_");
|
||||
if let Ok(value) = format!("attachment; filename=\"{safe_name}\"").parse() { response.headers_mut().insert(header::CONTENT_DISPOSITION, value); }
|
||||
if let Ok(value) = format!("attachment; filename=\"{safe_name}\"").parse() {
|
||||
response
|
||||
.headers_mut()
|
||||
.insert(header::CONTENT_DISPOSITION, value);
|
||||
}
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
use axum::{Router, routing::{get, post}, extract::DefaultBodyLimit};
|
||||
use crate::core::state::AppState;
|
||||
use super::handlers;
|
||||
use crate::core::state::AppState;
|
||||
use axum::{
|
||||
Router,
|
||||
extract::DefaultBodyLimit,
|
||||
routing::{get, post},
|
||||
};
|
||||
|
||||
pub fn secure_router() -> Router<AppState> {
|
||||
Router::new()
|
||||
|
||||
@@ -13,14 +13,30 @@ use axum::{
|
||||
extract::{Path, Query, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
||||
use uuid::Uuid;
|
||||
|
||||
async fn require_channel_member(state: &AppState, channel_id: Uuid, user_id: Uuid) -> Result<(), HTTPError> {
|
||||
let channel = state.repositories.channel.get_by_id(channel_id).await?.ok_or(HTTPError::NotFound)?;
|
||||
async fn require_channel_member(
|
||||
state: &AppState,
|
||||
channel_id: Uuid,
|
||||
user_id: Uuid,
|
||||
) -> Result<(), HTTPError> {
|
||||
let channel = state
|
||||
.repositories
|
||||
.channel
|
||||
.get_by_id(channel_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
if channel.channel_type == channel::ChannelType::DM
|
||||
&& channel_user::Entity::find().filter(channel_user::Column::ChannelId.eq(channel_id)).filter(channel_user::Column::UserId.eq(user_id)).one(&state.db).await?.is_none()
|
||||
{ return Err(HTTPError::Forbidden); }
|
||||
&& channel_user::Entity::find()
|
||||
.filter(channel_user::Column::ChannelId.eq(channel_id))
|
||||
.filter(channel_user::Column::UserId.eq(user_id))
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.is_none()
|
||||
{
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -6,70 +6,178 @@ use crate::domain::dto::conversation::{
|
||||
use crate::http::context::CurrentUser;
|
||||
use crate::http::error::HTTPError;
|
||||
use crate::models::{channel, channel_user, message, user};
|
||||
use axum::{Json, extract::{Path, State}};
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, State},
|
||||
};
|
||||
use chrono::Utc;
|
||||
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, QueryOrder, Set, TransactionTrait};
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, QueryOrder, Set, TransactionTrait,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use uuid::Uuid;
|
||||
|
||||
async fn member_ids(state: &AppState, channel_id: Uuid) -> Result<Vec<Uuid>, HTTPError> {
|
||||
Ok(channel_user::Entity::find()
|
||||
.filter(channel_user::Column::ChannelId.eq(channel_id))
|
||||
.all(&state.db).await?.into_iter().map(|m| m.user_id).collect())
|
||||
.all(&state.db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|m| m.user_id)
|
||||
.collect())
|
||||
}
|
||||
|
||||
async fn response(state: &AppState, current_user_id: Uuid, channel: channel::Model) -> Result<ConversationResponse, HTTPError> {
|
||||
async fn response(
|
||||
state: &AppState,
|
||||
current_user_id: Uuid,
|
||||
channel: channel::Model,
|
||||
) -> Result<ConversationResponse, HTTPError> {
|
||||
let ids = member_ids(state, channel.id).await?;
|
||||
let users = user::Entity::find().filter(user::Column::Id.is_in(ids.clone())).all(&state.db).await?;
|
||||
let mut participants: Vec<_> = users.into_iter().map(|u| ConversationParticipantResponse { id: u.id, username: u.username }).collect();
|
||||
let users = user::Entity::find()
|
||||
.filter(user::Column::Id.is_in(ids.clone()))
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
let mut participants: Vec<_> = users
|
||||
.into_iter()
|
||||
.map(|u| ConversationParticipantResponse {
|
||||
id: u.id,
|
||||
username: u.username,
|
||||
})
|
||||
.collect();
|
||||
participants.sort_by(|a, b| a.username.to_lowercase().cmp(&b.username.to_lowercase()));
|
||||
let title = participants.iter().filter(|p| p.id != current_user_id).map(|p| p.username.clone()).collect::<Vec<_>>().join(", ");
|
||||
let last_message = message::Entity::find().filter(message::Column::ChannelId.eq(channel.id)).order_by_desc(message::Column::Id).one(&state.db).await?.map(|m| m.content);
|
||||
let unread_count = state.repositories.read_state.unread_counts(&[channel.id], current_user_id).await?.get(&channel.id).copied().unwrap_or(0);
|
||||
Ok(ConversationResponse { id: channel.id, title: if title.is_empty() { "Discussion".into() } else { title }, participants, last_message, unread_count, created_at: channel.created_at, updated_at: channel.updated_at })
|
||||
let title = participants
|
||||
.iter()
|
||||
.filter(|p| p.id != current_user_id)
|
||||
.map(|p| p.username.clone())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ");
|
||||
let last_message = message::Entity::find()
|
||||
.filter(message::Column::ChannelId.eq(channel.id))
|
||||
.order_by_desc(message::Column::Id)
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.map(|m| m.content);
|
||||
let unread_count = state
|
||||
.repositories
|
||||
.read_state
|
||||
.unread_counts(&[channel.id], current_user_id)
|
||||
.await?
|
||||
.get(&channel.id)
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
Ok(ConversationResponse {
|
||||
id: channel.id,
|
||||
title: if title.is_empty() {
|
||||
"Discussion".into()
|
||||
} else {
|
||||
title
|
||||
},
|
||||
participants,
|
||||
last_message,
|
||||
unread_count,
|
||||
created_at: channel.created_at,
|
||||
updated_at: channel.updated_at,
|
||||
})
|
||||
}
|
||||
|
||||
async fn create_channel(state: &AppState, ids: &[Uuid]) -> Result<channel::Model, HTTPError> {
|
||||
let txn = state.db.begin().await?;
|
||||
let channel = channel::ActiveModel { server_id: Set(None), category_id: Set(None), channel_type: Set(channel::ChannelType::DM), name: Set(None), ..Default::default() }.insert(&txn).await?;
|
||||
let channel = channel::ActiveModel {
|
||||
server_id: Set(None),
|
||||
category_id: Set(None),
|
||||
channel_type: Set(channel::ChannelType::DM),
|
||||
name: Set(None),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&txn)
|
||||
.await?;
|
||||
for user_id in ids {
|
||||
channel_user::ActiveModel { channel_id: Set(channel.id), user_id: Set(*user_id), role: Set("member".into()), joined_at: Set(Utc::now()), ..Default::default() }.insert(&txn).await?;
|
||||
channel_user::ActiveModel {
|
||||
channel_id: Set(channel.id),
|
||||
user_id: Set(*user_id),
|
||||
role: Set("member".into()),
|
||||
joined_at: Set(Utc::now()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(&txn)
|
||||
.await?;
|
||||
}
|
||||
txn.commit().await?;
|
||||
state.services.realtime_registry.set_channel_users(channel.id, ids.iter().copied());
|
||||
state
|
||||
.services
|
||||
.realtime_registry
|
||||
.set_channel_users(channel.id, ids.iter().copied());
|
||||
state.event_bus.emit("channel_created", channel.clone());
|
||||
Ok(channel)
|
||||
}
|
||||
|
||||
async fn validate_ids(state: &AppState, ids: &[Uuid]) -> Result<(), HTTPError> {
|
||||
if ids.is_empty() { return Err(HTTPError::BadRequest("At least one participant is required".into())); }
|
||||
let found = user::Entity::find().filter(user::Column::Id.is_in(ids.to_vec())).all(&state.db).await?;
|
||||
if found.len() != ids.iter().collect::<HashSet<_>>().len() { return Err(HTTPError::BadRequest("Unknown participant".into())); }
|
||||
if ids.is_empty() {
|
||||
return Err(HTTPError::BadRequest(
|
||||
"At least one participant is required".into(),
|
||||
));
|
||||
}
|
||||
let found = user::Entity::find()
|
||||
.filter(user::Column::Id.is_in(ids.to_vec()))
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
if found.len() != ids.iter().collect::<HashSet<_>>().len() {
|
||||
return Err(HTTPError::BadRequest("Unknown participant".into()));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[utoipa::path(get, path = "/conversations", responses((status = 200, body = [ConversationResponse])), tag = "Conversations", security(("bearerAuth" = [])))]
|
||||
pub async fn list(user: CurrentUser, State(state): State<AppState>) -> Result<Json<Vec<ConversationResponse>>, HTTPError> {
|
||||
let memberships = channel_user::Entity::find().filter(channel_user::Column::UserId.eq(user.id)).all(&state.db).await?;
|
||||
pub async fn list(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Vec<ConversationResponse>>, HTTPError> {
|
||||
let memberships = channel_user::Entity::find()
|
||||
.filter(channel_user::Column::UserId.eq(user.id))
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
let ids: Vec<_> = memberships.into_iter().map(|m| m.channel_id).collect();
|
||||
if ids.is_empty() { return Ok(Json(Vec::new())); }
|
||||
let channels = channel::Entity::find().filter(channel::Column::Id.is_in(ids)).filter(channel::Column::ChannelType.eq(channel::ChannelType::DM)).order_by_desc(channel::Column::UpdatedAt).all(&state.db).await?;
|
||||
if ids.is_empty() {
|
||||
return Ok(Json(Vec::new()));
|
||||
}
|
||||
let channels = channel::Entity::find()
|
||||
.filter(channel::Column::Id.is_in(ids))
|
||||
.filter(channel::Column::ChannelType.eq(channel::ChannelType::DM))
|
||||
.order_by_desc(channel::Column::UpdatedAt)
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
let mut result = Vec::with_capacity(channels.len());
|
||||
for channel in channels { result.push(response(&state, user.id, channel).await?); }
|
||||
for channel in channels {
|
||||
result.push(response(&state, user.id, channel).await?);
|
||||
}
|
||||
Ok(Json(result))
|
||||
}
|
||||
|
||||
#[utoipa::path(post, path = "/conversations", request_body = CreateConversationRequest, responses((status = 200, body = ConversationResponse)), tag = "Conversations", security(("bearerAuth" = [])))]
|
||||
pub async fn create(user: CurrentUser, State(state): State<AppState>, Json(payload): Json<CreateConversationRequest>) -> Result<Json<ConversationResponse>, HTTPError> {
|
||||
pub async fn create(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
Json(payload): Json<CreateConversationRequest>,
|
||||
) -> Result<Json<ConversationResponse>, HTTPError> {
|
||||
let mut ids = payload.user_ids;
|
||||
ids.push(user.id);
|
||||
ids.sort_unstable(); ids.dedup();
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
validate_ids(&state, &ids).await?;
|
||||
let channels = channel_user::Entity::find().filter(channel_user::Column::UserId.eq(user.id)).all(&state.db).await?;
|
||||
let channels = channel_user::Entity::find()
|
||||
.filter(channel_user::Column::UserId.eq(user.id))
|
||||
.all(&state.db)
|
||||
.await?;
|
||||
for membership in channels {
|
||||
if let Some(channel) = channel::Entity::find_by_id(membership.channel_id).filter(channel::Column::ChannelType.eq(channel::ChannelType::DM)).one(&state.db).await? {
|
||||
if let Some(channel) = channel::Entity::find_by_id(membership.channel_id)
|
||||
.filter(channel::Column::ChannelType.eq(channel::ChannelType::DM))
|
||||
.one(&state.db)
|
||||
.await?
|
||||
{
|
||||
let existing: HashSet<_> = member_ids(&state, channel.id).await?.into_iter().collect();
|
||||
if existing == ids.iter().copied().collect() { return Ok(Json(response(&state, user.id, channel).await?)); }
|
||||
if existing == ids.iter().copied().collect() {
|
||||
return Ok(Json(response(&state, user.id, channel).await?));
|
||||
}
|
||||
}
|
||||
}
|
||||
let channel = create_channel(&state, &ids).await?;
|
||||
@@ -77,12 +185,25 @@ pub async fn create(user: CurrentUser, State(state): State<AppState>, Json(paylo
|
||||
}
|
||||
|
||||
#[utoipa::path(post, path = "/conversations/{id}/fork", request_body = ForkConversationRequest, params(("id" = Uuid, Path)), responses((status = 200, body = ConversationResponse)), tag = "Conversations", security(("bearerAuth" = [])))]
|
||||
pub async fn fork(user: CurrentUser, State(state): State<AppState>, Path(id): Path<Uuid>, Json(payload): Json<ForkConversationRequest>) -> Result<Json<ConversationResponse>, HTTPError> {
|
||||
let source = channel::Entity::find_by_id(id).one(&state.db).await?.ok_or(HTTPError::NotFound)?;
|
||||
if source.channel_type != channel::ChannelType::DM || !member_ids(&state, id).await?.contains(&user.id) { return Err(HTTPError::Forbidden); }
|
||||
pub async fn fork(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
Json(payload): Json<ForkConversationRequest>,
|
||||
) -> Result<Json<ConversationResponse>, HTTPError> {
|
||||
let source = channel::Entity::find_by_id(id)
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
if source.channel_type != channel::ChannelType::DM
|
||||
|| !member_ids(&state, id).await?.contains(&user.id)
|
||||
{
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
let mut ids = member_ids(&state, id).await?;
|
||||
ids.extend(payload.user_ids);
|
||||
ids.sort_unstable(); ids.dedup();
|
||||
ids.sort_unstable();
|
||||
ids.dedup();
|
||||
validate_ids(&state, &ids).await?;
|
||||
let channel = create_channel(&state, &ids).await?;
|
||||
Ok(Json(response(&state, user.id, channel).await?))
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use super::handlers;
|
||||
use crate::core::state::AppState;
|
||||
use axum::{routing::{get, post}, Router};
|
||||
use axum::{
|
||||
Router,
|
||||
routing::{get, post},
|
||||
};
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::handlers;
|
||||
use crate::core::AppState;
|
||||
use axum::Router;
|
||||
use axum::routing::get;
|
||||
use axum::Router;
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new().route("/gateway", get(handlers::ws_handler))
|
||||
|
||||
@@ -13,11 +13,18 @@ use axum::{
|
||||
extract::{Path, Query, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
use uuid::Uuid;
|
||||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub(crate) async fn can_access(state: &AppState, channel_id: Uuid, user_id: Uuid) -> Result<bool, HTTPError> {
|
||||
let Some(channel) = channel::Entity::find_by_id(channel_id).one(&state.db).await? else {
|
||||
pub(crate) async fn can_access(
|
||||
state: &AppState,
|
||||
channel_id: Uuid,
|
||||
user_id: Uuid,
|
||||
) -> Result<bool, HTTPError> {
|
||||
let Some(channel) = channel::Entity::find_by_id(channel_id)
|
||||
.one(&state.db)
|
||||
.await?
|
||||
else {
|
||||
return Ok(false);
|
||||
};
|
||||
if channel.channel_type != channel::ChannelType::DM {
|
||||
@@ -26,7 +33,9 @@ pub(crate) async fn can_access(state: &AppState, channel_id: Uuid, user_id: Uuid
|
||||
Ok(channel_user::Entity::find()
|
||||
.filter(channel_user::Column::ChannelId.eq(channel_id))
|
||||
.filter(channel_user::Column::UserId.eq(user_id))
|
||||
.one(&state.db).await?.is_some())
|
||||
.one(&state.db)
|
||||
.await?
|
||||
.is_some())
|
||||
}
|
||||
|
||||
/// Liste une fenêtre paginée de messages
|
||||
@@ -67,7 +76,11 @@ pub async fn get_all(
|
||||
.message_reaction
|
||||
.grouped_for_messages(&message_ids)
|
||||
.await?;
|
||||
let mut attachments = state.repositories.message.attachments_for_messages(&message_ids).await?;
|
||||
let mut attachments = state
|
||||
.repositories
|
||||
.message
|
||||
.attachments_for_messages(&message_ids)
|
||||
.await?;
|
||||
let oldest_id = page.messages.first().map(|message| message.id);
|
||||
let newest_id = page.messages.last().map(|message| message.id);
|
||||
|
||||
@@ -124,11 +137,20 @@ pub async fn get_by_id(
|
||||
.await?
|
||||
.remove(&id)
|
||||
.unwrap_or_default();
|
||||
let attachments = state.repositories.message.attachments_for_messages(&[id]).await?.remove(&id).unwrap_or_default();
|
||||
let attachments = state
|
||||
.repositories
|
||||
.message
|
||||
.attachments_for_messages(&[id])
|
||||
.await?
|
||||
.remove(&id)
|
||||
.unwrap_or_default();
|
||||
|
||||
Ok(Json(
|
||||
mapper::message_model_to_message_response_with_data(message, None, reactions, attachments),
|
||||
))
|
||||
Ok(Json(mapper::message_model_to_message_response_with_data(
|
||||
message,
|
||||
None,
|
||||
reactions,
|
||||
attachments,
|
||||
)))
|
||||
}
|
||||
|
||||
/// Crée un nouveau message
|
||||
@@ -163,7 +185,9 @@ pub async fn create(
|
||||
}
|
||||
|
||||
if payload.content.trim().is_empty() && payload.file_ids.is_empty() {
|
||||
return Err(HTTPError::BadRequest("content or at least one file is required".into()));
|
||||
return Err(HTTPError::BadRequest(
|
||||
"content or at least one file is required".into(),
|
||||
));
|
||||
}
|
||||
|
||||
// Optionnel: vérifier reply_to_id
|
||||
@@ -181,10 +205,22 @@ pub async fn create(
|
||||
let message = state
|
||||
.services
|
||||
.message
|
||||
.create_message_with_attachments(payload.channel_id, user.id, payload.content, payload.file_ids, payload.reply_to_id)
|
||||
.create_message_with_attachments(
|
||||
payload.channel_id,
|
||||
user.id,
|
||||
payload.content,
|
||||
payload.file_ids,
|
||||
payload.reply_to_id,
|
||||
)
|
||||
.await
|
||||
.map_err(|error| HTTPError::BadRequest(error.to_string()))?;
|
||||
let attachments = state.repositories.message.attachments_for_messages(&[message.id]).await?.remove(&message.id).unwrap_or_default();
|
||||
let attachments = state
|
||||
.repositories
|
||||
.message
|
||||
.attachments_for_messages(&[message.id])
|
||||
.await?
|
||||
.remove(&message.id)
|
||||
.unwrap_or_default();
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(mapper::message_model_to_message_response_with_data(
|
||||
@@ -247,10 +283,19 @@ pub async fn update(
|
||||
.await?
|
||||
.remove(&id)
|
||||
.unwrap_or_default();
|
||||
let attachments = state.repositories.message.attachments_for_messages(&[id]).await?.remove(&id).unwrap_or_default();
|
||||
Ok(Json(
|
||||
mapper::message_model_to_message_response_with_data(message, None, reactions, attachments),
|
||||
))
|
||||
let attachments = state
|
||||
.repositories
|
||||
.message
|
||||
.attachments_for_messages(&[id])
|
||||
.await?
|
||||
.remove(&id)
|
||||
.unwrap_or_default();
|
||||
Ok(Json(mapper::message_model_to_message_response_with_data(
|
||||
message,
|
||||
None,
|
||||
reactions,
|
||||
attachments,
|
||||
)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
@@ -273,8 +318,15 @@ pub async fn add_reaction(
|
||||
Path(message_id): Path<Uuid>,
|
||||
Json(payload): Json<CreateReactionRequest>,
|
||||
) -> Result<(StatusCode, Json<ReactionResponse>), HTTPError> {
|
||||
let message = state.repositories.message.get_by_id(message_id).await?.ok_or(HTTPError::NotFound)?;
|
||||
if !can_access(&state, message.channel_id, user.id).await? { return Err(HTTPError::Forbidden); }
|
||||
let message = state
|
||||
.repositories
|
||||
.message
|
||||
.get_by_id(message_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
if !can_access(&state, message.channel_id, user.id).await? {
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
let (reaction, created) = state
|
||||
.services
|
||||
.message_reaction
|
||||
@@ -312,8 +364,15 @@ pub async fn remove_reaction(
|
||||
Path((message_id, emoji_id)): Path<(Uuid, Uuid)>,
|
||||
Query(query): Query<DeleteReactionQuery>,
|
||||
) -> Result<StatusCode, HTTPError> {
|
||||
let message = state.repositories.message.get_by_id(message_id).await?.ok_or(HTTPError::NotFound)?;
|
||||
if !can_access(&state, message.channel_id, user.id).await? { return Err(HTTPError::Forbidden); }
|
||||
let message = state
|
||||
.repositories
|
||||
.message
|
||||
.get_by_id(message_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::NotFound)?;
|
||||
if !can_access(&state, message.channel_id, user.id).await? {
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
state
|
||||
.services
|
||||
.message_reaction
|
||||
|
||||
@@ -3,8 +3,8 @@ use crate::domain::dto::message::{
|
||||
};
|
||||
use crate::domain::dto::reaction::ReactionGroupResponse;
|
||||
use crate::domain::dto::reaction::ReactionResponse;
|
||||
use crate::models::message;
|
||||
use crate::models::attachment;
|
||||
use crate::models::message;
|
||||
use crate::models::message_reaction;
|
||||
use crate::repositories::types::MessageFilter;
|
||||
use chrono::Utc;
|
||||
@@ -38,7 +38,10 @@ pub fn message_model_to_message_response_with_data(
|
||||
updated_at: model.updated_at,
|
||||
reply_to_id: model.reply_to_id,
|
||||
reactions,
|
||||
attachments: attachments.into_iter().map(crate::routes::attachment::mapper::to_response).collect(),
|
||||
attachments: attachments
|
||||
.into_iter()
|
||||
.map(crate::routes::attachment::mapper::to_response)
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user