Rewrite Post::into_activity in a nicer way

And prefer expect over unwrap
This commit is contained in:
Bat 2018-06-22 21:45:37 +01:00
parent 6a5d806b1d
commit 3b2ca041d0
2 changed files with 11 additions and 15 deletions

View file

@ -23,7 +23,7 @@ pub struct Like {
pub ap_url: String pub ap_url: String
} }
#[derive(Insertable)] #[derive(Default, Insertable)]
#[table_name = "likes"] #[table_name = "likes"]
pub struct NewLike { pub struct NewLike {
pub user_id: i32, pub user_id: i32,

View file

@ -1,9 +1,9 @@
use activitypub::{ use activitypub::{
activity::Create, activity::Create,
link, 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 diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any};
use serde_json; 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::<Vec<link::Mention>>(); let mentions = Mention::list_for_post(conn, self.id).into_iter().map(|m| m.to_activity(conn)).collect::<Vec<link::Mention>>();
let mut article = Article::default(); let mut article = Article::default();
article.object_props = ObjectProperties { article.object_props.set_name_string(self.title.clone()).expect("Article::intro activity: name error");
name: Some(serde_json::to_value(self.title.clone()).unwrap()), article.object_props.set_id_string(self.ap_url.clone()).expect("Article::intro activity: id error");
id: Some(serde_json::to_value(self.ap_url.clone()).unwrap()), article.object_props.set_attributed_to_link_vec::<Id>(self.get_authors(conn).into_iter().map(|x| Id::new(x.ap_url)).collect()).expect("Article::intro activity: attributedTo error");
attributed_to: Some(serde_json::to_value(self.get_authors(conn).into_iter().map(|x| x.ap_url).collect::<Vec<String>>()).unwrap()), article.object_props.set_content_string(self.content.get().clone()).expect("Article::intro activity: content error");
content: Some(serde_json::to_value(self.content.clone()).unwrap()), article.object_props.set_published_utctime(Utc.from_utc_datetime(&self.creation_date)).expect("Article::intro activity: published error");
published: Some(serde_json::to_value(self.creation_date).unwrap()), article.object_props.set_tag_link_vec(mentions).expect("Article::intro activity: tag error");
tag: Some(serde_json::to_value(mentions).unwrap()), article.object_props.set_url_string(self.ap_url.clone()).expect("Article::intro activity: url error");
url: Some(serde_json::to_value(self.ap_url.clone()).unwrap()), article.object_props.set_to_link_vec::<Id>(to.into_iter().map(Id::new).collect()).expect("Article::intro activity: to error");
to: Some(serde_json::to_value(to).unwrap()),
cc: Some(serde_json::to_value(Vec::<serde_json::Value>::new()).unwrap()),
..ObjectProperties::default()
};
article article
} }