gourami

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

commit ececc7f1623cbe9a7ac106c374caa9a7256dba59
parent 87cdf6ec05d32b9e65cc06498877fe94e0cfc189
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Sat, 13 Jun 2020 19:45:27 -0500

Add invite system

Diffstat:
MCargo.lock | 2+-
MCargo.toml | 2+-
Mmigrations/2020-04-13-014917_initialize/up.sql | 8++++++--
Dmigrations/2020-06-05-211246_add_wesbite/down.sql | 2--
Dmigrations/2020-06-05-211246_add_wesbite/up.sql | 5-----
Asrc/db/invite.rs | 9+++++++++
Msrc/db/mod.rs | 1+
Msrc/db/schema.rs | 2++
Msrc/db/user.rs | 10+---------
Msrc/lib.rs | 20++++++++++++++++++++
Msrc/routes.rs | 7++++++-
Msrc/schema.rs | 6+++++-
Mtemplates/createnote.html | 2+-
Mtemplates/user_header.html | 6++++++
14 files changed, 59 insertions(+), 23 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -649,7 +649,7 @@ dependencies = [ [[package]] name = "gourami_social" -version = "0.1.5" +version = "0.1.7" dependencies = [ "activitystreams", "ammonia", diff --git a/Cargo.toml b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gourami_social" -version = "0.1.6" +version = "0.1.7" authors = ["alex wennerberg <alex@alexwennerberg.com>"] edition = "2018" diff --git a/migrations/2020-04-13-014917_initialize/up.sql b/migrations/2020-04-13-014917_initialize/up.sql @@ -9,13 +9,17 @@ CREATE TABLE users ( password VARCHAR(255), admin BOOLEAN default false, show_email BOOLEAN default false, - remote_user BOOLEAN default false + remote_user BOOLEAN default false, + website VARCHAR(255) default "", + can_invite boolean default false ); CREATE UNIQUE INDEX users_username_idx ON users (username); CREATE UNIQUE INDEX users_email_idx ON users (email); CREATE TABLE registration_keys ( - value VARCHAR PRIMARY KEY + value VARCHAR PRIMARY KEY, + created_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + inviting_user_id REFERENCES users(id) ); CREATE TABLE activities ( diff --git a/migrations/2020-06-05-211246_add_wesbite/down.sql b/migrations/2020-06-05-211246_add_wesbite/down.sql @@ -1 +0,0 @@ --- This file should undo anything in `up.sql` -\ No newline at end of file diff --git a/migrations/2020-06-05-211246_add_wesbite/up.sql b/migrations/2020-06-05-211246_add_wesbite/up.sql @@ -1,5 +0,0 @@ --- Your SQL goes here - -ALTER TABLE users -add column website VARCHAR(255) default ""; - diff --git a/src/db/invite.rs b/src/db/invite.rs @@ -0,0 +1,9 @@ +use super::schema::registration_keys; + + +#[derive(Insertable)] +#[table_name = "registration_keys"] +pub struct NewRegistrationKey { + pub value: String, + pub inviting_user_id: i32, +} diff --git a/src/db/mod.rs b/src/db/mod.rs @@ -2,4 +2,5 @@ pub mod conn; pub mod note; pub mod schema; pub mod server_mutuals; +pub mod invite; pub mod user; // TODO singular diff --git a/src/db/schema.rs b/src/db/schema.rs @@ -14,6 +14,7 @@ table! { table! { registration_keys (value) { value -> Varchar, + inviting_user_id -> Nullable<Integer>, } } @@ -29,6 +30,7 @@ table! { admin -> Bool, show_email -> Bool, remote_user -> Bool, + can_invite -> Bool, } } diff --git a/src/db/user.rs b/src/db/user.rs @@ -54,6 +54,7 @@ pub struct User { pub admin: bool, pub show_email: bool, pub remote_user: bool, + pub can_invite: bool, } impl User { @@ -107,12 +108,3 @@ pub struct NewRemoteUser { pub username: String, pub remote_user: bool, } -// impl NewUser { - -// } -// impl validate -fn validate_username() {} - -fn validate_password() {} - -fn validate_email() {} diff --git a/src/lib.rs b/src/lib.rs @@ -13,6 +13,7 @@ use serde_json::Value; use std::collections::BTreeMap; use std::convert::Infallible; use zxcvbn::zxcvbn; +use uuid::Uuid; use warp::filters::path::FullPath; use warp::http; @@ -26,6 +27,7 @@ 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; @@ -525,6 +527,24 @@ fn server_info_page(auth_user: Option<User>) -> impl Reply { }) } +fn generate_invite(auth_user: Option<User>) -> impl Reply { + if let Some(u) = auth_user { + if u.can_invite { + // generate invite + use db::schema::registration_keys::dsl::*; + let invite_uuid = Uuid::new_v4(); + let new_reg_key = NewRegistrationKey { + inviting_user_id: u.id, + value: invite_uuid.to_string(), + }; + 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) + } + } + redirect(http::Uri::from_static("/register")) // TODO better error +} + fn render_user_edit_page(user: Option<User>, user_name: String) -> impl Reply { let u = user.clone().unwrap(); let global = Global::create(user, "/edit"); diff --git a/src/routes.rs b/src/routes.rs @@ -95,6 +95,10 @@ pub async fn run_server() { .and(form()) .map(edit_user); + let generate_invite = session_filter() + .and(path!("generate_invite")) + .map(generate_invite); + let server_info_page = session_filter() .and(path("server_info")) .map(server_info_page); @@ -176,7 +180,8 @@ pub async fn run_server() { .or(do_logout) .or(create_note) .or(delete_note) - .or(edit_user); + .or(edit_user) + .or(generate_invite); let api_post = post_server_inbox; let routes = warp::get() diff --git a/src/schema.rs b/src/schema.rs @@ -23,6 +23,8 @@ table! { table! { registration_keys (value) { value -> Nullable<Text>, + created_time -> Nullable<Timestamp>, + inviting_user_id -> Nullable<Binary>, } } @@ -52,16 +54,18 @@ table! { username -> Nullable<Text>, email -> Nullable<Text>, bio -> Nullable<Text>, - website -> Varchar, created_time -> Nullable<Timestamp>, password -> Nullable<Text>, admin -> Nullable<Bool>, show_email -> Nullable<Bool>, remote_user -> Nullable<Bool>, + website -> Nullable<Text>, + can_invite -> Nullable<Bool>, } } joinable!(notes -> users (user_id)); +joinable!(registration_keys -> users (inviting_user_id)); joinable!(sessions -> users (user_id)); allow_tables_to_appear_in_same_query!( diff --git a/templates/createnote.html b/templates/createnote.html @@ -4,7 +4,7 @@ <br> <button id="post" class="submit-button-style">create note</button> <input type="checkbox" id="neighborhood" name="neighborhood" -{% if global.page_title == "neighborhood" %} +{% if params.neighborhood.is_some() %} checked> {% else %} unchecked> diff --git a/templates/user_header.html b/templates/user_header.html @@ -13,6 +13,12 @@ {%endmatch%} <b>website:</b> <a href="{{user.website}}">{{user.website}}</a> {% if global.me.id == user.id%} + {% if global.me.can_invite %} + <br> + <form method="post" action="/generate_invite" class="inline"> + <button type="submit" name="submit_param" value="submit_value" + class="link-button">invite</button></form> + {% endif %} <br> <a href="/user/{{user.username}}/edit">edit</a> <div>