diff options
Diffstat (limited to 'server')
64 files changed, 2548 insertions, 1095 deletions
diff --git a/server/Cargo.lock b/server/Cargo.lock index 6d6364d4..d90b9679 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -1400,12 +1400,6 @@ dependencies = [ ] [[package]] -name = "htmlescape" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9025058dae765dee5070ec375f591e2ba14638c63feff74f13805a72e523163" - -[[package]] name = "http" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1428,9 +1422,9 @@ dependencies = [ [[package]] name = "http-signature-normalization-actix" -version = "0.4.0-alpha.0" +version = "0.4.0-alpha.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09afff6987c7edbed101d1cddd2185786fb0af0dd9c06b654aca73a0a763680f" +checksum = "131fc982391a6b37847888b568cbe0e9cd302f1b0015f4f6f4a50234bebd049c" dependencies = [ "actix-http", "actix-web", @@ -1573,6 +1567,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] +name = "lemmy_db" +version = "0.1.0" +dependencies = [ + "bcrypt", + "chrono", + "diesel", + "log", + "serde 1.0.114", + "serde_json", + "sha2", + "strum", + "strum_macros", +] + +[[package]] name = "lemmy_server" version = "0.0.1" dependencies = [ @@ -1589,27 +1598,23 @@ dependencies = [ "base64 0.12.3", "bcrypt", "chrono", - "comrak", - "config", "diesel", "diesel_migrations", "dotenv", "env_logger", "failure", "futures", - "htmlescape", "http", "http-signature-normalization-actix", "itertools", "jsonwebtoken", "lazy_static", - "lettre", - "lettre_email", + "lemmy_db", + "lemmy_utils", "log", "openssl", "percent-encoding", "rand 0.7.3", - "regex", "rss", "serde 1.0.114", "serde_json", @@ -1622,6 +1627,26 @@ dependencies = [ ] [[package]] +name = "lemmy_utils" +version = "0.1.0" +dependencies = [ + "chrono", + "comrak", + "config", + "itertools", + "lazy_static", + "lettre", + "lettre_email", + "log", + "openssl", + "rand 0.7.3", + "regex", + "serde 1.0.114", + "serde_json", + "url", +] + +[[package]] name = "lettre" version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/server/Cargo.toml b/server/Cargo.toml index 8daf72c4..2aa3c139 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -1,14 +1,21 @@ [package] name = "lemmy_server" version = "0.0.1" -authors = ["Dessalines <tyhou13@gmx.com>"] edition = "2018" [profile.release] lto = true +[workspace] +members = [ + "lemmy_utils", + "lemmy_db" +] + [dependencies] -diesel = { version = "1.4.4", features = ["postgres","chrono","r2d2","64-column-tables","serde_json"] } +lemmy_utils = { path = "./lemmy_utils" } +lemmy_db = { path = "./lemmy_db" } +diesel = "1.4.4" diesel_migrations = "1.4.0" dotenv = "0.15.0" activitystreams = "0.6.2" @@ -31,19 +38,13 @@ rand = "0.7.3" strum = "0.18.0" strum_macros = "0.18.0" jsonwebtoken = "7.0.1" -regex = "1.3.5" lazy_static = "1.3.0" -lettre = "0.9.3" -lettre_email = "0.9.4" rss = "1.9.0" -htmlescape = "0.3.1" url = { version = "2.1.1", features = ["serde"] } -config = {version = "0.10.1", default-features = false, features = ["hjson"] } percent-encoding = "2.1.0" -comrak = "0.7" openssl = "0.10" http = "0.2.1" -http-signature-normalization-actix = { version = "0.4.0-alpha.0", default-features = false, features = ["sha-2"] } +http-signature-normalization-actix = { version = "0.4.0-alpha.2", default-features = false, features = ["sha-2"] } base64 = "0.12.1" tokio = "0.2.21" futures = "0.3.5" diff --git a/server/db-init.sh b/server/db-init.sh index a2ad77b5..ccecb7de 100755 --- a/server/db-init.sh +++ b/server/db-init.sh @@ -1,4 +1,5 @@ -#!/bin/sh +#!/bin/bash +set -e # Default configurations username=lemmy diff --git a/server/diesel.toml b/server/diesel.toml index 92267c82..1644558f 100644 --- a/server/diesel.toml +++ b/server/diesel.toml @@ -2,4 +2,4 @@ # see diesel.rs/guides/configuring-diesel-cli [print_schema] -file = "src/schema.rs" +file = "lemmy_db/src/schema.rs" diff --git a/server/lemmy_db/Cargo.toml b/server/lemmy_db/Cargo.toml new file mode 100644 index 00000000..d94cf5fc --- /dev/null +++ b/server/lemmy_db/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "lemmy_db" +version = "0.1.0" +edition = "2018" + +[dependencies] +diesel = { version = "1.4.4", features = ["postgres","chrono","r2d2","64-column-tables","serde_json"] } +chrono = { version = "0.4.7", features = ["serde"] } +serde = { version = "1.0.105", features = ["derive"] } +serde_json = { version = "1.0.52", features = ["preserve_order"]} +strum = "0.18.0" +strum_macros = "0.18.0" +log = "0.4.0" +sha2 = "0.9" +bcrypt = "0.8.0"
\ No newline at end of file diff --git a/server/src/db/activity.rs b/server/lemmy_db/src/activity.rs index 8c2b0c74..83f85ca1 100644 --- a/server/src/db/activity.rs +++ b/server/lemmy_db/src/activity.rs @@ -1,9 +1,12 @@ -use crate::{blocking, db::Crud, schema::activity, DbPool, LemmyError}; +use crate::{schema::activity, Crud}; use diesel::{dsl::*, result::Error, *}; use log::debug; use serde::{Deserialize, Serialize}; use serde_json::Value; -use std::fmt::Debug; +use std::{ + fmt::Debug, + io::{Error as IoError, ErrorKind}, +}; #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[table_name = "activity"] @@ -55,46 +58,43 @@ impl Crud<ActivityForm> for Activity { } } -pub async fn insert_activity<T>( - user_id: i32, - data: T, - local: bool, - pool: &DbPool, -) -> Result<(), LemmyError> -where - T: Serialize + Debug + Send + 'static, -{ - blocking(pool, move |conn| { - do_insert_activity(conn, user_id, &data, local) - }) - .await??; - Ok(()) -} - -fn do_insert_activity<T>( +pub fn do_insert_activity<T>( conn: &PgConnection, user_id: i32, data: &T, local: bool, -) -> Result<(), LemmyError> +) -> Result<Activity, IoError> where T: Serialize + Debug, { + debug!("inserting activity for user {}, data {:?}", user_id, &data); let activity_form = ActivityForm { user_id, data: serde_json::to_value(&data)?, local, updated: None, }; - debug!("inserting activity for user {}, data {:?}", user_id, data); - Activity::create(&conn, &activity_form)?; - Ok(()) + let result = Activity::create(&conn, &activity_form); + match result { + Ok(s) => Ok(s), + Err(e) => Err(IoError::new( + ErrorKind::Other, + format!("Failed to insert activity into database: {}", e), + )), + } } #[cfg(test)] mod tests { - use super::{super::user::*, *}; - use crate::db::{establish_unpooled_connection, Crud, ListingType, SortType}; + use crate::{ + activity::{Activity, ActivityForm}, + tests::establish_unpooled_connection, + user::{UserForm, User_}, + Crud, + ListingType, + SortType, + }; + use serde_json::Value; #[test] fn test_crud() { diff --git a/server/src/db/category.rs b/server/lemmy_db/src/category.rs index ff49bbbe..ec2efc7b 100644 --- a/server/src/db/category.rs +++ b/server/lemmy_db/src/category.rs @@ -1,6 +1,6 @@ use crate::{ - db::Crud, schema::{category, category::dsl::*}, + Crud, }; use diesel::{dsl::*, result::Error, *}; use serde::{Deserialize, Serialize}; @@ -52,8 +52,7 @@ impl Category { #[cfg(test)] mod tests { - use super::*; - use crate::db::establish_unpooled_connection; + use crate::{category::Category, tests::establish_unpooled_connection}; #[test] fn test_crud() { diff --git a/server/src/db/comment.rs b/server/lemmy_db/src/comment.rs index 7e76770f..602070d5 100644 --- a/server/src/db/comment.rs +++ b/server/lemmy_db/src/comment.rs @@ -1,9 +1,5 @@ use super::{post::Post, *}; -use crate::{ - apub::{make_apub_endpoint, EndpointType}, - naive_now, - schema::{comment, comment_like, comment_saved}, -}; +use crate::schema::{comment, comment_like, comment_saved}; // WITH RECURSIVE MyTree AS ( // SELECT * FROM comment WHERE parent_id IS NULL @@ -77,12 +73,15 @@ impl Crud<CommentForm> for Comment { } impl Comment { - pub fn update_ap_id(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> { + pub fn update_ap_id( + conn: &PgConnection, + comment_id: i32, + apub_id: String, + ) -> Result<Self, Error> { use crate::schema::comment::dsl::*; - let apid = make_apub_endpoint(EndpointType::Comment, &comment_id.to_string()).to_string(); diesel::update(comment.find(comment_id)) - .set(ap_id.eq(apid)) + .set(ap_id.eq(apub_id)) .get_result::<Self>(conn) } @@ -204,10 +203,8 @@ impl Saveable<CommentSavedForm> for CommentSaved { #[cfg(test)] mod tests { - use super::{ - super::{community::*, post::*, user::*}, - *, - }; + use crate::{comment::*, community::*, post::*, tests::establish_unpooled_connection, user::*}; + #[test] fn test_crud() { let conn = establish_unpooled_connection(); diff --git a/server/src/db/comment_view.rs b/server/lemmy_db/src/comment_view.rs index d1b27a3c..4af13c2d 100644 --- a/server/src/db/comment_view.rs +++ b/server/lemmy_db/src/comment_view.rs @@ -1,5 +1,5 @@ // TODO, remove the cross join here, just join to user directly -use crate::db::{fuzzy_search, limit_and_offset, ListingType, MaybeOptional, SortType}; +use crate::{fuzzy_search, limit_and_offset, ListingType, MaybeOptional, SortType}; use diesel::{dsl::*, pg::Pg, result::Error, *}; use serde::{Deserialize, Serialize}; @@ -9,6 +9,7 @@ table! { id -> Int4, creator_id -> Int4, post_id -> Int4, + post_name -> Varchar, parent_id -> Nullable<Int4>, content -> Text, removed -> Bool, @@ -27,6 +28,7 @@ table! { creator_actor_id -> Text, creator_local -> Bool, creator_name -> Varchar, + creator_published -> Timestamp, creator_avatar -> Nullable<Text>, score -> BigInt, upvotes -> BigInt, @@ -44,6 +46,7 @@ table! { id -> Int4, creator_id -> Int4, post_id -> Int4, + post_name -> Varchar, parent_id -> Nullable<Int4>, content -> Text, removed -> Bool, @@ -62,6 +65,7 @@ table! { creator_actor_id -> Text, creator_local -> Bool, creator_name -> Varchar, + creator_published -> Timestamp, creator_avatar -> Nullable<Text>, score -> BigInt, upvotes -> BigInt, @@ -82,6 +86,7 @@ pub struct CommentView { pub id: i32, pub creator_id: i32, pub post_id: i32, + pub post_name: String, pub parent_id: Option<i32>, pub content: String, pub removed: bool, @@ -100,6 +105,7 @@ pub struct CommentView { pub creator_actor_id: String, pub creator_local: bool, pub creator_name: String, + pub creator_published: chrono::NaiveDateTime, pub creator_avatar: Option<String>, pub score: i64, pub upvotes: i64, @@ -295,6 +301,7 @@ table! { id -> Int4, creator_id -> Int4, post_id -> Int4, + post_name -> Varchar, parent_id -> Nullable<Int4>, content -> Text, removed -> Bool, @@ -314,6 +321,7 @@ table! { creator_local -> Bool, creator_name -> Varchar, creator_avatar -> Nullable<Text>, + creator_published -> Timestamp, score -> BigInt, upvotes -> BigInt, downvotes -> BigInt, @@ -334,6 +342,7 @@ pub struct ReplyView { pub id: i32, pub creator_id: i32, pub post_id: i32, + pub post_name: String, pub parent_id: Option<i32>, pub content: String, pub removed: bool, @@ -353,6 +362,7 @@ pub struct ReplyView { pub creator_local: bool, pub creator_name: String, pub creator_avatar: Option<String>, + pub creator_published: chrono::NaiveDateTime, pub score: i64, pub upvotes: i64, pub downvotes: i64, @@ -455,11 +465,17 @@ impl<'a> ReplyQueryBuilder<'a> { #[cfg(test)] mod tests { - use super::{ - super::{comment::*, community::*, post::*, user::*}, + use crate::{ + comment::*, + comment_view::*, + community::*, + post::*, + tests::establish_unpooled_connection, + user::*, + Crud, + Likeable, *, }; - use crate::db::{establish_unpooled_connection, Crud, Likeable}; #[test] fn test_crud() { @@ -565,6 +581,7 @@ mod tests { content: "A test comment 32".into(), creator_id: inserted_user.id, post_id: inserted_post.id, + post_name: inserted_post.name.to_owned(), community_id: inserted_community.id, community_name: inserted_community.name.to_owned(), parent_id: None, @@ -576,6 +593,7 @@ mod tests { published: inserted_comment.published, updated: None, creator_name: inserted_user.name.to_owned(), + creator_published: inserted_user.published, creator_avatar: None, score: 1, downvotes: 0, @@ -598,6 +616,7 @@ mod tests { content: "A test comment 32".into(), creator_id: inserted_user.id, post_id: inserted_post.id, + post_name: inserted_post.name.to_owned(), community_id: inserted_community.id, community_name: inserted_community.name.to_owned(), parent_id: None, @@ -609,6 +628,7 @@ mod tests { published: inserted_comment.published, updated: None, creator_name: inserted_user.name.to_owned(), + creator_published: inserted_user.published, creator_avatar: None, score: 1, downvotes: 0, diff --git a/server/src/db/community.rs b/server/lemmy_db/src/community.rs index 461ba473..60752080 100644 --- a/server/src/db/community.rs +++ b/server/lemmy_db/src/community.rs @@ -1,6 +1,9 @@ use crate::{ - db::{Bannable, Crud, Followable, Joinable}, schema::{community, community_follower, community_moderator, community_user_ban}, + Bannable, + Crud, + Followable, + Joinable, }; use diesel::{dsl::*, result::Error, *}; use serde::{Deserialize, Serialize}; @@ -232,8 +235,7 @@ impl Followable<CommunityFollowerForm> for CommunityFollower { #[cfg(test)] mod tests { - use super::{super::user::*, *}; - use crate::db::{establish_unpooled_connection, ListingType, SortType}; + use crate::{community::*, tests::establish_unpooled_connection, user::*, ListingType, SortType}; #[test] fn test_crud() { diff --git a/server/src/db/community_view.rs b/server/lemmy_db/src/community_view.rs index 4ec839ac..5c6bd81a 100644 --- a/server/src/db/community_view.rs +++ b/server/lemmy_db/src/community_view.rs @@ -1,5 +1,5 @@ use super::community_view::community_fast_view::BoxedQuery; -use crate::db::{fuzzy_search, limit_and_offset, MaybeOptional, SortType}; +use crate::{fuzzy_search, limit_and_offset, MaybeOptional, SortType}; use diesel::{pg::Pg, result::Error, *}; use serde::{Deserialize, Serialize}; @@ -299,6 +299,7 @@ impl CommunityModeratorView { use super::community_view::community_moderator_view::dsl::*; community_moderator_view .filter(community_id.eq(from_community_id)) + .order_by(published) .load::<Self>(conn) } @@ -306,6 +307,7 @@ impl CommunityModeratorView { use super::community_view::community_moderator_view::dsl::*; community_moderator_view .filter(user_id.eq(from_user_id)) + .order_by(published) .load::<Self>(conn) } } diff --git a/server/src/db/mod.rs b/server/lemmy_db/src/lib.rs index da69f8dc..5cf20bb3 100644 --- a/server/src/db/mod.rs +++ b/server/lemmy_db/src/lib.rs @@ -1,10 +1,22 @@ -use crate::settings::Settings; +#[macro_use] +pub extern crate diesel; +#[macro_use] +pub extern crate strum_macros; +pub extern crate bcrypt; +pub extern crate chrono; +pub extern crate log; +pub extern crate serde; +pub extern crate serde_json; +pub extern crate sha2; +pub extern crate strum; + +use chrono::NaiveDateTime; use diesel::{dsl::*, result::Error, *}; use serde::{Deserialize, Serialize}; +use std::{env, env::VarError}; pub mod activity; pub mod category; -pub mod code_migrations; pub mod comment; pub mod comment_view; pub mod community; @@ -16,6 +28,7 @@ pub mod post; pub mod post_view; pub mod private_message; pub mod private_message_view; +pub mod schema; pub mod site; pub mod site_view; pub mod user; @@ -111,9 +124,8 @@ impl<T> MaybeOptional<T> for Option<T> { } } -pub fn establish_unpooled_connection() -> PgConnection { - let db_url = Settings::get().get_database_url(); - PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)) +pub fn get_database_url_from_env() -> Result<String, VarError> { + env::var("LEMMY_DATABASE_URL") } #[derive(EnumString, ToString, Debug, Serialize, Deserialize)] @@ -155,9 +167,25 @@ pub fn limit_and_offset(page: Option<i64>, limit: Option<i64>) -> (i64, i64) { let offset = limit * (page - 1); (limit, offset) } + +pub fn naive_now() -> NaiveDateTime { + chrono::prelude::Utc::now().naive_utc() +} + #[cfg(test)] mod tests { use super::fuzzy_search; + use crate::get_database_url_from_env; + use diesel::{Connection, PgConnection}; + + pub fn establish_unpooled_connection() -> PgConnection { + let db_url = match get_database_url_from_env() { + Ok(url) => url, + Err(e) => panic!("Failed to read database URL from env var LEMMY_DATABASE_URL: {}", e), + }; + PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url)) + } + #[test] fn test_fuzzy_search() { let |