more fixes

This commit is contained in:
Felix Ableitner 2020-05-30 15:38:01 +02:00
parent d6e2119277
commit 52206998aa
4 changed files with 85 additions and 45 deletions

View file

@ -34,9 +34,6 @@ use diesel::{
use failure::Error;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use crate::apub::get_apub_protocol_string;
use crate::db::community::Community;
use url::Url;
#[derive(Serialize, Deserialize, Debug)]
pub struct CreatePost {
@ -193,17 +190,7 @@ impl Perform for Oper<CreatePost> {
}
};
// TODO: we should be able to remove Post::update_ap_id with this
let community = Url::parse(&Community::read(&conn, data.community_id)?.actor_id)?;
let apub_id =
format!(
"{}://{}/{}/{}",
get_apub_protocol_string(),
community.domain().ok_or(format_err!("community has invalid domain"))?,
"post",
inserted_post.id
);
let updated_post = match Post::update_ap_id(&conn, inserted_post.id, &apub_id) {
let updated_post = match Post::update_ap_id(&conn, inserted_post.id) {
Ok(post) => post,
Err(_e) => return Err(APIError::err("couldnt_create_post").into()),
};

View file

@ -183,34 +183,92 @@ pub async fn shared_inbox(
(SharedAcceptedObjects::Undo(u), Some("Delete")) => receive_undo_delete(&u, &conn, chat_server),
(SharedAcceptedObjects::Undo(u), Some("Remove")) => receive_undo_remove(&u, &conn, chat_server),
(SharedAcceptedObjects::Undo(u), Some("Like")) => receive_undo_like(&u, &conn, chat_server),
(SharedAcceptedObjects::Announce(_a), Some("Create")) => {
let create = object.into_concrete::<Create>()?;
receive_create_post(&create, &conn, chat_server)
}
(SharedAcceptedObjects::Announce(_a), Some("Update")) => {
let update = object.into_concrete::<Update>()?;
receive_update_post(&update, &conn, chat_server)
}
(SharedAcceptedObjects::Announce(_a), Some("Like")) => {
let like = object.into_concrete::<Like>()?;
receive_like_post(&like, &conn, chat_server)
}
(SharedAcceptedObjects::Announce(_a), Some("Dislike")) => {
let dislike = object.into_concrete::<Dislike>()?;
receive_dislike_post(&dislike, &conn, chat_server)
}
(SharedAcceptedObjects::Announce(_a), Some("Delete")) => {
let delete = object.into_concrete::<Delete>()?;
receive_delete_post(&delete, &conn, chat_server)
}
(SharedAcceptedObjects::Announce(_a), Some("Remove")) => {
let remove = object.into_concrete::<Remove>()?;
receive_remove_post(&remove, &conn, chat_server)
}
(SharedAcceptedObjects::Announce(a), _) => receive_announce(a, &conn, chat_server),
_ => Err(format_err!("Unknown incoming activity type.")),
}
}
fn receive_announce(
announce: Box<Announce>,
conn: &PgConnection,
chat_server: ChatServerParam,
) -> Result<HttpResponse, Error> {
let object = announce
.announce_props
.get_object_base_box()
.unwrap()
.to_owned();
// TODO: too much copy paste
// TODO: we should log all unhandled events
match object.kind() {
Some("Create") => {
let create = object.into_concrete::<Create>()?;
let inner_object = create.create_props.get_object_base_box().unwrap();
match inner_object.kind() {
Some("Page") => receive_create_post(&create, &conn, chat_server),
Some("Note") => receive_create_comment(&create, &conn, chat_server),
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
Some("Update") => {
let update = object.into_concrete::<Update>()?;
let inner_object = update.update_props.get_object_base_box().unwrap();
match inner_object.kind() {
Some("Page") => receive_update_post(&update, &conn, chat_server),
Some("Note") => receive_update_comment(&update, &conn, chat_server),
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
Some("Like") => {
let like = object.into_concrete::<Like>()?;
let inner_object = like.like_props.get_object_base_box().unwrap();
match inner_object.kind() {
Some("Page") => receive_like_post(&like, &conn, chat_server),
Some("Note") => receive_like_comment(&like, &conn, chat_server),
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
Some("Dislike") => {
let dislike = object.into_concrete::<Dislike>()?;
let inner_object = dislike.dislike_props.get_object_base_box().unwrap();
match inner_object.kind() {
Some("Page") => receive_dislike_post(&dislike, &conn, chat_server),
Some("Note") => receive_dislike_comment(&dislike, &conn, chat_server),
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
Some("Delete") => {
let delete = object.into_concrete::<Delete>()?;
let inner_object = delete.delete_props.get_object_base_box().unwrap();
match inner_object.kind() {
Some("Page") => receive_delete_post(&delete, &conn, chat_server),
Some("Note") => receive_delete_comment(&delete, &conn, chat_server),
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
Some("Remove") => {
let remove = object.into_concrete::<Remove>()?;
let inner_object = remove.remove_props.get_object_base_box().unwrap();
match inner_object.kind() {
Some("Page") => receive_remove_post(&remove, &conn, chat_server),
Some("Note") => receive_remove_comment(&remove, &conn, chat_server),
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
Some("Undo") => {
let undo = object.into_concrete::<Undo>()?;
let inner_object = undo.undo_props.get_object_base_box().unwrap();
match inner_object.kind() {
Some("Delete") => receive_undo_delete(&undo, &conn, chat_server),
Some("Remove") => receive_undo_remove(&undo, &conn, chat_server),
Some("Like") => receive_undo_like(&undo, &conn, chat_server),
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
_ => Ok(HttpResponse::NotImplemented().finish()),
}
}
fn receive_create_post(
create: &Create,
conn: &PgConnection,
@ -1518,8 +1576,6 @@ pub fn do_announce<A>(
where
A: Activity + Base + Serialize + Debug,
{
dbg!(&community_uri);
// TODO: this fails for some reason
let community = Community::read_from_actor_id(conn, &community_uri)?;
// TODO: need to add boolean param is_local_activity
@ -1545,9 +1601,6 @@ where
// this seems to be the "easiest" stable alternative for remove_item()
to.retain(|x| *x != sending_user.get_shared_inbox_url());
dbg!(&announce);
dbg!(&to);
send_activity(&announce, &community, to)?;
Ok(HttpResponse::Ok().finish())

View file

@ -124,7 +124,7 @@ fn post_updates_2020_04_03(conn: &PgConnection) -> Result<(), Error> {
.load::<Post>(conn)?;
for cpost in &incorrect_posts {
Post::update_ap_id(&conn, cpost.id, todo!())?;
Post::update_ap_id(&conn, cpost.id)?;
}
info!("{} post rows updated.", incorrect_posts.len());

View file

@ -75,7 +75,7 @@ impl Post {
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
}
pub fn update_ap_id(conn: &PgConnection, post_id: i32, ap_id: &str) -> Result<Self, Error> {
pub fn update_ap_id(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use crate::schema::post::dsl::*;
let apid = make_apub_endpoint(EndpointType::Post, &post_id.to_string()).to_string();