From 5e353c27cbc271a9c2b97d83a1003e61ce0f7f13 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 2 Jul 2023 19:09:12 +0200 Subject: [PATCH] Show communities moderated by a person --- .vscode/settings.json | 16 ---- src/components/comment_row.rs | 8 +- src/components/community_row.rs | 1 - src/components/instance_row.rs | 1 - src/components/mention_row.rs | 1 - src/components/mod.rs | 1 + src/components/moderates_row.rs | 116 ++++++++++++++++++++++++++ src/components/post_row.rs | 1 - src/components/private_message_row.rs | 1 - src/components/profile_page.rs | 33 +++++--- 10 files changed, 141 insertions(+), 38 deletions(-) delete mode 100644 .vscode/settings.json create mode 100644 src/components/moderates_row.rs diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index b16afe0..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "C_Cpp.default.compileCommands": "_build/vscode_compile_commands.json", - "files.watcherExclude": { - "**/.dart_tool": true, - ".flatpak/**": true, - "_build/**": true - }, - "mesonbuild.configureOnOpen": false, - "mesonbuild.buildFolder": "_build", - "mesonbuild.mesonPath": "${workspaceFolder}/.flatpak/meson.sh", - "rust-analyzer.server.path": "${workspaceFolder}/.flatpak/rust-analyzer.sh", - "rust-analyzer.runnables.command": "${workspaceFolder}/.flatpak/cargo.sh", - "rust-analyzer.files.excludeDirs": [ - ".flatpak" - ] -} diff --git a/src/components/comment_row.rs b/src/components/comment_row.rs index 460ca38..ff9515f 100644 --- a/src/components/comment_row.rs +++ b/src/components/comment_row.rs @@ -40,7 +40,6 @@ impl FactoryComponent for CommentRow { type Input = CommentRowMsg; type Output = crate::AppMsg; type CommandOutput = (); - type Widgets = PostViewWidgets; type ParentInput = crate::AppMsg; type ParentWidget = gtk::Box; @@ -176,7 +175,9 @@ impl FactoryComponent for CommentRow { let comment_id = self.comment.comment.id; std::thread::spawn(move || { let _ = api::comment::delete_comment(comment_id); - sender.output_sender().emit(crate::AppMsg::StartFetchPosts(None, true)); + sender + .output_sender() + .emit(crate::AppMsg::StartFetchPosts(None, true)); }); } CommentRowMsg::OpenEditor(is_new) => { @@ -216,8 +217,7 @@ impl FactoryComponent for CommentRow { let post_id = self.comment.comment.post_id; let parent_id = self.comment.comment.id; std::thread::spawn(move || { - match api::comment::create_comment(post_id, data.body, Some(parent_id)) - { + match api::comment::create_comment(post_id, data.body, Some(parent_id)) { Ok(_comment) => { // TODO sender.output_sender().emit(PostPageInput::CreatedComment(comment.comment_view)); } diff --git a/src/components/community_row.rs b/src/components/community_row.rs index 8f53756..5a42670 100644 --- a/src/components/community_row.rs +++ b/src/components/community_row.rs @@ -22,7 +22,6 @@ impl FactoryComponent for CommunityRow { type Input = CommunityRowMsg; type Output = crate::AppMsg; type CommandOutput = (); - type Widgets = PostViewWidgets; type ParentInput = crate::AppMsg; type ParentWidget = gtk::Box; diff --git a/src/components/instance_row.rs b/src/components/instance_row.rs index 2f3f3c0..1be4c86 100644 --- a/src/components/instance_row.rs +++ b/src/components/instance_row.rs @@ -20,7 +20,6 @@ impl FactoryComponent for InstanceRow { type Input = InstanceRowMsg; type Output = InstancesPageInput; type CommandOutput = (); - type Widgets = PostViewWidgets; type ParentInput = InstancesPageInput; type ParentWidget = gtk::Box; diff --git a/src/components/mention_row.rs b/src/components/mention_row.rs index a0453cb..1a6c4cc 100644 --- a/src/components/mention_row.rs +++ b/src/components/mention_row.rs @@ -31,7 +31,6 @@ impl FactoryComponent for MentionRow { type Input = MentionRowMsg; type Output = crate::AppMsg; type CommandOutput = (); - type Widgets = MentionRowWidgets; type ParentInput = crate::AppMsg; type ParentWidget = gtk::Box; diff --git a/src/components/mod.rs b/src/components/mod.rs index 0b81e76..c6cb1cb 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -7,6 +7,7 @@ pub mod instance_row; pub mod instances_page; pub mod login_page; pub mod mention_row; +pub mod moderates_row; pub mod post_page; pub mod post_row; pub mod private_message_row; diff --git a/src/components/moderates_row.rs b/src/components/moderates_row.rs new file mode 100644 index 0000000..19816da --- /dev/null +++ b/src/components/moderates_row.rs @@ -0,0 +1,116 @@ +use gtk::prelude::*; +use lemmy_api_common::lemmy_db_views_actor::structs::CommunityModeratorView; +use relm4::prelude::*; +use relm4_components::web_image::WebImage; + +use crate::util::get_web_image_url; + +#[derive(Debug)] +pub struct ModeratesRow { + community: CommunityModeratorView, + community_image: Controller, +} + +#[derive(Debug)] +pub enum ModeratesRowMsg { + OpenCommunity, +} + +#[relm4::factory(pub)] +impl FactoryComponent for ModeratesRow { + type Init = CommunityModeratorView; + type Input = ModeratesRowMsg; + type Output = crate::AppMsg; + type CommandOutput = (); + type ParentInput = crate::AppMsg; + type ParentWidget = gtk::Box; + + view! { + root = gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_spacing: 10, + set_margin_end: 10, + set_margin_start: 10, + set_vexpand: false, + + add_controller = gtk::GestureClick { + connect_pressed[sender] => move |_, _, _, _| { + sender.input(ModeratesRowMsg::OpenCommunity); + } + }, + + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + + if self.community.community.icon.is_some() { + gtk::Box { + set_hexpand: false, + #[local_ref] + community_image -> gtk::Box { + set_height_request: 35, + set_width_request: 35, + } + } + } else { + gtk::Box {} + }, + + gtk::Label { + set_label: &self.community.community.title, + }, + + gtk::Box { + set_hexpand: true, + }, + + gtk::Label { + set_label: "NSFW", + set_visible: self.community.community.nsfw, + } + }, + + gtk::Label { + set_label: &self.community.community.description.clone().unwrap_or("".to_string()), + set_halign: gtk::Align::Start, + }, + + gtk::Separator {} + } + } + + fn forward_to_parent(output: Self::Output) -> Option { + Some(output) + } + + fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender) -> Self { + let community_image = WebImage::builder() + .launch(get_web_image_url(value.community.clone().icon)) + .detach(); + + Self { + community: value, + community_image, + } + } + + fn init_widgets( + &mut self, + _index: &Self::Index, + root: &Self::Root, + _returned_widget: &::ReturnedWidget, + sender: FactorySender, + ) -> Self::Widgets { + let community_image = self.community_image.widget(); + let widgets = view_output!(); + widgets + } + + fn update(&mut self, message: Self::Input, sender: FactorySender) { + match message { + ModeratesRowMsg::OpenCommunity => sender.output(crate::AppMsg::OpenCommunity( + self.community.community.id.clone(), + )), + } + } +} diff --git a/src/components/post_row.rs b/src/components/post_row.rs index 712e3aa..ab2e95c 100644 --- a/src/components/post_row.rs +++ b/src/components/post_row.rs @@ -30,7 +30,6 @@ impl FactoryComponent for PostRow { type Input = PostRowMsg; type Output = crate::AppMsg; type CommandOutput = (); - type Widgets = PostViewWidgets; type ParentInput = crate::AppMsg; type ParentWidget = gtk::Box; diff --git a/src/components/private_message_row.rs b/src/components/private_message_row.rs index f2897f0..73efdd4 100644 --- a/src/components/private_message_row.rs +++ b/src/components/private_message_row.rs @@ -22,7 +22,6 @@ impl FactoryComponent for PrivateMessageRow { type Input = PrivateMessageRowInput; type Output = crate::AppMsg; type CommandOutput = (); - type Widgets = PrivateMessageRowWidgets; type ParentInput = crate::AppMsg; type ParentWidget = gtk::Box; diff --git a/src/components/profile_page.rs b/src/components/profile_page.rs index 07ef382..f9ba660 100644 --- a/src/components/profile_page.rs +++ b/src/components/profile_page.rs @@ -1,5 +1,5 @@ use gtk::prelude::*; -use lemmy_api_common::{person::GetPersonDetailsResponse, lemmy_db_schema::newtypes::PersonId}; +use lemmy_api_common::{lemmy_db_schema::newtypes::PersonId, person::GetPersonDetailsResponse}; use relm4::{factory::FactoryVecDeque, prelude::*}; use relm4_components::web_image::WebImage; @@ -12,16 +12,16 @@ use crate::settings; use crate::util::get_web_image_msg; use crate::util::markdown_to_pango_markup; -use super::post_row::PostRow; -use super::community_row::CommunityRow; use super::comment_row::CommentRow; +use super::moderates_row::ModeratesRow; +use super::post_row::PostRow; pub struct ProfilePage { info: GetPersonDetailsResponse, avatar: Controller, posts: FactoryVecDeque, comments: FactoryVecDeque, - communities: FactoryVecDeque, + moderates: FactoryVecDeque, editor_dialog: Controller, current_profile_page: i64, } @@ -112,7 +112,7 @@ impl SimpleComponent for ProfilePage { }, add_child = >k::Box { #[local_ref] - communities -> gtk::Box { + moderates -> gtk::Box { set_orientation: gtk::Orientation::Vertical, } } -> { @@ -137,7 +137,7 @@ impl SimpleComponent for ProfilePage { ) -> relm4::ComponentParts { let avatar = WebImage::builder().launch("".to_string()).detach(); let posts = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender()); - let communities = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender()); + let moderates = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender()); let comments = FactoryVecDeque::new(gtk::Box::default(), sender.output_sender()); let editor_dialog = EditorDialog::builder() .transient_for(root) @@ -151,14 +151,14 @@ impl SimpleComponent for ProfilePage { avatar, posts, comments, - communities, + moderates, editor_dialog, current_profile_page: 1, }; let avatar = model.avatar.widget(); let posts = model.posts.widget(); let comments = model.comments.widget(); - let communities = model.communities.widget(); + let moderates = model.moderates.widget(); let widgets = view_output!(); ComponentParts { model, widgets } @@ -167,7 +167,9 @@ impl SimpleComponent for ProfilePage { fn update(&mut self, message: Self::Input, sender: ComponentSender) { match message { ProfileInput::UpdatePerson(person, clear) => { - sender.output_sender().emit(crate::AppMsg::UpdateState(crate::AppState::Person)); + sender + .output_sender() + .emit(crate::AppMsg::UpdateState(crate::AppState::Person)); if clear { self.info = person.clone(); @@ -176,7 +178,7 @@ impl SimpleComponent for ProfilePage { self.posts.guard().clear(); self.comments.guard().clear(); - self.communities.guard().clear(); + self.moderates.guard().clear(); } for post in person.posts { @@ -185,6 +187,9 @@ impl SimpleComponent for ProfilePage { for comment in person.comments { self.comments.guard().push_back(comment); } + for community in person.moderates { + self.moderates.guard().push_back(community); + } } ProfileInput::SendMessageRequest => self.editor_dialog.sender().emit(DialogMsg::Show), ProfileInput::SendMessage(content) => { @@ -205,10 +210,12 @@ impl SimpleComponent for ProfilePage { match api::user::get_user(person_id, page) { Ok(person) => { sender.input(ProfileInput::UpdatePerson(person, page == 1)); - }, + } Err(err) => { - sender.output_sender().emit(crate::AppMsg::ShowMessage(err.to_string())); - }, + sender + .output_sender() + .emit(crate::AppMsg::ShowMessage(err.to_string())); + } }; }); }