From 62fcb4640d99e028844cf31baf9dc2c08c0aab53 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 14 Sep 2023 19:57:25 +0200 Subject: [PATCH] feat: support for reporting comments under posts --- src/api/comment.rs | 15 ++++- src/api/post.rs | 13 ++++- src/components/comment_row.rs | 20 ++++++- src/dialogs/mod.rs | 1 + src/dialogs/report_dialog.rs | 102 ++++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 4 deletions(-) create mode 100644 src/dialogs/report_dialog.rs diff --git a/src/api/comment.rs b/src/api/comment.rs index 276990a..a3eb0f0 100644 --- a/src/api/comment.rs +++ b/src/api/comment.rs @@ -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 Result { + let params = CreateCommentReport { + comment_id, + reason, + auth: settings::get_current_account().jwt.unwrap(), + }; + super::post("/comment/report", ¶ms) +} diff --git a/src/api/post.rs b/src/api/post.rs index 4270835..d8235c6 100644 --- a/src/api/post.rs +++ b/src/api/post.rs @@ -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 Result { + let params = CreatePostReport { + post_id, + reason, + auth: settings::get_current_account().jwt.unwrap(), + }; + super::post("/post/report", ¶ms) +} diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index d25c552..b67952f 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -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, voting_row: Controller, comment_editor_dialog: Controller, + report_comment_dialog: Controller, } #[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), } } } diff --git a/src/dialogs/mod.rs b/src/dialogs/mod.rs index 6cf6743..edc22d6 100644 --- a/src/dialogs/mod.rs +++ b/src/dialogs/mod.rs @@ -1,4 +1,5 @@ pub mod about; pub mod editor; +pub mod report_dialog; pub mod settings; pub mod site_info; diff --git a/src/dialogs/report_dialog.rs b/src/dialogs/report_dialog.rs new file mode 100644 index 0000000..11b234c --- /dev/null +++ b/src/dialogs/report_dialog.rs @@ -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, + comment_id: Option, +} + +#[derive(Debug)] +pub enum ReportDialogInput { + Show, + Hide, + Report(String), + UpdateId(Option, Option), +} + +#[relm4::component(pub)] +impl SimpleComponent for ReportDialog { + type Init = (Option, Option); + 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, + ) -> ComponentParts { + 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) { + 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; + } + } + } +}