mirror of
https://github.com/nullishamy/ferri.git
synced 2025-06-28 09:04:18 +00:00
feat: logging; fixes; error handloing
This commit is contained in:
parent
9c7c2858cc
commit
3719fae102
18 changed files with 228 additions and 106 deletions
|
@ -1,8 +1,9 @@
|
|||
use crate::ap::{Actor, User, http};
|
||||
use chrono::{DateTime, Local};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::Sqlite;
|
||||
use std::fmt::Debug;
|
||||
use tracing::{event, Level};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ActivityType {
|
||||
|
@ -28,7 +29,7 @@ pub struct Activity<T: Serialize + Debug> {
|
|||
pub id: String,
|
||||
pub ty: ActivityType,
|
||||
pub object: T,
|
||||
pub published: DateTime<Local>,
|
||||
pub published: DateTime<Utc>,
|
||||
pub to: Vec<String>,
|
||||
pub cc: Vec<String>,
|
||||
}
|
||||
|
@ -39,7 +40,7 @@ impl<T: Serialize + Debug + Default> Default for Activity<T> {
|
|||
id: Default::default(),
|
||||
ty: ActivityType::Unknown,
|
||||
object: Default::default(),
|
||||
published: Local::now(),
|
||||
published: Utc::now(),
|
||||
to: Default::default(),
|
||||
cc: Default::default(),
|
||||
}
|
||||
|
@ -102,19 +103,18 @@ impl<'a> Outbox<'a> {
|
|||
}
|
||||
|
||||
pub async fn post<T: Serialize + Debug>(&self, activity: OutgoingActivity<T>) {
|
||||
dbg!(&activity);
|
||||
event!(Level::INFO, ?activity, "activity in outbox");
|
||||
|
||||
let raw = RawActivity {
|
||||
context: "https://www.w3.org/ns/activitystreams".to_string(),
|
||||
id: activity.req.id,
|
||||
id: activity.req.id.clone(),
|
||||
ty: activity.req.ty.to_raw(),
|
||||
actor: self.user.actor().id().to_string(),
|
||||
object: activity.req.object,
|
||||
published: activity.req.published.to_rfc3339(),
|
||||
};
|
||||
|
||||
dbg!(&raw);
|
||||
|
||||
let follow_res = self
|
||||
let outbox_res = self
|
||||
.transport
|
||||
.post(activity.to.inbox())
|
||||
.activity()
|
||||
|
@ -127,7 +127,10 @@ impl<'a> Outbox<'a> {
|
|||
.await
|
||||
.unwrap();
|
||||
|
||||
dbg!(follow_res);
|
||||
event!(Level::DEBUG,
|
||||
outbox_res, activity = activity.req.id,
|
||||
"got response for outbox dispatch"
|
||||
);
|
||||
}
|
||||
|
||||
pub fn for_user(user: User, transport: &'a OutboxTransport) -> Outbox<'a> {
|
||||
|
|
|
@ -12,6 +12,7 @@ use rsa::{
|
|||
|
||||
use base64::prelude::*;
|
||||
use chrono::Utc;
|
||||
use tracing::{event, Level};
|
||||
|
||||
pub struct HttpClient {
|
||||
client: reqwest::Client,
|
||||
|
@ -59,7 +60,8 @@ impl RequestBuilder {
|
|||
}
|
||||
|
||||
pub async fn send(self) -> Result<Response, reqwest::Error> {
|
||||
dbg!(&self.inner);
|
||||
event!(Level::DEBUG, ?self.inner, "sending an http request");
|
||||
|
||||
self.inner.send().await
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ pub use user::*;
|
|||
mod post;
|
||||
pub use post::*;
|
||||
|
||||
pub const AS_CONTEXT: &'static str = "https://www.w3.org/ns/activitystreams";
|
||||
pub const AS_CONTEXT: &str = "https://www.w3.org/ns/activitystreams";
|
||||
|
||||
pub fn new_id() -> String {
|
||||
Uuid::new_v4().to_string()
|
||||
|
|
|
@ -3,7 +3,7 @@ use chrono::{DateTime, Utc};
|
|||
use serde::Serialize;
|
||||
use sqlx::Sqlite;
|
||||
|
||||
const POST_TYPE: &'static str = "Post";
|
||||
const POST_TYPE: &str = "Post";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Post {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use sqlx::Sqlite;
|
||||
use std::fmt::Debug;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Actor {
|
||||
|
@ -20,6 +21,10 @@ impl Actor {
|
|||
pub fn inbox(&self) -> &str {
|
||||
&self.inbox
|
||||
}
|
||||
|
||||
pub fn outbox(&self) -> &str {
|
||||
&self.outbox
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
@ -30,6 +35,13 @@ pub struct User {
|
|||
display_name: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum UserError {
|
||||
#[error("user `{0}` not found")]
|
||||
NotFound(String),
|
||||
}
|
||||
|
||||
impl User {
|
||||
pub fn id(&self) -> &str {
|
||||
&self.id
|
||||
|
@ -55,7 +67,7 @@ impl User {
|
|||
format!("https://ferri.amy.mov/users/{}", self.id())
|
||||
}
|
||||
|
||||
pub async fn from_id(uuid: &str, conn: impl sqlx::Executor<'_, Database = Sqlite>) -> User {
|
||||
pub async fn from_id(uuid: &str, conn: impl sqlx::Executor<'_, Database = Sqlite>) -> Result<User, UserError> {
|
||||
let user = sqlx::query!(
|
||||
r#"
|
||||
SELECT u.*, a.id as "actor_own_id", a.inbox, a.outbox
|
||||
|
@ -65,10 +77,11 @@ impl User {
|
|||
"#,
|
||||
uuid
|
||||
)
|
||||
.fetch_one(conn)
|
||||
.await
|
||||
.unwrap();
|
||||
User {
|
||||
.fetch_one(conn)
|
||||
.await
|
||||
.map_err(|_| UserError::NotFound(uuid.to_string()))?;
|
||||
|
||||
Ok(User {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
actor: Actor {
|
||||
|
@ -77,7 +90,7 @@ impl User {
|
|||
outbox: user.outbox,
|
||||
},
|
||||
display_name: user.display_name,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn from_username(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue