Fix deletion of private notes (#814)
This commit is contained in:
parent
f199ecf8e9
commit
cf9c8cbb4f
@ -241,7 +241,8 @@ impl ThreadMessage {
|
||||
id: ThreadMessageId(x.id),
|
||||
thread_id: ThreadId(x.thread_id),
|
||||
author_id: x.author_id.map(UserId),
|
||||
body: serde_json::from_value(x.body).unwrap_or(MessageBody::Deleted),
|
||||
body: serde_json::from_value(x.body)
|
||||
.unwrap_or(MessageBody::Deleted { private: false }),
|
||||
created: x.created,
|
||||
}))
|
||||
})
|
||||
@ -253,6 +254,7 @@ impl ThreadMessage {
|
||||
|
||||
pub async fn remove_full(
|
||||
id: ThreadMessageId,
|
||||
private: bool,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<Option<()>, sqlx::error::Error> {
|
||||
sqlx::query!(
|
||||
@ -262,7 +264,7 @@ impl ThreadMessage {
|
||||
WHERE id = $1
|
||||
",
|
||||
id as ThreadMessageId,
|
||||
serde_json::to_value(MessageBody::Deleted).unwrap_or(serde_json::json!({}))
|
||||
serde_json::to_value(MessageBody::Deleted { private }).unwrap_or(serde_json::json!({}))
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
|
||||
@ -40,7 +40,10 @@ pub enum LegacyMessageBody {
|
||||
},
|
||||
ThreadClosure,
|
||||
ThreadReopen,
|
||||
Deleted,
|
||||
Deleted {
|
||||
#[serde(default)]
|
||||
private: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Eq, PartialEq, Copy, Clone)]
|
||||
@ -90,7 +93,9 @@ impl From<crate::models::v3::threads::MessageBody> for LegacyMessageBody {
|
||||
crate::models::v3::threads::MessageBody::ThreadReopen => {
|
||||
LegacyMessageBody::ThreadReopen
|
||||
}
|
||||
crate::models::v3::threads::MessageBody::Deleted => LegacyMessageBody::Deleted,
|
||||
crate::models::v3::threads::MessageBody::Deleted { private } => {
|
||||
LegacyMessageBody::Deleted { private }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,7 +51,10 @@ pub enum MessageBody {
|
||||
},
|
||||
ThreadClosure,
|
||||
ThreadReopen,
|
||||
Deleted,
|
||||
Deleted {
|
||||
#[serde(default)]
|
||||
private: bool,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Eq, PartialEq, Copy, Clone)]
|
||||
@ -103,6 +106,8 @@ impl Thread {
|
||||
.filter(|x| {
|
||||
if let MessageBody::Text { private, .. } = x.body {
|
||||
!private || user.role.is_mod()
|
||||
} else if let MessageBody::Deleted { private, .. } = x.body {
|
||||
!private || user.role.is_mod()
|
||||
} else {
|
||||
true
|
||||
}
|
||||
|
||||
@ -611,7 +611,15 @@ pub async fn message_delete(
|
||||
database::Image::remove(image.id, &mut transaction, &redis).await?;
|
||||
}
|
||||
|
||||
database::models::ThreadMessage::remove_full(thread.id, &mut transaction).await?;
|
||||
let private = if let MessageBody::Text { private, .. } = thread.body {
|
||||
private
|
||||
} else if let MessageBody::Deleted { private, .. } = thread.body {
|
||||
private
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
database::models::ThreadMessage::remove_full(thread.id, private, &mut transaction).await?;
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(HttpResponse::NoContent().body(""))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user