Skip to content

Commit

Permalink
feat: follow lysand -> ap
Browse files Browse the repository at this point in the history
  • Loading branch information
CutestNekoAqua committed Aug 3, 2024
1 parent b666d33 commit f480bc0
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
21 changes: 17 additions & 4 deletions src/activities/follow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl ActivityHandler for Follow {
}

async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
accept_follow(self, data).await?;
//accept_follow(self, data).await?; TODO replace w/ lysand forward
Ok(())
}
}
Expand All @@ -140,11 +140,12 @@ impl ActivityHandler for Accept {
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
let user = self.actor.dereference(data).await?;
let follower = self.object.actor.dereference(data).await?;
save_follow(user, follower).await?;
save_accept_follow(user, follower, self).await?;
Ok(())
}
}

/*
async fn accept_follow(
follow_req: Follow,
data: &Data<StateHandle>,
Expand All @@ -155,10 +156,12 @@ async fn accept_follow(
Accept::send(follow_relation, follow_req, follower.inbox().clone(), data).await?;
Ok(())
}
*/

async fn save_follow(
async fn save_accept_follow(
followee: user::Model,
follower: user::Model,
accept_activity: Accept,
) -> Result<follow_relation::Model, crate::error::Error> {
let db = DB.get().unwrap();
let query = prelude::FollowRelation::find()
Expand All @@ -169,8 +172,18 @@ async fn save_follow(
if query.is_none() {
return Err(crate::error::Error(anyhow::anyhow!("oopsie woopise")));
}
let lysand_accept_id = uuid::Uuid::now_v7().to_string();
// all values in the ActiveModel that are set, except the id, will be updated
let active_query = follow_relation::ActiveModel {
id: Set(query.unwrap().id),
ap_accept_id: Set(Some(accept_activity.id.to_string())),
ap_accept_json: Set(Some(serde_json::to_string(&accept_activity).unwrap())),
accept_id: Set(Some(lysand_accept_id)),
..Default::default()
};
// modify db entry
let res = prelude::FollowRelation::update(query.unwrap());
let res = prelude::FollowRelation::update(active_query);
let model = res.exec(db).await?;

Ok(model)
}
61 changes: 61 additions & 0 deletions src/lysand/funcs.rs
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use time::OffsetDateTime;
use url::Url;

use crate::{
entities::{follow_relation, prelude, user},
utils::generate_follow_accept_id,
API_DOMAIN, DB,
};

use super::{
conversion::{fetch_user_from_url, lysand_user_from_db},
objects::FollowResult,
superx::request_client,
};

async fn send_follow_accept_to_lysand(model: follow_relation::Model) -> anyhow::Result<()> {
let request_client = request_client();
let db = DB.get().unwrap();

let id_raw = model.accept_id.unwrap();
let id = uuid::Uuid::parse_str(&id_raw)?;
let uri = generate_follow_accept_id(API_DOMAIN.as_str(), &id_raw)?;

let follower_model = prelude::User::find()
.filter(user::Column::Id.eq(model.follower_id))
.one(db)
.await?
.unwrap();
let lysand_follower = fetch_user_from_url(Url::parse(&follower_model.url)?).await?;

let followee_model = prelude::User::find()
.filter(user::Column::Id.eq(model.followee_id))
.one(db)
.await?
.unwrap();
let lysand_followee = lysand_user_from_db(followee_model).await?;

let entity = FollowResult {
rtype: super::objects::LysandType::FollowAccept,
id,
uri,
created_at: OffsetDateTime::now_utc(),
author: lysand_followee.uri,
follower: lysand_follower.uri,
};

let request = request_client
.post(lysand_follower.inbox.as_str())
.header("Content-Type", "application/json; charset=utf-8")
.header("Accept", "application/json")
.header("Date", entity.created_at.clone().to_string())
.json(&entity);

let response = request.send().await?;

if response.status().is_success() {
Ok(())
} else {
Err(anyhow::anyhow!("Failed to send follow accept to Lysand"))
}
}
7 changes: 6 additions & 1 deletion src/lysand/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use crate::{
prelude, user,
},
error,
lysand::conversion::{lysand_post_from_db, lysand_user_from_db},
lysand::{
conversion::{lysand_post_from_db, lysand_user_from_db},
inbox::inbox_entry,
},
objects::{self, person::Person},
utils::{base_url_decode, generate_create_id, generate_user_id},
Response, API_DOMAIN, DB, FEDERATION_CONFIG,
Expand Down Expand Up @@ -88,6 +91,8 @@ async fn lysand_inbox(
body: web::Bytes,
state: web::Data<State>,
) -> actix_web::Result<HttpResponse, error::Error> {
let string = String::from_utf8(body.to_vec())?;
inbox_entry(&string).await?;
Ok(HttpResponse::Created().finish())
}

Expand Down
1 change: 1 addition & 0 deletions src/lysand/inbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub async fn inbox_entry(json: &str) -> Result<()> {
}
Some("Follow") => {
let follow_req: super::objects::Follow = serde_json::from_str(json)?;
follow_request(follow_req).await?;
}
Some("FollowAccept") => {
let follow_accept: super::objects::FollowResult = serde_json::from_str(json)?;
Expand Down

0 comments on commit f480bc0

Please sign in to comment.