Add blog model
This commit is contained in:
@@ -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<Blog> {
|
||||
blogs::table.filter(blogs::id.eq(id))
|
||||
.limit(1)
|
||||
.load::<Blog>(conn)
|
||||
.expect("Error loading blog by id")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
pub fn find_by_actor_id(conn: &PgConnection, username: String) -> Option<Blog> {
|
||||
blogs::table.filter(blogs::actor_id.eq(username))
|
||||
.limit(1)
|
||||
.load::<Blog>(conn)
|
||||
.expect("Error loading blog by email")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod blogs;
|
||||
pub mod instance;
|
||||
pub mod user;
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user