init
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
use crate::core::state::AppState;
|
||||
use crate::http::context::Superuser;
|
||||
use crate::http::context::{CurrentUser, Superuser};
|
||||
use crate::http::error::HTTPError;
|
||||
use crate::domain::dto::channel::{
|
||||
ChannelQueryParams, ChannelResponse, ChannelPermissionsResponse, ChannelRolePermissionResponse,
|
||||
ChannelUserPermissionResponse, CreateChannelRequest, SetChannelPermissionRequest,
|
||||
ChannelUserPermissionResponse, CreateChannelRequest, ReadStateResponse,
|
||||
SetChannelPermissionRequest, SetReadStateRequest,
|
||||
UpdateChannelRequest,
|
||||
};
|
||||
use crate::routes::channel::mapper;
|
||||
@@ -41,6 +42,91 @@ pub async fn get_all(
|
||||
))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/channels/{channel_id}/read-state",
|
||||
params(("channel_id" = Uuid, Path, description = "ID du canal")),
|
||||
responses((status = 200, body = ReadStateResponse), (status = 404, description = "Canal non trouvé")),
|
||||
tag = "Channels",
|
||||
security(("bearerAuth" = []))
|
||||
)]
|
||||
pub async fn get_read_state(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
Path(channel_id): Path<Uuid>,
|
||||
) -> Result<Json<ReadStateResponse>, HTTPError> {
|
||||
state.repositories.channel.get_by_id(channel_id).await?.ok_or(HTTPError::NotFound)?;
|
||||
let read_state = state.repositories.read_state.get(channel_id, user.id).await?;
|
||||
let unread_count = state
|
||||
.repositories
|
||||
.read_state
|
||||
.unread_counts(&[channel_id], user.id)
|
||||
.await?
|
||||
.get(&channel_id)
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
|
||||
Ok(Json(ReadStateResponse {
|
||||
channel_id,
|
||||
last_read_message_id: read_state.as_ref().and_then(|value| value.last_read_message_id),
|
||||
updated_at: read_state.map(|value| value.updated_at),
|
||||
unread_count,
|
||||
}))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
put,
|
||||
path = "/channels/{channel_id}/read-state",
|
||||
request_body = SetReadStateRequest,
|
||||
params(("channel_id" = Uuid, Path, description = "ID du canal")),
|
||||
responses((status = 200, body = ReadStateResponse), (status = 400, description = "Message invalide"), (status = 404, description = "Canal non trouvé")),
|
||||
tag = "Channels",
|
||||
security(("bearerAuth" = []))
|
||||
)]
|
||||
pub async fn set_read_state(
|
||||
user: CurrentUser,
|
||||
State(state): State<AppState>,
|
||||
Path(channel_id): Path<Uuid>,
|
||||
Json(payload): Json<SetReadStateRequest>,
|
||||
) -> Result<Json<ReadStateResponse>, HTTPError> {
|
||||
state.repositories.channel.get_by_id(channel_id).await?.ok_or(HTTPError::NotFound)?;
|
||||
|
||||
if let Some(message_id) = payload.last_read_message_id {
|
||||
let message = state
|
||||
.repositories
|
||||
.message
|
||||
.get_by_id(message_id)
|
||||
.await?
|
||||
.ok_or(HTTPError::BadRequest("Message not found".to_string()))?;
|
||||
if message.channel_id != channel_id {
|
||||
return Err(HTTPError::BadRequest(
|
||||
"Message does not belong to this channel".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let read_state = state
|
||||
.repositories
|
||||
.read_state
|
||||
.set(channel_id, user.id, payload.last_read_message_id)
|
||||
.await?;
|
||||
let unread_count = state
|
||||
.repositories
|
||||
.read_state
|
||||
.unread_counts(&[channel_id], user.id)
|
||||
.await?
|
||||
.get(&channel_id)
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
|
||||
Ok(Json(ReadStateResponse {
|
||||
channel_id,
|
||||
last_read_message_id: read_state.last_read_message_id,
|
||||
updated_at: Some(read_state.updated_at),
|
||||
unread_count,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Récupère un channel par son ID
|
||||
#[utoipa::path(
|
||||
get,
|
||||
|
||||
Reference in New Issue
Block a user