From 3b2ca041d054d7433752f5477c08a5a31db55623 Mon Sep 17 00:00:00 2001 From: Bat Date: Fri, 22 Jun 2018 21:45:37 +0100 Subject: [PATCH] Rewrite Post::into_activity in a nicer way And prefer expect over unwrap --- src/models/likes.rs | 2 +- src/models/posts.rs | 24 ++++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/models/likes.rs b/src/models/likes.rs index f7c6fb9..31da7cc 100644 --- a/src/models/likes.rs +++ b/src/models/likes.rs @@ -23,7 +23,7 @@ pub struct Like { pub ap_url: String } -#[derive(Insertable)] +#[derive(Default, Insertable)] #[table_name = "likes"] pub struct NewLike { pub user_id: i32, diff --git a/src/models/posts.rs b/src/models/posts.rs index d75888e..a15b0a1 100644 --- a/src/models/posts.rs +++ b/src/models/posts.rs @@ -1,9 +1,9 @@ use activitypub::{ activity::Create, link, - object::{Article, properties::ObjectProperties} + object::Article }; -use chrono::NaiveDateTime; +use chrono::{NaiveDateTime, TimeZone, Utc}; use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any}; use serde_json; @@ -150,18 +150,14 @@ impl Post { let mentions = Mention::list_for_post(conn, self.id).into_iter().map(|m| m.to_activity(conn)).collect::>(); let mut article = Article::default(); - article.object_props = ObjectProperties { - name: Some(serde_json::to_value(self.title.clone()).unwrap()), - id: Some(serde_json::to_value(self.ap_url.clone()).unwrap()), - attributed_to: Some(serde_json::to_value(self.get_authors(conn).into_iter().map(|x| x.ap_url).collect::>()).unwrap()), - content: Some(serde_json::to_value(self.content.clone()).unwrap()), - published: Some(serde_json::to_value(self.creation_date).unwrap()), - tag: Some(serde_json::to_value(mentions).unwrap()), - url: Some(serde_json::to_value(self.ap_url.clone()).unwrap()), - to: Some(serde_json::to_value(to).unwrap()), - cc: Some(serde_json::to_value(Vec::::new()).unwrap()), - ..ObjectProperties::default() - }; + article.object_props.set_name_string(self.title.clone()).expect("Article::intro activity: name error"); + article.object_props.set_id_string(self.ap_url.clone()).expect("Article::intro activity: id error"); + article.object_props.set_attributed_to_link_vec::(self.get_authors(conn).into_iter().map(|x| Id::new(x.ap_url)).collect()).expect("Article::intro activity: attributedTo error"); + article.object_props.set_content_string(self.content.get().clone()).expect("Article::intro activity: content error"); + article.object_props.set_published_utctime(Utc.from_utc_datetime(&self.creation_date)).expect("Article::intro activity: published error"); + article.object_props.set_tag_link_vec(mentions).expect("Article::intro activity: tag error"); + article.object_props.set_url_string(self.ap_url.clone()).expect("Article::intro activity: url error"); + article.object_props.set_to_link_vec::(to.into_iter().map(Id::new).collect()).expect("Article::intro activity: to error"); article }