add basic guild create + message create endpoints

This commit is contained in:
sam 2024-01-20 16:43:03 +01:00
parent 5b23095520
commit e57bff00c2
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
27 changed files with 367 additions and 36 deletions

View file

@ -9,7 +9,7 @@ edition = "2021"
addr = "0.15.6"
axum = "0.7.4"
base64 = "0.21.7"
chrono = "0.4.31"
chrono = { version = "0.4.31", features = ["serde"] }
eyre = "0.6.11"
once_cell = "1.19.0"
rand = "0.8.5"
@ -20,4 +20,5 @@ serde_json = "1.0.111"
sqlx = "0.7.3"
thiserror = "1.0.56"
tracing = "0.1.40"
ulid = "1.1.0"
uuid = { version = "1.6.1", features = ["v7"] }

View file

@ -7,6 +7,8 @@ pub enum FoxError {
NotFound,
#[error("date for signature out of range")]
SignatureDateOutOfRange(&'static str),
#[error("database error")]
DatabaseError,
#[error("non-200 response to federation request")]
ResponseNotOk,
#[error("server is invalid")]
@ -23,6 +25,10 @@ pub enum FoxError {
Unauthorized,
#[error("missing target user ID")]
MissingUser,
#[error("user is not in guild or guild doesn't exist")]
NotInGuild,
#[error("channel not found")]
ChannelNotFound,
}
impl From<ToStrError> for FoxError {

View file

@ -42,6 +42,7 @@ pub enum ErrorCode {
InvalidDate,
InvalidSignature,
MissingSignature,
GuildNotFound,
Unauthorized,
}
@ -87,6 +88,11 @@ impl From<FoxError> for ApiError {
code: ErrorCode::ObjectNotFound,
message: "Object not found".into(),
},
FoxError::DatabaseError => ApiError {
status: StatusCode::INTERNAL_SERVER_ERROR,
code: ErrorCode::InternalServerError,
message: "Database error".into(),
},
FoxError::SignatureDateOutOfRange(s) => ApiError {
status: StatusCode::BAD_REQUEST,
code: ErrorCode::InvalidSignature,
@ -127,10 +133,20 @@ impl From<FoxError> for ApiError {
code: ErrorCode::InvalidHeader,
message: "Missing user header".into(),
},
FoxError::NotInGuild => ApiError {
status: StatusCode::NOT_FOUND,
code: ErrorCode::GuildNotFound,
message: "Channel or guild not found".into(),
},
FoxError::Unauthorized => ApiError {
status: StatusCode::UNAUTHORIZED,
code: ErrorCode::Unauthorized,
message: "Missing or invalid token".into(),
},
FoxError::ChannelNotFound => ApiError {
status: StatusCode::NOT_FOUND,
code: ErrorCode::GuildNotFound,
message: "Channel or guild not found".into(),
}
}
}

View file

@ -6,3 +6,20 @@ pub mod s2s;
pub use error::FoxError;
pub use fed::signature;
use chrono::{DateTime, Utc};
use ulid::Ulid;
/// Extracts a DateTime from a ULID.
/// This function should only be used on valid ULIDs (such as those used as primary keys), else it will fail and panic!
pub fn ulid_timestamp(id: &str) -> DateTime<Utc> {
let ts = Ulid::from_string(id).expect("invalid ULID").timestamp_ms();
let (secs, rem) = (ts / 1000, ts % 1000);
let nsecs = rem * 1000000;
DateTime::from_timestamp(
secs.try_into().expect("converting secs to i64"),
nsecs.try_into().expect("converting nsecs to i32"),
)
.expect("converting timestamp to DateTime")
}

View file

@ -0,0 +1,15 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Channel {
pub id: String,
pub guild_id: String,
pub name: String,
pub topic: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct PartialChannel {
pub id: String,
pub name: String,
}

View file

@ -1,10 +1,11 @@
use serde::{Serialize, Deserialize};
use super::user::PartialUser;
use super::{user::PartialUser, channel::PartialChannel};
#[derive(Serialize, Deserialize, Debug)]
pub struct Guild {
pub id: String,
pub name: String,
pub owner: PartialUser,
pub default_channel: PartialChannel,
}

View file

@ -0,0 +1,6 @@
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct CreateMessageParams {
pub content: String,
}

View file

@ -1 +1,2 @@
pub mod guild;
pub mod channel;

View file

@ -0,0 +1,13 @@
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use super::user::PartialUser;
#[derive(Serialize, Deserialize, Debug)]
pub struct Message {
pub id: String,
pub channel_id: String,
pub author: PartialUser,
pub content: Option<String>,
pub created_at: DateTime<Utc>,
}

View file

@ -1,6 +1,10 @@
pub mod channel;
pub mod guild;
pub mod user;
pub mod http;
pub mod message;
pub mod user;
pub use channel::Channel;
pub use guild::Guild;
pub use message::Message;
pub use user::User;

View file

@ -0,0 +1,7 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "t", content = "d", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Dispatch {
MessageCreate
}

View file

@ -1,11 +1,13 @@
use serde::{Deserialize, Serialize};
use super::Dispatch;
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "t", content = "d", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Payload {
Dispatch {
#[serde(rename = "e")]
event: DispatchEvent,
event: Dispatch,
#[serde(rename = "r")]
recipients: Vec<String>,
},
@ -14,7 +16,3 @@ pub enum Payload {
token: String,
},
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "t", content = "d", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum DispatchEvent {}

View file

@ -1,4 +1,6 @@
mod dispatch;
mod event;
pub mod http;
pub use event::{DispatchEvent, Payload};
pub use event::Payload;
pub use dispatch::Dispatch;