From 5b3eca63e01cbe916483416ba6787b30e616c867 Mon Sep 17 00:00:00 2001 From: Bat Date: Wed, 5 Sep 2018 21:18:27 +0100 Subject: [PATCH] Make it possible to tag articles and display them --- plume-models/src/posts.rs | 4 +++- plume-models/src/tags.rs | 3 ++- src/routes/posts.rs | 13 ++++++++++++- static/css/main.css | 23 ++++++++++++++++++++--- templates/posts/details.html.tera | 5 +++++ templates/posts/new.html.tera | 2 ++ 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index c826f76..2bb978f 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -19,6 +19,7 @@ use likes::Like; use mentions::Mention; use post_authors::*; use reshares::Reshare; +use tags::Tag; use users::User; use schema::posts; use safe_string::SafeString; @@ -244,7 +245,8 @@ impl Post { "author": self.get_authors(conn)[0].to_json(conn), "url": format!("/~/{}/{}/", blog.get_fqn(conn), self.slug), "date": self.creation_date.timestamp(), - "blog": blog.to_json(conn) + "blog": blog.to_json(conn), + "tags": Tag::for_post(&*conn, self.id) }) } diff --git a/plume-models/src/tags.rs b/plume-models/src/tags.rs index 359f32d..e94a54d 100644 --- a/plume-models/src/tags.rs +++ b/plume-models/src/tags.rs @@ -1,7 +1,7 @@ use diesel::{self, PgConnection, ExpressionMethods, RunQueryDsl, QueryDsl}; use schema::tags; -#[derive(Queryable)] +#[derive(Serialize, Queryable)] pub struct Tag { pub id: i32, pub tag: String, @@ -20,4 +20,5 @@ pub struct NewTag { impl Tag { insert!(tags, NewTag); get!(tags); + list_by!(tags, for_post, post_id as i32); } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 7ecf9f4..3c9ca02 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -1,5 +1,5 @@ use activitypub::object::Article; -use heck::KebabCase; +use heck::{CamelCase, KebabCase}; use rocket::{State, request::LenientForm}; use rocket::response::{Redirect, Flash}; use rocket_contrib::Template; @@ -19,6 +19,7 @@ use plume_models::{ post_authors::*, posts::*, safe_string::SafeString, + tags::*, users::User }; @@ -101,6 +102,7 @@ struct NewPostForm { pub title: String, pub subtitle: String, pub content: String, + pub tags: String, pub license: String } @@ -165,6 +167,15 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D Mention::from_activity(&*conn, Mention::build_activity(&*conn, m), post.id, true); } + let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0); + for tag in tags { + Tag::insert(&*conn, NewTag { + tag: tag, + is_hastag: false, + post_id: post.id + }); + } + let act = post.create_activity(&*conn); let followers = user.get_followers(&*conn); worker.execute(Thunk::of(move || broadcast(&user, act, followers))); diff --git a/static/css/main.css b/static/css/main.css index d1a9f1b..a19e654 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -217,10 +217,27 @@ } main .article-meta > * { margin: 0 20%; } - main .article-meta > p { - margin: 2em 20%; +main .article-meta > p { + margin: 2em 20%; font-size: 0.9em; - } +} + +/** Tags **/ + +main .article-meta .tags { + list-style: none; + display: inline-block; + padding: 0px; + margin-bottom: 2em; +} + +main .article-meta .tags li { + display: inline; + background: #DADADA; + padding: 10px 20px; + margin-right: 10px; + border-radius: 3px; +} /* ~ Likes ~ */ diff --git a/templates/posts/details.html.tera b/templates/posts/details.html.tera index e64efff..3a3158d 100644 --- a/templates/posts/details.html.tera +++ b/templates/posts/details.html.tera @@ -34,6 +34,11 @@