feat: Refactor migration and follow code

This commit is contained in:
aprilthepink 2024-05-05 17:05:09 +02:00
parent 468371d43d
commit ce5f97ac33
5 changed files with 78 additions and 6 deletions

View file

@ -8,7 +8,6 @@ pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
@ -21,8 +20,16 @@ impl MigrationTrait for Migration {
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(FollowRelation::FolloweeId).string().not_null())
.col(ColumnDef::new(FollowRelation::FollowerId).string().not_null())
.col(
ColumnDef::new(FollowRelation::FolloweeId)
.string()
.not_null(),
)
.col(
ColumnDef::new(FollowRelation::FollowerId)
.string()
.not_null(),
)
.col(ColumnDef::new(FollowRelation::FolloweeHost).string())
.col(ColumnDef::new(FollowRelation::FollowerHost).string())
.col(ColumnDef::new(FollowRelation::FolloweeInbox).string())
@ -62,5 +69,5 @@ enum FollowRelation {
FolloweeHost,
FollowerHost,
FolloweeInbox,
FollowerInbox
FollowerInbox,
}

63
src/activities/follow.rs Normal file
View 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(())
}

View file

@ -1 +1,2 @@
pub mod create_post;
pub mod follow;

View file

@ -2,6 +2,6 @@
pub mod prelude;
pub mod follow_relation;
pub mod post;
pub mod user;
pub mod follow_relation;

View file

@ -1,5 +1,5 @@
use crate::{
activities::create_post::CreatePost,
activities::{create_post::CreatePost, follow::Follow},
database::{State, StateHandle},
entities::{self, user},
error::Error,
@ -40,6 +40,7 @@ pub struct DbUser {
#[enum_delegate::implement(ActivityHandler)]
pub enum PersonAcceptedActivities {
CreateNote(CreatePost),
Follow(Follow),
}
impl DbUser {