init
This commit is contained in:
@@ -67,6 +67,7 @@ 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 oldest_id = page.messages.first().map(|message| message.id);
|
||||
let newest_id = page.messages.last().map(|message| message.id);
|
||||
|
||||
@@ -76,7 +77,8 @@ pub async fn get_all(
|
||||
.into_iter()
|
||||
.map(|message| {
|
||||
let groups = reactions.remove(&message.id).unwrap_or_default();
|
||||
mapper::message_model_to_message_response_with_reactions(message, None, groups)
|
||||
let files = attachments.remove(&message.id).unwrap_or_default();
|
||||
mapper::message_model_to_message_response_with_data(message, None, groups, files)
|
||||
})
|
||||
.collect(),
|
||||
oldest_id,
|
||||
@@ -122,9 +124,10 @@ 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();
|
||||
|
||||
Ok(Json(
|
||||
mapper::message_model_to_message_response_with_reactions(message, None, reactions),
|
||||
mapper::message_model_to_message_response_with_data(message, None, reactions, attachments),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -159,6 +162,10 @@ pub async fn create(
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
|
||||
if payload.content.trim().is_empty() && payload.file_ids.is_empty() {
|
||||
return Err(HTTPError::BadRequest("content or at least one file is required".into()));
|
||||
}
|
||||
|
||||
// Optionnel: vérifier reply_to_id
|
||||
if let Some(reply_id) = payload.reply_to_id {
|
||||
state
|
||||
@@ -174,13 +181,17 @@ pub async fn create(
|
||||
let message = state
|
||||
.services
|
||||
.message
|
||||
.create_message(payload.channel_id, user.id, payload.content)
|
||||
.await?;
|
||||
.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();
|
||||
Ok((
|
||||
StatusCode::CREATED,
|
||||
Json(mapper::message_model_to_message_response_with_server_id(
|
||||
Json(mapper::message_model_to_message_response_with_data(
|
||||
message,
|
||||
channel.server_id,
|
||||
Vec::new(),
|
||||
attachments,
|
||||
)),
|
||||
))
|
||||
}
|
||||
@@ -236,8 +247,9 @@ 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_reactions(message, None, reactions),
|
||||
mapper::message_model_to_message_response_with_data(message, None, reactions, attachments),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -345,7 +357,22 @@ pub async fn delete(
|
||||
return Err(HTTPError::Forbidden);
|
||||
}
|
||||
|
||||
let attachment_ids: Vec<_> = state
|
||||
.repositories
|
||||
.message
|
||||
.attachments_for_messages(&[id])
|
||||
.await?
|
||||
.remove(&id)
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|item| item.file_path)
|
||||
.collect();
|
||||
|
||||
if state.services.message.delete_message(id).await? {
|
||||
for attachment_path in attachment_ids {
|
||||
let path = std::path::PathBuf::from(&state.config.media.root).join(attachment_path);
|
||||
let _ = tokio::fs::remove_file(path).await;
|
||||
}
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
} else {
|
||||
Err(HTTPError::NotFound)
|
||||
|
||||
Reference in New Issue
Block a user