Compare commits

...

1 Commits

Author SHA1 Message Date
Felix Ableitner 1751337d82 wip: try workbench 2020-06-10 16:18:47 +02:00
54 changed files with 351 additions and 602 deletions

46
server/Cargo.lock generated vendored
View File

@ -791,6 +791,23 @@ dependencies = [
"syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", "syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "db"
version = "0.1.0"
dependencies = [
"bcrypt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel 1.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_migrations 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonwebtoken 7.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"schema 0.1.0",
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"strum 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"utils 0.1.0",
]
[[package]] [[package]]
name = "derive_builder" name = "derive_builder"
version = "0.9.0" version = "0.9.0"
@ -1415,31 +1432,28 @@ dependencies = [
"bcrypt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "bcrypt 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"comrak 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "comrak 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"config 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)", "db 0.1.0",
"diesel 1.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
"diesel_migrations 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
"hjson 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "htmlescape 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"isahc 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "isahc 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",
"jsonwebtoken 7.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lettre 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)", "lettre 0.9.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lettre_email 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)", "lettre_email 0.9.4 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"r2d2 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"rss 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "rss 1.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)",
"sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"strum 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "strum 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"strum_macros 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)", "strum_macros 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
"utils 0.1.0",
] ]
[[package]] [[package]]
@ -2243,6 +2257,13 @@ dependencies = [
"parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "schema"
version = "0.1.0"
dependencies = [
"diesel 1.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.1.0" version = "1.1.0"
@ -2784,6 +2805,19 @@ name = "utf8-ranges"
version = "0.1.3" version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utils"
version = "0.1.0"
dependencies = [
"chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)",
"config 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"hjson 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "0.7.4" version = "0.7.4"

48
server/Cargo.toml vendored
View File

@ -1,41 +1,7 @@
[package] [workspace]
name = "lemmy_server" members = [
version = "0.0.1" "lemmy_server",
authors = ["Dessalines <tyhou13@gmx.com>"] "db",
edition = "2018" "schema",
"utils",
[dependencies] ]
diesel = { version = "1.4.2", features = ["postgres","chrono", "r2d2", "64-column-tables"] }
diesel_migrations = "1.4.0"
dotenv = "0.15.0"
bcrypt = "0.7.0"
activitypub = "0.2.0"
chrono = { version = "0.4.7", features = ["serde"] }
failure = "0.1.8"
serde_json = { version = "1.0.52", features = ["preserve_order"]}
serde = { version = "1.0.105", features = ["derive"] }
actix = "0.9.0"
actix-web = "2.0.0"
actix-files = "0.2.1"
actix-web-actors = "2.0.0"
actix-rt = "1.1.1"
log = "0.4.0"
env_logger = "0.7.1"
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"
sha2 = "0.8.1"
rss = "1.9.0"
htmlescape = "0.3.1"
config = "0.10.1"
hjson = "0.8.2"
percent-encoding = "2.1.0"
isahc = "0.9"
comrak = "0.7"
tokio = "0.2.20"
futures = "0.3.4"

20
server/db/Cargo.toml vendored Normal file
View File

@ -0,0 +1,20 @@
[package]
name = "db"
version = "0.1.0"
authors = ["Felix Ableitner <me@nutomic.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
utils = { path = "../utils" }
schema = { path = "../schema" }
diesel = { version = "1.4.2", features = ["postgres","chrono", "r2d2", "64-column-tables"] }
diesel_migrations = "1.4.0"
chrono = { version = "0.4.7", features = ["serde"] }
serde = { version = "1.0.105", features = ["derive"] }
strum = "0.18.0"
strum_macros = "0.18.0"
sha2 = "0.8.1"
bcrypt = "0.7.0"
jsonwebtoken = "7.0.1"

View File

@ -1,6 +1,6 @@
use super::*; use super::*;
use crate::schema::category; use schema::schema::category;
use crate::schema::category::dsl::*; use schema::schema::category::dsl::*;
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name = "category"] #[table_name = "category"]

View File

@ -1,6 +1,6 @@
use super::post::Post; use super::post::Post;
use super::*; use super::*;
use crate::schema::{comment, comment_like, comment_saved}; use schema::schema::{comment, comment_like, comment_saved};
// WITH RECURSIVE MyTree AS ( // WITH RECURSIVE MyTree AS (
// SELECT * FROM comment WHERE parent_id IS NULL // SELECT * FROM comment WHERE parent_id IS NULL
@ -40,17 +40,17 @@ pub struct CommentForm {
impl Crud<CommentForm> for Comment { impl Crud<CommentForm> for Comment {
fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
use crate::schema::comment::dsl::*; use schema::schema::comment::dsl::*;
comment.find(comment_id).first::<Self>(conn) comment.find(comment_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
use crate::schema::comment::dsl::*; use schema::schema::comment::dsl::*;
diesel::delete(comment.find(comment_id)).execute(conn) diesel::delete(comment.find(comment_id)).execute(conn)
} }
fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> { fn create(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
use crate::schema::comment::dsl::*; use schema::schema::comment::dsl::*;
insert_into(comment) insert_into(comment)
.values(comment_form) .values(comment_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -61,7 +61,7 @@ impl Crud<CommentForm> for Comment {
comment_id: i32, comment_id: i32,
comment_form: &CommentForm, comment_form: &CommentForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::comment::dsl::*; use schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id)) diesel::update(comment.find(comment_id))
.set(comment_form) .set(comment_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -91,20 +91,20 @@ pub struct CommentLikeForm {
impl Likeable<CommentLikeForm> for CommentLike { impl Likeable<CommentLikeForm> for CommentLike {
fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> { fn read(conn: &PgConnection, comment_id_from: i32) -> Result<Vec<Self>, Error> {
use crate::schema::comment_like::dsl::*; use schema::schema::comment_like::dsl::*;
comment_like comment_like
.filter(comment_id.eq(comment_id_from)) .filter(comment_id.eq(comment_id_from))
.load::<Self>(conn) .load::<Self>(conn)
} }
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> { fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use crate::schema::comment_like::dsl::*; use schema::schema::comment_like::dsl::*;
insert_into(comment_like) insert_into(comment_like)
.values(comment_like_form) .values(comment_like_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn remove(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<usize, Error> { fn remove(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<usize, Error> {
use crate::schema::comment_like::dsl::*; use schema::schema::comment_like::dsl::*;
diesel::delete( diesel::delete(
comment_like comment_like
.filter(comment_id.eq(comment_like_form.comment_id)) .filter(comment_id.eq(comment_like_form.comment_id))
@ -116,7 +116,7 @@ impl Likeable<CommentLikeForm> for CommentLike {
impl CommentLike { impl CommentLike {
pub fn from_post(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> { pub fn from_post(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
use crate::schema::comment_like::dsl::*; use schema::schema::comment_like::dsl::*;
comment_like comment_like
.filter(post_id.eq(post_id_from)) .filter(post_id.eq(post_id_from))
.load::<Self>(conn) .load::<Self>(conn)
@ -142,13 +142,13 @@ pub struct CommentSavedForm {
impl Saveable<CommentSavedForm> for CommentSaved { impl Saveable<CommentSavedForm> for CommentSaved {
fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> { fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<Self, Error> {
use crate::schema::comment_saved::dsl::*; use schema::schema::comment_saved::dsl::*;
insert_into(comment_saved) insert_into(comment_saved)
.values(comment_saved_form) .values(comment_saved_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> { fn unsave(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result<usize, Error> {
use crate::schema::comment_saved::dsl::*; use schema::schema::comment_saved::dsl::*;
diesel::delete( diesel::delete(
comment_saved comment_saved
.filter(comment_id.eq(comment_saved_form.comment_id)) .filter(comment_id.eq(comment_saved_form.comment_id))

View File

@ -1,5 +1,6 @@
use super::*; use super::*;
use crate::schema::{community, community_follower, community_moderator, community_user_ban}; use schema::schema::{community, community_follower, community_moderator, community_user_ban};
use utils::settings::Settings;
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name = "community"] #[table_name = "community"]
@ -33,17 +34,17 @@ pub struct CommunityForm {
impl Crud<CommunityForm> for Community { impl Crud<CommunityForm> for Community {
fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
use crate::schema::community::dsl::*; use schema::schema::community::dsl::*;
community.find(community_id).first::<Self>(conn) community.find(community_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
use crate::schema::community::dsl::*; use schema::schema::community::dsl::*;
diesel::delete(community.find(community_id)).execute(conn) diesel::delete(community.find(community_id)).execute(conn)
} }
fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, new_community: &CommunityForm) -> Result<Self, Error> {
use crate::schema::community::dsl::*; use schema::schema::community::dsl::*;
insert_into(community) insert_into(community)
.values(new_community) .values(new_community)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -54,7 +55,7 @@ impl Crud<CommunityForm> for Community {
community_id: i32, community_id: i32,
new_community: &CommunityForm, new_community: &CommunityForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community::dsl::*; use schema::schema::community::dsl::*;
diesel::update(community.find(community_id)) diesel::update(community.find(community_id))
.set(new_community) .set(new_community)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -63,7 +64,7 @@ impl Crud<CommunityForm> for Community {
impl Community { impl Community {
pub fn read_from_name(conn: &PgConnection, community_name: String) -> Result<Self, Error> { pub fn read_from_name(conn: &PgConnection, community_name: String) -> Result<Self, Error> {
use crate::schema::community::dsl::*; use schema::schema::community::dsl::*;
community community
.filter(name.eq(community_name)) .filter(name.eq(community_name))
.first::<Self>(conn) .first::<Self>(conn)
@ -96,7 +97,7 @@ impl Joinable<CommunityModeratorForm> for CommunityModerator {
conn: &PgConnection, conn: &PgConnection,
community_user_form: &CommunityModeratorForm, community_user_form: &CommunityModeratorForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community_moderator::dsl::*; use schema::schema::community_moderator::dsl::*;
insert_into(community_moderator) insert_into(community_moderator)
.values(community_user_form) .values(community_user_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -106,7 +107,7 @@ impl Joinable<CommunityModeratorForm> for CommunityModerator {
conn: &PgConnection, conn: &PgConnection,
community_user_form: &CommunityModeratorForm, community_user_form: &CommunityModeratorForm,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::*; use schema::schema::community_moderator::dsl::*;
diesel::delete( diesel::delete(
community_moderator community_moderator
.filter(community_id.eq(community_user_form.community_id)) .filter(community_id.eq(community_user_form.community_id))
@ -118,7 +119,7 @@ impl Joinable<CommunityModeratorForm> for CommunityModerator {
impl CommunityModerator { impl CommunityModerator {
pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> { pub fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
use crate::schema::community_moderator::dsl::*; use schema::schema::community_moderator::dsl::*;
diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn) diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
} }
} }
@ -145,7 +146,7 @@ impl Bannable<CommunityUserBanForm> for CommunityUserBan {
conn: &PgConnection, conn: &PgConnection,
community_user_ban_form: &CommunityUserBanForm, community_user_ban_form: &CommunityUserBanForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community_user_ban::dsl::*; use schema::schema::community_user_ban::dsl::*;
insert_into(community_user_ban) insert_into(community_user_ban)
.values(community_user_ban_form) .values(community_user_ban_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -155,7 +156,7 @@ impl Bannable<CommunityUserBanForm> for CommunityUserBan {
conn: &PgConnection, conn: &PgConnection,
community_user_ban_form: &CommunityUserBanForm, community_user_ban_form: &CommunityUserBanForm,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
use crate::schema::community_user_ban::dsl::*; use schema::schema::community_user_ban::dsl::*;
diesel::delete( diesel::delete(
community_user_ban community_user_ban
.filter(community_id.eq(community_user_ban_form.community_id)) .filter(community_id.eq(community_user_ban_form.community_id))
@ -187,7 +188,7 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
conn: &PgConnection, conn: &PgConnection,
community_follower_form: &CommunityFollowerForm, community_follower_form: &CommunityFollowerForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::community_follower::dsl::*; use schema::schema::community_follower::dsl::*;
insert_into(community_follower) insert_into(community_follower)
.values(community_follower_form) .values(community_follower_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -196,7 +197,7 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
conn: &PgConnection, conn: &PgConnection,
community_follower_form: &CommunityFollowerForm, community_follower_form: &CommunityFollowerForm,
) -> Result<usize, Error> { ) -> Result<usize, Error> {
use crate::schema::community_follower::dsl::*; use schema::schema::community_follower::dsl::*;
diesel::delete( diesel::delete(
community_follower community_follower
.filter(community_id.eq(&community_follower_form.community_id)) .filter(community_id.eq(&community_follower_form.community_id))

View File

@ -1,4 +1,11 @@
use crate::settings::Settings; #[macro_use]
pub extern crate diesel;
#[macro_use]
pub extern crate strum_macros;
#[macro_use]
extern crate diesel_migrations;
use utils::settings::Settings;
use diesel::dsl::*; use diesel::dsl::*;
use diesel::result::Error; use diesel::result::Error;
use diesel::*; use diesel::*;
@ -23,6 +30,10 @@ pub mod user_mention;
pub mod user_mention_view; pub mod user_mention_view;
pub mod user_view; pub mod user_view;
// TODO: need to execute this
embed_migrations!();
//embedded_migrations::run(&conn).unwrap();
pub trait Crud<T> { pub trait Crud<T> {
fn create(conn: &PgConnection, form: &T) -> Result<Self, Error> fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
where where

View File

@ -1,5 +1,5 @@
use super::*; use super::*;
use crate::schema::{ use schema::schema::{
mod_add, mod_add_community, mod_ban, mod_ban_from_community, mod_lock_post, mod_remove_comment, mod_add, mod_add_community, mod_ban, mod_ban_from_community, mod_lock_post, mod_remove_comment,
mod_remove_community, mod_remove_post, mod_sticky_post, mod_remove_community, mod_remove_post, mod_sticky_post,
}; };
@ -26,24 +26,24 @@ pub struct ModRemovePostForm {
impl Crud<ModRemovePostForm> for ModRemovePost { impl Crud<ModRemovePostForm> for ModRemovePost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::*; use schema::schema::mod_remove_post::dsl::*;
mod_remove_post.find(from_id).first::<Self>(conn) mod_remove_post.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_remove_post::dsl::*; use schema::schema::mod_remove_post::dsl::*;
diesel::delete(mod_remove_post.find(from_id)).execute(conn) diesel::delete(mod_remove_post.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModRemovePostForm) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::*; use schema::schema::mod_remove_post::dsl::*;
insert_into(mod_remove_post) insert_into(mod_remove_post)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> { fn update(conn: &PgConnection, from_id: i32, form: &ModRemovePostForm) -> Result<Self, Error> {
use crate::schema::mod_remove_post::dsl::*; use schema::schema::mod_remove_post::dsl::*;
diesel::update(mod_remove_post.find(from_id)) diesel::update(mod_remove_post.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -70,24 +70,24 @@ pub struct ModLockPostForm {
impl Crud<ModLockPostForm> for ModLockPost { impl Crud<ModLockPostForm> for ModLockPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::*; use schema::schema::mod_lock_post::dsl::*;
mod_lock_post.find(from_id).first::<Self>(conn) mod_lock_post.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_lock_post::dsl::*; use schema::schema::mod_lock_post::dsl::*;
diesel::delete(mod_lock_post.find(from_id)).execute(conn) diesel::delete(mod_lock_post.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModLockPostForm) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::*; use schema::schema::mod_lock_post::dsl::*;
insert_into(mod_lock_post) insert_into(mod_lock_post)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> { fn update(conn: &PgConnection, from_id: i32, form: &ModLockPostForm) -> Result<Self, Error> {
use crate::schema::mod_lock_post::dsl::*; use schema::schema::mod_lock_post::dsl::*;
diesel::update(mod_lock_post.find(from_id)) diesel::update(mod_lock_post.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -114,24 +114,24 @@ pub struct ModStickyPostForm {
impl Crud<ModStickyPostForm> for ModStickyPost { impl Crud<ModStickyPostForm> for ModStickyPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_sticky_post::dsl::*; use schema::schema::mod_sticky_post::dsl::*;
mod_sticky_post.find(from_id).first::<Self>(conn) mod_sticky_post.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_sticky_post::dsl::*; use schema::schema::mod_sticky_post::dsl::*;
diesel::delete(mod_sticky_post.find(from_id)).execute(conn) diesel::delete(mod_sticky_post.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModStickyPostForm) -> Result<Self, Error> {
use crate::schema::mod_sticky_post::dsl::*; use schema::schema::mod_sticky_post::dsl::*;
insert_into(mod_sticky_post) insert_into(mod_sticky_post)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> { fn update(conn: &PgConnection, from_id: i32, form: &ModStickyPostForm) -> Result<Self, Error> {
use crate::schema::mod_sticky_post::dsl::*; use schema::schema::mod_sticky_post::dsl::*;
diesel::update(mod_sticky_post.find(from_id)) diesel::update(mod_sticky_post.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -160,24 +160,24 @@ pub struct ModRemoveCommentForm {
impl Crud<ModRemoveCommentForm> for ModRemoveComment { impl Crud<ModRemoveCommentForm> for ModRemoveComment {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::*; use schema::schema::mod_remove_comment::dsl::*;
mod_remove_comment.find(from_id).first::<Self>(conn) mod_remove_comment.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_remove_comment::dsl::*; use schema::schema::mod_remove_comment::dsl::*;
diesel::delete(mod_remove_comment.find(from_id)).execute(conn) diesel::delete(mod_remove_comment.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModRemoveCommentForm) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::*; use schema::schema::mod_remove_comment::dsl::*;
insert_into(mod_remove_comment) insert_into(mod_remove_comment)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> { fn update(conn: &PgConnection, from_id: i32, form: &ModRemoveCommentForm) -> Result<Self, Error> {
use crate::schema::mod_remove_comment::dsl::*; use schema::schema::mod_remove_comment::dsl::*;
diesel::update(mod_remove_comment.find(from_id)) diesel::update(mod_remove_comment.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -208,17 +208,17 @@ pub struct ModRemoveCommunityForm {
impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity { impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*; use schema::schema::mod_remove_community::dsl::*;
mod_remove_community.find(from_id).first::<Self>(conn) mod_remove_community.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_remove_community::dsl::*; use schema::schema::mod_remove_community::dsl::*;
diesel::delete(mod_remove_community.find(from_id)).execute(conn) diesel::delete(mod_remove_community.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModRemoveCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*; use schema::schema::mod_remove_community::dsl::*;
insert_into(mod_remove_community) insert_into(mod_remove_community)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -229,7 +229,7 @@ impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
from_id: i32, from_id: i32,
form: &ModRemoveCommunityForm, form: &ModRemoveCommunityForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::mod_remove_community::dsl::*; use schema::schema::mod_remove_community::dsl::*;
diesel::update(mod_remove_community.find(from_id)) diesel::update(mod_remove_community.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -262,17 +262,17 @@ pub struct ModBanFromCommunityForm {
impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity { impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::*; use schema::schema::mod_ban_from_community::dsl::*;
mod_ban_from_community.find(from_id).first::<Self>(conn) mod_ban_from_community.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_ban_from_community::dsl::*; use schema::schema::mod_ban_from_community::dsl::*;
diesel::delete(mod_ban_from_community.find(from_id)).execute(conn) diesel::delete(mod_ban_from_community.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModBanFromCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::*; use schema::schema::mod_ban_from_community::dsl::*;
insert_into(mod_ban_from_community) insert_into(mod_ban_from_community)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -283,7 +283,7 @@ impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
from_id: i32, from_id: i32,
form: &ModBanFromCommunityForm, form: &ModBanFromCommunityForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::mod_ban_from_community::dsl::*; use schema::schema::mod_ban_from_community::dsl::*;
diesel::update(mod_ban_from_community.find(from_id)) diesel::update(mod_ban_from_community.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -314,22 +314,22 @@ pub struct ModBanForm {
impl Crud<ModBanForm> for ModBan { impl Crud<ModBanForm> for ModBan {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*; use schema::schema::mod_ban::dsl::*;
mod_ban.find(from_id).first::<Self>(conn) mod_ban.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_ban::dsl::*; use schema::schema::mod_ban::dsl::*;
diesel::delete(mod_ban.find(from_id)).execute(conn) diesel::delete(mod_ban.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*; use schema::schema::mod_ban::dsl::*;
insert_into(mod_ban).values(form).get_result::<Self>(conn) insert_into(mod_ban).values(form).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> { fn update(conn: &PgConnection, from_id: i32, form: &ModBanForm) -> Result<Self, Error> {
use crate::schema::mod_ban::dsl::*; use schema::schema::mod_ban::dsl::*;
diesel::update(mod_ban.find(from_id)) diesel::update(mod_ban.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -358,24 +358,24 @@ pub struct ModAddCommunityForm {
impl Crud<ModAddCommunityForm> for ModAddCommunity { impl Crud<ModAddCommunityForm> for ModAddCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::*; use schema::schema::mod_add_community::dsl::*;
mod_add_community.find(from_id).first::<Self>(conn) mod_add_community.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_add_community::dsl::*; use schema::schema::mod_add_community::dsl::*;
diesel::delete(mod_add_community.find(from_id)).execute(conn) diesel::delete(mod_add_community.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModAddCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::*; use schema::schema::mod_add_community::dsl::*;
insert_into(mod_add_community) insert_into(mod_add_community)
.values(form) .values(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> { fn update(conn: &PgConnection, from_id: i32, form: &ModAddCommunityForm) -> Result<Self, Error> {
use crate::schema::mod_add_community::dsl::*; use schema::schema::mod_add_community::dsl::*;
diesel::update(mod_add_community.find(from_id)) diesel::update(mod_add_community.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -402,22 +402,22 @@ pub struct ModAddForm {
impl Crud<ModAddForm> for ModAdd { impl Crud<ModAddForm> for ModAdd {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*; use schema::schema::mod_add::dsl::*;
mod_add.find(from_id).first::<Self>(conn) mod_add.find(from_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, from_id: i32) -> Result<usize, Error> {
use crate::schema::mod_add::dsl::*; use schema::schema::mod_add::dsl::*;
diesel::delete(mod_add.find(from_id)).execute(conn) diesel::delete(mod_add.find(from_id)).execute(conn)
} }
fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> { fn create(conn: &PgConnection, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*; use schema::schema::mod_add::dsl::*;
insert_into(mod_add).values(form).get_result::<Self>(conn) insert_into(mod_add).values(form).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> { fn update(conn: &PgConnection, from_id: i32, form: &ModAddForm) -> Result<Self, Error> {
use crate::schema::mod_add::dsl::*; use schema::schema::mod_add::dsl::*;
diesel::update(mod_add.find(from_id)) diesel::update(mod_add.find(from_id))
.set(form) .set(form)
.get_result::<Self>(conn) .get_result::<Self>(conn)

View File

@ -1,6 +1,6 @@
use super::*; use super::*;
use crate::schema::password_reset_request; use schema::schema::password_reset_request;
use crate::schema::password_reset_request::dsl::*; use schema::schema::password_reset_request::dsl::*;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
#[derive(Queryable, Identifiable, PartialEq, Debug)] #[derive(Queryable, Identifiable, PartialEq, Debug)]
@ -21,7 +21,7 @@ pub struct PasswordResetRequestForm {
impl Crud<PasswordResetRequestForm> for PasswordResetRequest { impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
use crate::schema::password_reset_request::dsl::*; use schema::schema::password_reset_request::dsl::*;
password_reset_request password_reset_request
.find(password_reset_request_id) .find(password_reset_request_id)
.first::<Self>(conn) .first::<Self>(conn)

View File

@ -1,5 +1,5 @@
use super::*; use super::*;
use crate::schema::{post, post_like, post_read, post_saved}; use schema::schema::{post, post_like, post_read, post_saved};
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name = "post"] #[table_name = "post"]
@ -45,22 +45,22 @@ pub struct PostForm {
impl Crud<PostForm> for Post { impl Crud<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*; use schema::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn) post.find(post_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
use crate::schema::post::dsl::*; use schema::schema::post::dsl::*;
diesel::delete(post.find(post_id)).execute(conn) diesel::delete(post.find(post_id)).execute(conn)
} }
fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> { fn create(conn: &PgConnection, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*; use schema::schema::post::dsl::*;
insert_into(post).values(new_post).get_result::<Self>(conn) insert_into(post).values(new_post).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> { fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
use crate::schema::post::dsl::*; use schema::schema::post::dsl::*;
diesel::update(post.find(post_id)) diesel::update(post.find(post_id))
.set(new_post) .set(new_post)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -88,19 +88,19 @@ pub struct PostLikeForm {
impl Likeable<PostLikeForm> for PostLike { impl Likeable<PostLikeForm> for PostLike {
fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> { fn read(conn: &PgConnection, post_id_from: i32) -> Result<Vec<Self>, Error> {
use crate::schema::post_like::dsl::*; use schema::schema::post_like::dsl::*;
post_like post_like
.filter(post_id.eq(post_id_from)) .filter(post_id.eq(post_id_from))
.load::<Self>(conn) .load::<Self>(conn)
} }
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> { fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use crate::schema::post_like::dsl::*; use schema::schema::post_like::dsl::*;
insert_into(post_like) insert_into(post_like)
.values(post_like_form) .values(post_like_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> { fn remove(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<usize, Error> {
use crate::schema::post_like::dsl::*; use schema::schema::post_like::dsl::*;
diesel::delete( diesel::delete(
post_like post_like
.filter(post_id.eq(post_like_form.post_id)) .filter(post_id.eq(post_like_form.post_id))
@ -129,13 +129,13 @@ pub struct PostSavedForm {
impl Saveable<PostSavedForm> for PostSaved { impl Saveable<PostSavedForm> for PostSaved {
fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> { fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<Self, Error> {
use crate::schema::post_saved::dsl::*; use schema::schema::post_saved::dsl::*;
insert_into(post_saved) insert_into(post_saved)
.values(post_saved_form) .values(post_saved_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> { fn unsave(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result<usize, Error> {
use crate::schema::post_saved::dsl::*; use schema::schema::post_saved::dsl::*;
diesel::delete( diesel::delete(
post_saved post_saved
.filter(post_id.eq(post_saved_form.post_id)) .filter(post_id.eq(post_saved_form.post_id))
@ -164,13 +164,13 @@ pub struct PostReadForm {
impl Readable<PostReadForm> for PostRead { impl Readable<PostReadForm> for PostRead {
fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> { fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<Self, Error> {
use crate::schema::post_read::dsl::*; use schema::schema::post_read::dsl::*;
insert_into(post_read) insert_into(post_read)
.values(post_read_form) .values(post_read_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
} }
fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> { fn mark_as_unread(conn: &PgConnection, post_read_form: &PostReadForm) -> Result<usize, Error> {
use crate::schema::post_read::dsl::*; use schema::schema::post_read::dsl::*;
diesel::delete( diesel::delete(
post_read post_read
.filter(post_id.eq(post_read_form.post_id)) .filter(post_id.eq(post_read_form.post_id))

View File

@ -1,5 +1,5 @@
use super::*; use super::*;
use crate::schema::private_message; use schema::schema::private_message;
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name = "private_message"] #[table_name = "private_message"]
@ -27,17 +27,17 @@ pub struct PrivateMessageForm {
impl Crud<PrivateMessageForm> for PrivateMessage { impl Crud<PrivateMessageForm> for PrivateMessage {
fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*; use schema::schema::private_message::dsl::*;
private_message.find(private_message_id).first::<Self>(conn) private_message.find(private_message_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, private_message_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, private_message_id: i32) -> Result<usize, Error> {
use crate::schema::private_message::dsl::*; use schema::schema::private_message::dsl::*;
diesel::delete(private_message.find(private_message_id)).execute(conn) diesel::delete(private_message.find(private_message_id)).execute(conn)
} }
fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> { fn create(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*; use schema::schema::private_message::dsl::*;
insert_into(private_message) insert_into(private_message)
.values(private_message_form) .values(private_message_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -48,7 +48,7 @@ impl Crud<PrivateMessageForm> for PrivateMessage {
private_message_id: i32, private_message_id: i32,
private_message_form: &PrivateMessageForm, private_message_form: &PrivateMessageForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::private_message::dsl::*; use schema::schema::private_message::dsl::*;
diesel::update(private_message.find(private_message_id)) diesel::update(private_message.find(private_message_id))
.set(private_message_form) .set(private_message_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)

View File

@ -1,5 +1,5 @@
use super::*; use super::*;
use crate::schema::site; use schema::schema::site;
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[table_name = "site"] #[table_name = "site"]
@ -29,22 +29,22 @@ pub struct SiteForm {
impl Crud<SiteForm> for Site { impl Crud<SiteForm> for Site {
fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
use crate::schema::site::dsl::*; use schema::schema::site::dsl::*;
site.first::<Self>(conn) site.first::<Self>(conn)
} }
fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, site_id: i32) -> Result<usize, Error> {
use crate::schema::site::dsl::*; use schema::schema::site::dsl::*;
diesel::delete(site.find(site_id)).execute(conn) diesel::delete(site.find(site_id)).execute(conn)
} }
fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> { fn create(conn: &PgConnection, new_site: &SiteForm) -> Result<Self, Error> {
use crate::schema::site::dsl::*; use schema::schema::site::dsl::*;
insert_into(site).values(new_site).get_result::<Self>(conn) insert_into(site).values(new_site).get_result::<Self>(conn)
} }
fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> { fn update(conn: &PgConnection, site_id: i32, new_site: &SiteForm) -> Result<Self, Error> {
use crate::schema::site::dsl::*; use schema::schema::site::dsl::*;
diesel::update(site.find(site_id)) diesel::update(site.find(site_id))
.set(new_site) .set(new_site)
.get_result::<Self>(conn) .get_result::<Self>(conn)

View File

@ -1,7 +1,7 @@
use super::*; use super::*;
use crate::schema::user_; use schema::schema::user_;
use crate::schema::user_::dsl::*; use schema::schema::user_::dsl::*;
use crate::{is_email_regex, Settings}; use utils::{is_email_regex, settings::Settings};
use bcrypt::{hash, DEFAULT_COST}; use bcrypt::{hash, DEFAULT_COST};
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation}; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation};

View File

@ -1,6 +1,6 @@
use super::comment::Comment; use super::comment::Comment;
use super::*; use super::*;
use crate::schema::user_mention; use schema::schema::user_mention;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)] #[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Deserialize)]
#[belongs_to(Comment)] #[belongs_to(Comment)]
@ -23,17 +23,17 @@ pub struct UserMentionForm {
impl Crud<UserMentionForm> for UserMention { impl Crud<UserMentionForm> for UserMention {
fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> { fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
use crate::schema::user_mention::dsl::*; use schema::schema::user_mention::dsl::*;
user_mention.find(user_mention_id).first::<Self>(conn) user_mention.find(user_mention_id).first::<Self>(conn)
} }
fn delete(conn: &PgConnection, user_mention_id: i32) -> Result<usize, Error> { fn delete(conn: &PgConnection, user_mention_id: i32) -> Result<usize, Error> {
use crate::schema::user_mention::dsl::*; use schema::schema::user_mention::dsl::*;
diesel::delete(user_mention.find(user_mention_id)).execute(conn) diesel::delete(user_mention.find(user_mention_id)).execute(conn)
} }
fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> { fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
use crate::schema::user_mention::dsl::*; use schema::schema::user_mention::dsl::*;
insert_into(user_mention) insert_into(user_mention)
.values(user_mention_form) .values(user_mention_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)
@ -44,7 +44,7 @@ impl Crud<UserMentionForm> for UserMention {
user_mention_id: i32, user_mention_id: i32,
user_mention_form: &UserMentionForm, user_mention_form: &UserMentionForm,
) -> Result<Self, Error> { ) -> Result<Self, Error> {
use crate::schema::user_mention::dsl::*; use schema::schema::user_mention::dsl::*;
diesel::update(user_mention.find(user_mention_id)) diesel::update(user_mention.find(user_mention_id))
.set(user_mention_form) .set(user_mention_form)
.get_result::<Self>(conn) .get_result::<Self>(conn)

38
server/lemmy_server/Cargo.toml vendored Normal file
View File

@ -0,0 +1,38 @@
[package]
name = "lemmy_server"
version = "0.0.1"
authors = ["Dessalines <tyhou13@gmx.com>"]
edition = "2018"
[dependencies]
utils = { path = "../utils" }
db = { path = "../db" }
r2d2 = { version = ">= 0.8, < 0.9" }
chrono = { version = "0.4.7", features = ["serde"] }
serde_json = { version = "1.0.52", features = ["preserve_order"]}
serde = { version = "1.0.105", features = ["derive"] }
strum = "0.18.0"
strum_macros = "0.18.0"
dotenv = "0.15.0"
activitypub = "0.2.0"
failure = "0.1.8"
actix = "0.9.0"
actix-web = "2.0.0"
actix-files = "0.2.1"
actix-web-actors = "2.0.0"
actix-rt = "1.1.1"
log = "0.4.0"
env_logger = "0.7.1"
rand = "0.7.3"
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"
percent-encoding = "2.1.0"
isahc = "0.9"
comrak = "0.7"
tokio = "0.2.20"
futures = "0.3.4"
bcrypt = "0.7.0"

View File

@ -1,28 +1,28 @@
use crate::db::category::*; use db::category::*;
use crate::db::comment::*; use db::comment::*;
use crate::db::comment_view::*; use db::comment_view::*;
use crate::db::community::*; use db::community::*;
use crate::db::community_view::*; use db::community_view::*;
use crate::db::moderator::*; use db::moderator::*;
use crate::db::moderator_views::*; use db::moderator_views::*;
use crate::db::password_reset_request::*; use db::password_reset_request::*;
use crate::db::post::*; use db::post::*;
use crate::db::post_view::*; use db::post_view::*;
use crate::db::private_message::*; use db::private_message::*;
use crate::db::private_message_view::*; use db::private_message_view::*;
use crate::db::site::*; use db::site::*;
use crate::db::site_view::*; use db::site_view::*;
use crate::db::user::*; use db::user::*;
use crate::db::user_mention::*; use db::user_mention::*;
use crate::db::user_mention_view::*; use db::user_mention_view::*;
use crate::db::user_view::*; use db::user_view::*;
use crate::db::*; use db::*;
use crate::{ use crate::{
extract_usernames, fetch_iframely_and_pictshare_data, generate_random_string, naive_from_unix, extract_usernames, fetch_iframely_and_pictshare_data, generate_random_string, naive_from_unix,
naive_now, remove_slurs, send_email, slur_check, slurs_vec_to_str, naive_now, remove_slurs, send_email, slur_check, slurs_vec_to_str,
}; };
use crate::settings::Settings; use utils::settings::Settings;
use crate::websocket::UserOperation; use crate::websocket::UserOperation;
use crate::websocket::{ use crate::websocket::{
server::{ server::{

View File

@ -5,15 +5,13 @@ pub extern crate strum_macros;
pub extern crate lazy_static; pub extern crate lazy_static;
#[macro_use] #[macro_use]
pub extern crate failure; pub extern crate failure;
#[macro_use]
pub extern crate diesel;
pub extern crate actix; pub extern crate actix;
pub extern crate actix_web; pub extern crate actix_web;
pub extern crate bcrypt; //pub extern crate bcrypt;
pub extern crate chrono; pub extern crate chrono;
pub extern crate comrak; pub extern crate comrak;
pub extern crate dotenv; pub extern crate dotenv;
pub extern crate jsonwebtoken; //pub extern crate jsonwebtoken;
pub extern crate lettre; pub extern crate lettre;
pub extern crate lettre_email; pub extern crate lettre_email;
pub extern crate rand; pub extern crate rand;
@ -21,16 +19,10 @@ pub extern crate regex;
pub extern crate rss; pub extern crate rss;
pub extern crate serde; pub extern crate serde;
pub extern crate serde_json; pub extern crate serde_json;
pub extern crate sha2;
pub extern crate strum;
pub mod api; pub mod api;
pub mod apub;
pub mod db;
pub mod rate_limit; pub mod rate_limit;
pub mod routes; pub mod routes;
pub mod schema;
pub mod settings;
pub mod version; pub mod version;
pub mod websocket; pub mod websocket;
@ -49,7 +41,7 @@ use rand::{thread_rng, Rng};
use regex::{Regex, RegexBuilder}; use regex::{Regex, RegexBuilder};
use serde::Deserialize; use serde::Deserialize;
use crate::settings::Settings; use utils::settings::Settings;
pub type ConnectionId = usize; pub type ConnectionId = usize;
pub type PostId = i32; pub type PostId = i32;
@ -57,6 +49,49 @@ pub type CommunityId = i32;
pub type UserId = i32; pub type UserId = i32;
pub type IPAddr = String; pub type IPAddr = String;
pub fn send_email(
subject: &str,
to_email: &str,
to_username: &str,
html: &str,
) -> Result<(), String> {
let email_config = Settings::get().email.ok_or("no_email_setup")?;
let email = Email::builder()
.to((to_email, to_username))
.from(email_config.smtp_from_address.to_owned())
.subject(subject)
.html(html)
.build()
.unwrap();
let mailer = if email_config.use_tls {
SmtpClient::new_simple(&email_config.smtp_server).unwrap()
} else {
SmtpClient::new(&email_config.smtp_server, ClientSecurity::None).unwrap()
}
.hello_name(ClientId::Domain(Settings::get().hostname))
.smtp_utf8(true)
.authentication_mechanism(Mechanism::Plain)
.connection_reuse(ConnectionReuseParameters::ReuseUnlimited);
let mailer = if let (Some(login), Some(password)) =
(&email_config.smtp_login, &email_config.smtp_password)
{
mailer.credentials(Credentials::new(login.to_owned(), password.to_owned()))
} else {
mailer
};
let mut transport = mailer.transport();
let result = transport.send(email.into());
transport.close();
match result {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
}
pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> { pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
DateTime::<Utc>::from_utc(ndt, Utc) DateTime::<Utc>::from_utc(ndt, Utc)
} }
@ -69,10 +104,6 @@ pub fn naive_from_unix(time: i64) -> NaiveDateTime {
NaiveDateTime::from_timestamp(time, 0) NaiveDateTime::from_timestamp(time, 0)
} }
pub fn is_email_regex(test: &str) -> bool {
EMAIL_REGEX.is_match(test)
}
pub fn is_image_content_type(test: &str) -> Result<(), failure::Error> { pub fn is_image_content_type(test: &str) -> Result<(), failure::Error> {
if isahc::get(test)? if isahc::get(test)?
.headers() .headers()
@ -129,49 +160,6 @@ pub fn generate_random_string() -> String {
thread_rng().sample_iter(&Alphanumeric).take(30).collect() thread_rng().sample_iter(&Alphanumeric).take(30).collect()
} }
pub fn send_email(
subject: &str,
to_email: &str,
to_username: &str,
html: &str,
) -> Result<(), String> {
let email_config = Settings::get().email.ok_or("no_email_setup")?;
let email = Email::builder()
.to((to_email, to_username))
.from(email_config.smtp_from_address.to_owned())
.subject(subject)
.html(html)
.build()
.unwrap();
let mailer = if email_config.use_tls {
SmtpClient::new_simple(&email_config.smtp_server).unwrap()
} else {
SmtpClient::new(&email_config.smtp_server, ClientSecurity::None).unwrap()
}
.hello_name(ClientId::Domain(Settings::get().hostname))
.smtp_utf8(true)
.authentication_mechanism(Mechanism::Plain)
.connection_reuse(ConnectionReuseParameters::ReuseUnlimited);
let mailer = if let (Some(login), Some(password)) =
(&email_config.smtp_login, &email_config.smtp_password)
{
mailer.credentials(Credentials::new(login.to_owned(), password.to_owned()))
} else {
mailer
};
let mut transport = mailer.transport();
let result = transport.send(email.into());
transport.close();
match result {
Ok(_) => Ok(()),
Err(e) => Err(e.to_string()),
}
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct IframelyResponse { pub struct IframelyResponse {
title: Option<String>, title: Option<String>,
@ -362,7 +350,6 @@ mod tests {
} }
lazy_static! { lazy_static! {
static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
static ref SLUR_REGEX: Regex = RegexBuilder::new(r"(fag(g|got|tard)?|maricos?|cock\s?sucker(s|ing)?|nig(\b|g?(a|er)?(s|z)?)\b|dindu(s?)|mudslime?s?|kikes?|mongoloids?|towel\s*heads?|\bspi(c|k)s?\b|\bchinks?|niglets?|beaners?|\bnips?\b|\bcoons?\b|jungle\s*bunn(y|ies?)|jigg?aboo?s?|\bpakis?\b|rag\s*heads?|gooks?|cunts?|bitch(es|ing|y)?|puss(y|ies?)|twats?|feminazis?|whor(es?|ing)|\bslut(s|t?y)?|\btrann?(y|ies?)|ladyboy(s?)|\b(b|re|r)tard(ed)?s?)").case_insensitive(true).build().unwrap(); static ref SLUR_REGEX: Regex = RegexBuilder::new(r"(fag(g|got|tard)?|maricos?|cock\s?sucker(s|ing)?|nig(\b|g?(a|er)?(s|z)?)\b|dindu(s?)|mudslime?s?|kikes?|mongoloids?|towel\s*heads?|\bspi(c|k)s?\b|\bchinks?|niglets?|beaners?|\bnips?\b|\bcoons?\b|jungle\s*bunn(y|ies?)|jigg?aboo?s?|\bpakis?\b|rag\s*heads?|gooks?|cunts?|bitch(es|ing|y)?|puss(y|ies?)|twats?|feminazis?|whor(es?|ing)|\bslut(s|t?y)?|\btrann?(y|ies?)|ladyboy(s?)|\b(b|re|r)tard(ed)?s?)").case_insensitive(true).build().unwrap();
static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap(); static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap();
static ref VALID_USERNAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,20}$").unwrap(); static ref VALID_USERNAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,20}$").unwrap();

View File

@ -1,7 +1,5 @@
extern crate lemmy_server; extern crate lemmy_server;
#[macro_use] #[macro_use]
extern crate diesel_migrations;
#[macro_use]
pub extern crate lazy_static; pub extern crate lazy_static;
use crate::lemmy_server::actix_web::dev::Service; use crate::lemmy_server::actix_web::dev::Service;
@ -11,17 +9,18 @@ use actix_web::dev::{ServiceRequest, ServiceResponse};
use actix_web::http::header::CONTENT_TYPE; use actix_web::http::header::CONTENT_TYPE;
use actix_web::http::{header::CACHE_CONTROL, HeaderValue}; use actix_web::http::{header::CACHE_CONTROL, HeaderValue};
use actix_web::*; use actix_web::*;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
use lemmy_server::{ use lemmy_server::{
rate_limit::{rate_limiter::RateLimiter, RateLimit}, rate_limit::{rate_limiter::RateLimiter, RateLimit},
routes::{api, federation, feeds, index, nodeinfo, webfinger}, routes::{api, feeds, index, nodeinfo, webfinger},
settings::Settings,
websocket::server::*, websocket::server::*,
}; };
use utils::settings::Settings;
use regex::Regex; use regex::Regex;
use std::{io, sync::Arc}; use std::{io, sync::Arc};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use db::diesel::r2d2::ConnectionManager;
use r2d2::Pool;
use db::diesel::PgConnection;
lazy_static! { lazy_static! {
static ref CACHE_CONTROL_REGEX: Regex = static ref CACHE_CONTROL_REGEX: Regex =
@ -31,10 +30,9 @@ lazy_static! {
static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 60 * 60); static ref CACHE_CONTROL_VALUE: String = format!("public, max-age={}", 60 * 60);
} }
embed_migrations!();
#[actix_rt::main] #[actix_rt::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
println!("test");
env_logger::init(); env_logger::init();
let settings = Settings::get(); let settings = Settings::get();
@ -45,10 +43,6 @@ async fn main() -> io::Result<()> {
.build(manager) .build(manager)
.unwrap_or_else(|_| panic!("Error connecting to {}", settings.get_database_url())); .unwrap_or_else(|_| panic!("Error connecting to {}", settings.get_database_url()));
// Run the migrations from code
let conn = pool.get().unwrap();
embedded_migrations::run(&conn).unwrap();
// Set up the rate limiter // Set up the rate limiter
let rate_limiter = RateLimit { let rate_limiter = RateLimit {
rate_limiter: Arc::new(Mutex::new(RateLimiter::default())), rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
@ -73,7 +67,6 @@ async fn main() -> io::Result<()> {
.data(server.clone()) .data(server.clone())
// The routes // The routes
.configure(move |cfg| api::config(cfg, &rate_limiter)) .configure(move |cfg| api::config(cfg, &rate_limiter))
.configure(federation::config)
.configure(feeds::config) .configure(feeds::config)
.configure(index::config) .configure(index::config)
.configure(nodeinfo::config) .configure(nodeinfo::config)

View File

@ -1,9 +1,10 @@
pub mod rate_limiter; pub mod rate_limiter;
use super::{IPAddr, Settings}; use super::IPAddr;
use utils::settings::Settings;
use crate::api::APIError; use crate::api::APIError;
use crate::get_ip; use crate::get_ip;
use crate::settings::RateLimitConfig; use utils::settings::RateLimitConfig;
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
use failure::Error; use failure::Error;
use futures::future::{ok, Ready}; use futures::future::{ok, Ready};

View File

@ -1,11 +1,11 @@
use super::*; use super::*;
use crate::db::comment_view::{ReplyQueryBuilder, ReplyView}; use db::comment_view::{ReplyQueryBuilder, ReplyView};
use crate::db::community::Community; use db::community::Community;
use crate::db::post_view::{PostQueryBuilder, PostView}; use db::post_view::{PostQueryBuilder, PostView};
use crate::db::site_view::SiteView; use db::site_view::SiteView;
use crate::db::user::{Claims, User_}; use db::user::{Claims, User_};
use crate::db::user_mention_view::{UserMentionQueryBuilder, UserMentionView}; use db::user_mention_view::{UserMentionQueryBuilder, UserMentionView};
use crate::db::{ListingType, SortType}; use db::{ListingType, SortType};
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Params { pub struct Params {

View File

@ -1,5 +1,5 @@
use crate::api::{Oper, Perform}; use crate::api::{Oper, Perform};
use crate::db::site_view::SiteView; use db::site_view::SiteView;
use crate::rate_limit::rate_limiter::RateLimiter; use crate::rate_limit::rate_limiter::RateLimiter;
use crate::websocket::{server::ChatServer, WebsocketInfo}; use crate::websocket::{server::ChatServer, WebsocketInfo};
use crate::{get_ip, markdown_to_html, version, Settings}; use crate::{get_ip, markdown_to_html, version, Settings};
@ -8,10 +8,6 @@ use actix_files::NamedFile;
use actix_web::{body::Body, error::ErrorBadRequest, web::Query, *}; use actix_web::{body::Body, error::ErrorBadRequest, web::Query, *};
use actix_web_actors::ws; use actix_web_actors::ws;
use chrono::{DateTime, NaiveDateTime, Utc}; use chrono::{DateTime, NaiveDateTime, Utc};
use diesel::{
r2d2::{ConnectionManager, Pool},
PgConnection,
};
use log::{error, info}; use log::{error, info};
use regex::Regex; use regex::Regex;
use rss::{CategoryBuilder, ChannelBuilder, GuidBuilder, Item, ItemBuilder}; use rss::{CategoryBuilder, ChannelBuilder, GuidBuilder, Item, ItemBuilder};
@ -21,13 +17,15 @@ use std::str::FromStr;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use strum::ParseError; use strum::ParseError;
use db::diesel::r2d2::ConnectionManager;
use r2d2::Pool;
use db::diesel::PgConnection;
pub type DbPoolParam = web::Data<Pool<ConnectionManager<PgConnection>>>; pub type DbPoolParam = web::Data<Pool<ConnectionManager<PgConnection>>>;
pub type RateLimitParam = web::Data<Arc<Mutex<RateLimiter>>>; pub type RateLimitParam = web::Data<Arc<Mutex<RateLimiter>>>;
pub type ChatServerParam = web::Data<Addr<ChatServer>>; pub type ChatServerParam = web::Data<Addr<ChatServer>>;
pub mod api; pub mod api;
pub mod federation;
pub mod feeds; pub mod feeds;
pub mod index; pub mod index;
pub mod nodeinfo; pub mod nodeinfo;

View File

@ -1,5 +1,6 @@
use super::*; use super::*;
use crate::db::community::Community; use db::community::Community;
use db::diesel::r2d2::ConnectionManager;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct Params { pub struct Params {

View File

@ -2,8 +2,6 @@ pub mod server;
use crate::ConnectionId; use crate::ConnectionId;
use actix::prelude::*; use actix::prelude::*;
use diesel::r2d2::{ConnectionManager, Pool};
use diesel::PgConnection;
use failure::Error; use failure::Error;
use log::{error, info}; use log::{error, info};
use rand::{rngs::ThreadRng, Rng}; use rand::{rngs::ThreadRng, Rng};

View File

@ -12,6 +12,9 @@ use crate::api::*;
use crate::rate_limit::RateLimit; use crate::rate_limit::RateLimit;
use crate::websocket::UserOperation; use crate::websocket::UserOperation;
use crate::{CommunityId, ConnectionId, IPAddr, PostId, UserId}; use crate::{CommunityId, ConnectionId, IPAddr, PostId, UserId};
use r2d2::Pool;
use db::diesel::r2d2::ConnectionManager;
use db::diesel::PgConnection;
/// Chat server sends this messages to session /// Chat server sends this messages to session
#[derive(Message)] #[derive(Message)]

10
server/schema/Cargo.toml vendored Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "schema"
version = "0.1.0"
authors = ["Felix Ableitner <me@nutomic.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
diesel = { version = "1.4.2", features = ["postgres", "64-column-tables"] }

4
server/schema/src/lib.rs Normal file
View File

@ -0,0 +1,4 @@
#[macro_use]
pub extern crate diesel;
pub mod schema;

View File

@ -1,109 +0,0 @@
use crate::apub::make_apub_endpoint;
use crate::db::community::Community;
use crate::db::community_view::CommunityFollowerView;
use crate::db::establish_unpooled_connection;
use crate::to_datetime_utc;
use activitypub::{actor::Group, collection::UnorderedCollection, context};
use actix_web::body::Body;
use actix_web::web::Path;
use actix_web::HttpResponse;
use serde::Deserialize;
impl Community {
pub fn as_group(&self) -> Group {
let base_url = make_apub_endpoint("c", &self.name);
let mut group = Group::default();
group.object_props.set_context_object(context()).ok();
group.object_props.set_id_string(base_url.to_string()).ok();
group
.object_props
.set_name_string(self.name.to_owned())
.ok();
group
.object_props
.set_published_utctime(to_datetime_utc(self.published))
.ok();
if let Some(updated) = self.updated {
group
.object_props
.set_updated_utctime(to_datetime_utc(updated))
.ok();
}
if let Some(description) = &self.description {
group
.object_props
.set_summary_string(description.to_string())
.ok();
}
group
.ap_actor_props
.set_inbox_string(format!("{}/inbox", &base_url))
.ok();
group
.ap_actor_props
.set_outbox_string(format!("{}/outbox", &base_url))
.ok();
group
.ap_actor_props
.set_followers_string(format!("{}/followers", &base_url))
.ok();
group
}
pub fn followers_as_collection(&self) -> UnorderedCollection {
let base_url = make_apub_endpoint("c", &self.name);
let mut collection = UnorderedCollection::default();
collection.object_props.set_context_object(context()).ok();
collection.object_props.set_id_string(base_url).ok();
let connection = establish_unpooled_connection();
//As we are an object, we validated that the community id was valid
let community_followers = CommunityFollowerView::for_community(&connection, self.id).unwrap();
let ap_followers = community_followers
.iter()
.map(|follower| make_apub_endpoint("u", &follower.user_name))
.collect();
collection
.collection_props
.set_items_string_vec(ap_followers)
.unwrap();
collection
}
}
#[derive(Deserialize)]
pub struct CommunityQuery {
community_name: String,
}
pub async fn get_apub_community(info: Path<CommunityQuery>) -> HttpResponse<Body> {
let connection = establish_unpooled_connection();
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
HttpResponse::Ok()
.content_type("application/activity+json")
.body(serde_json::to_string(&community.as_group()).unwrap())
} else {
HttpResponse::NotFound().finish()
}
}
pub async fn get_apub_community_followers(info: Path<CommunityQuery>) -> HttpResponse<Body> {
let connection = establish_unpooled_connection();
if let Ok(community) = Community::read_from_name(&connection, info.community_name.to_owned()) {
HttpResponse::Ok()
.content_type("application/activity+json")
.body(serde_json::to_string(&community.followers_as_collection()).unwrap())
} else {
HttpResponse::NotFound().finish()
}
}

View File

@ -1,107 +0,0 @@
pub mod community;
pub mod post;
pub mod user;
use crate::Settings;
use std::fmt::Display;
#[cfg(test)]
mod tests {
use crate::db::community::Community;
use crate::db::post::Post;
use crate::db::user::User_;
use crate::db::{ListingType, SortType};
use crate::{naive_now, Settings};
#[test]
fn test_person() {
let user = User_ {
id: 52,
name: "thom".into(),
fedi_name: "rrf".into(),
preferred_username: None,
password_encrypted: "here".into(),
email: None,
matrix_user_id: None,
avatar: None,
published: naive_now(),
admin: false,
banned: false,
updated: None,
show_nsfw: false,
theme: "darkly".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
};
let person = user.as_person();
assert_eq!(
format!("https://{}/federation/u/thom", Settings::get().hostname),
person.object_props.id_string().unwrap()
);
}
#[test]
fn test_community() {
let community = Community {
id: 42,
name: "Test".into(),
title: "Test Title".into(),
description: Some("Test community".into()),
category_id: 32,
creator_id: 52,
removed: false,
published: naive_now(),
updated: Some(naive_now()),
deleted: false,
nsfw: false,
};
let group = community.as_group();
assert_eq!(
format!("https://{}/federation/c/Test", Settings::get().hostname),
group.object_props.id_string().unwrap()
);
}
#[test]
fn test_post() {
let post = Post {
id: 62,
name: "A test post".into(),
url: None,
body: None,
creator_id: 52,
community_id: 42,
published: naive_now(),
removed: false,
locked: false,
stickied: false,
nsfw: false,
deleted: false,
updated: None,
embed_title: None,
embed_description: None,
embed_html: None,
thumbnail_url: None,
};
let page = post.as_page();
assert_eq!(
format!("https://{}/federation/post/62", Settings::get().hostname),
page.object_props.id_string().unwrap()
);
}
}
pub fn make_apub_endpoint<S: Display, T: Display>(point: S, value: T) -> String {
format!(
"https://{}/federation/{}/{}",
Settings::get().hostname,
point,
value
)
}

View File

@ -1,38 +0,0 @@
use crate::apub::make_apub_endpoint;
use crate::db::post::Post;
use crate::to_datetime_utc;
use activitypub::{context, object::Page};
impl Post {
pub fn as_page(&self) -> Page {
let base_url = make_apub_endpoint("post", self.id);
let mut page = Page::default();
page.object_props.set_context_object(context()).ok();
page.object_props.set_id_string(base_url).ok();
page.object_props.set_name_string(self.name.to_owned()).ok();
if let Some(body) = &self.body {
page.object_props.set_content_string(body.to_owned()).ok();
}
if let Some(url) = &self.url {
page.object_props.set_url_string(url.to_owned()).ok();
}
//page.object_props.set_attributed_to_string
page
.object_props
.set_published_utctime(to_datetime_utc(self.published))
.ok();
if let Some(updated) = self.updated {
page
.object_props
.set_updated_utctime(to_datetime_utc(updated))
.ok();
}
page
}
}

View File

@ -1,74 +0,0 @@
use crate::apub::make_apub_endpoint;
use crate::db::establish_unpooled_connection;
use crate::db::user::User_;
use crate::to_datetime_utc;
use activitypub::{actor::Person, context};
use actix_web::body::Body;
use actix_web::web::Path;
use actix_web::HttpResponse;
use serde::Deserialize;
impl User_ {
pub fn as_person(&self) -> Person {
let base_url = make_apub_endpoint("u", &self.name);
let mut person = Person::default();
person.object_props.set_context_object(context()).ok();
person.object_props.set_id_string(base_url.to_string()).ok();
person
.object_props
.set_name_string(self.name.to_owned())
.ok();
person
.object_props
.set_published_utctime(to_datetime_utc(self.published))
.ok();
if let Some(updated) = self.updated {
person
.object_props
.set_updated_utctime(to_datetime_utc(updated))
.ok();
}
person
.ap_actor_props
.set_inbox_string(format!("{}/inbox", &base_url))
.ok();
person
.ap_actor_props
.set_outbox_string(format!("{}/outbox", &base_url))
.ok();
person
.ap_actor_props
.set_following_string(format!("{}/following", &base_url))
.ok();
person
.ap_actor_props
.set_liked_string(format!("{}/liked", &base_url))
.ok();
if let Some(i) = &self.preferred_username {
person
.ap_actor_props
.set_preferred_username_string(i.to_string())
.ok();
}
person
}
}
#[derive(Deserialize)]
pub struct UserQuery {
user_name: String,
}
pub async fn get_apub_user(info: Path<UserQuery>) -> HttpResponse<Body> {
let connection = establish_unpooled_connection();
if let Ok(user) = User_::find_by_email_or_username(&connection, &info.user_name) {
HttpResponse::Ok()
.content_type("application/activity+json")
.body(serde_json::to_string(&user.as_person()).unwrap())
} else {
HttpResponse::NotFound().finish()
}
}

View File

@ -1,18 +0,0 @@
use super::*;
use crate::apub;
pub fn config(cfg: &mut web::ServiceConfig) {
cfg
.route(
"/federation/c/{community_name}",
web::get().to(apub::community::get_apub_community),
)
.route(
"/federation/c/{community_name}/followers",
web::get().to(apub::community::get_apub_community_followers),
)
.route(
"/federation/u/{user_name}",
web::get().to(apub::user::get_apub_user),
);
}

16
server/utils/Cargo.toml vendored Normal file
View File

@ -0,0 +1,16 @@
[package]
name = "utils"
version = "0.1.0"
authors = ["Felix Ableitner <me@nutomic.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lazy_static = "1.3.0"
regex = "1.3.5"
serde = { version = "1.0.105", features = ["derive"] }
failure = "0.1.8"
config = "0.10.1"
hjson = "0.8.2"
chrono = { version = "0.4.7", features = ["serde"] }

14
server/utils/src/lib.rs Normal file
View File

@ -0,0 +1,14 @@
#[macro_use]
pub extern crate lazy_static;
use regex::{Regex, RegexBuilder};
use chrono::{NaiveDateTime, DateTime, Utc};
pub mod settings;
pub fn is_email_regex(test: &str) -> bool {
EMAIL_REGEX.is_match(test)
}
lazy_static! {
static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
}