mirror of
https://github.com/versia-pub/activitypub.git
synced 2026-03-13 10:59:17 +01:00
feat: Refactor migration and follow code
This commit is contained in:
parent
468371d43d
commit
ce5f97ac33
5 changed files with 78 additions and 6 deletions
63
src/activities/follow.rs
Normal file
63
src/activities/follow.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use activitypub_federation::{config::Data, fetch::object_id::ObjectId, traits::ActivityHandler};
|
||||
use activitystreams_kinds::activity::FollowType;
|
||||
use sea_orm::{ActiveModelTrait, Set};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use url::Url;
|
||||
|
||||
use crate::{
|
||||
database::StateHandle,
|
||||
entities::{follow_relation, prelude::FollowRelation, user},
|
||||
DB,
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
pub struct Follow {
|
||||
actor: ObjectId<user::Model>,
|
||||
object: ObjectId<user::Model>,
|
||||
#[serde(rename = "type")]
|
||||
kind: FollowType,
|
||||
id: Url,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl ActivityHandler for Follow {
|
||||
type DataType = StateHandle;
|
||||
type Error = crate::error::Error;
|
||||
|
||||
fn id(&self) -> &Url {
|
||||
&self.id
|
||||
}
|
||||
|
||||
fn actor(&self) -> &Url {
|
||||
self.actor.inner()
|
||||
}
|
||||
|
||||
async fn verify(&self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn receive(self, data: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
||||
let local_user = self.object.dereference(data).await?;
|
||||
let follower = self.actor.dereference(data).await?;
|
||||
save_follow(local_user, follower).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn save_follow(
|
||||
local_user: user::Model,
|
||||
follower: user::Model,
|
||||
) -> Result<(), crate::error::Error> {
|
||||
let url = Url::parse(&follower.url)?;
|
||||
let follow_relation = follow_relation::ActiveModel {
|
||||
followee_id: Set(local_user.id.clone()),
|
||||
follower_id: Set(follower.id.clone()),
|
||||
followee_host: Set(None),
|
||||
follower_host: Set(Some(url.host_str().unwrap().to_string())),
|
||||
followee_inbox: Set(Some(local_user.inbox.clone())),
|
||||
follower_inbox: Set(Some(follower.inbox.clone())),
|
||||
..Default::default()
|
||||
};
|
||||
follow_relation.insert(DB.get().unwrap()).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1 +1,2 @@
|
|||
pub mod create_post;
|
||||
pub mod follow;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue