From ca7224c0860f9544043a1c3eb6718543d803667a Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 2 Dec 2020 13:32:47 -0600 Subject: [PATCH] Starting on siteview. --- lemmy_api/src/comment.rs | 6 +-- lemmy_api/src/post.rs | 6 +-- lemmy_api/src/site.rs | 16 ++++++-- lemmy_api/src/user.rs | 7 ++-- lemmy_db/src/lib.rs | 1 + lemmy_db/src/site.rs | 3 +- lemmy_db/src/user.rs | 39 +++++++++++++++++++ lemmy_db/src/views/mod.rs | 1 + lemmy_db/src/views/site_view.rs | 26 +++++++++++++ lemmy_structs/src/site.rs | 9 ++++- .../2020-12-02-152437_remove_views/down.sql | 1 + .../2020-12-02-152437_remove_views/up.sql | 1 + src/routes/feeds.rs | 18 ++++----- src/routes/nodeinfo.rs | 13 +++---- 14 files changed, 115 insertions(+), 32 deletions(-) create mode 100644 lemmy_db/src/views/mod.rs create mode 100644 lemmy_db/src/views/site_view.rs create mode 100644 migrations/2020-12-02-152437_remove_views/down.sql create mode 100644 migrations/2020-12-02-152437_remove_views/up.sql diff --git a/lemmy_api/src/comment.rs b/lemmy_api/src/comment.rs index e74fa808..5ad62f14 100644 --- a/lemmy_api/src/comment.rs +++ b/lemmy_api/src/comment.rs @@ -15,8 +15,8 @@ use lemmy_db::{ comment_view::*, moderator::*, post::*, - site_view::*, user::*, + views::site_view::SiteView, Crud, Likeable, ListingType, @@ -552,8 +552,8 @@ impl Perform for CreateCommentLike { // Don't do a downvote if site has downvotes disabled if data.score == -1 { - let site = blocking(context.pool(), move |conn| SiteView::read(conn)).await??; - if !site.enable_downvotes { + let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??; + if !site_view.site.enable_downvotes { return Err(APIError::err("downvotes_disabled").into()); } } diff --git a/lemmy_api/src/post.rs b/lemmy_api/src/post.rs index 298076f7..cc121c44 100644 --- a/lemmy_api/src/post.rs +++ b/lemmy_api/src/post.rs @@ -17,7 +17,7 @@ use lemmy_db::{ post::*, post_report::*, post_view::*, - site_view::*, + views::site_view::SiteView, Crud, Likeable, ListingType, @@ -281,8 +281,8 @@ impl Perform for CreatePostLike { // Don't do a downvote if site has downvotes disabled if data.score == -1 { - let site = blocking(context.pool(), move |conn| SiteView::read(conn)).await??; - if !site.enable_downvotes { + let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??; + if !site_view.site.enable_downvotes { return Err(APIError::err("downvotes_disabled").into()); } } diff --git a/lemmy_api/src/site.rs b/lemmy_api/src/site.rs index e4b1dd21..e8d0df04 100644 --- a/lemmy_api/src/site.rs +++ b/lemmy_api/src/site.rs @@ -19,8 +19,8 @@ use lemmy_db::{ naive_now, post_view::*, site::*, - site_view::*, user_view::*, + views::site_view::SiteView, Crud, SearchType, SortType, @@ -284,7 +284,7 @@ impl Perform for GetSite { // Make sure the site creator is the top admin if let Some(site_view) = site_view.to_owned() { - let site_creator_id = site_view.creator_id; + let site_creator_id = site_view.creator.id; // TODO investigate why this is sometimes coming back null // Maybe user_.admin isn't being set to true? if let Some(creator_index) = admins.iter().position(|r| r.id == site_creator_id) { @@ -318,6 +318,11 @@ impl Perform for GetSite { version: version::VERSION.to_string(), my_user, federated_instances: linked_instances(context.pool()).await?, + // TODO + number_of_users: 0, + number_of_posts: 0, + number_of_comments: 0, + number_of_communities: 0, }) } } @@ -534,7 +539,7 @@ impl Perform for TransferSite { let mut admins = blocking(context.pool(), move |conn| UserView::admins(conn)).await??; let creator_index = admins .iter() - .position(|r| r.id == site_view.creator_id) + .position(|r| r.id == site_view.creator.id) .context(location_info!())?; let creator_user = admins.remove(creator_index); admins.insert(0, creator_user); @@ -549,6 +554,11 @@ impl Perform for TransferSite { version: version::VERSION.to_string(), my_user: Some(user), federated_instances: linked_instances(context.pool()).await?, + // TODO + number_of_users: 0, + number_of_posts: 0, + number_of_comments: 0, + number_of_communities: 0, }) } } diff --git a/lemmy_api/src/user.rs b/lemmy_api/src/user.rs index 0d96c2a2..693bd6d8 100644 --- a/lemmy_api/src/user.rs +++ b/lemmy_api/src/user.rs @@ -30,11 +30,11 @@ use lemmy_db::{ private_message::*, private_message_view::*, site::*, - site_view::*, user::*, user_mention::*, user_mention_view::*, user_view::*, + views::site_view::SiteView, Crud, Followable, Joinable, @@ -113,9 +113,8 @@ impl Perform for Register { let data: &Register = &self; // Make sure site has open registration - if let Ok(site) = blocking(context.pool(), move |conn| SiteView::read(conn)).await? { - let site: SiteView = site; - if !site.open_registration { + if let Ok(site_view) = blocking(context.pool(), move |conn| SiteView::read(conn)).await? { + if !site_view.site.open_registration { return Err(APIError::err("registration_closed").into()); } } diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs index bad646d1..bf291db3 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@ -33,6 +33,7 @@ pub mod user; pub mod user_mention; pub mod user_mention_view; pub mod user_view; +pub mod views; pub type DbPool = diesel::r2d2::Pool>; diff --git a/lemmy_db/src/site.rs b/lemmy_db/src/site.rs index 5e68fead..2f3fbcdf 100644 --- a/lemmy_db/src/site.rs +++ b/lemmy_db/src/site.rs @@ -1,7 +1,8 @@ use crate::{naive_now, schema::site, Crud}; use diesel::{dsl::*, result::Error, *}; +use serde::Serialize; -#[derive(Queryable, Identifiable, PartialEq, Debug)] +#[derive(Queryable, Identifiable, PartialEq, Debug, Clone, Serialize)] #[table_name = "site"] pub struct Site { pub id: i32, diff --git a/lemmy_db/src/user.rs b/lemmy_db/src/user.rs index 2c4c67ea..96483c1d 100644 --- a/lemmy_db/src/user.rs +++ b/lemmy_db/src/user.rs @@ -69,6 +69,25 @@ pub struct UserForm { pub banner: Option>, } +/// A safe representation of user, without the sensitive info +#[derive(Clone, Debug, Serialize)] +pub struct UserSafe { + pub id: i32, + pub name: String, + pub preferred_username: Option, + pub avatar: Option, + pub admin: bool, + pub banned: bool, + pub published: chrono::NaiveDateTime, + pub updated: Option, + pub matrix_user_id: Option, + pub actor_id: String, + pub bio: Option, + pub local: bool, + pub banner: Option, + pub deleted: bool, +} + impl Crud for User_ { fn read(conn: &PgConnection, user_id: i32) -> Result { user_ @@ -200,6 +219,25 @@ impl User_ { )) .get_result::(conn) } + + pub fn to_safe(&self) -> UserSafe { + UserSafe { + id: self.id, + name: self.name.to_owned(), + preferred_username: self.preferred_username.to_owned(), + avatar: self.avatar.to_owned(), + admin: self.admin, + banned: self.banned, + published: self.published, + updated: self.updated, + matrix_user_id: self.matrix_user_id.to_owned(), + actor_id: self.actor_id.to_owned(), + bio: self.bio.to_owned(), + local: self.local, + banner: self.banner.to_owned(), + deleted: self.deleted, + } + } } #[cfg(test)] @@ -265,6 +303,7 @@ mod tests { private_key: None, public_key: None, last_refreshed_at: inserted_user.published, + deleted: false, }; let read_user = User_::read(&conn, inserted_user.id).unwrap(); diff --git a/lemmy_db/src/views/mod.rs b/lemmy_db/src/views/mod.rs new file mode 100644 index 00000000..41fabde8 --- /dev/null +++ b/lemmy_db/src/views/mod.rs @@ -0,0 +1 @@ +pub mod site_view; diff --git a/lemmy_db/src/views/site_view.rs b/lemmy_db/src/views/site_view.rs new file mode 100644 index 00000000..9b14056e --- /dev/null +++ b/lemmy_db/src/views/site_view.rs @@ -0,0 +1,26 @@ +use crate::{ + schema::{site as site_table, user_}, + site::Site, + user::{UserSafe, User_}, +}; +use diesel::{result::Error, *}; +use serde::Serialize; + +#[derive(Debug, Serialize, Clone)] +pub struct SiteView { + pub site: Site, + pub creator: UserSafe, +} + +impl SiteView { + pub fn read(conn: &PgConnection) -> Result { + let site_join = site_table::table + .inner_join(user_::table) + .first::<(Site, User_)>(conn)?; + + Ok(SiteView { + site: site_join.0, + creator: site_join.1.to_safe(), + }) + } +} diff --git a/lemmy_structs/src/site.rs b/lemmy_structs/src/site.rs index 3f185928..2192c4fd 100644 --- a/lemmy_structs/src/site.rs +++ b/lemmy_structs/src/site.rs @@ -4,9 +4,9 @@ use lemmy_db::{ community_view::*, moderator_views::*, post_view::*, - site_view::*, user::*, user_view::*, + views::site_view::SiteView, }; use serde::{Deserialize, Serialize}; @@ -89,6 +89,7 @@ pub struct GetSite { pub auth: Option, } +// TODO combine siteresponse and getsiteresponse #[derive(Serialize, Clone)] pub struct SiteResponse { pub site: SiteView, @@ -96,7 +97,11 @@ pub struct SiteResponse { #[derive(Serialize)] pub struct GetSiteResponse { - pub site: Option, + pub site: Option, // Because the site might not be set up yet + pub number_of_users: i64, + pub number_of_posts: i64, + pub number_of_comments: i64, + pub number_of_communities: i64, pub admins: Vec, pub banned: Vec, pub online: usize, diff --git a/migrations/2020-12-02-152437_remove_views/down.sql b/migrations/2020-12-02-152437_remove_views/down.sql new file mode 100644 index 00000000..291a97c5 --- /dev/null +++ b/migrations/2020-12-02-152437_remove_views/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` \ No newline at end of file diff --git a/migrations/2020-12-02-152437_remove_views/up.sql b/migrations/2020-12-02-152437_remove_views/up.sql new file mode 100644 index 00000000..33cf74b5 --- /dev/null +++ b/migrations/2020-12-02-152437_remove_views/up.sql @@ -0,0 +1 @@ +-- Your SQL goes here \ No newline at end of file diff --git a/src/routes/feeds.rs b/src/routes/feeds.rs index fc4a3137..1d00556e 100644 --- a/src/routes/feeds.rs +++ b/src/routes/feeds.rs @@ -7,9 +7,9 @@ use lemmy_db::{ comment_view::{ReplyQueryBuilder, ReplyView}, community::Community, post_view::{PostQueryBuilder, PostView}, - site_view::SiteView, user::User_, user_mention_view::{UserMentionQueryBuilder, UserMentionView}, + views::site_view::SiteView, ListingType, SortType, }; @@ -96,13 +96,13 @@ async fn get_feed_data( .namespaces(RSS_NAMESPACE.to_owned()) .title(&format!( "{} - {}", - site_view.name, + site_view.site.name, listing_type.to_string() )) .link(Settings::get().get_protocol_and_hostname()) .items(items); - if let Some(site_desc) = site_view.description { + if let Some(site_desc) = site_view.site.description { channel_builder.description(&site_desc); } @@ -175,7 +175,7 @@ fn get_feed_user( let mut channel_builder = ChannelBuilder::default(); channel_builder .namespaces(RSS_NAMESPACE.to_owned()) - .title(&format!("{} - {}", site_view.name, user.name)) + .title(&format!("{} - {}", site_view.site.name, user.name)) .link(user_url) .items(items); @@ -201,7 +201,7 @@ fn get_feed_community( let mut channel_builder = ChannelBuilder::default(); channel_builder .namespaces(RSS_NAMESPACE.to_owned()) - .title(&format!("{} - {}", site_view.name, community.name)) + .title(&format!("{} - {}", site_view.site.name, community.name)) .link(community.actor_id) .items(items); @@ -231,11 +231,11 @@ fn get_feed_front( let mut channel_builder = ChannelBuilder::default(); channel_builder .namespaces(RSS_NAMESPACE.to_owned()) - .title(&format!("{} - Subscribed", site_view.name)) + .title(&format!("{} - Subscribed", site_view.site.name)) .link(Settings::get().get_protocol_and_hostname()) .items(items); - if let Some(site_desc) = site_view.description { + if let Some(site_desc) = site_view.site.description { channel_builder.description(&site_desc); } @@ -261,14 +261,14 @@ fn get_feed_inbox(conn: &PgConnection, jwt: String) -> Result) -> Result