gourami

[UNMAINTAINED] Activitypub server in Rust
Log | Files | Refs | README | LICENSE

commit 6f62f7c9afab82c423450ee85b83f81f3856039d
parent c56ea7fda972bb9b4310e0e10a7cdfe1f6bd7d66
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon, 15 Jun 2020 12:33:00 -0500

Run cargo fmt

Diffstat:
Msrc/db/invite.rs | 1-
Msrc/db/mod.rs | 2+-
Msrc/db/note.rs | 3++-
Msrc/db/user.rs | 10+++-------
Msrc/lib.rs | 36++++++++++++++++++++++++------------
Msrc/routes.rs | 48++++++++++++++++++++++++++++++++++--------------
6 files changed, 64 insertions(+), 36 deletions(-)

diff --git a/src/db/invite.rs b/src/db/invite.rs @@ -1,6 +1,5 @@ use super::schema::registration_keys; - #[derive(Insertable)] #[table_name = "registration_keys"] pub struct NewRegistrationKey { diff --git a/src/db/mod.rs b/src/db/mod.rs @@ -1,6 +1,6 @@ pub mod conn; +pub mod invite; pub mod note; pub mod schema; pub mod server_mutuals; -pub mod invite; pub mod user; // TODO singular diff --git a/src/db/note.rs b/src/db/note.rs @@ -194,7 +194,8 @@ mod tests { #[test] fn test_user_replace() { let src = "@joe whats up @sally"; - let linked = "<a href=\"?username=joe\">@joe</a> whats up <a href=\"?username=sally\">@sally</a>"; + let linked = + "<a href=\"?username=joe\">@joe</a> whats up <a href=\"?username=sally\">@sally</a>"; assert!(parse_note_text(src) == linked) } diff --git a/src/db/user.rs b/src/db/user.rs @@ -1,10 +1,10 @@ use super::schema::users; +use crate::ap::SERVER; use bcrypt; use diesel::prelude::*; use diesel::sqlite::SqliteConnection; use serde::Deserialize; use std::env; -use crate::ap::SERVER; #[derive(Debug, Clone, Default, Queryable, Deserialize)] pub struct RegistrationKey { @@ -47,7 +47,7 @@ pub struct User { pub id: i32, pub username: String, pub email: Option<String>, // TODO option - pub website: String, // TODO option + pub website: String, // TODO option pub bio: String, pub created_time: String, pub password: Option<String>, @@ -59,11 +59,7 @@ pub struct User { impl User { pub fn get_url(&self) -> String { - format!( - "{}/?user_id={}", - SERVER.global_id, - self.id - ) + format!("{}/?user_id={}", SERVER.global_id, self.id) // remote url? } pub fn authenticate(conn: &SqliteConnection, user: &str, pass: &str) -> Option<Self> { diff --git a/src/lib.rs b/src/lib.rs @@ -12,8 +12,8 @@ extern crate lazy_static; use serde_json::Value; use std::collections::BTreeMap; use std::convert::Infallible; -use zxcvbn::zxcvbn; use uuid::Uuid; +use zxcvbn::zxcvbn; use warp::filters::path::FullPath; use warp::http; @@ -23,11 +23,11 @@ use warp::{Filter, Rejection, Reply}; use askama::Template; pub use db::conn::POOL; +use db::invite::NewRegistrationKey; use db::note; use db::note::{Note, NoteInput}; use db::server_mutuals::ServerMutual; use db::user::{NewUser, RegistrationKey, User, UserNameId}; -use db::invite::{NewRegistrationKey}; use diesel::insert_into; use diesel::prelude::*; use hyper; @@ -63,7 +63,7 @@ struct TimelineTemplate<'a> { global: Global<'a>, notes: Vec<UserNote>, params: &'a GetPostsParams, - user: Option<User> + user: Option<User>, } #[derive(Template)] @@ -415,13 +415,16 @@ fn get_local_users() -> Result<Vec<User>, diesel::result::Error> { } /// We have to do a join here -fn get_notes(logged_in: bool, params: &GetPostsParams) -> Result<Vec<UserNote>, diesel::result::Error> { +fn get_notes( + logged_in: bool, + params: &GetPostsParams, +) -> Result<Vec<UserNote>, diesel::result::Error> { use db::schema::notes::dsl as n; use db::schema::users::dsl as u; // TODO -- add whether this is complete so i can page properly - if logged_in { + if logged_in { if let Some(n_id) = params.note_id { - return Ok(get_single_note(n_id).unwrap()) // TODO filter replies logged out + return Ok(get_single_note(n_id).unwrap()); // TODO filter replies logged out } } let mut query = n::notes @@ -479,11 +482,17 @@ fn render_timeline( // wonky use db::schema::users::dsl as u; let user = match params.user_id { - Some(u_id) => u::users.filter(u::id.eq(u_id)).first(&POOL.get().unwrap()).ok(), + Some(u_id) => u::users + .filter(u::id.eq(u_id)) + .first(&POOL.get().unwrap()) + .ok(), None => match &params.username { - Some(uname) => u::users.filter(u::username.eq(uname)).first(&POOL.get().unwrap()).ok(), + Some(uname) => u::users + .filter(u::username.eq(uname)) + .first(&POOL.get().unwrap()) + .ok(), None => None, - } + }, }; header.page_num = params.page; // TODO -- ignore neighborhood replies @@ -497,7 +506,7 @@ fn render_timeline( global: header, notes: n, user: user, - params: params + params: params, }) } _ => render_template(&ErrorTemplate { @@ -537,9 +546,12 @@ fn generate_invite(auth_user: Option<User>) -> impl Reply { inviting_user_id: u.id, value: invite_uuid.to_string(), }; - insert_into(registration_keys).values(new_reg_key).execute(&POOL.get().unwrap()).unwrap(); + insert_into(registration_keys) + .values(new_reg_key) + .execute(&POOL.get().unwrap()) + .unwrap(); let uri = format!("/register?key={}", invite_uuid).parse().unwrap(); - return redirect(uri) + return redirect(uri); } } redirect(http::Uri::from_static("/register")) // TODO better error diff --git a/src/routes.rs b/src/routes.rs @@ -64,27 +64,48 @@ pub async fn run_server() { .and(session_filter()) .and(query()) .and(path::full()) - .map(|user: Option<User>, params, url| render_timeline(user.clone(), &params, url, get_notes(user.is_some(), &params))); + .map(|user: Option<User>, params, url| { + render_timeline( + user.clone(), + &params, + url, + get_notes(user.is_some(), &params), + ) + }); // a bit awkward let user_alias = warp::path!("user" / String) .and(session_filter()) .and(query::<GetPostsParams>()) .and(path::full()) - .map(|username: String, user: Option<User>, mut params: GetPostsParams, url| { - params.username = Some(username); - render_timeline(user.clone(), &params, url, get_notes(user.is_some(), &params)) - }); + .map( + |username: String, user: Option<User>, mut params: GetPostsParams, url| { + params.username = Some(username); + render_timeline( + user.clone(), + &params, + url, + get_notes(user.is_some(), &params), + ) + }, + ); // a bit awkward let note_alias = warp::path!("note" / i32) .and(session_filter()) .and(query::<GetPostsParams>()) .and(path::full()) - .map(|note: i32, user: Option<User>, mut params: GetPostsParams, url| { - params.note_id = Some(note); - render_timeline(user.clone(), &params, url, get_notes(user.is_some(), &params)) - }); + .map( + |note: i32, user: Option<User>, mut params: GetPostsParams, url| { + params.note_id = Some(note); + render_timeline( + user.clone(), + &params, + url, + get_notes(user.is_some(), &params), + ) + }, + ); let user_edit_page = session_filter() .and(path!("user" / String / "edit")) @@ -121,17 +142,16 @@ pub async fn run_server() { .and(with_sender) .and_then(handle_new_note_form); - let delete_note = path("delete_note") - .and(session_filter()) - .and(form()) - .map(|u: Option<User>, f: DeleteNoteRequest| match u { + let delete_note = path("delete_note").and(session_filter()).and(form()).map( + |u: Option<User>, f: DeleteNoteRequest| match u { Some(u) => { delete_note(f.note_id).unwrap(); // TODO fix unwrap let red_url: http::Uri = f.redirect_url.parse().unwrap(); redirect(red_url) } None => redirect(http::Uri::from_static("error")), - }); + }, + ); // couldn't figure out how to get this folder to render on root properly let robots = warp::path("robots.txt").and(warp::fs::file("./static/robots.txt"));