diff --git a/migrations/2018-04-23-101717_create_blogs/down.sql b/migrations/2018-04-23-101717_create_blogs/down.sql new file mode 100644 index 00000000..4f8b0a68 --- /dev/null +++ b/migrations/2018-04-23-101717_create_blogs/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE blogs; diff --git a/migrations/2018-04-23-101717_create_blogs/up.sql b/migrations/2018-04-23-101717_create_blogs/up.sql new file mode 100644 index 00000000..4b26e85d --- /dev/null +++ b/migrations/2018-04-23-101717_create_blogs/up.sql @@ -0,0 +1,10 @@ +-- Your SQL goes here +CREATE TABLE blogs ( + id SERIAL PRIMARY KEY, + actor_id VARCHAR NOT NULL, + title VARCHAR NOT NULL, + summary TEXT NOT NULL DEFAULT '', + outbox_url VARCHAR NOT NULL, + inbox_url VARCHAR NOT NULL, + instance_id INTEGER REFERENCES instances(id) ON DELETE CASCADE NOT NULL +) diff --git a/src/models/blogs.rs b/src/models/blogs.rs new file mode 100644 index 00000000..ea168ebf --- /dev/null +++ b/src/models/blogs.rs @@ -0,0 +1,58 @@ +use diesel; +use diesel::{QueryDsl, RunQueryDsl, ExpressionMethods, PgConnection}; +use schema::blogs; + +#[derive(Queryable, Identifiable)] +pub struct Blog { + pub id: i32, + pub actor_id: String, + pub title: String, + pub summary: String, + pub outbox_url: String, + pub inbox_url: String, + pub instance_id: i32 +} + +#[derive(Insertable)] +#[table_name = "blogs"] +pub struct NewBlog { + pub actor_id: String, + pub title: String, + pub summary: String, + pub outbox_url: String, + pub inbox_url: String, + pub instance_id: i32 +} + +impl Blog { + pub fn insert (conn: &PgConnection, new: NewBlog) -> Blog { + diesel::insert_into(blogs::table) + .values(new) + .get_result(conn) + .expect("Error saving new blog") + } + + pub fn compute_outbox(blog: String, hostname: String) -> String { + format!("https://{}/~/{}/outbox", hostname, blog) + } + + pub fn compute_inbox(blog: String, hostname: String) -> String { + format!("https://{}/~/{}/inbox", hostname, blog) + } + + pub fn get(conn: &PgConnection, id: i32) -> Option { + blogs::table.filter(blogs::id.eq(id)) + .limit(1) + .load::(conn) + .expect("Error loading blog by id") + .into_iter().nth(0) + } + + pub fn find_by_actor_id(conn: &PgConnection, username: String) -> Option { + blogs::table.filter(blogs::actor_id.eq(username)) + .limit(1) + .load::(conn) + .expect("Error loading blog by email") + .into_iter().nth(0) + } +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 5cd0b566..284de04a 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,2 +1,3 @@ +pub mod blogs; pub mod instance; pub mod user; diff --git a/src/models/user.rs b/src/models/user.rs index b4e7fdf0..9f5d0f26 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -38,7 +38,7 @@ pub struct NewUser { } impl User { - fn grant_admin_rights() {} + pub fn grant_admin_rights() {} pub fn insert (conn: &PgConnection, new: NewUser) -> User { diesel::insert_into(users::table) diff --git a/src/schema.rs b/src/schema.rs index 9a5abdca..5af7ab02 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,3 +1,15 @@ +table! { + blogs (id) { + id -> Int4, + actor_id -> Varchar, + title -> Varchar, + summary -> Text, + outbox_url -> Varchar, + inbox_url -> Varchar, + instance_id -> Int4, + } +} + table! { instances (id) { id -> Int4, @@ -24,9 +36,11 @@ table! { } } +joinable!(blogs -> instances (instance_id)); joinable!(users -> instances (instance_id)); allow_tables_to_appear_in_same_query!( + blogs, instances, users, );