From 5ae9a140b4ff132aa70b801711d2221271626302 Mon Sep 17 00:00:00 2001 From: alemi Date: Mon, 29 Apr 2024 20:30:29 +0200 Subject: [PATCH] feat: show likes and shares and replies in objects it's done with anonymous inline collections, which hold a "totalItems" field. for replies it's perfect, for likes it's stretched ("audience", used as a Collection) and for shares it's really stretched ("generator", used as a Collection). also using audience and generator as collections seems weird because they should be objects but collections are objects so it should be fine? i haven't seen these fields used anyway so it should be safe to "claim" it for ourselves? --- src/model/object.rs | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/model/object.rs b/src/model/object.rs index 3087b12..731288c 100644 --- a/src/model/object.rs +++ b/src/model/object.rs @@ -1,4 +1,4 @@ -use apb::{BaseMut, ObjectMut, Collection}; +use apb::{BaseMut, Object, Collection, CollectionMut, ObjectMut}; use sea_orm::entity::prelude::*; use crate::routes::activitypub::jsonld::LD; @@ -41,9 +41,21 @@ impl Model { context: object.context().id(), in_reply_to: object.in_reply_to().id(), published: object.published().ok_or(super::FieldError("published"))?, - comments: object.replies().get().map(|x| x.total_items().unwrap_or(0)).unwrap_or(0) as i64, - likes: 0, - shares: 0, + comments: object.replies() + .get() + .map_or(0, |x| x.total_items().unwrap_or(0)) as i64, + likes: object.audience() + .get() + .map_or(0, |x| + x.as_collection() + .map_or(0, |x| x.total_items().unwrap_or(0)) + ) as i64, + shares: object.generator() + .get() + .map_or(0, |x| + x.as_collection() + .map_or(0, |x| x.total_items().unwrap_or(0)) + ) as i64, to: object.to().into(), bto: object.bto().into(), cc: object.cc().into(), @@ -69,6 +81,21 @@ impl Model { .set_cc(apb::Node::links(self.cc.0.clone())) .set_bcc(apb::Node::Empty) .set_sensitive(Some(self.sensitive)) + .set_generator(apb::Node::object( + serde_json::Value::new_object() + .set_collection_type(Some(apb::CollectionType::OrderedCollection)) + .set_total_items(Some(self.shares as u64)) + )) + .set_audience(apb::Node::object( + serde_json::Value::new_object() + .set_collection_type(Some(apb::CollectionType::OrderedCollection)) + .set_total_items(Some(self.likes as u64)) + )) + .set_replies(apb::Node::object( + serde_json::Value::new_object() + .set_collection_type(Some(apb::CollectionType::OrderedCollection)) + .set_total_items(Some(self.comments as u64)) + )) } }