Skip to content

Commit

Permalink
fomat
Browse files Browse the repository at this point in the history
  • Loading branch information
CutestNekoAqua committed Apr 18, 2024
1 parent 4d4e3ed commit fa3e463
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 19 deletions.
5 changes: 3 additions & 2 deletions migration/src/m20240417_233430_post_user_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {

manager.drop_table(Table::drop().table(Post::Table).to_owned()).await?;
manager
.drop_table(Table::drop().table(Post::Table).to_owned())
.await?;

manager
.create_table(
Expand Down
19 changes: 15 additions & 4 deletions src/activities/create_post.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use crate::{
database::StateHandle, entities::{post, user}, error::Error, objects::{person::DbUser, post::{DbPost, Note}}, utils::generate_object_id
database::StateHandle,
entities::{post, user},
error::Error,
objects::{
person::DbUser,
post::{DbPost, Note},
},
utils::generate_object_id,
};
use activitypub_federation::{
activity_sending::SendActivityTask,
Expand Down Expand Up @@ -35,9 +42,13 @@ impl CreatePost {
id: generate_object_id(data.domain())?,
};
let create_with_context = WithContext::new_default(create);
let sends =
SendActivityTask::prepare(&create_with_context, &data.local_user().await?, vec![inbox], data)
.await?;
let sends = SendActivityTask::prepare(
&create_with_context,
&data.local_user().await?,
vec![inbox],
data,
)
.await?;
for send in sends {
send.sign_and_send(data).await?;
}
Expand Down
7 changes: 5 additions & 2 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::entities::prelude::User;
use crate::{entities::user, error::Error, objects::person::DbUser};
use anyhow::anyhow;
use sea_orm::{DatabaseConnection, EntityTrait};
use serde::{Deserialize, Serialize};
use std::sync::{Arc, Mutex};
use super::entities::prelude::User;

#[derive(Debug, Clone)]
pub struct Config {}
Expand All @@ -24,7 +24,10 @@ pub struct Database {

impl State {
pub async fn local_user(&self) -> Result<user::Model, Error> {
let user = User::find().one(self.database_connection.as_ref()).await?.unwrap();
let user = User::find()
.one(self.database_connection.as_ref())
.await?
.unwrap();
Ok(user.clone())
}

Expand Down
5 changes: 4 additions & 1 deletion src/http.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{
database::StateHandle, entities::user, error::Error, objects::person::{DbUser, PersonAcceptedActivities}
database::StateHandle,
entities::user,
error::Error,
objects::person::{DbUser, PersonAcceptedActivities},
};
use activitypub_federation::{
actix_web::{inbox::receive_activity, signing_actor},
Expand Down
33 changes: 27 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use activitypub_federation::config::{FederationConfig, FederationMiddleware};
use activitypub_federation::{
config::{FederationConfig, FederationMiddleware},
http_signatures::generate_actor_keypair,
};
use actix_web::{get, http::KeepAlive, middleware, web, App, Error, HttpResponse, HttpServer};
use actix_web_prom::PrometheusMetricsBuilder;
use chrono::{DateTime, Utc};
use clap::Parser;
use database::Database;
use http::{http_get_user, http_post_user_inbox, webfinger};
use objects::person::DbUser;
use sea_orm::DatabaseConnection;
use sea_orm::{ActiveModelTrait, DatabaseConnection, Set};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
Expand All @@ -15,12 +19,13 @@ use std::{
};
use tokio::signal;
use tracing::info;
use url::Url;

use crate::database::{Config, State};

mod entities;
mod activities;
mod database;
mod entities;
mod error;
mod http;
mod objects;
Expand Down Expand Up @@ -69,12 +74,28 @@ async fn main() -> actix_web::Result<(), anyhow::Error> {
)
.unwrap();

let new_database = Arc::new(Database {
users: Mutex::new(vec![local_user]),
});
let username = env::var("LOCAL_USER_NAME").unwrap_or(LOCAL_USER_NAME.to_string());
let domain = env::var("FEDERATED_DOMAIN").unwrap_or(DOMAIN.to_string());

let ap_id = Url::parse(&format!("https://{}/{}", domain, &username))?;
let inbox = Url::parse(&format!("https://{}/{}/inbox", domain, &username))?;
let keypair = generate_actor_keypair()?;

let user = entities::user::ActiveModel {
id: Set(ap_id.into()),
username: Set(username),
inbox: Set(inbox.to_string()),
public_key: Set(keypair.public_key.clone()),
private_key: Set(Some(keypair.private_key.clone())),
last_refreshed_at: Set(chrono::offset::Utc::now()),
local: Set(true),
..Default::default()
};

let db = sea_orm::Database::connect(database_url).await?;

let user = user.insert(&db).await;

let config = Config {};

let state: State = State {
Expand Down
7 changes: 6 additions & 1 deletion src/objects/person.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{activities::create_post::CreatePost, database::{State, StateHandle}, entities::{self, user}, error::Error};
use crate::{
activities::create_post::CreatePost,
database::{State, StateHandle},
entities::{self, user},
error::Error,
};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,
Expand Down
12 changes: 9 additions & 3 deletions src/objects/post.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use crate::{
activities::create_post::CreatePost, database::StateHandle, entities::{post, user}, error::Error, objects::person::DbUser, utils::generate_object_id
activities::create_post::CreatePost,
database::StateHandle,
entities::{post, user},
error::Error,
objects::person::DbUser,
utils::generate_object_id,
};
use activitypub_federation::{
config::Data,
Expand Down Expand Up @@ -81,8 +86,9 @@ impl Object for post::Model {
local: Set(false),
..Default::default()
};
let post = post.insert(data.app_data().database_connection.clone().as_ref())
.await?;
let post = post
.insert(data.app_data().database_connection.clone().as_ref())
.await?;

let mention = Mention {
href: Url::parse(&creator.id)?,
Expand Down

0 comments on commit fa3e463

Please sign in to comment.