feat: support for reporting comments under posts

This commit is contained in:
Bnyro 2023-09-14 19:57:25 +02:00
parent 5d47f33661
commit 62fcb4640d
5 changed files with 147 additions and 4 deletions

View File

@ -1,6 +1,7 @@
use lemmy_api_common::{
comment::{
CommentResponse, CreateComment, CreateCommentLike, DeleteComment, EditComment, SaveComment,
CommentReportResponse, CommentResponse, CreateComment, CreateCommentLike,
CreateCommentReport, DeleteComment, EditComment, SaveComment,
},
lemmy_db_schema::newtypes::{CommentId, PostId},
};
@ -62,3 +63,15 @@ pub fn save_comment(comment_id: CommentId, save: bool) -> Result<CommentResponse
};
super::put("/comment/save", &params)
}
pub fn report_comment(
comment_id: CommentId,
reason: String,
) -> Result<CommentReportResponse, reqwest::Error> {
let params = CreateCommentReport {
comment_id,
reason,
auth: settings::get_current_account().jwt.unwrap(),
};
super::post("/comment/report", &params)
}

View File

@ -8,8 +8,8 @@ use lemmy_api_common::{
},
lemmy_db_views::structs::CommentView,
post::{
CreatePost, CreatePostLike, DeletePost, EditPost, GetPost, GetPostResponse, PostResponse,
SavePost,
CreatePost, CreatePostLike, CreatePostReport, DeletePost, EditPost, GetPost,
GetPostResponse, PostReportResponse, PostResponse, SavePost,
},
};
use std::result::Result;
@ -120,3 +120,12 @@ pub fn save_post(post_id: PostId, save: bool) -> Result<PostResponse, reqwest::E
};
super::put("/post/save", &params)
}
pub fn report_post(post_id: PostId, reason: String) -> Result<PostReportResponse, reqwest::Error> {
let params = CreatePostReport {
post_id,
reason,
auth: settings::get_current_account().jwt.unwrap(),
};
super::post("/post/report", &params)
}

View File

@ -9,6 +9,8 @@ use crate::dialogs::editor::EditorData;
use crate::dialogs::editor::EditorDialog;
use crate::dialogs::editor::EditorOutput;
use crate::dialogs::editor::EditorType;
use crate::dialogs::report_dialog::ReportDialog;
use crate::dialogs::report_dialog::ReportDialogInput;
use crate::settings;
use crate::util;
use crate::util::get_web_image_url;
@ -22,6 +24,7 @@ pub struct CommentRow {
avatar: Controller<WebImage>,
voting_row: Controller<VotingRowModel>,
comment_editor_dialog: Controller<EditorDialog>,
report_comment_dialog: Controller<ReportDialog>,
}
#[derive(Debug)]
@ -33,6 +36,7 @@ pub enum CommentRowMsg {
EditCommentRequest(EditorData),
CreateCommentRequest(EditorData),
UpdateComment(CommentView),
ShowReportDialog,
}
#[relm4::factory(pub)]
@ -107,6 +111,12 @@ impl FactoryComponent for CommentRow {
set_active: self.comment.saved,
},
gtk::Button {
set_icon_name: "action-unavailable",
connect_clicked => CommentRowMsg::ShowReportDialog,
set_visible: settings::get_current_account().jwt.is_some(),
},
gtk::Button {
set_icon_name: "document-edit",
connect_clicked => CommentRowMsg::OpenEditor(false),
@ -117,7 +127,7 @@ impl FactoryComponent for CommentRow {
set_icon_name: "edit-delete",
connect_clicked => CommentRowMsg::DeleteComment,
set_visible: self.comment.creator.id.0 == settings::get_current_account().id,
}
},
},
}
}
@ -143,12 +153,16 @@ impl FactoryComponent for CommentRow {
EditorOutput::CreateRequest(data, _) => CommentRowMsg::CreateCommentRequest(data),
},
);
let report_comment_dialog = ReportDialog::builder()
.launch((None, Some(value.comment.id)))
.detach();
Self {
comment: value,
avatar,
voting_row,
comment_editor_dialog,
report_comment_dialog,
}
}
@ -236,6 +250,10 @@ impl FactoryComponent for CommentRow {
}
});
}
CommentRowMsg::ShowReportDialog => self
.report_comment_dialog
.sender()
.emit(ReportDialogInput::Show),
}
}
}

View File

@ -1,4 +1,5 @@
pub mod about;
pub mod editor;
pub mod report_dialog;
pub mod settings;
pub mod site_info;

View File

@ -0,0 +1,102 @@
use gtk::prelude::*;
use lemmy_api_common::lemmy_db_schema::newtypes::{CommentId, PostId};
use relm4::prelude::*;
use crate::api;
pub struct ReportDialog {
visible: bool,
post_id: Option<PostId>,
comment_id: Option<CommentId>,
}
#[derive(Debug)]
pub enum ReportDialogInput {
Show,
Hide,
Report(String),
UpdateId(Option<PostId>, Option<CommentId>),
}
#[relm4::component(pub)]
impl SimpleComponent for ReportDialog {
type Init = (Option<PostId>, Option<CommentId>);
type Input = ReportDialogInput;
type Output = crate::AppMsg;
view! {
dialog = gtk::Dialog {
#[watch]
set_visible: model.visible,
set_modal: true,
set_title: Some("Report post/comment"),
connect_close_request[sender] => move |_| {
sender.input(ReportDialogInput::Hide);
gtk::Inhibit(false)
},
gtk::Box {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 5,
set_margin_all: 15,
#[name(report_message)]
gtk::Entry {
set_placeholder_text: Some("Reason"),
},
gtk::Button {
set_margin_top: 10,
set_margin_bottom: 10,
set_label: "Report",
connect_clicked[report_message] => move |_| {
let reason = report_message.text().to_string();
ReportDialogInput::Report(reason);
},
},
}
}
}
fn init(
init: Self::Init,
root: &Self::Root,
sender: ComponentSender<Self>,
) -> ComponentParts<Self> {
let model = Self {
visible: false,
post_id: init.0,
comment_id: init.1,
};
let widgets = view_output!();
ComponentParts { model, widgets }
}
fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) {
match message {
ReportDialogInput::Show => {
self.visible = true;
}
ReportDialogInput::Hide => {
self.visible = false;
}
ReportDialogInput::Report(reason) => {
let post_id = self.post_id;
let comment_id = self.comment_id;
std::thread::spawn(move || {
if let Some(post_id) = post_id {
_ = api::post::report_post(post_id, reason);
} else if let Some(comment_id) = comment_id {
_ = api::comment::report_comment(comment_id, reason);
}
sender.input_sender().emit(ReportDialogInput::Hide);
});
}
ReportDialogInput::UpdateId(post_id, comment_id) => {
self.post_id = post_id;
self.comment_id = comment_id;
}
}
}
}