Follow endpoint

This commit is contained in:
Bat 2018-05-01 20:57:30 +01:00
parent 2e71912941
commit 9fdfb2b25e
3 changed files with 24 additions and 3 deletions

View File

@ -8,7 +8,8 @@ use activity_pub::object::Object;
#[derive(Clone)] #[derive(Clone)]
pub enum Activity { pub enum Activity {
Create(Payload), Create(Payload),
Accept(Payload) Accept(Payload),
Follow(Payload)
} }
impl Activity { impl Activity {
pub fn serialize(&self) -> serde_json::Value { pub fn serialize(&self) -> serde_json::Value {
@ -23,14 +24,16 @@ impl Activity {
pub fn get_type(&self) -> String { pub fn get_type(&self) -> String {
match self { match self {
Activity::Accept(_) => String::from("Accept"), 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 { pub fn payload(&self) -> Payload {
match self { match self {
Activity::Accept(p) => p.clone(), 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<A: Actor>(by: &A, what: String, conn: &PgConnection) -> Activity { pub fn accept<A: Actor>(by: &A, what: String, conn: &PgConnection) -> Activity {
Activity::Accept(Payload::new(serde_json::Value::String(by.compute_id(conn)), serde_json::Value::String(what))) Activity::Accept(Payload::new(serde_json::Value::String(by.compute_id(conn)), serde_json::Value::String(what)))
} }
pub fn follow<A: Actor, B: Actor>(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)] #[derive(Clone)]

View File

@ -56,6 +56,7 @@ fn main() {
routes::user::me, routes::user::me,
routes::user::details, routes::user::details,
routes::user::follow,
routes::user::activity_details, routes::user::activity_details,
routes::user::outbox, routes::user::outbox,
routes::user::inbox, routes::user::inbox,

View File

@ -5,10 +5,12 @@ use serde_json;
use std::collections::HashMap; use std::collections::HashMap;
use activity_pub::ActivityPub; use activity_pub::ActivityPub;
use activity_pub::activity::Activity;
use activity_pub::actor::Actor; use activity_pub::actor::Actor;
use activity_pub::inbox::Inbox; use activity_pub::inbox::Inbox;
use activity_pub::outbox::Outbox; use activity_pub::outbox::Outbox;
use db_conn::DbConn; use db_conn::DbConn;
use models::follows::*;
use models::instance::Instance; use models::instance::Instance;
use models::users::*; use models::users::*;
@ -25,6 +27,17 @@ fn details(name: String, conn: DbConn) -> Template {
})) }))
} }
#[get("/@/<name>/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("/@/<name>", format = "application/activity+json", rank = 1)] #[get("/@/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityPub { fn activity_details(name: String, conn: DbConn) -> ActivityPub {
let user = User::find_local(&*conn, name).unwrap(); let user = User::find_local(&*conn, name).unwrap();