chore: replace .get/.extract (and use shortcuts)

This commit is contained in:
əlemi 2024-11-20 18:05:00 +01:00
parent 7a78b07467
commit 298f0a3774
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 21 additions and 25 deletions

View file

@ -34,7 +34,7 @@ pub async fn fetch(ctx: upub::Context, uri: String, save: bool, actor: Option<St
}
let obj = node.extract().expect("node still empty after fetch?");
let obj = node.into_inner().expect("node still empty after fetch?");
println!("{}", serde_json::to_string_pretty(&obj).unwrap());

View file

@ -4,7 +4,7 @@ use apb::{Activity, Actor, ActorMut, Base, Collection, CollectionPage, Object};
use reqwest::{header::{ACCEPT, CONTENT_TYPE, USER_AGENT}, Method, Response};
use sea_orm::{ActiveValue::Set, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, IntoActiveModel, NotSet, QueryFilter};
use crate::traits::normalize::AP;
use crate::{ext::Shortcuts, traits::normalize::AP};
use super::{Addresser, Cloaker, Normalizer};
use httpsign::HttpSignature;
@ -414,7 +414,7 @@ impl Fetcher for crate::Context {
// fix for mastodon: at some point it introduces ?only_other_accounts=true and then returns a
// collection, not a page anymore ???
if matches!(page.object_type()?, apb::ObjectType::Collection(apb::CollectionType::Collection)) {
page = page.first().extract().ok_or(RequestError::Tombstone)?;
page = page.first().into_inner()?;
}
for obj in page.items().flat() {

View file

@ -1,5 +1,6 @@
use apb::{field::OptionalString, Collection, Document, Endpoints, Node, Object, PublicKey};
use apb::{field::OptionalString, Document, Endpoints, Node, Object, PublicKey};
use sea_orm::{sea_query::Expr, ActiveModelTrait, ActiveValue::{Unchanged, NotSet, Set}, ColumnTrait, ConnectionTrait, DbErr, EntityTrait, IntoActiveModel, QueryFilter};
use crate::ext::Shortcuts;
use super::{Cloaker, Fetcher};
@ -310,19 +311,16 @@ impl AP {
name: object.name().str(),
summary: object.summary().str(),
content: object.content().str(),
image: object.image().get().and_then(|x| x.url().id().str()),
image: object.image_url().ok(),
context: object.context().id().str(),
in_reply_to: object.in_reply_to().id().str(),
quote: object.quote_url().id().str(),
published: object.published().unwrap_or_else(|_| chrono::Utc::now()),
updated: object.updated().unwrap_or_else(|_| chrono::Utc::now()),
url: object.url().id().str(),
replies: object.replies().get()
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32,
likes: object.likes().get()
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32,
announces: object.shares().get()
.map_or(0, |x| x.total_items().unwrap_or(0)) as i32,
replies: object.replies_count().unwrap_or_default(),
likes: object.likes_count().unwrap_or_default(),
announces: object.shares_count().unwrap_or_default(),
audience: object.audience().id().str(),
to: object.to().all_ids().into(),
bto: object.bto().all_ids().into(),
@ -368,11 +366,11 @@ impl AP {
actor_type: actor.actor_type()?,
name: actor.name().str(),
summary: actor.summary().str(),
icon: actor.icon().get().and_then(|x| x.url().id().str()),
image: actor.image().get().and_then(|x| x.url().id().str()),
icon: actor.icon_url().ok(),
image: actor.image_url().ok(),
inbox: actor.inbox().id().str(),
outbox: actor.outbox().id().str(),
shared_inbox: actor.endpoints().get().and_then(|x| x.shared_inbox().str()),
shared_inbox: actor.endpoints().inner().and_then(|x| x.shared_inbox()).map(|x| x.to_string()).ok(),
followers: actor.followers().id().str(),
following: actor.following().id().str(),
also_known_as: actor.also_known_as().flat().into_iter().filter_map(|x| x.id().str()).collect::<Vec<String>>().into(),
@ -382,12 +380,12 @@ impl AP {
following_count: actor.following_count().unwrap_or(0) as i32,
followers_count: actor.followers_count().unwrap_or(0) as i32,
statuses_count: actor.statuses_count().unwrap_or(0) as i32,
public_key: actor.public_key().get().ok_or(apb::FieldErr("publicKey"))?.public_key_pem().to_string(),
public_key: actor.public_key().inner()?.public_key_pem().to_string(),
private_key: None, // there's no way to transport privkey over AP json, must come from DB
fields: actor.attachment()
.flat()
.into_iter()
.filter_map(|x| Some(crate::model::actor::Field::from(x.extract()?)))
.filter_map(|x| Some(crate::model::actor::Field::from(x.into_inner().ok()?)))
.collect::<Vec<crate::model::actor::Field>>()
.into(),
})

View file

@ -55,7 +55,7 @@ impl Processor for crate::Context {
}
pub async fn create(ctx: &crate::Context, activity: impl apb::Activity, tx: &DatabaseTransaction) -> Result<(), ProcessorError> {
let Some(object_node) = activity.object().extract() else {
let Ok(object_node) = activity.object().into_inner() else {
// TODO we could process non-embedded activities or arrays but im lazy rn
tracing::error!("refusing to process activity without embedded object");
return Err(ProcessorError::Unprocessable(activity.id()?.to_string()));
@ -356,7 +356,7 @@ pub async fn delete(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat
pub async fn update(ctx: &crate::Context, activity: impl apb::Activity, tx: &DatabaseTransaction) -> Result<(), ProcessorError> {
// TODO when attachments get updated we do nothing!!!!!!!!!!
let Some(object_node) = activity.object().extract() else {
let Ok(object_node) = activity.object().into_inner() else {
tracing::error!("refusing to process activity without embedded object");
return Err(ProcessorError::Unprocessable(activity.id()?.to_string()));
};
@ -415,9 +415,7 @@ pub async fn update(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat
pub async fn undo(ctx: &crate::Context, activity: impl apb::Activity, tx: &DatabaseTransaction) -> Result<(), ProcessorError> {
// TODO in theory we could work with just object_id but right now only accept embedded
let undone_activity = activity.object()
.extract()
.ok_or(apb::FieldErr("object"))?;
let undone_activity = activity.object().into_inner()?;
let uid = activity.actor().id()?.to_string();
let internal_uid = crate::model::actor::Entity::ap_to_internal(&uid, tx)

View file

@ -46,7 +46,7 @@ pub async fn process(ctx: Context, job: &model::job::Model) -> crate::JobResult<
.set_published(Some(now));
if matches!(t, apb::ObjectType::Activity(apb::ActivityType::Undo)) {
let mut undone = activity.object().extract().ok_or(crate::JobError::MissingPayload)?;
let mut undone = activity.object().into_inner()?;
if undone.id().is_err() {
let undone_target = undone.object().id().str().ok_or(crate::JobError::MissingPayload)?;
let undone_type = undone.activity_type().map_err(|_| crate::JobError::MissingPayload)?;
@ -74,7 +74,7 @@ pub async fn process(ctx: Context, job: &model::job::Model) -> crate::JobResult<
}
if matches!(t, apb::ObjectType::Activity(apb::ActivityType::Update)) {
let mut updated = activity.object().extract().ok_or(crate::JobError::MissingPayload)?;
let mut updated = activity.object().into_inner()?;
match updated.object_type()? {
apb::ObjectType::Actor(_) => {
let mut prev = model::actor::Entity::find_by_ap_id(updated.id()?)
@ -95,7 +95,7 @@ pub async fn process(ctx: Context, job: &model::job::Model) -> crate::JobResult<
prev.fields = updated.attachment()
.flat()
.into_iter()
.filter_map(|x| x.extract())
.filter_map(|x| x.into_inner().ok())
.map(Field::from)
.collect::<Vec<Field>>()
.into();
@ -133,7 +133,7 @@ pub async fn process(ctx: Context, job: &model::job::Model) -> crate::JobResult<
let raw_oid = Context::new_id();
let oid = ctx.oid(&raw_oid);
// object must be embedded, wont dereference here
let object = activity.object().extract().ok_or(apb::FieldErr("object"))?;
let object = activity.object().into_inner()?;
// TODO regex hell here i come...
let re = regex::Regex::new(r"@(.+)@([^ ]+)").expect("failed compiling regex pattern");
let mut content = object.content().map(|x| x.to_string()).ok();