Reorganize uses

This commit is contained in:
Bat 2018-04-24 10:21:39 +01:00
parent f0b08b2d6d
commit 0c9a1bfc3a
18 changed files with 144 additions and 145 deletions

86
src/activity_pub/actor.rs Normal file
View File

@ -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()
)
}
}

View File

@ -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()
)
}
}

View File

@ -1,5 +1,5 @@
use serde_json;
use diesel::PgConnection;
use serde_json;
pub trait Webfinger {
fn webfinger_subject(&self, conn: &PgConnection) -> String;

View File

@ -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

View File

@ -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;

View File

@ -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)]

View File

@ -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 {

View File

@ -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 {

View File

@ -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)]

View File

@ -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)]

View File

@ -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";

View File

@ -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("/~/<name>", rank = 2)]
fn details(name: String) -> String {

View File

@ -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;

View File

@ -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;

View File

@ -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("/~/<blog>/<slug>", 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("/~/<blog>/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::<String, String>::new())
}
#[get("/~/<blog>/new", rank = 2)]
fn new_auth(blog: String) -> Redirect {
#[get("/~/<_blog>/new", rank = 2)]
fn new_auth(_blog: String) -> Redirect {
utils::requires_login()
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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 {