Accept follow requests
This commit is contained in:
+21
-4
@@ -18,7 +18,8 @@ pub struct Blog {
|
||||
pub outbox_url: String,
|
||||
pub inbox_url: String,
|
||||
pub instance_id: i32,
|
||||
pub creation_date: NaiveDateTime
|
||||
pub creation_date: NaiveDateTime,
|
||||
pub ap_url: String
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
@@ -29,7 +30,8 @@ pub struct NewBlog {
|
||||
pub summary: String,
|
||||
pub outbox_url: String,
|
||||
pub inbox_url: String,
|
||||
pub instance_id: i32
|
||||
pub instance_id: i32,
|
||||
pub ap_url: String
|
||||
}
|
||||
|
||||
impl Blog {
|
||||
@@ -52,7 +54,7 @@ impl Blog {
|
||||
blogs::table.filter(blogs::actor_id.eq(username))
|
||||
.limit(1)
|
||||
.load::<Blog>(conn)
|
||||
.expect("Error loading blog by email")
|
||||
.expect("Error loading blog by actor_id")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
@@ -68,6 +70,12 @@ impl Blog {
|
||||
.set(blogs::inbox_url.eq(self.compute_inbox(conn)))
|
||||
.get_result::<Blog>(conn).expect("Couldn't update inbox URL");
|
||||
}
|
||||
|
||||
if self.ap_url.len() == 0 {
|
||||
diesel::update(self)
|
||||
.set(blogs::ap_url.eq(self.compute_id(conn)))
|
||||
.get_result::<Blog>(conn).expect("Couldn't update AP URL");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn outbox(&self, conn: &PgConnection) -> Outbox {
|
||||
@@ -95,6 +103,14 @@ impl Actor for Blog {
|
||||
fn get_actor_type () -> ActorType {
|
||||
ActorType::Blog
|
||||
}
|
||||
|
||||
fn from_url(conn: &PgConnection, url: String) -> Option<Blog> {
|
||||
blogs::table.filter(blogs::ap_url.eq(url))
|
||||
.limit(1)
|
||||
.load::<Blog>(conn)
|
||||
.expect("Error loading blog from url")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Webfinger for Blog {
|
||||
@@ -137,7 +153,8 @@ impl NewBlog {
|
||||
summary: summary,
|
||||
outbox_url: String::from(""),
|
||||
inbox_url: String::from(""),
|
||||
instance_id: instance_id
|
||||
instance_id: instance_id,
|
||||
ap_url: String::from("")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+52
-19
@@ -8,6 +8,7 @@ use reqwest::mime::Mime;
|
||||
use rocket::request::{self, FromRequest, Request};
|
||||
use rocket::outcome::IntoOutcome;
|
||||
use serde_json;
|
||||
use url::Url;
|
||||
|
||||
use activity_pub::activity::Activity;
|
||||
use activity_pub::actor::{ActorType, Actor};
|
||||
@@ -35,7 +36,8 @@ pub struct User {
|
||||
pub email: Option<String>,
|
||||
pub hashed_password: Option<String>,
|
||||
pub instance_id: i32,
|
||||
pub creation_date: NaiveDateTime
|
||||
pub creation_date: NaiveDateTime,
|
||||
pub ap_url: String
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
@@ -49,7 +51,8 @@ pub struct NewUser {
|
||||
pub summary: String,
|
||||
pub email: Option<String>,
|
||||
pub hashed_password: Option<String>,
|
||||
pub instance_id: i32
|
||||
pub instance_id: i32,
|
||||
pub ap_url: String
|
||||
}
|
||||
|
||||
impl User {
|
||||
@@ -83,7 +86,7 @@ impl User {
|
||||
.filter(users::instance_id.eq(instance_id))
|
||||
.limit(1)
|
||||
.load::<User>(conn)
|
||||
.expect("Error loading user by email")
|
||||
.expect("Error loading user by name")
|
||||
.into_iter().nth(0)
|
||||
}
|
||||
|
||||
@@ -109,19 +112,7 @@ impl User {
|
||||
|
||||
fn fetch_from_webfinger(conn: &PgConnection, acct: String) -> Option<User> {
|
||||
match resolve(acct.clone()) {
|
||||
Ok(url) => {
|
||||
let req = Client::new()
|
||||
.get(&url[..])
|
||||
.header(Accept(vec![qitem("application/activity+json".parse::<Mime>().unwrap())]))
|
||||
.send();
|
||||
match req {
|
||||
Ok(mut res) => {
|
||||
let json: serde_json::Value = serde_json::from_str(&res.text().unwrap()).unwrap();
|
||||
Some(User::from_activity(conn, json, acct.split("@").last().unwrap().to_string()))
|
||||
},
|
||||
Err(_) => None
|
||||
}
|
||||
},
|
||||
Ok(url) => User::fetch_from_url(conn, url),
|
||||
Err(details) => {
|
||||
println!("{}", details);
|
||||
None
|
||||
@@ -129,6 +120,20 @@ impl User {
|
||||
}
|
||||
}
|
||||
|
||||
fn fetch_from_url(conn: &PgConnection, url: String) -> Option<User> {
|
||||
let req = Client::new()
|
||||
.get(&url[..])
|
||||
.header(Accept(vec![qitem("application/activity+json".parse::<Mime>().unwrap())]))
|
||||
.send();
|
||||
match req {
|
||||
Ok(mut res) => {
|
||||
let json: serde_json::Value = serde_json::from_str(&res.text().unwrap()).unwrap();
|
||||
Some(User::from_activity(conn, json, Url::parse(url.as_ref()).unwrap().host_str().unwrap().to_string()))
|
||||
},
|
||||
Err(_) => None
|
||||
}
|
||||
}
|
||||
|
||||
fn from_activity(conn: &PgConnection, acct: serde_json::Value, inst: String) -> User {
|
||||
let instance = match Instance::get_by_domain(conn, inst.clone()) {
|
||||
Some(instance) => instance,
|
||||
@@ -145,7 +150,8 @@ impl User {
|
||||
summary: acct["summary"].as_str().unwrap().to_string(),
|
||||
email: None,
|
||||
hashed_password: None,
|
||||
instance_id: instance.id
|
||||
instance_id: instance.id,
|
||||
ap_url: acct["id"].as_str().unwrap().to_string()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -167,7 +173,13 @@ impl User {
|
||||
if self.inbox_url.len() == 0 {
|
||||
diesel::update(self)
|
||||
.set(users::inbox_url.eq(self.compute_inbox(conn)))
|
||||
.get_result::<User>(conn).expect("Couldn't update outbox URL");
|
||||
.get_result::<User>(conn).expect("Couldn't update inbox URL");
|
||||
}
|
||||
|
||||
if self.ap_url.len() == 0 {
|
||||
diesel::update(self)
|
||||
.set(users::ap_url.eq(self.compute_id(conn)))
|
||||
.get_result::<User>(conn).expect("Couldn't update AP URL");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -225,6 +237,26 @@ impl Actor for User {
|
||||
fn get_actor_type() -> ActorType {
|
||||
ActorType::Person
|
||||
}
|
||||
|
||||
fn from_url(conn: &PgConnection, url: String) -> Option<User> {
|
||||
let in_db = users::table.filter(users::ap_url.eq(url.clone()))
|
||||
.limit(1)
|
||||
.load::<User>(conn)
|
||||
.expect("Error loading user by AP url")
|
||||
.into_iter().nth(0);
|
||||
match in_db {
|
||||
Some(u) => Some(u),
|
||||
None => {
|
||||
// The requested user was not in the DB
|
||||
// We try to fetch it if it is remote
|
||||
if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != Instance::get_local(conn).unwrap().public_domain {
|
||||
Some(User::fetch_from_url(conn, url).unwrap())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Inbox for User {
|
||||
@@ -281,7 +313,8 @@ impl NewUser {
|
||||
summary: summary,
|
||||
email: Some(email),
|
||||
hashed_password: Some(password),
|
||||
instance_id: instance_id
|
||||
instance_id: instance_id,
|
||||
ap_url: String::from("")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user