summaryrefslogtreecommitdiffstats
path: root/server/src/db
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/db')
-rw-r--r--server/src/db/community.rs4
-rw-r--r--server/src/db/community_view.rs22
-rw-r--r--server/src/db/post.rs4
-rw-r--r--server/src/db/post_view.rs38
-rw-r--r--server/src/db/user.rs14
5 files changed, 59 insertions, 23 deletions
diff --git a/server/src/db/community.rs b/server/src/db/community.rs
index b32230b9..aa28c9c7 100644
--- a/server/src/db/community.rs
+++ b/server/src/db/community.rs
@@ -14,6 +14,7 @@ pub struct Community {
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
+ pub nsfw: bool,
}
#[derive(Insertable, AsChangeset, Clone, Serialize, Deserialize)]
@@ -27,6 +28,7 @@ pub struct CommunityForm {
pub removed: Option<bool>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
+ pub nsfw: bool,
}
impl Crud<CommunityForm> for Community {
@@ -240,6 +242,7 @@ mod tests {
title: "nada".to_owned(),
description: None,
category_id: 1,
+ nsfw: false,
removed: None,
deleted: None,
updated: None,
@@ -254,6 +257,7 @@ mod tests {
title: "nada".to_owned(),
description: None,
category_id: 1,
+ nsfw: false,
removed: false,
deleted: false,
published: inserted_community.published,
diff --git a/server/src/db/community_view.rs b/server/src/db/community_view.rs
index 6249090d..88ab10af 100644
--- a/server/src/db/community_view.rs
+++ b/server/src/db/community_view.rs
@@ -12,6 +12,7 @@ table! {
published -> Timestamp,
updated -> Nullable<Timestamp>,
deleted -> Bool,
+ nsfw -> Bool,
creator_name -> Varchar,
category_name -> Varchar,
number_of_subscribers -> BigInt,
@@ -84,6 +85,7 @@ pub struct CommunityView {
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
+ pub nsfw: bool,
pub creator_name: String,
pub category_name: String,
pub number_of_subscribers: i64,
@@ -112,13 +114,15 @@ impl CommunityView {
query.first::<Self>(conn)
}
- pub fn list(conn: &PgConnection,
- sort: &SortType,
- from_user_id: Option<i32>,
- search_term: Option<String>,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ sort: &SortType,
+ from_user_id: Option<i32>,
+ show_nsfw: bool,
+ search_term: Option<String>,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::community_view::community_view::dsl::*;
let mut query = community_view.into_boxed();
@@ -143,6 +147,10 @@ impl CommunityView {
_ => ()
};
+ if !show_nsfw {
+ query = query.filter(nsfw.eq(false));
+ };
+
query
.limit(limit)
.offset(offset)
diff --git a/server/src/db/post.rs b/server/src/db/post.rs
index d8fd27b0..6a8bf26d 100644
--- a/server/src/db/post.rs
+++ b/server/src/db/post.rs
@@ -15,6 +15,7 @@ pub struct Post {
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
+ pub nsfw: bool,
}
#[derive(Insertable, AsChangeset, Clone)]
@@ -29,6 +30,7 @@ pub struct PostForm {
pub locked: Option<bool>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
+ pub nsfw: bool,
}
impl Crud<PostForm> for Post {
@@ -210,6 +212,7 @@ mod tests {
removed: None,
deleted: None,
locked: None,
+ nsfw: false,
updated: None
};
@@ -225,6 +228,7 @@ mod tests {
published: inserted_post.published,
removed: false,
locked: false,
+ nsfw: false,
deleted: false,
updated: None
};
diff --git a/server/src/db/post_view.rs b/server/src/db/post_view.rs
index bec00b98..73686266 100644
--- a/server/src/db/post_view.rs
+++ b/server/src/db/post_view.rs
@@ -19,10 +19,12 @@ table! {
published -> Timestamp,
updated -> Nullable<Timestamp>,
deleted -> Bool,
+ nsfw -> Bool,
creator_name -> Varchar,
community_name -> Varchar,
community_removed -> Bool,
community_deleted -> Bool,
+ community_nsfw -> Bool,
number_of_comments -> BigInt,
score -> BigInt,
upvotes -> BigInt,
@@ -51,10 +53,12 @@ pub struct PostView {
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
+ pub nsfw: bool,
pub creator_name: String,
pub community_name: String,
pub community_removed: bool,
pub community_deleted: bool,
+ pub community_nsfw: bool,
pub number_of_comments: i64,
pub score: i64,
pub upvotes: i64,
@@ -68,18 +72,20 @@ pub struct PostView {
}
impl PostView {
- pub fn list(conn: &PgConnection,
- type_: PostListingType,
- sort: &SortType,
- for_community_id: Option<i32>,
- for_creator_id: Option<i32>,
- search_term: Option<String>,
- my_user_id: Option<i32>,
- saved_only: bool,
- unread_only: bool,
- page: Option<i64>,
- limit: Option<i64>,
- ) -> Result<Vec<Self>, Error> {
+ pub fn list(
+ conn: &PgConnection,
+ type_: PostListingType,
+ sort: &SortType,
+ for_community_id: Option<i32>,
+ for_creator_id: Option<i32>,
+ search_term: Option<String>,
+ my_user_id: Option<i32>,
+ show_nsfw: bool,
+ saved_only: bool,
+ unread_only: bool,
+ page: Option<i64>,
+ limit: Option<i64>,
+ ) -> Result<Vec<Self>, Error> {
use super::post_view::post_view::dsl::*;
let (limit, offset) = limit_and_offset(page, limit);
@@ -121,6 +127,12 @@ impl PostView {
query = query.filter(user_id.is_null());
}
+ if !show_nsfw {
+ query = query
+ .filter(nsfw.eq(false))
+ .filter(community_nsfw.eq(false));
+ };
+
query = match sort {
SortType::Hot => query.order_by(hot_rank.desc())
.then_order_by(published.desc()),
@@ -266,6 +278,7 @@ mod tests {
community_name: community_name.to_owned(),
community_removed: false,
community_deleted: false,
+ community_nsfw: false,
number_of_comments: 0,
score: 1,
upvotes: 1,
@@ -294,6 +307,7 @@ mod tests {
community_name: community_name.to_owned(),
community_removed: false,
community_deleted: false,
+ community_nsfw: false,
number_of_comments: 0,
score: 1,
upvotes: 1,
diff --git a/server/src/db/user.rs b/server/src/db/user.rs
index aed5e890..b794524c 100644
--- a/server/src/db/user.rs
+++ b/server/src/db/user.rs
@@ -18,7 +18,8 @@ pub struct User_ {
pub admin: bool,
pub banned: bool,
pub published: chrono::NaiveDateTime,
- pub updated: Option<chrono::NaiveDateTime>
+ pub updated: Option<chrono::NaiveDateTime>,
+ pub show_nsfw: bool,
}
#[derive(Insertable, AsChangeset, Clone)]
@@ -31,7 +32,8 @@ pub struct UserForm {
pub admin: bool,
pub banned: bool,
pub email: Option<String>,
- pub updated: Option<chrono::NaiveDateTime>
+ pub updated: Option<chrono::NaiveDateTime>,
+ pub show_nsfw: bool,
}
impl Crud<UserForm> for User_ {
@@ -77,6 +79,7 @@ pub struct Claims {
pub id: i32,
pub username: String,
pub iss: String,
+ pub show_nsfw: bool,
}
impl Claims {
@@ -96,6 +99,7 @@ impl User_ {
id: self.id,
username: self.name.to_owned(),
iss: self.fedi_name.to_owned(),
+ show_nsfw: self.show_nsfw,
};
encode(&Header::default(), &my_claims, Settings::get().jwt_secret.as_ref()).unwrap()
}
@@ -133,7 +137,8 @@ mod tests {
email: None,
admin: false,
banned: false,
- updated: None
+ updated: None,
+ show_nsfw: false,
};
let inserted_user = User_::create(&conn, &new_user).unwrap();
@@ -149,7 +154,8 @@ mod tests {
admin: false,
banned: false,
published: inserted_user.published,
- updated: None
+ updated: None,
+ show_nsfw: false,
};
let read_user = User_::read(&conn, inserted_user.id).unwrap();