Handle reshares from AP

This commit is contained in:
Bat 2018-05-23 18:09:59 +01:00
parent 9b98a45f2e
commit c0d1a914c4
2 changed files with 17 additions and 2 deletions

View File

@ -1,7 +1,7 @@
use activitystreams_traits::Actor; use activitystreams_traits::Actor;
use activitystreams_types::{ use activitystreams_types::{
actor::Person, actor::Person,
activity::{Accept, Create, Follow, Like, Undo}, activity::{Accept, Announce, Create, Follow, Like, Undo},
object::{Article, Note} object::{Article, Note}
}; };
use diesel::PgConnection; use diesel::PgConnection;
@ -19,6 +19,7 @@ use models::{
follows, follows,
likes, likes,
posts::*, posts::*,
reshares::*,
users::User users::User
}; };
@ -94,10 +95,22 @@ pub trait Inbox {
Ok(()) Ok(())
} }
fn announce(&self, conn: &PgConnection, announce: Announce) -> Result<(), Error> {
let user = User::from_url(conn, announce.actor.as_str().unwrap().to_string());
let post = Post::find_by_ap_url(conn, announce.object.as_str().unwrap().to_string());
Reshare::insert(conn, NewReshare {
post_id: post.unwrap().id,
user_id: user.unwrap().id,
ap_url: announce.object_props.id_string()?
});
Ok(())
}
fn save(&self, conn: &PgConnection, act: serde_json::Value) -> Result<(), Error> { fn save(&self, conn: &PgConnection, act: serde_json::Value) -> Result<(), Error> {
match act["type"].as_str() { match act["type"].as_str() {
Some(t) => { Some(t) => {
match t { match t {
"Announce" => self.announce(conn, serde_json::from_value(act.clone())?),
"Create" => { "Create" => {
let act: Create = serde_json::from_value(act.clone())?; let act: Create = serde_json::from_value(act.clone())?;
match act.object["type"].as_str().unwrap() { match act.object["type"].as_str().unwrap() {

View File

@ -438,7 +438,9 @@ impl WithInbox for User {
impl Inbox for User { impl Inbox for User {
fn received(&self, conn: &PgConnection, act: serde_json::Value) { fn received(&self, conn: &PgConnection, act: serde_json::Value) {
self.save(conn, act.clone()).unwrap(); if let Err(err) = self.save(conn, act.clone()) {
println!("Inbox error:\n{}\n{}", err.cause(), err.backtrace());
}
// Notifications // Notifications
match act["type"].as_str().unwrap() { match act["type"].as_str().unwrap() {