forked from nutomic/lemmy
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:
parent
5976006859
commit
1043a6b7c2
1 changed files with 106 additions and 100 deletions
|
@ -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,43 +69,74 @@ 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,
|
||||||
|
updated: Option<XsdDateTime>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct ValidOutbox {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
kind: OrderedCollectionType,
|
||||||
|
|
||||||
|
items: Vec<ValidPage>,
|
||||||
|
total_items: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
struct ValidPage {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
kind: PageType,
|
||||||
|
|
||||||
|
name: String,
|
||||||
|
url: Option<String>,
|
||||||
|
content: Option<String>,
|
||||||
|
published: XsdDateTime,
|
||||||
|
updated: Option<XsdDateTime>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct ValidFollowers {
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[serde(rename = "type")]
|
||||||
|
kind: CollectionType,
|
||||||
|
|
||||||
|
total_items: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse, Error> {
|
||||||
|
let community = fetch_remote_object::<ValidCommunity>(&get_remote_community_uri(identifier))?;
|
||||||
|
let outbox_uri = &community.outbox;
|
||||||
|
|
||||||
|
let outbox = fetch_remote_object::<ValidOutbox>(outbox_uri)?;
|
||||||
|
|
||||||
|
let posts: Vec<PostView> = outbox
|
||||||
|
.items
|
||||||
|
.into_iter()
|
||||||
|
.map(|page| PostView {
|
||||||
id: -1,
|
id: -1,
|
||||||
name: page.object_props.get_name_xsd_string().unwrap().to_string(),
|
name: page.name,
|
||||||
url: page
|
url: page.url,
|
||||||
.object_props
|
body: page.content,
|
||||||
.get_url_xsd_any_uri()
|
|
||||||
.map(|u| u.to_string()),
|
|
||||||
body: page
|
|
||||||
.object_props
|
|
||||||
.get_content_xsd_string()
|
|
||||||
.map(|c| c.to_string()),
|
|
||||||
creator_id: -1,
|
creator_id: -1,
|
||||||
community_id: -1,
|
community_id: -1,
|
||||||
removed: false,
|
removed: false,
|
||||||
locked: false,
|
locked: false,
|
||||||
published: page
|
published: page.published.as_datetime().naive_local(),
|
||||||
.object_props
|
updated: page.updated.map(|u| u.as_ref().naive_local()),
|
||||||
.get_published()
|
|
||||||
.unwrap()
|
|
||||||
.as_ref()
|
|
||||||
.naive_local()
|
|
||||||
.to_owned(),
|
|
||||||
updated: page
|
|
||||||
.object_props
|
|
||||||
.get_updated()
|
|
||||||
.map(|u| u.as_ref().to_owned().naive_local()),
|
|
||||||
deleted: false,
|
deleted: false,
|
||||||
nsfw: false,
|
nsfw: false,
|
||||||
stickied: false,
|
stickied: false,
|
||||||
|
@ -133,19 +163,15 @@ pub fn get_remote_community_posts(identifier: &str) -> Result<GetPostsResponse,
|
||||||
subscribed: None,
|
subscribed: None,
|
||||||
read: None,
|
read: None,
|
||||||
saved: 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,
|
||||||
|
|
Loading…
Reference in a new issue