activitypub/src/versia/funcs.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

2024-08-03 06:25:16 +02:00
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use time::OffsetDateTime;
use url::Url;
feat: basic AP <-> lysand conversion (#4) * feat: Update API domain variable name * save changes, fake commit * feat: function to receive lysand note * [feat]: lysand to ap for posts and users * feat: Add .env file to gitignore and update dependencies The commit adds the `.env` file to the `.gitignore` and updates the dependencies in the `Cargo.toml` and `Cargo.lock` files. This change ensures that sensitive environment variables are not committed to the repository and keeps the dependencies up to date. * feat: Add db_post_from_url function The commit adds the `db_post_from_url` function to the `conversion.rs` file. This function retrieves a post from the database based on a given URL. If the post is not found in the database, it fetches the post from the URL, saves it to the database, and returns the post. This change enhances the functionality of the codebase by providing a convenient way to retrieve and store posts. * feat: Update dependencies and add async-recursion crate * fix: Refactor lysand note handling in conversion.rs The commit refactors the lysand note handling in the `conversion.rs` file. It updates the `receive_lysand_note` function to properly handle quoting and replying to notes. This change improves the functionality and readability of the codebase. * fix: use example as default user * me oopid * fix: Refactor lysand note handling in conversion.rs * fix: fix post printing * fix: Refactor lysand note handling in conversion.rs * fix: Refactor lysand note handling in conversion.rs * fix: make nix-bootstrap executable * fix: remove unused linux only import * fix: make person stop screaming at me :( * feat: remove test code * fix: update deps * fix: rm build.rs fully * feat: standard user to apservice * fix: format files
2024-06-18 03:56:25 +02:00
2024-08-03 06:25:16 +02:00
use crate::{
entities::{follow_relation, prelude, user},
utils::generate_follow_accept_id,
API_DOMAIN, DB,
};
use super::{
2024-08-28 15:24:22 +02:00
conversion::{fetch_user_from_url, versia_user_from_db},
2024-08-03 06:25:16 +02:00
objects::FollowResult,
superx::request_client,
};
2024-08-28 15:24:22 +02:00
pub async fn send_follow_accept_to_versia(model: follow_relation::Model) -> anyhow::Result<()> {
2024-08-03 06:25:16 +02:00
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();
2024-08-28 15:24:22 +02:00
let versia_follower = fetch_user_from_url(Url::parse(&follower_model.url)?).await?;
2024-08-03 06:25:16 +02:00
let followee_model = prelude::User::find()
.filter(user::Column::Id.eq(model.followee_id))
.one(db)
.await?
.unwrap();
2024-08-28 15:24:22 +02:00
let versia_followee = versia_user_from_db(followee_model).await?;
2024-08-03 06:25:16 +02:00
let entity = FollowResult {
2024-08-28 15:24:22 +02:00
rtype: super::objects::VersiaType::FollowAccept,
2024-08-03 06:25:16 +02:00
id,
uri,
created_at: OffsetDateTime::now_utc(),
2024-08-28 15:24:22 +02:00
author: versia_followee.uri,
follower: versia_follower.uri,
2024-08-03 06:25:16 +02:00
};
let request = request_client
2024-08-28 15:24:22 +02:00
.post(versia_follower.inbox.as_str())
2024-08-03 06:25:16 +02:00
.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 {
2024-08-28 15:24:22 +02:00
Err(anyhow::anyhow!("Failed to send follow accept to Versia"))
2024-08-03 06:25:16 +02:00
}
}