activitypub-federation-rust/examples/local_federation/main.rs

67 lines
2.1 KiB
Rust
Raw Normal View History

use crate::{
2023-03-02 19:48:06 +05:30
instance::{listen, new_instance, Webserver},
objects::post::DbPost,
utils::generate_object_id,
};
use error::Error;
2023-03-02 19:48:06 +05:30
use std::{env::args, str::FromStr};
use tracing::log::{info, LevelFilter};
2022-06-02 16:47:12 +05:30
mod activities;
#[cfg(feature = "actix-web")]
mod actix_web;
#[cfg(feature = "axum")]
mod axum;
2022-06-02 16:47:12 +05:30
mod error;
mod instance;
mod objects;
2022-11-26 21:42:08 +05:30
mod utils;
2022-06-02 16:47:12 +05:30
#[tokio::main]
2022-06-02 16:47:12 +05:30
async fn main() -> Result<(), Error> {
env_logger::builder()
.filter_level(LevelFilter::Warn)
2023-03-08 03:31:36 +05:30
.filter_module("activitypub_federation", LevelFilter::Info)
.filter_module("local_federation", LevelFilter::Info)
.format_timestamp(None)
2022-06-02 16:47:12 +05:30
.init();
2023-03-02 19:48:06 +05:30
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?;
2023-03-02 19:48:06 +05:30
listen(&alpha, &webserver)?;
listen(&beta, &webserver)?;
info!("Local instances started");
2022-06-02 16:47:12 +05:30
2023-03-02 04:49:10 +05:30
info!("Alpha user follows beta user via webfinger");
2022-06-02 16:47:12 +05:30
alpha
.local_user()
2023-03-02 04:49:10 +05:30
.follow("beta@localhost:8002", &alpha.to_request_data())
2022-06-02 16:47:12 +05:30
.await?;
assert_eq!(
beta.local_user().followers(),
&vec![alpha.local_user().ap_id.inner().clone()]
);
info!("Follow was successful");
2022-06-02 16:47:12 +05:30
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?;
2022-06-02 16:47:12 +05:30
let received_post = alpha.posts.lock().unwrap().first().cloned().unwrap();
info!("Alpha received post: {}", received_post.text);
2022-06-02 16:47:12 +05:30
// 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");
2022-06-02 16:47:12 +05:30
Ok(())
}