]> Untitled Git - lemmy.git/commitdiff
Starting to work on community view
authorDessalines <tyhou13@gmx.com>
Fri, 4 Dec 2020 16:29:44 +0000 (11:29 -0500)
committerDessalines <tyhou13@gmx.com>
Fri, 4 Dec 2020 16:29:44 +0000 (11:29 -0500)
lemmy_db/src/category.rs
lemmy_db/src/community.rs
lemmy_db/src/views/community_view.rs [new file with mode: 0644]
lemmy_db/src/views/mod.rs

index 36beb9ff638b1a2070d9af70ecb7f4a2c2c6c6d3..af2e72265c22e37b21886c864c43d8fd0fa0a9a2 100644 (file)
@@ -5,7 +5,7 @@ use crate::{
 use diesel::{dsl::*, result::Error, *};
 use serde::Serialize;
 
-#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize)]
+#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Clone)]
 #[table_name = "category"]
 pub struct Category {
   pub id: i32,
index 845b386c22aeee662d4c2ffea436a400d0eab5f0..971bbf248889e91b561ca3e39a4313c272d50c42 100644 (file)
@@ -7,8 +7,9 @@ use crate::{
   Joinable,
 };
 use diesel::{dsl::*, result::Error, *};
+use serde::Serialize;
 
-#[derive(Clone, Queryable, Identifiable, PartialEq, Debug)]
+#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)]
 #[table_name = "community"]
 pub struct Community {
   pub id: i32,
diff --git a/lemmy_db/src/views/community_view.rs b/lemmy_db/src/views/community_view.rs
new file mode 100644 (file)
index 0000000..c7b9b39
--- /dev/null
@@ -0,0 +1,61 @@
+use crate::{
+  category::Category,
+  community::{Community, CommunityFollower},
+  schema::{category, community, community_follower, user_},
+  user::{UserSafe, User_},
+};
+use diesel::{result::Error, *};
+use serde::Serialize;
+
+#[derive(Debug, Serialize, Clone)]
+pub struct CommunityView {
+  pub community: Community,
+  pub creator: UserSafe,
+  pub category: Category,
+  pub subscribed: bool,
+}
+
+// creator_actor_id -> Text,
+// creator_local -> Bool,
+// creator_name -> Varchar,
+// creator_preferred_username -> Nullable<Varchar>,
+// creator_avatar -> Nullable<Text>,
+// category_name -> Varchar,
+// number_of_subscribers -> BigInt,
+// number_of_posts -> BigInt,
+// number_of_comments -> BigInt,
+// hot_rank -> Int4,
+// user_id -> Nullable<Int4>,
+// subscribed -> Nullable<Bool>,
+
+impl CommunityView {
+  pub fn read(
+    conn: &PgConnection,
+    community_id: i32,
+    my_user_id: Option<i32>,
+  ) -> Result<Self, Error> {
+    let subscribed = match my_user_id {
+      Some(user_id) => {
+        let res = community_follower::table
+          .filter(community_follower::community_id.eq(community_id))
+          .filter(community_follower::user_id.eq(user_id))
+          .get_result::<CommunityFollower>(conn);
+        res.is_ok()
+      }
+      None => false,
+    };
+
+    let (community, creator, category) = community::table
+      .find(community_id)
+      .inner_join(user_::table)
+      .inner_join(category::table)
+      .first::<(Community, User_, Category)>(conn)?;
+
+    Ok(CommunityView {
+      community,
+      creator: creator.to_safe(),
+      category,
+      subscribed,
+    })
+  }
+}
index cd68cfe0a94a904bc3de5a10ef70e1322b7b3e8d..c092e8ec3db102b27b437f2f3db2e1ed552e7e20 100644 (file)
@@ -1,2 +1,5 @@
+pub mod community_view;
 pub mod site_view;
 pub mod user_view;
+
+// TODO Every single aggregate trigger is likely broken, you need to test every one of these out