Consider the following

If you're going to throw out records where certain fields are missing
anyway, you might as well define your own types that are easier to work
with. You don't need the full expressiveness of the activitystreams
built-in types if you're just calling unwrap everywhere.
This commit is contained in:
asonix 2020-03-19 15:37:00 -05:00
parent 5976006859
commit 1043a6b7c2
1 changed files with 106 additions and 100 deletions

View File

@ -8,11 +8,10 @@ use crate::db::post_view::PostView;
use crate::naive_now; use crate::naive_now;
use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown}; use crate::routes::nodeinfo::{NodeInfo, NodeInfoWellKnown};
use crate::settings::Settings; use crate::settings::Settings;
use activitystreams::actor::{properties::ApActorProperties, Group}; use activitystreams::actor::kind::GroupType;
use activitystreams::collection::{OrderedCollection, UnorderedCollection}; use activitystreams::collection::kind::{CollectionType, OrderedCollectionType};
use activitystreams::ext::Ext; use activitystreams::object::kind::PageType;
use activitystreams::object::ObjectBox; use activitystreams::primitives::XsdDateTime;
use activitystreams::object::Page;
use failure::Error; use failure::Error;
use log::warn; use log::warn;
use serde::Deserialize; use serde::Deserialize;
@ -70,82 +69,109 @@ where
Ok(x) Ok(x)
} }
pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> { #[derive(serde::Deserialize)]
let community = struct ValidCommunity {
fetch_remote_object::<Ext<Group, ApActorProperties>>(&get_remote_community_uri(identifier))?; #[allow(dead_code)]
let outbox_uri = &community.extension.get_outbox().to_string(); #[serde(rename = "type")]
let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?; kind: GroupType,
let items = outbox.collection_props.get_many_items_object_boxs();
let posts: Vec<PostView> = items followers: String,
.unwrap() outbox: String,
.map(|obox: &ObjectBox| { name: String,
let page: Page = obox.clone().to_concrete::<Page>().unwrap(); summary: Option<String>,
PostView { published: XsdDateTime,
id: -1, updated: Option<XsdDateTime>,
name: page.object_props.get_name_xsd_string().unwrap().to_string(), }
url: page
.object_props #[derive(serde::Deserialize)]
.get_url_xsd_any_uri() #[serde(rename_all = "camelCase")]
.map(|u| u.to_string()), struct ValidOutbox {
body: page #[allow(dead_code)]
.object_props #[serde(rename = "type")]
.get_content_xsd_string() kind: OrderedCollectionType,
.map(|c| c.to_string()),
creator_id: -1, items: Vec<ValidPage>,
community_id: -1, total_items: i64,
removed: false, }
locked: false,
published: page #[derive(serde::Deserialize)]
.object_props struct ValidPage {
.get_published() #[allow(dead_code)]
.unwrap() #[serde(rename = "type")]
.as_ref() kind: PageType,
.naive_local()
.to_owned(), name: String,
updated: page url: Option<String>,
.object_props content: Option<String>,
.get_updated() published: XsdDateTime,
.map(|u| u.as_ref().to_owned().naive_local()), updated: Option<XsdDateTime>,
deleted: false, }
nsfw: false,
stickied: false, #[derive(serde::Deserialize)]
embed_title: None, #[serde(rename_all = "camelCase")]
embed_description: None, struct ValidFollowers {
embed_html: None, #[allow(dead_code)]
thumbnail_url: None, #[serde(rename = "type")]
banned: false, kind: CollectionType,
banned_from_community: false,
creator_name: "".to_string(), total_items: i64,
creator_avatar: None, }
community_name: "".to_string(),
community_removed: false, pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> {
community_deleted: false, let community = fetch_remote_object::<ValidCommunity>(&get_remote_community_uri(identifier))?;
community_nsfw: false, let outbox_uri = &community.outbox;
number_of_comments: -1,
score: -1, let outbox = fetch_remote_object::<ValidOutbox>(outbox_uri)?;
upvotes: -1,
downvotes: -1, let posts: Vec<PostView> = outbox
hot_rank: -1, .items
newest_activity_time: naive_now(), .into_iter()
user_id: None, .map(|page| PostView {
my_vote: None, id: -1,
subscribed: None, name: page.name,
read: None, url: page.url,
saved: None, body: page.content,
} creator_id: -1,
community_id: -1,
removed: false,
locked: false,
published: page.published.as_datetime().naive_local(),
updated: page.updated.map(|u| u.as_ref().naive_local()),
deleted: false,
nsfw: false,
stickied: false,
embed_title: None,
embed_description: None,
embed_html: None,
thumbnail_url: None,
banned: false,
banned_from_community: false,
creator_name: "".to_string(),
creator_avatar: None,
community_name: "".to_string(),
community_removed: false,
community_deleted: false,
community_nsfw: false,
number_of_comments: -1,
score: -1,
upvotes: -1,
downvotes: -1,
hot_rank: -1,
newest_activity_time: naive_now(),
user_id: None,
my_vote: None,
subscribed: None,
read: None,
saved: None,
}) })
.collect(); .collect();
Ok(GetPostsResponse { posts }) Ok(GetPostsResponse { posts })
} }
pub fn get_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> { pub fn get_remote_community(identifier: &str) -> Result<GetCommunityResponse, failure::Error> {
let community = let community = fetch_remote_object::<ValidCommunity>(&get_remote_community_uri(identifier))?;
fetch_remote_object::<Ext<Group, ApActorProperties>>(&get_remote_community_uri(identifier))?; let outbox = fetch_remote_object::<ValidOutbox>(&community.outbox)?;
let followers_uri = &community.extension.get_followers().unwrap().to_string(); let followers = fetch_remote_object::<ValidFollowers>(&community.followers)?;
let outbox_uri = &community.extension.get_outbox().to_string();
let outbox = fetch_remote_object::<OrderedCollection>(outbox_uri)?;
let followers = fetch_remote_object::<UnorderedCollection>(followers_uri)?;
// TODO: this is only for testing until we can call that function from GetPosts // TODO: this is only for testing until we can call that function from GetPosts
// (once string ids are supported) // (once string ids are supported)
//dbg!(get_remote_community_posts(identifier)?); //dbg!(get_remote_community_posts(identifier)?);
@ -157,40 +183,20 @@ pub fn get_remote_community(identifier: &str) -> Result<GetCommunityResponse, fa
// TODO: we need to merge id and name into a single thing (stuff like @user@instance.com) // TODO: we need to merge id and name into a single thing (stuff like @user@instance.com)
id: 1337, //community.object_props.get_id() id: 1337, //community.object_props.get_id()
name: identifier.to_string(), name: identifier.to_string(),
title: community title: community.name,
.as_ref() description: community.summary,
.get_name_xsd_string()
.unwrap()
.to_string(),
description: community
.as_ref()
.get_summary_xsd_string()
.map(|s| s.to_string()),
category_id: -1, category_id: -1,
creator_id: -1, //community.object_props.get_attributed_to_xsd_any_uri() creator_id: -1, //community.object_props.get_attributed_to_xsd_any_uri()
removed: false, removed: false,
published: community published: community.published.as_datetime().naive_local(),
.as_ref() updated: community.updated.map(|u| u.as_datetime().naive_local()),
.get_published()
.unwrap()
.as_ref()
.naive_local()
.to_owned(),
updated: community
.as_ref()
.get_updated()
.map(|u| u.as_ref().to_owned().naive_local()),
deleted: false, deleted: false,
nsfw: false, nsfw: false,
creator_name: "".to_string(), creator_name: "".to_string(),
creator_avatar: None, creator_avatar: None,
category_name: "".to_string(), category_name: "".to_string(),
number_of_subscribers: *followers number_of_subscribers: followers.total_items,
.collection_props number_of_posts: outbox.total_items,
.get_total_items()
.unwrap()
.as_ref() as i64, // TODO: need to use the same type
number_of_posts: *outbox.collection_props.get_total_items().unwrap().as_ref() as i64,
number_of_comments: -1, number_of_comments: -1,
hot_rank: -1, hot_rank: -1,
user_id: None, user_id: None,