From 0c9a1bfc3a082207e255c7c573c46936ca7ffb0c Mon Sep 17 00:00:00 2001 From: Bat Date: Tue, 24 Apr 2018 10:21:39 +0100 Subject: [PATCH] Reorganize uses --- src/activity_pub/actor.rs | 86 ++++++++++++++++++++++++++++++++++ src/activity_pub/mod.rs | 87 +---------------------------------- src/activity_pub/webfinger.rs | 2 +- src/db_conn.rs | 8 ++-- src/main.rs | 6 +-- src/models/blog_authors.rs | 4 +- src/models/blogs.rs | 11 +++-- src/models/instance.rs | 6 +-- src/models/post_authors.rs | 4 +- src/models/posts.rs | 4 +- src/models/users.rs | 17 ++++--- src/routes/blogs.rs | 6 +-- src/routes/instance.rs | 4 +- src/routes/mod.rs | 2 +- src/routes/posts.rs | 16 +++---- src/routes/session.rs | 13 +++--- src/routes/user.rs | 4 +- src/routes/well_known.rs | 9 ++-- 18 files changed, 144 insertions(+), 145 deletions(-) create mode 100644 src/activity_pub/actor.rs diff --git a/src/activity_pub/actor.rs b/src/activity_pub/actor.rs new file mode 100644 index 00000000..e7beb8ee --- /dev/null +++ b/src/activity_pub/actor.rs @@ -0,0 +1,86 @@ +use diesel::PgConnection; +use serde_json::Value; + +use models::instance::Instance; + +pub enum ActorType { + Person, + Blog +} + +impl ToString for ActorType { + fn to_string(&self) -> String { + String::from(match self { + ActorType::Person => "Person", + ActorType::Blog => "Blog" + }) + } +} + +pub trait Actor { + fn get_box_prefix() -> &'static str; + + fn get_actor_id(&self) -> String; + + fn get_instance(&self, conn: &PgConnection) -> Instance; + + fn get_actor_type() -> ActorType; + + fn as_activity_pub (&self, conn: &PgConnection) -> Value { + json!({ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", + "sensitive": "as:sensitive", + "movedTo": "as:movedTo", + "Hashtag": "as:Hashtag", + "ostatus":"http://ostatus.org#", + "atomUri":"ostatus:atomUri", + "inReplyToAtomUri":"ostatus:inReplyToAtomUri", + "conversation":"ostatus:conversation", + "toot":"http://joinmastodon.org/ns#", + "Emoji":"toot:Emoji", + "focalPoint": { + "@container":"@list", + "@id":"toot:focalPoint" + }, + "featured":"toot:featured" + } + ], + "id": self.compute_id(conn), + "type": Self::get_actor_type().to_string(), + "inbox": self.compute_inbox(conn), + "outbox": self.compute_outbox(conn), + "preferredUsername": self.get_actor_id(), + "name": "", + "summary": "", + "url": self.compute_id(conn), + "endpoints": { + "sharedInbox": "https://plu.me/inbox" + } + }) + } + + fn compute_outbox(&self, conn: &PgConnection) -> String { + self.compute_box(conn, "outbox") + } + + fn compute_inbox(&self, conn: &PgConnection) -> String { + self.compute_box(conn, "inbox") + } + + fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String { + format!("{id}/{name}", id = self.compute_id(conn), name = box_name) + } + + fn compute_id(&self, conn: &PgConnection) -> String { + format!( + "https://{instance}/{prefix}/{user}", + instance = self.get_instance(conn).public_domain, + prefix = Self::get_box_prefix(), + user = self.get_actor_id() + ) + } +} diff --git a/src/activity_pub/mod.rs b/src/activity_pub/mod.rs index 569ccb12..f5f42555 100644 --- a/src/activity_pub/mod.rs +++ b/src/activity_pub/mod.rs @@ -1,87 +1,2 @@ -use models::instance::Instance; -use diesel::PgConnection; -use serde_json::Value; - +pub mod actor; pub mod webfinger; - -pub enum ActorType { - Person, - Blog -} - -impl ToString for ActorType { - fn to_string(&self) -> String { - String::from(match self { - ActorType::Person => "Person", - ActorType::Blog => "Blog" - }) - } -} - -pub trait Actor { - fn get_box_prefix() -> &'static str; - - fn get_actor_id(&self) -> String; - - fn get_instance(&self, conn: &PgConnection) -> Instance; - - fn get_actor_type() -> ActorType; - - fn as_activity_pub (&self, conn: &PgConnection) -> Value { - json!({ - "@context": [ - "https://www.w3.org/ns/activitystreams", - "https://w3id.org/security/v1", - { - "manuallyApprovesFollowers": "as:manuallyApprovesFollowers", - "sensitive": "as:sensitive", - "movedTo": "as:movedTo", - "Hashtag": "as:Hashtag", - "ostatus":"http://ostatus.org#", - "atomUri":"ostatus:atomUri", - "inReplyToAtomUri":"ostatus:inReplyToAtomUri", - "conversation":"ostatus:conversation", - "toot":"http://joinmastodon.org/ns#", - "Emoji":"toot:Emoji", - "focalPoint": { - "@container":"@list", - "@id":"toot:focalPoint" - }, - "featured":"toot:featured" - } - ], - "id": self.compute_id(conn), - "type": Self::get_actor_type().to_string(), - "inbox": self.compute_inbox(conn), - "outbox": self.compute_outbox(conn), - "preferredUsername": self.get_actor_id(), - "name": "", - "summary": "", - "url": self.compute_id(conn), - "endpoints": { - "sharedInbox": "https://plu.me/inbox" - } - }) - } - - fn compute_outbox(&self, conn: &PgConnection) -> String { - self.compute_box(conn, "outbox") - } - - fn compute_inbox(&self, conn: &PgConnection) -> String { - self.compute_box(conn, "inbox") - } - - fn compute_box(&self, conn: &PgConnection, box_name: &str) -> String { - format!("{id}/{name}", id = self.compute_id(conn), name = box_name) - } - - fn compute_id(&self, conn: &PgConnection) -> String { - format!( - "https://{instance}/{prefix}/{user}", - instance = self.get_instance(conn).public_domain, - prefix = Self::get_box_prefix(), - user = self.get_actor_id() - ) - } -} diff --git a/src/activity_pub/webfinger.rs b/src/activity_pub/webfinger.rs index 901d0148..0780cd79 100644 --- a/src/activity_pub/webfinger.rs +++ b/src/activity_pub/webfinger.rs @@ -1,5 +1,5 @@ -use serde_json; use diesel::PgConnection; +use serde_json; pub trait Webfinger { fn webfinger_subject(&self, conn: &PgConnection) -> String; diff --git a/src/db_conn.rs b/src/db_conn.rs index 7eced78e..f2c520d9 100644 --- a/src/db_conn.rs +++ b/src/db_conn.rs @@ -1,9 +1,9 @@ -use std::ops::Deref; -use rocket::http::Status; -use rocket::request::{self, FromRequest}; -use rocket::{Request, State, Outcome}; use diesel::pg::PgConnection; use diesel::r2d2::{ConnectionManager, Pool, PooledConnection}; +use rocket::{Request, State, Outcome}; +use rocket::http::Status; +use rocket::request::{self, FromRequest}; +use std::ops::Deref; // From rocket documentation diff --git a/src/main.rs b/src/main.rs index 262e3d75..490083a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,21 +1,21 @@ #![feature(plugin, custom_derive)] #![plugin(rocket_codegen)] +extern crate bcrypt; +extern crate heck; #[macro_use] extern crate diesel; extern crate dotenv; extern crate rocket; extern crate rocket_contrib; -extern crate bcrypt; -extern crate heck; #[macro_use] extern crate serde_json; use diesel::pg::PgConnection; use diesel::r2d2::{ConnectionManager, Pool}; use dotenv::dotenv; -use std::env; use rocket_contrib::Template; +use std::env; mod activity_pub; mod db_conn; diff --git a/src/models/blog_authors.rs b/src/models/blog_authors.rs index 440bed91..28fd3f46 100644 --- a/src/models/blog_authors.rs +++ b/src/models/blog_authors.rs @@ -1,5 +1,5 @@ -use diesel; -use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; +use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; + use schema::blog_authors; #[derive(Queryable, Identifiable)] diff --git a/src/models/blogs.rs b/src/models/blogs.rs index e30012e6..f459641a 100644 --- a/src/models/blogs.rs +++ b/src/models/blogs.rs @@ -1,9 +1,10 @@ -use diesel; -use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; -use schema::blogs; -use activity_pub::{Actor, ActorType}; -use models::instance::Instance; +use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; + +use activity_pub::actor::{Actor, ActorType}; use activity_pub::webfinger::*; +use models::instance::Instance; +use schema::blogs; + #[derive(Queryable, Identifiable)] pub struct Blog { diff --git a/src/models/instance.rs b/src/models/instance.rs index 09befd27..0194d870 100644 --- a/src/models/instance.rs +++ b/src/models/instance.rs @@ -1,8 +1,8 @@ -use diesel; -use diesel::{ QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection }; +use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; use std::iter::Iterator; -use schema::{instances, users}; + use models::users::User; +use schema::{instances, users}; #[derive(Identifiable, Queryable)] pub struct Instance { diff --git a/src/models/post_authors.rs b/src/models/post_authors.rs index f91592eb..783b2e09 100644 --- a/src/models/post_authors.rs +++ b/src/models/post_authors.rs @@ -1,5 +1,5 @@ -use diesel; -use diesel::{PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; +use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; + use schema::post_authors; #[derive(Queryable, Identifiable)] diff --git a/src/models/posts.rs b/src/models/posts.rs index 6c9b49f9..ba94d8d2 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -1,5 +1,5 @@ -use diesel; -use diesel::{PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; +use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods}; + use schema::posts; #[derive(Queryable, Identifiable)] diff --git a/src/models/users.rs b/src/models/users.rs index d67d50fb..514d6126 100644 --- a/src/models/users.rs +++ b/src/models/users.rs @@ -1,14 +1,13 @@ -use rocket::request; -use rocket::request::{FromRequest, Request}; -use rocket::outcome::IntoOutcome; -use diesel; -use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; -use schema::users; -use db_conn::DbConn; -use activity_pub::{ActorType, Actor}; -use models::instance::Instance; use bcrypt; +use diesel::{self, QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; +use rocket::request::{self, FromRequest, Request}; +use rocket::outcome::IntoOutcome; + +use activity_pub::actor::{ActorType, Actor}; use activity_pub::webfinger::Webfinger; +use db_conn::DbConn; +use models::instance::Instance; +use schema::users; pub const AUTH_COOKIE: &'static str = "user_id"; diff --git a/src/routes/blogs.rs b/src/routes/blogs.rs index f199b7fb..4c78be74 100644 --- a/src/routes/blogs.rs +++ b/src/routes/blogs.rs @@ -3,13 +3,13 @@ use rocket::response::Redirect; use rocket_contrib::{Json, Template}; use std::collections::HashMap; -use utils; +use activity_pub::actor::Actor; use db_conn::DbConn; -use models::blogs::*; use models::blog_authors::*; +use models::blogs::*; use models::instance::Instance; use models::users::User; -use activity_pub::Actor; +use utils; #[get("/~/", rank = 2)] fn details(name: String) -> String { diff --git a/src/routes/instance.rs b/src/routes/instance.rs index b26a0744..cc7f4703 100644 --- a/src/routes/instance.rs +++ b/src/routes/instance.rs @@ -1,6 +1,6 @@ -use rocket_contrib::Template; -use rocket::response::Redirect; use rocket::request::Form; +use rocket::response::Redirect; +use rocket_contrib::Template; use std::collections::HashMap; use db_conn::DbConn; diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 278d5bb5..8d7aae67 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1,6 +1,6 @@ pub mod blogs; pub mod instance; -pub mod session; pub mod posts; +pub mod session; pub mod user; pub mod well_known; diff --git a/src/routes/posts.rs b/src/routes/posts.rs index cee2def9..c503ea30 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -1,15 +1,15 @@ -use rocket::response::Redirect; +use heck::KebabCase; use rocket::request::Form; +use rocket::response::Redirect; use rocket_contrib::Template; use std::collections::HashMap; -use heck::KebabCase; -use utils; use db_conn::DbConn; use models::blogs::*; -use models::posts::*; use models::post_authors::*; +use models::posts::*; use models::users::User; +use utils; #[get("/~//", rank = 3)] fn details(blog: String, slug: String, conn: DbConn) -> String { @@ -18,13 +18,13 @@ fn details(blog: String, slug: String, conn: DbConn) -> String { format!("{} in {}", post.title, blog.title) } -#[get("/~//new", rank = 1)] -fn new(blog: String, user: User) -> Template { +#[get("/~/<_blog>/new", rank = 1)] +fn new(_blog: String, _user: User) -> Template { Template::render("posts/new", HashMap::::new()) } -#[get("/~//new", rank = 2)] -fn new_auth(blog: String) -> Redirect { +#[get("/~/<_blog>/new", rank = 2)] +fn new_auth(_blog: String) -> Redirect { utils::requires_login() } diff --git a/src/routes/session.rs b/src/routes/session.rs index 621171dd..bd8f2350 100644 --- a/src/routes/session.rs +++ b/src/routes/session.rs @@ -1,13 +1,12 @@ -use std::collections::HashMap; -use rocket_contrib::Template; +use rocket::http::{Cookie, Cookies}; use rocket::response::Redirect; -use rocket::request::Form; -use models::users::User; use rocket::response::status::NotFound; -use rocket::http::Cookies; +use rocket::request::Form; +use rocket_contrib::Template; +use std::collections::HashMap; + use db_conn::DbConn; -use rocket::http::Cookie; -use models::users::AUTH_COOKIE; +use models::users::{User, AUTH_COOKIE}; #[get("/login")] fn new() -> Template { diff --git a/src/routes/user.rs b/src/routes/user.rs index 11f1a54f..dc01cb7f 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -3,10 +3,10 @@ use rocket::response::Redirect; use rocket_contrib::{Json, Template}; use std::collections::HashMap; +use activity_pub::actor::Actor; use db_conn::DbConn; -use models::users::*; use models::instance::Instance; -use activity_pub::Actor; +use models::users::*; #[get("/me")] fn me(user: User) -> String { diff --git a/src/routes/well_known.rs b/src/routes/well_known.rs index c16aa2df..5668631f 100644 --- a/src/routes/well_known.rs +++ b/src/routes/well_known.rs @@ -1,9 +1,8 @@ -use models::instance::Instance; -use db_conn::DbConn; -use models::users::User; -use models::blogs::Blog; -use rocket_contrib::Json; use activity_pub::webfinger::Webfinger; +use db_conn::DbConn; +use models::blogs::Blog; +use models::instance::Instance; +use models::users::User; #[get("/.well-known/host-meta", format = "application/xml")] fn host_meta(conn: DbConn) -> String {