Add a Mention model

pull/70/head
Bat 6 years ago
parent 24ef3d00d1
commit e074af57ff

@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE mentions;

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

@ -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<Self, Self::Error> {
let pool = request.guard::<State<Pool<ConnectionManager<PgConnection>>>>()?;
let pool = request.guard::<State<PgPool>>()?;
match pool.get() {
Ok(conn) => Outcome::Success(DbConn(conn)),
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ()))

@ -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<i32>,
pub comment_id: Option<i32>
}
#[derive(Insertable)]
#[table_name = "mentions"]
pub struct NewMention {
pub mentioned_id: i32,
pub post_id: Option<i32>,
pub comment_id: Option<i32>
}
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<Post> {
self.post_id.and_then(|id| Post::get(conn, id))
}
pub fn get_comment(&self, conn: &PgConnection) -> Option<Comment> {
self.post_id.and_then(|id| Comment::get(conn, id))
}
}

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

@ -13,8 +13,8 @@ use models::{
posts::*,
users::User
};
use utils;
use safe_string::SafeString;
use utils;
#[get("/~/<blog>/<slug>", rank = 4)]
fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Template {

@ -66,6 +66,15 @@ table! {
}
}
table! {
mentions (id) {
id -> Int4,
mentioned_id -> Int4,
post_id -> Nullable<Int4>,
comment_id -> Nullable<Int4>,
}
}
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,

@ -12,7 +12,7 @@ use db_conn::DbConn;
use models::instance::*;
use models::users::*;
type PgPool = Pool<ConnectionManager<PgConnection>>;
pub type PgPool = Pool<ConnectionManager<PgConnection>>;
/// Initializes a database pool.
fn init_pool() -> Option<PgPool> {

@ -23,6 +23,7 @@ pub fn requires_login(message: &str, url: Uri) -> Flash<Redirect> {
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()
// });
}

Loading…
Cancel
Save