]> Untitled Git - lemmy.git/commitdiff
Starting on siteview.
authorDessalines <tyhou13@gmx.com>
Wed, 2 Dec 2020 19:32:47 +0000 (13:32 -0600)
committerDessalines <tyhou13@gmx.com>
Wed, 2 Dec 2020 19:32:47 +0000 (13:32 -0600)
14 files changed:
lemmy_api/src/comment.rs
lemmy_api/src/post.rs
lemmy_api/src/site.rs
lemmy_api/src/user.rs
lemmy_db/src/lib.rs
lemmy_db/src/site.rs
lemmy_db/src/user.rs
lemmy_db/src/views/mod.rs [new file with mode: 0644]
lemmy_db/src/views/site_view.rs [new file with mode: 0644]
lemmy_structs/src/site.rs
migrations/2020-12-02-152437_remove_views/down.sql [new file with mode: 0644]
migrations/2020-12-02-152437_remove_views/up.sql [new file with mode: 0644]
src/routes/feeds.rs
src/routes/nodeinfo.rs

index e74fa808e6ef30ce76e074156d0a3b0463cf213d..5ad62f146f2ae498ebeb7e9ba512bbe03fec2ac2 100644 (file)
@@ -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());
       }
     }
index 298076f75bbdcf1dd4dc9d9981b9e7d36d98ef31..cc121c44c13a8fdba224f619a66805ca4a2d27e7 100644 (file)
@@ -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());
       }
     }
index e4b1dd21330a8508eda289b448c7243555e24422..e8d0df04b6f2182d588ed3bfa8c1019c9bf0387d 100644 (file)
@@ -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,
     })
   }
 }
index 0d96c2a2f1d9c178a44978b558eca8ce9b807495..693bd6d8c80806f1b7f47e8a4a019cfe0126dd83 100644 (file)
@@ -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());
       }
     }
index bad646d14be2b3d73dd0080da80e10b3f341932a..bf291db3fc835ad4f1a668b891712ac58c3ea71d 100644 (file)
@@ -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<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
 
index 5e68fead8afbb1078c791678e747fe8fa2d31095..2f3fbcdff15859930d23a1676b3397295fa5e65a 100644 (file)
@@ -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,
index 2c4c67ea2f53f51364d620ffde60100da4e1f48f..96483c1d7e23061b0629c4c9a5659dce3d4fddd8 100644 (file)
@@ -69,6 +69,25 @@ pub struct UserForm {
   pub banner: Option<Option<String>>,
 }
 
+/// 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<String>,
+  pub avatar: Option<String>,
+  pub admin: bool,
+  pub banned: bool,
+  pub published: chrono::NaiveDateTime,
+  pub updated: Option<chrono::NaiveDateTime>,
+  pub matrix_user_id: Option<String>,
+  pub actor_id: String,
+  pub bio: Option<String>,
+  pub local: bool,
+  pub banner: Option<String>,
+  pub deleted: bool,
+}
+
 impl Crud<UserForm> for User_ {
   fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
     user_
@@ -200,6 +219,25 @@ impl User_ {
       ))
       .get_result::<Self>(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 (file)
index 0000000..41fabde
--- /dev/null
@@ -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 (file)
index 0000000..9b14056
--- /dev/null
@@ -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<Self, Error> {
+    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(),
+    })
+  }
+}
index 3f185928b4b2c1fb61322fb8a74da9a96b03f6e4..2192c4fd799830a858bdd1e31bcf3cbd118321f5 100644 (file)
@@ -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<String>,
 }
 
+// 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<SiteView>,
+  pub site: Option<SiteView>, // 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<UserView>,
   pub banned: Vec<UserView>,
   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 (file)
index 0000000..291a97c
--- /dev/null
@@ -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 (file)
index 0000000..33cf74b
--- /dev/null
@@ -0,0 +1 @@
+-- Your SQL goes here
\ No newline at end of file
index fc4a313724eca25014441bd82f921efe3dcfc73b..1d00556ec8fc57413a39a62787c5262afef5ecbb 100644 (file)
@@ -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<ChannelBuilder, Le
   let mut channel_builder = ChannelBuilder::default();
   channel_builder
     .namespaces(RSS_NAMESPACE.to_owned())
-    .title(&format!("{} - Inbox", site_view.name))
+    .title(&format!("{} - Inbox", site_view.site.name))
     .link(format!(
       "{}/inbox",
       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);
   }
 
index 1d9525ef2ba21bea072ca240b034a4171082f3ff..9f63a523b1632dc9bc950d1b6dc7c4e51c4be040 100644 (file)
@@ -1,7 +1,7 @@
 use actix_web::{body::Body, error::ErrorBadRequest, *};
 use anyhow::anyhow;
 use lemmy_api::version;
-use lemmy_db::site_view::SiteView;
+use lemmy_db::views::site_view::SiteView;
 use lemmy_structs::blocking;
 use lemmy_utils::{settings::Settings, LemmyError};
 use lemmy_websocket::LemmyContext;
@@ -46,12 +46,11 @@ async fn node_info(context: web::Data<LemmyContext>) -> Result<HttpResponse, Err
     },
     protocols,
     usage: NodeInfoUsage {
-      users: NodeInfoUsers {
-        total: site_view.number_of_users,
-      },
-      local_posts: site_view.number_of_posts,
-      local_comments: site_view.number_of_comments,
-      open_registrations: site_view.open_registration,
+      // TODO get these again
+      users: NodeInfoUsers { total: 0 },
+      local_posts: 0,
+      local_comments: 0,
+      open_registrations: site_view.site.open_registration,
     },
   };