Use Post::create_activity07() instead of create_activity()

This commit is contained in:
Kitaiti Makoto 2022-05-02 22:19:00 +09:00
parent 39b49c707e
commit a589435f4f
4 changed files with 14 additions and 122 deletions

View file

@ -74,7 +74,7 @@ impl<T> ActivityStream<T> {
}
}
impl<'r, O: Object> Responder<'r> for ActivityStream<O> {
impl<'r, O: serde::Serialize> Responder<'r> for ActivityStream<O> {
fn respond_to(self, request: &Request<'_>) -> Result<Response<'r>, Status> {
let mut json = serde_json::to_value(&self.0).map_err(|_| Status::InternalServerError)?;
json["@context"] = context();

View file

@ -4,7 +4,7 @@ use crate::{
Connection, Error, PostEvent::*, Result, CONFIG, POST_CHAN,
};
use activitypub::{
activity::{Create, Delete, Update},
activity::{Delete, Update},
object::{Article, Image, Tombstone},
CustomObject,
};
@ -496,21 +496,6 @@ impl Post {
Ok(LicensedArticle07::new(article, license, source))
}
pub fn create_activity(&self, conn: &Connection) -> Result<Create> {
let article = self.to_activity(conn)?;
let mut act = Create::default();
act.object_props
.set_id_string(format!("{}/activity", self.ap_url))?;
act.object_props
.set_to_link_vec::<Id>(article.object.object_props.to_link_vec()?)?;
act.object_props
.set_cc_link_vec::<Id>(article.object.object_props.cc_link_vec()?)?;
act.create_props
.set_actor_link(Id::new(self.get_authors(conn)?[0].clone().ap_url))?;
act.create_props.set_object_object(article)?;
Ok(act)
}
pub fn create_activity07(&self, conn: &Connection) -> Result<Create07> {
let article = self.to_activity07(conn)?;
let to = article.to().ok_or(Error::MissingApProperty)?.clone();
@ -1236,56 +1221,6 @@ mod tests {
(post.to_owned(), mention.to_owned(), posts, users, blogs)
}
// creates a post, get it's Create activity, delete the post,
// "send" the Create to the inbox, and check it works
#[test]
fn self_federation() {
let conn = &db();
conn.test_transaction::<_, (), _>(|| {
let (_, users, blogs) = fill_database(&conn);
let post = Post::insert(
&conn,
NewPost {
blog_id: blogs[0].id,
slug: "yo".into(),
title: "Yo".into(),
content: SafeString::new("Hello"),
published: true,
license: "WTFPL".to_string(),
creation_date: None,
ap_url: String::new(), // automatically updated when inserting
subtitle: "Testing".into(),
source: "Hello".into(),
cover_id: None,
},
)
.unwrap();
PostAuthor::insert(
&conn,
NewPostAuthor {
post_id: post.id,
author_id: users[0].id,
},
)
.unwrap();
let create = post.create_activity(&conn).unwrap();
post.delete(&conn).unwrap();
match inbox(&conn, serde_json::to_value(create).unwrap()).unwrap() {
InboxResult::Post(p) => {
assert!(p.is_author(&conn, users[0].id).unwrap());
assert_eq!(p.source, "Hello".to_owned());
assert_eq!(p.blog_id, blogs[0].id);
assert_eq!(p.content, SafeString::new("Hello"));
assert_eq!(p.subtitle, "Testing".to_owned());
assert_eq!(p.title, "Yo".to_owned());
}
_ => panic!("Unexpected result"),
};
Ok(())
});
}
// creates a post, get it's Create activity, delete the post,
// "send" the Create to the inbox, and check it works
#[test]
@ -1455,51 +1390,6 @@ mod tests {
});
}
#[test]
fn create_activity() {
let conn = db();
conn.test_transaction::<_, Error, _>(|| {
let (post, _mention, _posts, _users, _blogs) = prepare_activity(&conn);
let act = post.create_activity(&conn)?;
let expected = json!({
"actor": "https://plu.me/@/admin/",
"cc": [],
"id": "https://plu.me/~/BlogName/testing/activity",
"object": {
"attributedTo": ["https://plu.me/@/admin/", "https://plu.me/~/BlogName/"],
"cc": [],
"content": "Hello",
"id": "https://plu.me/~/BlogName/testing",
"license": "WTFPL",
"name": "Testing",
"published": format_datetime(&post.creation_date),
"source": {
"content": "Hello",
"mediaType": "text/markdown"
},
"summary": "Bye",
"tag": [
{
"href": "https://plu.me/@/user/",
"name": "@user",
"type": "Mention"
}
],
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"type": "Article",
"url": "https://plu.me/~/BlogName/testing"
},
"to": ["https://www.w3.org/ns/activitystreams#Public"],
"type": "Create"
});
assert_json_eq!(to_value(act)?, expected);
Ok(())
});
}
#[test]
fn create_activity07() {
let conn = db();

View file

@ -724,7 +724,7 @@ impl User {
Ok(posts
.into_iter()
.filter_map(|p| {
p.create_activity(conn)
p.create_activity07(conn)
.ok()
.and_then(|a| serde_json::to_value(a).ok())
})

View file

@ -15,7 +15,9 @@ use crate::routes::{
};
use crate::template_utils::{IntoContext, Ructe};
use crate::utils::requires_login;
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest};
use plume_common::activity_pub::{
broadcast, broadcast07, ActivityStream, ApRequest, LicensedArticle as LicensedArticle07,
};
use plume_common::utils::md_to_html;
use plume_models::{
blogs::*,
@ -106,12 +108,12 @@ pub fn activity_details(
slug: String,
_ap: ApRequest,
conn: DbConn,
) -> Result<ActivityStream<LicensedArticle>, Option<String>> {
) -> Result<ActivityStream<LicensedArticle07>, Option<String>> {
let blog = Blog::find_by_fqn(&conn, &blog).map_err(|_| None)?;
let post = Post::find_by_slug(&conn, &slug, blog.id).map_err(|_| None)?;
if post.published {
Ok(ActivityStream::new(
post.to_activity(&conn)
post.to_activity07(&conn)
.map_err(|_| String::from("Post serialization error"))?,
))
} else {
@ -340,22 +342,22 @@ pub fn update(
if post.published {
if newly_published {
let act = post
.create_activity(&conn)
.create_activity07(&conn)
.expect("post::update: act error");
let dest = User::one_by_instance(&conn).expect("post::update: dest error");
rockets
.worker
.execute(move || broadcast(&user, act, dest, CONFIG.proxy().cloned()));
.execute(move || broadcast07(&user, act, dest, CONFIG.proxy().cloned()));
Timeline::add_to_all_timelines(&conn, &post, Kind::Original).ok();
} else {
let act = post
.update_activity(&conn)
.update_activity07(&conn)
.expect("post::update: act error");
let dest = User::one_by_instance(&conn).expect("posts::update: dest error");
rockets
.worker
.execute(move || broadcast(&user, act, dest, CONFIG.proxy().cloned()));
.execute(move || broadcast07(&user, act, dest, CONFIG.proxy().cloned()));
}
}
@ -539,11 +541,11 @@ pub fn create(
}
let act = post
.create_activity(&conn)
.create_activity07(&conn)
.expect("posts::create: activity error");
let dest = User::one_by_instance(&conn).expect("posts::create: dest error");
let worker = &rockets.worker;
worker.execute(move || broadcast(&user, act, dest, CONFIG.proxy().cloned()));
worker.execute(move || broadcast07(&user, act, dest, CONFIG.proxy().cloned()));
Timeline::add_to_all_timelines(&conn, &post, Kind::Original)?;
}