From 7152d714aef6083cba1ba1f08787aab9ab68792f Mon Sep 17 00:00:00 2001 From: Bat Date: Thu, 6 Sep 2018 20:00:55 +0100 Subject: [PATCH] Add a source property to posts To store the Markdown --- .../2018-09-06-182637_posts_add_source/down.sql | 2 ++ migrations/2018-09-06-182637_posts_add_source/up.sql | 2 ++ plume-common/src/activity_pub/mod.rs | 10 ++++++++++ plume-models/src/posts.rs | 11 +++++++++-- plume-models/src/schema.rs | 1 + src/routes/posts.rs | 3 ++- 6 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 migrations/2018-09-06-182637_posts_add_source/down.sql create mode 100644 migrations/2018-09-06-182637_posts_add_source/up.sql diff --git a/migrations/2018-09-06-182637_posts_add_source/down.sql b/migrations/2018-09-06-182637_posts_add_source/down.sql new file mode 100644 index 00000000..43773773 --- /dev/null +++ b/migrations/2018-09-06-182637_posts_add_source/down.sql @@ -0,0 +1,2 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE posts DROP COLUMN source; diff --git a/migrations/2018-09-06-182637_posts_add_source/up.sql b/migrations/2018-09-06-182637_posts_add_source/up.sql new file mode 100644 index 00000000..a880b5ba --- /dev/null +++ b/migrations/2018-09-06-182637_posts_add_source/up.sql @@ -0,0 +1,2 @@ +-- Your SQL goes here +ALTER TABLE posts ADD COLUMN source TEXT NOT NULL DEFAULT ''; diff --git a/plume-common/src/activity_pub/mod.rs b/plume-common/src/activity_pub/mod.rs index d3089833..d421553f 100644 --- a/plume-common/src/activity_pub/mod.rs +++ b/plume-common/src/activity_pub/mod.rs @@ -175,3 +175,13 @@ pub struct Hashtag { #[activitystreams(concrete(String), functional)] pub name: Option, } + +#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Source { + pub media_type: String, + + pub content: String, +} + +impl Object for Source {} diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index def6def7..967edf71 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -9,7 +9,7 @@ use heck::KebabCase; use serde_json; use plume_common::activity_pub::{ - Hashtag, + Hashtag, Source, PUBLIC_VISIBILTY, Id, IntoId, inbox::{Deletable, FromActivity} }; @@ -37,6 +37,7 @@ pub struct Post { pub creation_date: NaiveDateTime, pub ap_url: String, pub subtitle: String, + pub source: String, } #[derive(Insertable)] @@ -51,6 +52,7 @@ pub struct NewPost { pub creation_date: Option, pub ap_url: String, pub subtitle: String, + pub source: String, } impl Post { @@ -239,6 +241,10 @@ impl Post { authors.push(self.get_blog(conn).into_id()); // add the blog URL here too article.object_props.set_attributed_to_link_vec::(authors).expect("Article::into_activity: attributedTo error"); article.object_props.set_content_string(self.content.get().clone()).expect("Article::into_activity: content error"); + article.ap_object_props.set_source_object(Source { + content: self.source.clone(), + media_type: String::from("text/markdown"), + }).expect("Article::into_activity: source error"); article.object_props.set_published_utctime(Utc.from_utc_datetime(&self.creation_date)).expect("Article::into_activity: published error"); article.object_props.set_summary_string(self.subtitle.clone()).expect("Article::into_activity: summary error"); article.object_props.tag = Some(json!(mentions_json.append(&mut tags_json))); @@ -308,7 +314,8 @@ impl FromActivity for Post { // FIXME: This is wrong: with this logic, we may use the display URL as the AP ID. We need two different fields ap_url: article.object_props.url_string().unwrap_or(article.object_props.id_string().expect("Post::from_activity: url + id error")), creation_date: Some(article.object_props.published_utctime().expect("Post::from_activity: published error").naive_utc()), - subtitle: article.object_props.summary_string().expect("Post::from_activity: summary error") + subtitle: article.object_props.summary_string().expect("Post::from_activity: summary error"), + source: article.ap_object_props.source_object::().expect("Post::from_activity: source error").content }); for author in authors.into_iter() { diff --git a/plume-models/src/schema.rs b/plume-models/src/schema.rs index 1a9cd293..7d51d678 100644 --- a/plume-models/src/schema.rs +++ b/plume-models/src/schema.rs @@ -126,6 +126,7 @@ table! { creation_date -> Timestamp, ap_url -> Varchar, subtitle -> Text, + source -> Text, } } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index 3c9ca023..85eb86c5 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -155,7 +155,8 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D }, ap_url: "".to_string(), creation_date: None, - subtitle: form.subtitle.clone() + subtitle: form.subtitle.clone(), + source: form.content.clone(), }); let post = post.update_ap_url(&*conn); PostAuthor::insert(&*conn, NewPostAuthor {