diff --git a/src/activity_pub/activity.rs b/src/activity_pub/activity.rs index a4d178eb..6ffb0540 100644 --- a/src/activity_pub/activity.rs +++ b/src/activity_pub/activity.rs @@ -8,7 +8,8 @@ use activity_pub::object::Object; #[derive(Clone)] pub enum Activity { Create(Payload), - Accept(Payload) + Accept(Payload), + Follow(Payload) } impl Activity { pub fn serialize(&self) -> serde_json::Value { @@ -23,14 +24,16 @@ impl Activity { pub fn get_type(&self) -> String { match self { Activity::Accept(_) => String::from("Accept"), - Activity::Create(_) => String::from("Create") + Activity::Create(_) => String::from("Create"), + Activity::Follow(_) => String::from("Follow") } } pub fn payload(&self) -> Payload { match self { Activity::Accept(p) => p.clone(), - Activity::Create(p) => p.clone() + Activity::Create(p) => p.clone(), + Activity::Follow(p) => p.clone() } } @@ -41,6 +44,10 @@ impl Activity { pub fn accept(by: &A, what: String, conn: &PgConnection) -> Activity { Activity::Accept(Payload::new(serde_json::Value::String(by.compute_id(conn)), serde_json::Value::String(what))) } + + pub fn follow(by: &A, obj: &B, conn: &PgConnection) -> Activity { + Activity::Follow(Payload::new(serde_json::Value::String(by.compute_id(conn)), serde_json::Value::String(obj.compute_id(conn)))) + } } #[derive(Clone)] diff --git a/src/main.rs b/src/main.rs index db8cf237..465ce315 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,7 @@ fn main() { routes::user::me, routes::user::details, + routes::user::follow, routes::user::activity_details, routes::user::outbox, routes::user::inbox, diff --git a/src/routes/user.rs b/src/routes/user.rs index 1aa75002..79e5de02 100644 --- a/src/routes/user.rs +++ b/src/routes/user.rs @@ -5,10 +5,12 @@ use serde_json; use std::collections::HashMap; use activity_pub::ActivityPub; +use activity_pub::activity::Activity; use activity_pub::actor::Actor; use activity_pub::inbox::Inbox; use activity_pub::outbox::Outbox; use db_conn::DbConn; +use models::follows::*; use models::instance::Instance; use models::users::*; @@ -25,6 +27,17 @@ fn details(name: String, conn: DbConn) -> Template { })) } +#[get("/@//follow")] +fn follow(name: String, conn: DbConn, user: User) -> Redirect { + let target = User::find_by_fqn(&*conn, name.clone()).unwrap(); + Follow::insert(&*conn, NewFollow { + follower_id: user.id, + following_id: target.id + }); + target.send_to_inbox(&*conn, Activity::follow(&user, &target, &*conn)); + Redirect::to(format!("/@/{}", name).as_ref()) +} + #[get("/@/", format = "application/activity+json", rank = 1)] fn activity_details(name: String, conn: DbConn) -> ActivityPub { let user = User::find_local(&*conn, name).unwrap();