activitypub-federation-rust/examples/local_federation/main.rs
藍+85CD 5fc5546aec
refactor: update dependencies (#2)
* chore(deps): bump reqwest

* chore(deps): bump http

* chore(deps): bump http-signature-normalization-reqwest

* chore(deps): bump hyper

* chore(deps): bump axum

* chore(deps): use axum-extra

* refactor(tests): use axum::serve

* fix: axum inbox

Co-Authored-By: j0 <me@j0.lol>

* fix(examples): use axum::serve

* chore(ci): add doc test

* fix: docs test

* fix(tests): fix port

* fix(examples): use tokio::spawn

* chore(examples): remove actix-web code

---------

Co-authored-by: j0 <me@j0.lol>
2024-07-25 19:40:57 +08:00

66 lines
2.1 KiB
Rust

#![allow(clippy::unwrap_used)]
use crate::{
instance::{listen, new_instance, Webserver},
objects::post::DbPost,
utils::generate_object_id,
};
use error::Error;
use std::{env::args, str::FromStr};
use tracing::log::{info, LevelFilter};
mod activities;
#[cfg(feature = "axum")]
mod axum;
mod error;
mod instance;
mod objects;
mod utils;
#[tokio::main]
async fn main() -> Result<(), Error> {
env_logger::builder()
.filter_level(LevelFilter::Warn)
.filter_module("activitypub_federation", LevelFilter::Info)
.filter_module("local_federation", LevelFilter::Info)
.format_timestamp(None)
.init();
info!("Start with parameter `axum` or `actix-web` to select the webserver");
let webserver = args()
.nth(1)
.map(|arg| Webserver::from_str(&arg).unwrap())
.unwrap_or(Webserver::Axum);
let alpha = new_instance("localhost:8001", "alpha".to_string()).await?;
let beta = new_instance("localhost:8002", "beta".to_string()).await?;
listen(&alpha, &webserver).await?;
listen(&beta, &webserver).await?;
info!("Local instances started");
info!("Alpha user follows beta user via webfinger");
alpha
.local_user()
.follow("beta@localhost:8002", &alpha.to_request_data())
.await?;
assert_eq!(
beta.local_user().followers(),
&vec![alpha.local_user().ap_id.inner().clone()]
);
info!("Follow was successful");
info!("Beta sends a post to its followers");
let sent_post = DbPost::new("Hello world!".to_string(), beta.local_user().ap_id)?;
beta.local_user()
.post(sent_post.clone(), &beta.to_request_data())
.await?;
let received_post = alpha.posts.lock().unwrap().first().cloned().unwrap();
info!("Alpha received post: {}", received_post.text);
// assert that alpha received the post
assert_eq!(received_post.text, sent_post.text);
assert_eq!(received_post.ap_id.inner(), sent_post.ap_id.inner());
assert_eq!(received_post.creator.inner(), sent_post.creator.inner());
info!("Test completed");
Ok(())
}