mirror of
https://github.com/nullishamy/ferri.git
synced 2025-06-28 17:14:18 +00:00
feat: more fixes; finish account api types for now; add some more useful fields to it
This commit is contained in:
parent
76fb8838c2
commit
62931ee20b
10 changed files with 168 additions and 65 deletions
|
@ -1,6 +1,6 @@
|
|||
pub mod ap;
|
||||
pub mod config;
|
||||
mod types_rewrite;
|
||||
pub mod types_rewrite;
|
||||
|
||||
use rand::{Rng, distributions::Alphanumeric};
|
||||
|
||||
|
|
|
@ -32,17 +32,17 @@ impl From<db::User> for api::Account {
|
|||
api::Account {
|
||||
id: val.id,
|
||||
username: val.username,
|
||||
acct: "FIXME_api::Account::acct".to_string(),
|
||||
acct: val.acct,
|
||||
display_name: val.display_name,
|
||||
|
||||
locked: false,
|
||||
bot: false,
|
||||
|
||||
created_at: "FIXME_api::Account::created_at".to_string(),
|
||||
created_at: val.created_at.to_rfc3339(),
|
||||
attribution_domains: vec![],
|
||||
|
||||
note: "".to_string(),
|
||||
url: "FIXME_api::Account::url".to_string(),
|
||||
url: val.url,
|
||||
|
||||
avatar: "https://ferri.amy.mov/assets/pfp.png".to_string(),
|
||||
avatar_static: "https://ferri.amy.mov/assets/pfp.png".to_string(),
|
||||
|
@ -52,7 +52,7 @@ impl From<db::User> for api::Account {
|
|||
followers_count: 0,
|
||||
following_count: 0,
|
||||
statuses_count: 0,
|
||||
last_status_at: "FIXME_api::Account::last_status_at".to_string(),
|
||||
last_status_at: val.posts.last_post_at.map(|ts| ts.to_rfc3339()),
|
||||
|
||||
emojis: vec![],
|
||||
fields: vec![],
|
||||
|
|
90
ferri-main/src/types_rewrite/fetch.rs
Normal file
90
ferri-main/src/types_rewrite/fetch.rs
Normal file
|
@ -0,0 +1,90 @@
|
|||
use crate::types_rewrite::{ObjectUuid, ObjectUri, db};
|
||||
use sqlx::SqliteConnection;
|
||||
use thiserror::Error;
|
||||
use tracing::info;
|
||||
use chrono::{NaiveDateTime, DateTime, Utc};
|
||||
|
||||
const SQLITE_TIME_FMT: &'static str = "%Y-%m-%d %H:%M:%S";
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum FetchError {
|
||||
#[error("an unknown error occured when fetching: {0}")]
|
||||
Unknown(String)
|
||||
}
|
||||
|
||||
fn parse_ts(ts: String) -> Option<DateTime<Utc>> {
|
||||
NaiveDateTime::parse_from_str(&ts, SQLITE_TIME_FMT)
|
||||
.ok()
|
||||
.map(|nt| nt.and_utc())
|
||||
}
|
||||
|
||||
pub async fn user_by_id(id: ObjectUuid, conn: &mut SqliteConnection) -> Result<db::User, FetchError> {
|
||||
info!("fetching user by uuid '{:?}' from the database", id);
|
||||
|
||||
let record = sqlx::query!(r#"
|
||||
SELECT
|
||||
u.id as "user_id",
|
||||
u.username,
|
||||
u.actor_id,
|
||||
u.display_name,
|
||||
a.inbox,
|
||||
a.outbox,
|
||||
u.url,
|
||||
u.acct,
|
||||
u.remote,
|
||||
u.created_at
|
||||
FROM "user" u
|
||||
INNER JOIN "actor" a ON u.actor_id = a.id
|
||||
WHERE u.id = ?1
|
||||
"#, id.0)
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.map_err(|e| FetchError::Unknown(e.to_string()))?;
|
||||
|
||||
let follower_count = sqlx::query_scalar!(r#"
|
||||
SELECT COUNT(follower_id)
|
||||
FROM "follow"
|
||||
WHERE followed_id = ?1
|
||||
"#, record.actor_id)
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.map_err(|e| FetchError::Unknown(e.to_string()))?;
|
||||
|
||||
let last_post_at = sqlx::query_scalar!(r#"
|
||||
SELECT datetime(p.created_at)
|
||||
FROM post p
|
||||
WHERE p.user_id = ?1
|
||||
ORDER BY datetime(p.created_at) DESC
|
||||
LIMIT 1
|
||||
"#, record.user_id)
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.map_err(|e| FetchError::Unknown(e.to_string()))?
|
||||
.and_then(|ts| {
|
||||
info!("parsing timestamp {}", ts);
|
||||
parse_ts(ts)
|
||||
});
|
||||
|
||||
let user_created = parse_ts(record.created_at).expect("no db corruption");
|
||||
|
||||
info!("user {:?} has {} followers", id, follower_count);
|
||||
info!("user {:?} last posted {:?}", id, last_post_at);
|
||||
|
||||
Ok(db::User {
|
||||
id: ObjectUuid(record.user_id),
|
||||
actor: db::Actor {
|
||||
id: ObjectUri(record.actor_id),
|
||||
inbox: record.inbox,
|
||||
outbox: record.outbox
|
||||
},
|
||||
acct: record.acct,
|
||||
remote: record.remote,
|
||||
username: record.username,
|
||||
display_name: record.display_name,
|
||||
created_at: user_created,
|
||||
url: record.url,
|
||||
posts: db::UserPosts {
|
||||
last_post_at
|
||||
}
|
||||
})
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
|
||||
mod convert;
|
||||
pub use convert::*;
|
||||
pub mod convert;
|
||||
pub mod fetch;
|
||||
|
||||
pub const AS_CONTEXT_RAW: &'static str = "https://www.w3.org/ns/activitystreams";
|
||||
pub fn as_context() -> ObjectContext {
|
||||
|
@ -16,10 +16,10 @@ pub enum ObjectContext {
|
|||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||
pub struct ObjectUri(String);
|
||||
pub struct ObjectUri(pub String);
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||
pub struct ObjectUuid(String);
|
||||
pub struct ObjectUuid(pub String);
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||
pub struct Object {
|
||||
|
@ -29,22 +29,34 @@ pub struct Object {
|
|||
}
|
||||
|
||||
pub mod db {
|
||||
use serde::{Serialize, Deserialize};
|
||||
use chrono::{DateTime, Utc};
|
||||
use super::*;
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Actor {
|
||||
pub id: ObjectUri,
|
||||
pub inbox: String,
|
||||
pub outbox: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct UserPosts {
|
||||
// User may have no posts
|
||||
pub last_post_at: Option<DateTime<Utc>>
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct User {
|
||||
pub id: ObjectUuid,
|
||||
pub actor_id: ObjectUri,
|
||||
pub actor: Actor,
|
||||
pub username: String,
|
||||
pub display_name: String
|
||||
pub display_name: String,
|
||||
pub acct: String,
|
||||
pub remote: bool,
|
||||
pub url: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
|
||||
pub posts: UserPosts
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -121,7 +133,7 @@ pub mod api {
|
|||
pub followers_count: i64,
|
||||
pub following_count: i64,
|
||||
pub statuses_count: i64,
|
||||
pub last_status_at: String,
|
||||
pub last_status_at: Option<String>,
|
||||
|
||||
pub emojis: Vec<Emoji>,
|
||||
pub fields: Vec<CustomField>,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue