d5939db
use db::conn::sea_orm::*;
use time::OffsetDateTime;
use uuid::Uuid;
use crate::error::{IssueError, IssueResult};
use crate::features::IssueService;
use super::super::{CommentView, UpdateCommentOption};
use super::helpers::{load_comment, require_authenticated};
pub async fn update_comment(
svc: &IssueService,
actor: Uuid,
channel_id: Uuid,
issue_id: Uuid,
comment_id: Uuid,
input: UpdateCommentOption,
) -> IssueResult<CommentView> {
require_authenticated(actor)?;
let cur = load_comment(svc, channel_id, issue_id, comment_id).await?;
if cur.poster_id != actor {
return Err(IssueError::Forbidden(
"only the poster can edit the comment".into(),
));
}
if input.content.trim().is_empty() || input.content.len() > svc.config.max_comment_len {
return Err(IssueError::BadRequest(format!(
"comment length must be in 1..={}",
svc.config.max_comment_len
)));
}
let db = svc.db.writer();
let mut active: db::issue::comment::ActiveModel = cur.into();
active.content = Set(input.content);
active.updated_at = Set(Some(OffsetDateTime::now_utc()));
let saved = active.update(&db).await?;
Ok(CommentView::from(saved))
}