fix: wrap all bare objects, with getter to skip

This commit is contained in:
əlemi 2024-06-28 01:27:21 +02:00
parent bb79ca7728
commit ddb1ee7319
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 50 additions and 32 deletions

View file

@ -30,6 +30,11 @@ impl Query {
select = select.select_column_as(col, format!("{}{}", model::object::Entity.table_name(), col.to_string())); select = select.select_column_as(col, format!("{}{}", model::object::Entity.table_name(), col.to_string()));
} }
select = select.select_column_as(
model::addressing::Column::Published,
format!("{}{}", model::addressing::Entity.table_name(), model::addressing::Column::Published.to_string())
);
if let Some(uid) = my_id { if let Some(uid) = my_id {
select = select select = select
.join( .join(

View file

@ -37,12 +37,14 @@ pub struct RichActivity {
pub attachments: Option<Vec<crate::model::attachment::Model>>, pub attachments: Option<Vec<crate::model::attachment::Model>>,
pub hashtags: Option<Vec<RichHashtag>>, pub hashtags: Option<Vec<RichHashtag>>,
pub mentions: Option<Vec<RichMention>>, pub mentions: Option<Vec<RichMention>>,
pub discovered: chrono::DateTime<chrono::Utc>,
} }
impl FromQueryResult for RichActivity { impl FromQueryResult for RichActivity {
fn from_query_result(res: &QueryResult, _pre: &str) -> Result<Self, DbErr> { fn from_query_result(res: &QueryResult, _pre: &str) -> Result<Self, DbErr> {
Ok(RichActivity { Ok(RichActivity {
attachments: None, hashtags: None, mentions: None, attachments: None, hashtags: None, mentions: None,
discovered: res.try_get(crate::model::addressing::Entity.table_name(), &crate::model::addressing::Column::Published.to_string())?,
liked: res.try_get(crate::model::like::Entity.table_name(), &crate::model::like::Column::Actor.to_string()).ok(), liked: res.try_get(crate::model::like::Entity.table_name(), &crate::model::like::Column::Actor.to_string()).ok(),
object: crate::model::object::Model::from_query_result(res, crate::model::object::Entity.table_name()).ok(), object: crate::model::object::Model::from_query_result(res, crate::model::object::Entity.table_name()).ok(),
activity: crate::model::activity::Model::from_query_result(res, crate::model::activity::Entity.table_name()).ok(), activity: crate::model::activity::Model::from_query_result(res, crate::model::activity::Entity.table_name()).ok(),
@ -50,7 +52,6 @@ impl FromQueryResult for RichActivity {
} }
} }
// TODO avoid repeating the tags code twice
impl RichActivity { impl RichActivity {
pub fn ap(self) -> serde_json::Value { pub fn ap(self) -> serde_json::Value {
use apb::ObjectMut; use apb::ObjectMut;
@ -62,7 +63,47 @@ impl RichActivity {
activity.ap().set_object(obj) activity.ap().set_object(obj)
}, },
(None, Some(object)) => { (maybe_activity, Some(object)) => {
let mut tags = Vec::new();
if let Some(mentions) = self.mentions {
for mention in mentions {
tags.push(mention.ap());
}
}
if let Some(hashtags) = self.hashtags {
for hash in hashtags {
tags.push(hash.ap());
}
}
let activity = match maybe_activity {
Some(activity) => activity.ap(),
None => apb::new()
.set_activity_type(Some(apb::ActivityType::View))
.set_published(Some(self.discovered))
};
activity
.set_object(apb::Node::object(
object.ap()
.set_liked_by_me(if self.liked.is_some() { Some(true) } else { None })
.set_tag(apb::Node::maybe_array(tags))
.set_attachment(match self.attachments {
None => apb::Node::Empty,
Some(vec) => apb::Node::array(
vec.into_iter().map(|x| x.ap()).collect()
),
})
))
},
}
}
// TODO ughhh cant make it a trait because there's this different one!!!
pub fn object_ap(self) -> serde_json::Value {
use apb::ObjectMut;
match self.object {
Some(object) => {
let mut tags = Vec::new(); let mut tags = Vec::new();
if let Some(mentions) = self.mentions { if let Some(mentions) = self.mentions {
for mention in mentions { for mention in mentions {
@ -84,34 +125,7 @@ impl RichActivity {
), ),
}) })
}, },
None => serde_json::Value::Null,
(Some(activity), Some(object)) => {
let mut tags = Vec::new();
if let Some(mentions) = self.mentions {
for mention in mentions {
tags.push(mention.ap());
}
}
if let Some(hashtags) = self.hashtags {
for hash in hashtags {
tags.push(hash.ap());
}
}
activity.ap()
.set_object(
apb::Node::object(
object.ap()
.set_liked_by_me(if self.liked.is_some() { Some(true) } else { None })
.set_tag(apb::Node::maybe_array(tags))
.set_attachment(match self.attachments {
None => apb::Node::Empty,
Some(vec) => apb::Node::array(
vec.into_iter().map(|x| x.ap()).collect()
),
})
)
)
},
} }
} }
} }

View file

@ -41,7 +41,6 @@ pub async fn view(
.with_batched::<upub::model::hashtag::Entity>(ctx.db()) .with_batched::<upub::model::hashtag::Entity>(ctx.db())
.await?; .await?;
let mut replies = apb::Node::Empty; let mut replies = apb::Node::Empty;
if ctx.cfg().security.show_reply_ids { if ctx.cfg().security.show_reply_ids {
@ -65,7 +64,7 @@ pub async fn view(
} }
Ok(JsonLD( Ok(JsonLD(
item.ap() item.object_ap()
.set_replies(replies) .set_replies(replies)
.ld_context() .ld_context()
)) ))