Use Blog::outbox_page07() instead of outbox_page()

This commit is contained in:
Kitaiti Makoto 2022-05-02 23:01:26 +09:00
parent 0524b0b153
commit 4b4c22cf8a
2 changed files with 11 additions and 65 deletions

View File

@ -2,13 +2,11 @@ use crate::{
db_conn::DbConn, instance::*, medias::Media, posts::Post, safe_string::SafeString, db_conn::DbConn, instance::*, medias::Media, posts::Post, safe_string::SafeString,
schema::blogs, users::User, Connection, Error, PlumeRocket, Result, CONFIG, ITEMS_PER_PAGE, schema::blogs, users::User, Connection, Error, PlumeRocket, Result, CONFIG, ITEMS_PER_PAGE,
}; };
use activitypub::{actor::Group, collection::OrderedCollectionPage, object::Image, CustomObject}; use activitypub::{actor::Group, object::Image, CustomObject};
use activitystreams::{ use activitystreams::{
actor::{ApActor, ApActorExt, AsApActor, Group as Group07}, actor::{ApActor, ApActorExt, AsApActor, Group as Group07},
base::AnyBase, base::AnyBase,
collection::{ collection::{OrderedCollection, OrderedCollectionPage},
OrderedCollection as OrderedCollection07, OrderedCollectionPage as OrderedCollectionPage07,
},
iri_string::types::IriString, iri_string::types::IriString,
object::{kind::ImageType, ApObject, Image as Image07, ObjectExt}, object::{kind::ImageType, ApObject, Image as Image07, ObjectExt},
prelude::*, prelude::*,
@ -288,17 +286,17 @@ impl Blog {
Ok(CustomGroup07::new(blog, ap_signature, source)) Ok(CustomGroup07::new(blog, ap_signature, source))
} }
pub fn outbox07(&self, conn: &Connection) -> Result<ActivityStream<OrderedCollection07>> { pub fn outbox07(&self, conn: &Connection) -> Result<ActivityStream<OrderedCollection>> {
self.outbox_collection07(conn).map(ActivityStream::new) self.outbox_collection07(conn).map(ActivityStream::new)
} }
pub fn outbox_collection07(&self, conn: &Connection) -> Result<OrderedCollection07> { pub fn outbox_collection07(&self, conn: &Connection) -> Result<OrderedCollection> {
let acts = self.get_activities(conn); let acts = self.get_activities(conn);
let acts = acts let acts = acts
.iter() .iter()
.filter_map(|value| AnyBase::from_arbitrary_json(value).ok()) .filter_map(|value| AnyBase::from_arbitrary_json(value).ok())
.collect::<Vec<AnyBase>>(); .collect::<Vec<AnyBase>>();
let n_acts = acts.len(); let n_acts = acts.len();
let mut coll = OrderedCollection07::new(); let mut coll = OrderedCollection::new();
coll.set_many_items(acts); coll.set_many_items(acts);
coll.set_total_items(n_acts as u64); coll.set_total_items(n_acts as u64);
coll.set_first(format!("{}?page=1", &self.outbox_url).parse::<IriString>()?); coll.set_first(format!("{}?page=1", &self.outbox_url).parse::<IriString>()?);
@ -312,41 +310,11 @@ impl Blog {
); );
Ok(coll) Ok(coll)
} }
pub fn outbox_page(
&self,
conn: &Connection,
(min, max): (i32, i32),
) -> Result<ActivityStream<OrderedCollectionPage>> {
self.outbox_collection_page(conn, (min, max))
.map(ActivityStream::new)
}
pub fn outbox_collection_page(
&self,
conn: &Connection,
(min, max): (i32, i32),
) -> Result<OrderedCollectionPage> {
let mut coll = OrderedCollectionPage::default();
let acts = self.get_activity_page(conn, (min, max));
//This still doesn't do anything because the outbox
//doesn't do anything yet
coll.collection_page_props.set_next_link(Id::new(&format!(
"{}?page={}",
&self.outbox_url,
min / ITEMS_PER_PAGE + 1
)))?;
coll.collection_page_props.set_prev_link(Id::new(&format!(
"{}?page={}",
&self.outbox_url,
min / ITEMS_PER_PAGE - 1
)))?;
coll.collection_props.items = serde_json::to_value(acts)?;
Ok(coll)
}
pub fn outbox_page07( pub fn outbox_page07(
&self, &self,
conn: &Connection, conn: &Connection,
(min, max): (i32, i32), (min, max): (i32, i32),
) -> Result<ActivityStream<OrderedCollectionPage07>> { ) -> Result<ActivityStream<OrderedCollectionPage>> {
self.outbox_collection_page07(conn, (min, max)) self.outbox_collection_page07(conn, (min, max))
.map(ActivityStream::new) .map(ActivityStream::new)
} }
@ -354,8 +322,8 @@ impl Blog {
&self, &self,
conn: &Connection, conn: &Connection,
(min, max): (i32, i32), (min, max): (i32, i32),
) -> Result<OrderedCollectionPage07> { ) -> Result<OrderedCollectionPage> {
let mut coll = OrderedCollectionPage07::new(); let mut coll = OrderedCollectionPage::new();
let acts = self.get_activity_page(conn, (min, max)); let acts = self.get_activity_page(conn, (min, max));
//This still doesn't do anything because the outbox //This still doesn't do anything because the outbox
//doesn't do anything yet //doesn't do anything yet
@ -1183,27 +1151,6 @@ pub(crate) mod tests {
}); });
} }
#[test]
fn outbox_collection_page() {
let conn = &db();
conn.test_transaction::<_, Error, _>(|| {
let (_users, blogs) = fill_database(conn);
let blog = &blogs[0];
let act = blog.outbox_collection_page(conn, (33, 36))?;
let expected = json!({
"next": "https://plu.me/~/BlogName/outbox?page=3",
"prev": "https://plu.me/~/BlogName/outbox?page=1",
"items": [],
"type": "OrderedCollectionPage"
});
assert_json_eq!(to_value(act)?, expected);
Ok(())
});
}
#[test] #[test]
fn outbox_collection_page07() { fn outbox_collection_page07() {
let conn = &db(); let conn = &db();

View File

@ -1,5 +1,4 @@
use activitypub::collection::OrderedCollectionPage; use activitystreams::collection::{OrderedCollection, OrderedCollectionPage};
use activitystreams::collection::OrderedCollection as OrderedCollection07;
use diesel::SaveChangesDsl; use diesel::SaveChangesDsl;
use rocket::{ use rocket::{
http::ContentType, http::ContentType,
@ -348,7 +347,7 @@ pub fn update(
} }
#[get("/~/<name>/outbox")] #[get("/~/<name>/outbox")]
pub fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection07>> { pub fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection>> {
let blog = Blog::find_by_fqn(&conn, &name).ok()?; let blog = Blog::find_by_fqn(&conn, &name).ok()?;
blog.outbox07(&conn).ok() blog.outbox07(&conn).ok()
} }
@ -360,7 +359,7 @@ pub fn outbox_page(
conn: DbConn, conn: DbConn,
) -> Option<ActivityStream<OrderedCollectionPage>> { ) -> Option<ActivityStream<OrderedCollectionPage>> {
let blog = Blog::find_by_fqn(&conn, &name).ok()?; let blog = Blog::find_by_fqn(&conn, &name).ok()?;
blog.outbox_page(&conn, page.limits()).ok() blog.outbox_page07(&conn, page.limits()).ok()
} }
#[get("/~/<name>/atom.xml")] #[get("/~/<name>/atom.xml")]
pub fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> { pub fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> {