diff --git a/migrations/2018-06-20-175532_create_mentions/down.sql b/migrations/2018-06-20-175532_create_mentions/down.sql new file mode 100644 index 0000000..e860c9a --- /dev/null +++ b/migrations/2018-06-20-175532_create_mentions/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +DROP TABLE mentions; diff --git a/migrations/2018-06-20-175532_create_mentions/up.sql b/migrations/2018-06-20-175532_create_mentions/up.sql new file mode 100644 index 0000000..7640e35 --- /dev/null +++ b/migrations/2018-06-20-175532_create_mentions/up.sql @@ -0,0 +1,7 @@ +-- Your SQL goes here +CREATE TABLE mentions ( + id SERIAL PRIMARY KEY, + mentioned_id INTEGER REFERENCES users(id) ON DELETE CASCADE NOT NULL, + post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE, + comment_id INTEGER REFERENCES comments(id) ON DELETE CASCADE +) diff --git a/src/db_conn.rs b/src/db_conn.rs index c12b011..7f75f35 100644 --- a/src/db_conn.rs +++ b/src/db_conn.rs @@ -1,10 +1,12 @@ use diesel::{ pg::PgConnection, - r2d2::{ConnectionManager, Pool, PooledConnection} + r2d2::{ConnectionManager, PooledConnection} }; use rocket::{Request, State, Outcome, http::Status, request::{self, FromRequest}}; use std::ops::Deref; +use setup::PgPool; + // From rocket documentation // Connection request guard type: a wrapper around an r2d2 pooled connection. @@ -17,7 +19,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for DbConn { type Error = (); fn from_request(request: &'a Request<'r>) -> request::Outcome { - let pool = request.guard::>>>()?; + let pool = request.guard::>()?; match pool.get() { Ok(conn) => Outcome::Success(DbConn(conn)), Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())) diff --git a/src/models/mentions.rs b/src/models/mentions.rs new file mode 100644 index 0000000..dd29dda --- /dev/null +++ b/src/models/mentions.rs @@ -0,0 +1,37 @@ +use diesel::{self, PgConnection, QueryDsl, RunQueryDsl, ExpressionMethods}; + +use models::{ + comments::Comment, + posts::Post +}; +use schema::mentions; + +#[derive(Queryable, Identifiable)] +pub struct Mention { + pub id: i32, + pub mentioned_id: i32, + pub post_id: Option, + pub comment_id: Option +} + +#[derive(Insertable)] +#[table_name = "mentions"] +pub struct NewMention { + pub mentioned_id: i32, + pub post_id: Option, + pub comment_id: Option +} + +impl Mention { + insert!(mentions, NewMention); + get!(mentions); + find_by!(mentions, find_for_user, mentioned_id as i32); + + pub fn get_post(&self, conn: &PgConnection) -> Option { + self.post_id.and_then(|id| Post::get(conn, id)) + } + + pub fn get_comment(&self, conn: &PgConnection) -> Option { + self.post_id.and_then(|id| Comment::get(conn, id)) + } +} diff --git a/src/models/mod.rs b/src/models/mod.rs index d0f01f3..13edc56 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -41,6 +41,7 @@ pub mod comments; pub mod follows; pub mod instance; pub mod likes; +pub mod mentions; pub mod notifications; pub mod post_authors; pub mod posts; diff --git a/src/routes/posts.rs b/src/routes/posts.rs index ae1de52..0755d57 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -13,8 +13,8 @@ use models::{ posts::*, users::User }; -use utils; use safe_string::SafeString; +use utils; #[get("/~//", rank = 4)] fn details(blog: String, slug: String, conn: DbConn, user: Option) -> Template { diff --git a/src/schema.rs b/src/schema.rs index 6614f44..87563fd 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -66,6 +66,15 @@ table! { } } +table! { + mentions (id) { + id -> Int4, + mentioned_id -> Int4, + post_id -> Nullable, + comment_id -> Nullable, + } +} + table! { notifications (id) { id -> Int4, @@ -137,6 +146,9 @@ joinable!(comments -> posts (post_id)); joinable!(comments -> users (author_id)); joinable!(likes -> posts (post_id)); joinable!(likes -> users (user_id)); +joinable!(mentions -> comments (comment_id)); +joinable!(mentions -> posts (post_id)); +joinable!(mentions -> users (mentioned_id)); joinable!(notifications -> users (user_id)); joinable!(post_authors -> posts (post_id)); joinable!(post_authors -> users (author_id)); @@ -152,6 +164,7 @@ allow_tables_to_appear_in_same_query!( follows, instances, likes, + mentions, notifications, post_authors, posts, diff --git a/src/setup.rs b/src/setup.rs index da366d0..9c67ed6 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -12,7 +12,7 @@ use db_conn::DbConn; use models::instance::*; use models::users::*; -type PgPool = Pool>; +pub type PgPool = Pool>; /// Initializes a database pool. fn init_pool() -> Option { diff --git a/src/utils.rs b/src/utils.rs index 787adcb..7774528 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -23,6 +23,7 @@ pub fn requires_login(message: &str, url: Uri) -> Flash { pub fn md_to_html(md: &str) -> String { let parser = Parser::new_ext(md, Options::all()); + let parser = parser.flat_map(|evt| match evt { Event::Text(txt) => txt.chars().fold((vec![], false, String::new(), 0), |(mut events, in_mention, text_acc, n), c| { if in_mention { @@ -54,21 +55,10 @@ pub fn md_to_html(md: &str) -> String { }).0, _ => vec![evt] }); + + // TODO: fetch mentionned profiles in background, if needed + let mut buf = String::new(); html::push_html(&mut buf, parser); buf - - // let root = parse_document(&arena, md, &ComrakOptions{ - // smart: true, - // safe: true, - // ext_strikethrough: true, - // ext_tagfilter: true, - // ext_table: true, - // // ext_autolink: true, - // ext_tasklist: true, - // ext_superscript: true, - // ext_header_ids: Some("title".to_string()), - // ext_footnotes: true, - // ..ComrakOptions::default() - // }); }