diff options
author | Dessalines <happydooby@gmail.com> | 2019-08-15 16:53:59 -0700 |
---|---|---|
committer | Dessalines <happydooby@gmail.com> | 2019-08-15 16:53:59 -0700 |
commit | 42c9c57a1c169720b8ac5182dc20da5051bd6dcc (patch) | |
tree | 02944106433ef4daa981094b6074583585b27c36 | |
parent | 1070cd6921ba533772d1ee1cf4e28cbb0c938f32 (diff) | |
parent | d3ecba4405e8a2bfb7ea02ac17c2c9f5be978d4a (diff) |
Merge branch 'dev'
37 files changed, 623 insertions, 130 deletions
@@ -37,6 +37,7 @@ Front Page|Post - Can ban and unban users from communities and the site. - Clean, mobile-friendly interface. - i18n / internationalization support. +- NSFW post / community support. - High performance. - Server is written in rust. - Front end is `~80kB` gzipped. diff --git a/docker-compose.yml b/docker-compose.yml index 2b9f3171..8607d546 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,12 +2,14 @@ version: '2.4' services: db: - image: postgres + image: postgres:12-alpine restart: always environment: POSTGRES_USER: rrr POSTGRES_PASSWORD: rrr POSTGRES_DB: rrr + volumes: + - db:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U rrr"] interval: 5s @@ -27,3 +29,5 @@ services: depends_on: db: condition: service_healthy +volumes: + db: diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 4f5d51a9..aafcae1a 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -2,12 +2,14 @@ version: '2.4' services: db: - image: postgres + image: postgres:12-alpine restart: always environment: POSTGRES_USER: rrr POSTGRES_PASSWORD: rrr POSTGRES_DB: rrr + volumes: + - db:/var/lib/postgresql/data healthcheck: test: ["CMD-SHELL", "pg_isready -U rrr"] interval: 5s @@ -26,3 +28,5 @@ services: depends_on: db: condition: service_healthy +volumes: + db: diff --git a/docker_db_backup.sh b/docker_db_backup.sh index 5b87b818..e361da19 100755 --- a/docker_db_backup.sh +++ b/docker_db_backup.sh @@ -1 +1 @@ -docker exec -it lemmy_db_1 pg_dumpall -c -U rrr > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql +docker exec -it lemmy_db_1 pg_dumpall -c -U rrr > dump_`date +%Y-%m-%d"_"%H_%M_%S`.sql diff --git a/docs/api.md b/docs/api.md index 744de5f3..90b931e6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -28,7 +28,7 @@ A simple test command: ## API ### List -`Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead` +`Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead, SaveUserSettings` ### Sort Types These go wherever there is a `sort` field. @@ -109,7 +109,21 @@ Only the first user will be able to be the admin. posts: Vec<PostView>, } ``` - +#### Save User Settings +##### Request +```rust +{ + show_nsfw: bool, + auth: String, +} +``` +##### Response +```rust +{ + op: String, + jwt: String +} +``` #### Get Replies / Inbox ##### Request ```rust diff --git a/server/migrations/2019-08-11-000918_add_nsfw_columns/down.sql b/server/migrations/2019-08-11-000918_add_nsfw_columns/down.sql new file mode 100644 index 00000000..2eefece4 --- /dev/null +++ b/server/migrations/2019-08-11-000918_add_nsfw_columns/down.sql @@ -0,0 +1,80 @@ +drop view community_view; +drop view post_view; +alter table community drop column nsfw; +alter table post drop column nsfw; +alter table user_ drop column show_nsfw; + +-- the views +create view community_view as +with all_community as +( + select *, + (select name from user_ u where c.creator_id = u.id) as creator_name, + (select name from category ct where c.category_id = ct.id) as category_name, + (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers, + (select count(*) from post p where p.community_id = c.id) as number_of_posts, + (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments, + hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank + from community c +) + +select +ac.*, +u.id as user_id, +(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed +from user_ u +cross join all_community ac + +union all + +select +ac.*, +null as user_id, +null as subscribed +from all_community ac +; + + +-- Post view +create view post_view as +with all_post as +( + select + p.*, + (select name from user_ where p.creator_id = user_.id) as creator_name, + (select name from community where p.community_id = community.id) as community_name, + (select removed from community c where p.community_id = c.id) as community_removed, + (select deleted from community c where p.community_id = c.id) as community_deleted, + (select count(*) from comment where comment.post_id = p.id) as number_of_comments, + coalesce(sum(pl.score), 0) as score, + count (case when pl.score = 1 then 1 else null end) as upvotes, + count (case when pl.score = -1 then 1 else null end) as downvotes, + hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank + from post p + left join post_like pl on p.id = pl.post_id + group by p.id +) + +select +ap.*, +u.id as user_id, +coalesce(pl.score, 0) as my_vote, +(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed, +(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read, +(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved +from user_ u +cross join all_post ap +left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id + +union all + +select +ap.*, +null as user_id, +null as my_vote, +null as subscribed, +null as read, +null as saved +from all_post ap +; + diff --git a/server/migrations/2019-08-11-000918_add_nsfw_columns/up.sql b/server/migrations/2019-08-11-000918_add_nsfw_columns/up.sql new file mode 100644 index 00000000..cc1e0074 --- /dev/null +++ b/server/migrations/2019-08-11-000918_add_nsfw_columns/up.sql @@ -0,0 +1,79 @@ +alter table community add column nsfw boolean default false not null; +alter table post add column nsfw boolean default false not null; +alter table user_ add column show_nsfw boolean default false not null; + +-- The views +drop view community_view; +create view community_view as +with all_community as +( + select *, + (select name from user_ u where c.creator_id = u.id) as creator_name, + (select name from category ct where c.category_id = ct.id) as category_name, + (select count(*) from community_follower cf where cf.community_id = c.id) as number_of_subscribers, + (select count(*) from post p where p.community_id = c.id) as number_of_posts, + (select count(*) from comment co, post p where c.id = p.community_id and p.id = co.post_id) as number_of_comments, + hot_rank((select count(*) from community_follower cf where cf.community_id = c.id), c.published) as hot_rank + from community c +) + +select +ac.*, +u.id as user_id, +(select cf.id::boolean from community_follower cf where u.id = cf.user_id and ac.id = cf.community_id) as subscribed +from user_ u +cross join all_community ac + +union all + +select +ac.*, +null as user_id, +null as subscribed +from all_community ac +; + +-- Post view +drop view post_view; +create view post_view as +with all_post as +( + select + p.*, + (select name from user_ where p.creator_id = user_.id) as creator_name, + (select name from community where p.community_id = community.id) as community_name, + (select removed from community c where p.community_id = c.id) as community_removed, + (select deleted from community c where p.community_id = c.id) as community_deleted, + (select nsfw from community c where p.community_id = c.id) as community_nsfw, + (select count(*) from comment where comment.post_id = p.id) as number_of_comments, + coalesce(sum(pl.score), 0) as score, + count (case when pl.score = 1 then 1 else null end) as upvotes, + count (case when pl.score = -1 then 1 else null end) as downvotes, + hot_rank(coalesce(sum(pl.score) , 0), p.published) as hot_rank + from post p + left join post_like pl on p.id = pl.post_id + group by p.id +) + +select +ap.*, +u.id as user_id, +coalesce(pl.score, 0) as my_vote, +(select cf.id::bool from community_follower cf where u.id = cf.user_id and cf.community_id = ap.community_id) as subscribed, +(select pr.id::bool from post_read pr where u.id = pr.user_id and pr.post_id = ap.id) as read, +(select ps.id::bool from post_saved ps where u.id = ps.user_id and ps.post_id = ap.id) as saved +from user_ u +cross join all_post ap +left join post_like pl on u.id = pl.user_id and ap.id = pl.post_id + +union all + +select +ap.*, +null as user_id, +null as my_vote, +null as subscribed, +null as read, +null as saved +from all_post ap +; diff --git a/server/src/api/community.rs b/server/src/api/community.rs index ca73de49..74058488 100644 --- a/server/src/api/community.rs +++ b/server/src/api/community.rs @@ -22,7 +22,8 @@ pub struct CreateCommunity { name: String, title: String, description: Option<String>, - category_id: i32 , + category_id: i32, + nsfw: bool, auth: String } @@ -86,6 +87,7 @@ pub struct EditCommunity { category_id: i32, removed: Option<bool>, deleted: Option<bool>, + nsfw: bool, reason: Option<String>, expires: Option<i64>, auth: String @@ -194,6 +196,7 @@ impl Perform<CommunityResponse> for Oper<CreateCommunity> { creator_id: user_id, removed: None, deleted: None, + nsfw: data.nsfw, updated: None, }; @@ -291,6 +294,7 @@ impl Perform<CommunityResponse> for Oper<EditCommunity> { creator_id: user_id, removed: data.removed.to_owned(), deleted: data.deleted.to_owned(), + nsfw: data.nsfw, updated: Some(naive_now()) }; @@ -333,22 +337,38 @@ impl Perform<ListCommunitiesResponse> for Oper<ListCommunities> { let data: &ListCommunities = &self.data; let conn = establish_connection(); - let user_id: Option<i32> = match &data.auth { + let user_claims: Option<Claims> = match &data.auth { Some(auth) => { match Claims::decode(&auth) { Ok(claims) => { - let user_id = claims.claims.id; - Some(user_id) + Some(claims.claims) } Err(_e) => None } } None => None }; + + let user_id = match &user_claims { + Some(claims) => Some(claims.id), + None => None + }; + + let show_nsfw = match &user_claims { + Some(claims) => claims.show_nsfw, + None => false + }; let sort = SortType::from_str(&data.sort)?; - let communities: Vec<CommunityView> = CommunityView::list(&conn, &sort, user_id, None, data.page, data.limit)?; + let communities: Vec<CommunityView> = CommunityView::list( + &conn, + &sort, + user_id, + show_nsfw, + None, + data.page, + data.limit)?; // Return the jwt Ok( diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index e10770b4..3a4a0865 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -22,7 +22,7 @@ pub mod site; #[derive(EnumString,ToString,Debug)] pub enum UserOperation { - Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead + Login, Register, CreateCommunity, CreatePost, ListCommunities, ListCategories, GetPost, GetCommunity, CreateComment, EditComment, SaveComment, CreateCommentLike, GetPosts, CreatePostLike, EditPost, SavePost, EditCommunity, FollowCommunity, GetFollowedCommunities, GetUserDetails, GetReplies, GetModlog, BanFromCommunity, AddModToCommunity, CreateSite, EditSite, GetSite, AddAdmin, BanUser, Search, MarkAllAsRead, SaveUserSettings } #[derive(Fail, Debug)] diff --git a/server/src/api/post.rs b/server/src/api/post.rs index df6ea852..35363a17 100644 --- a/server/src/api/post.rs +++ b/server/src/api/post.rs @@ -6,6 +6,7 @@ pub struct CreatePost { name: String, url: Option<String>, body: Option<String>, + nsfw: bool, community_id: i32, auth: String } @@ -73,6 +74,7 @@ pub struct EditPost { body: Option<String>, removed: Option<bool>, deleted: Option<bool>, + nsfw: bool, locked: Option<bool>, reason: Option<String>, auth: String @@ -123,6 +125,7 @@ impl Perform<PostResponse> for Oper<CreatePost> { creator_id: user_id, removed: None, deleted: None, + nsfw: data.nsfw, locked: None, updated: None }; @@ -219,40 +222,50 @@ impl Perform<GetPostsResponse> for Oper<GetPosts> { let data: &GetPosts = &self.data; let conn = establish_connection(); - let user_id: Option<i32> = match &data.auth { + let user_claims: Option<Claims> = match &data.auth { Some(auth) => { match Claims::decode(&auth) { Ok(claims) => { - let user_id = claims.claims.id; - Some(user_id) + Some(claims.claims) } Err(_e) => None } } None => None }; + + let user_id = match &user_claims { + Some(claims) => Some(claims.id), + None => None + }; + + let show_nsfw = match &user_claims { + Some(claims) => claims.show_nsfw, + None => false + }; let type_ = PostListingType::from_str(&data.type_)?; let sort = SortType::from_str(&data.sort)?; - let posts = match PostView::list(&conn, - type_, - &sort, - data.community_id, - None, - None, - user_id, - false, - false, - data.page, - data.limit) { + let posts = match PostView::list( + &conn, + type_, + &sort, + data.community_id, + None, + None, + user_id, + show_nsfw, + false, + false, + data.page, + data.limit) { Ok(posts) => posts, Err(_e) => { return Err(APIError::err(&self.op, "couldnt_get_posts"))? } }; - // Return the jwt Ok( GetPostsResponse { op: self.op.to_string(), @@ -381,6 +394,7 @@ impl Perform<PostResponse> for Oper<EditPost> { community_id: data.community_id, removed: data.removed.to_owned(), deleted: data.deleted.to_owned(), + nsfw: data.nsfw, locked: data.locked.to_owned(), updated: Some(naive_now()) }; diff --git a/server/src/api/site.rs b/server/src/api/site.rs index 09af742f..8f094aac 100644 --- a/server/src/api/site.rs +++ b/server/src/api/site.rs @@ -277,6 +277,8 @@ impl Perform<SearchResponse> for Oper<Search> { let mut communities = Vec::new(); let mut users = Vec::new(); + // TODO no clean / non-nsfw searching rn + match type_ { SearchType::Posts => { posts = PostView::list( @@ -287,6 +289,7 @@ impl Perform<SearchResponse> for Oper<Search> { None, Some(data.q.to_owned()), None, + true, false, false, data.page, @@ -309,6 +312,7 @@ impl Perform<SearchResponse> for Oper<Search> { &conn, &sort, None, + true, Some(data.q.to_owned()), data.page, data.limit)?; @@ -330,6 +334,7 @@ impl Perform<SearchResponse> for Oper<Search> { None, Some(data.q.to_owned()), None, + true, false, false, data.page, @@ -348,6 +353,7 @@ impl Perform<SearchResponse> for Oper<Search> { &conn, &sort, None, + true, Some(data.q.to_owned()), data.page, data.limit)?; diff --git a/server/src/api/user.rs b/server/src/api/user.rs index 5d5f1a6b..2a6c214a 100644 --- a/server/src/api/user.rs +++ b/server/src/api/user.rs @@ -15,6 +15,13 @@ pub struct Register { password: String, password_verify: String, admin: bool, + show_nsfw: bool, +} + +#[derive(Serialize, Deserialize)] +pub struct SaveUserSettings { + show_nsfw: bool, + auth: String, } #[derive(Serialize, Deserialize)] @@ -151,6 +158,7 @@ impl Perform<LoginResponse> for Oper<Register> { updated: None, admin: data.admin, banned: false, + show_nsfw: data.show_nsfw, }; // Create the user @@ -170,6 +178,7 @@ impl Perform<LoginResponse> for Oper<Register> { title: "The Default Community".to_string(), description: Some("The Default Community".to_string()), category_id: 1, + nsfw: false, creator_id: inserted_user.id, removed: None, deleted: None, @@ -218,24 +227,77 @@ impl Perform<LoginResponse> for Oper<Register> { } } +impl Perform<LoginResponse> for Oper<SaveUserSettings> { + fn perform(&self) -> Result<LoginResponse, Error> { + let data: &SaveUserSettings = &self.data; + let conn = establish_connection(); + + let claims = match Claims::decode(&data.auth) { + Ok(claims) => claims.claims, + Err(_e) => { + return Err(APIError::err(&self.op, "not_logged_in"))? + } + }; + + let user_id = claims.id; + + let read_user = User_::read(&conn, user_id)?; + + let user_form = UserForm { + name: read_user.name, + fedi_name: read_user.fedi_name, + email: read_user.email, + password_encrypted: read_user.password_encrypted, + preferred_username: read_user.preferred_username, + updated: Some(naive_now()), + admin: read_user.admin, + banned: read_user.banned, + show_nsfw: data.show_nsfw, + }; + + let updated_user = match User_::update(&conn, user_id, &user_form) { + Ok(user) => user, + Err(_e) => { + return Err(APIError::err(&self.op, "couldnt_update_user"))? + } + }; + + // Return the jwt + Ok( + LoginResponse { + op: self.op.to_string(), + jwt: updated_user.jwt() + } + ) + } +} impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> { fn perform(&self) -> Result<GetUserDetailsResponse, Error> { let data: &GetUserDetails = &self.data; let conn = establish_connection(); - let user_id: Option<i32> = match &data.auth { + let user_claims: Option<Claims> = match &data.auth { Some(auth) => { match Claims::decode(&auth) { Ok(claims) => { - let user_id = claims.claims.id; - Some(user_id) + Some(claims.claims) } Err(_e) => None } } None => None }; + + let user_id = match &user_claims { + Some(claims) => Some(claims.id), + None => None + }; + + let show_nsfw = match &user_claims { + Some(claims) => claims.show_nsfw, + None => false + }; //TODO add save let sort = SortType::from_str(&data.sort)?; @@ -249,50 +311,56 @@ impl Perform<GetUserDetailsResponse> for Oper<GetUserDetails> { // If its saved only, you don't care what creator it was let posts = if data.saved_only { - PostView::list(&conn, - PostListingType::All, - &sort, - data.community_id, - None, - None, - Some(user_details_id), - data.saved_only, - false, - data.page, - data.limit)? + PostView::list( + &conn, + PostListingType::All, + &sort, + data.community_id, + None, + None, + Some(user_details_id), + show_nsfw, + data.saved_only, + false, + data.page, + data.limit)? } else { - PostView::list(&conn, - PostListingType::All, - &sort, - data.community_id, - Some(user_details_id), - None, - user_id, - data.saved_only, - false, - data.page, - data.limit)? + PostView::list( + &conn, + PostListingType::All, + &sort, + data.community_id, + Some(user_details_id), + None, + user_id, + show_nsfw, + data.saved_only, + false, + data.page, + data.limit)? }; let comments = if data.saved_only { - CommentView::list(&conn, - &sort, - None, - None, - None, - Some(user_details_id), - data.saved_only, - data.page, - data.limit)? + CommentView::list( + &conn, + &sort, + None, + None, + None, + Some(user_details_id), + data.saved_only, + data.page, + data.limit)? } else { - CommentView::list(&conn, - &sort, - None, - Some(user_details_id), - None, - user_id, - data.saved_only, - data.page, - data.limit)? + CommentView::list( + &conn, + &sort, + None, + Some(user_details_id), + None, + user_id, + data.saved_only, + data.page, + data.limit)? }; let follows = CommunityFollowerView::for_user(&conn, user_details_id)?; @@ -343,6 +411,7 @@ impl Perform<AddAdminResponse> for Oper<AddAdmin> { updated: Some(naive_now()), admin: data.added, banned: read_user.banned, + show_nsfw: read_user.show_nsfw, }; match User_::update(&conn, data.user_id, &user_form) { @@ -402,6 +471,7 @@ impl Perform<BanUserResponse> for Oper<BanUser> { updated: Some(naive_now()), admin: read_user.admin, banned: data.ban, + show_nsfw: read_user.show_nsfw, }; match User_::update(&conn, data.user_id, &user_form) { diff --git a/server/src/apub.rs b/server/src/apub.rs index 23735e63..9de32401 100644 --- a/server/src/apub.rs +++ b/ |