Finegal private messages

This commit is contained in:
SleeplessOne1917 2024-12-17 20:24:17 -05:00
parent a5ac8eca3d
commit 502b5a1856
4 changed files with 26 additions and 17 deletions

View file

@ -1,9 +1,10 @@
use actix_web::web::{Data, Json};
use actix_web::web::{Data, Json, Path};
use lemmy_api_common::{
context::LemmyContext,
private_message::{MarkPrivateMessageAsRead, PrivateMessageResponse},
};
use lemmy_db_schema::{
newtypes::PrivateMessageId,
source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
traits::Crud,
};
@ -15,16 +16,16 @@ pub async fn mark_pm_as_read(
data: Json<MarkPrivateMessageAsRead>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
path: Path<PrivateMessageId>,
) -> LemmyResult<Json<PrivateMessageResponse>> {
// Checking permissions
let private_message_id = data.private_message_id;
let private_message_id = path.into_inner();
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
if local_user_view.person.id != orig_private_message.recipient_id {
Err(LemmyErrorType::CouldntUpdatePrivateMessage)?
}
// Doing the update
let private_message_id = data.private_message_id;
let read = data.read;
PrivateMessage::update(
&mut context.pool(),

View file

@ -1,11 +1,12 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use actix_web::web::{Json, Path};
use lemmy_api_common::{
context::LemmyContext,
private_message::{DeletePrivateMessage, PrivateMessageResponse},
send_activity::{ActivityChannel, SendActivityData},
};
use lemmy_db_schema::{
newtypes::PrivateMessageId,
source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
traits::Crud,
};
@ -17,16 +18,16 @@ pub async fn delete_private_message(
data: Json<DeletePrivateMessage>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
path: Path<PrivateMessageId>,
) -> LemmyResult<Json<PrivateMessageResponse>> {
// Checking permissions
let private_message_id = data.private_message_id;
let private_message_id = path.into_inner();
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
if local_user_view.person.id != orig_private_message.creator_id {
Err(LemmyErrorType::EditPrivateMessageNotAllowed)?
}
// Doing the update
let private_message_id = data.private_message_id;
let deleted = data.deleted;
let private_message = PrivateMessage::update(
&mut context.pool(),

View file

@ -1,5 +1,5 @@
use activitypub_federation::config::Data;
use actix_web::web::Json;
use actix_web::web::{Json, Path};
use chrono::Utc;
use lemmy_api_common::{
context::LemmyContext,
@ -8,6 +8,7 @@ use lemmy_api_common::{
utils::{get_url_blocklist, local_site_to_slur_regex, process_markdown},
};
use lemmy_db_schema::{
newtypes::PrivateMessageId,
source::{
local_site::LocalSite,
private_message::{PrivateMessage, PrivateMessageUpdateForm},
@ -25,11 +26,12 @@ pub async fn update_private_message(
data: Json<EditPrivateMessage>,
context: Data<LemmyContext>,
local_user_view: LocalUserView,
path: Path<PrivateMessageId>,
) -> LemmyResult<Json<PrivateMessageResponse>> {
let private_message_id = path.into_inner();
let local_site = LocalSite::read(&mut context.pool()).await?;
// Checking permissions
let private_message_id = data.private_message_id;
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
if local_user_view.person.id != orig_private_message.creator_id {
Err(LemmyErrorType::EditPrivateMessageNotAllowed)?
@ -41,7 +43,6 @@ pub async fn update_private_message(
let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?;
is_valid_body_field(&content, false)?;
let private_message_id = data.private_message_id;
PrivateMessage::update(
&mut context.pool(),
private_message_id,

View file

@ -258,15 +258,21 @@ pub fn config(cfg: &mut ServiceConfig, rate_limit: &RateLimitCell) {
)
// Private Message
.service(
scope("/private_message")
.route("/list", get().to(get_private_message))
scope("/direct-messages")
.route("", get().to(get_private_message))
.route("", post().to(create_private_message))
.route("", put().to(update_private_message))
.route("/delete", post().to(delete_private_message))
.route("/mark_as_read", post().to(mark_pm_as_read))
.route("/report", post().to(create_pm_report))
.route("/report/resolve", put().to(resolve_pm_report))
.route("/report/list", get().to(list_pm_reports)),
.service(
scope("/{message_id}")
.route("", put().to(update_private_message))
.route("", delete().to(delete_private_message))
.route("/mark-as-read", post().to(mark_pm_as_read))
.service(
scope("/reports")
.route("", get().to(list_pm_reports))
.route("", post().to(create_pm_report))
.service(scope("/{report_id}").route("", delete().to(resolve_pm_report))),
),
),
)
// User
.service(