Skip to content

Commit

Permalink
Merge pull request #107 from approvers/refactor
Browse files Browse the repository at this point in the history
fun: ✨ STATICALLY DISPATCHED CLIENTS ✨ + other stuff
  • Loading branch information
kawaemon authored Dec 2, 2024
2 parents 7258731 + a01e21d commit 3e9cce0
Show file tree
Hide file tree
Showing 30 changed files with 1,189 additions and 892 deletions.
967 changes: 532 additions & 435 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ default = ["prod"]

[dependencies]
anyhow = "1"
async-trait = "0.1"
chrono = { version = "0.4", features = ["serde"] }
chrono-tz = "0.10"
clap = { version = "4", features = ["derive"] }
Expand All @@ -34,7 +33,7 @@ dotenv = "0.15"
hex = "0.4"
humantime = "2"
image = "0.25"
libwebp-sys = "0.10"
libwebp-sys = "0.11"
once_cell = "1"
ordered-float = { version = "4" }
parking_lot = "0.12"
Expand All @@ -46,7 +45,7 @@ serde_json = "1"
sha2 = "0.10"
shellwords = "1"
static_assertions = "1"
thiserror = "1"
thiserror = "2"
tracing = "0.1"
tracing-subscriber = "0.3"
url = "2"
Expand All @@ -66,7 +65,7 @@ crossbeam = { version = "0.8", optional = true }
bzip2-sys = { version = "0.1.11", features = ["static"] }

[dependencies.charming]
version = "0.3"
version = "0.4"
optional = true
default-features = false
features = ["ssr"]
Expand Down
14 changes: 0 additions & 14 deletions Justfile

This file was deleted.

4 changes: 2 additions & 2 deletions src/bot/alias/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ impl<D: MessageAliasDatabase> MessageAliasBot<D> {

pub(super) async fn make(
&self,
ctx: &dyn Context,
ctx: &impl Context,
key: &str,
msg: Option<&str>,
attachments: &[&dyn Attachment],
attachments: &[impl Attachment],
force: bool,
) -> Result<()> {
let key = key.trim();
Expand Down
42 changes: 24 additions & 18 deletions src/bot/alias/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
mod command;
pub(crate) mod model;
pub mod model;

use {
crate::bot::{
alias::model::MessageAlias, parse_command, ui, BotService, Context, IsUpdated, Message,
SendAttachment, SendMessage,
Runtime, SendAttachment, SendMessage,
},
anyhow::Result,
async_trait::async_trait,
std::future::Future,
};

const NAME: &str = "rusty_ponyo::bot::alias";
Expand Down Expand Up @@ -50,27 +50,29 @@ enum Command {
},
}

#[async_trait]
pub(crate) trait MessageAliasDatabase: Send + Sync {
async fn save(&self, alias: MessageAlias) -> Result<()>;
async fn get(&self, key: &str) -> Result<Option<MessageAlias>>;
async fn get_and_increment_usage_count(&self, key: &str) -> Result<Option<MessageAlias>>;
async fn delete(&self, key: &str) -> Result<IsUpdated>;
async fn len(&self) -> Result<u32>;
async fn usage_count_top_n(&self, n: usize) -> Result<Vec<MessageAlias>>;
pub trait MessageAliasDatabase: Send + Sync {
fn save(&self, alias: MessageAlias) -> impl Future<Output = Result<()>> + Send;
fn get(&self, key: &str) -> impl Future<Output = Result<Option<MessageAlias>>> + Send;
fn get_and_increment_usage_count(
&self,
key: &str,
) -> impl Future<Output = Result<Option<MessageAlias>>> + Send;
fn delete(&self, key: &str) -> impl Future<Output = Result<IsUpdated>> + Send;
fn len(&self) -> impl Future<Output = Result<u32>> + Send;
fn usage_count_top_n(&self, n: usize)
-> impl Future<Output = Result<Vec<MessageAlias>>> + Send;
}

pub(crate) struct MessageAliasBot<D: MessageAliasDatabase> {
pub struct MessageAliasBot<D: MessageAliasDatabase> {
db: D,
}

#[async_trait]
impl<D: MessageAliasDatabase> BotService for MessageAliasBot<D> {
impl<R: Runtime, D: MessageAliasDatabase> BotService<R> for MessageAliasBot<D> {
fn name(&self) -> &'static str {
NAME
}

async fn on_message(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
async fn on_message(&self, msg: &R::Message, ctx: &R::Context) -> Result<()> {
if msg.content().starts_with(PREFIX) {
if let Some(msg) = self.on_command(msg, ctx).await? {
ctx.send_message(SendMessage {
Expand All @@ -90,11 +92,15 @@ impl<D: MessageAliasDatabase> BotService for MessageAliasBot<D> {
}

impl<D: MessageAliasDatabase> MessageAliasBot<D> {
pub(crate) fn new(db: D) -> Self {
pub fn new(db: D) -> Self {
Self { db }
}

async fn on_command(&self, message: &dyn Message, ctx: &dyn Context) -> Result<Option<String>> {
async fn on_command(
&self,
message: &impl Message,
ctx: &impl Context,
) -> Result<Option<String>> {
let Some(parsed) = parse_command::<Ui>(message.content(), ctx).await? else {
return Ok(None);
};
Expand All @@ -117,7 +123,7 @@ impl<D: MessageAliasDatabase> MessageAliasBot<D> {
}
}

async fn send_alias(&self, ctx: &dyn Context, alias: &MessageAlias) -> Result<()> {
async fn send_alias(&self, ctx: &impl Context, alias: &MessageAlias) -> Result<()> {
ctx.send_message(SendMessage {
content: &alias.message,
attachments: &alias
Expand Down
18 changes: 9 additions & 9 deletions src/bot/alias/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ use {
};

#[derive(Clone, Serialize)]
pub(crate) struct MessageAlias {
pub(crate) key: String,
pub(crate) message: String,
pub(crate) attachments: Vec<MessageAliasAttachment>,
pub(crate) usage_count: u32,
pub(crate) created_at: DateTime<Utc>,
pub struct MessageAlias {
pub key: String,
pub message: String,
pub attachments: Vec<MessageAliasAttachment>,
pub usage_count: u32,
pub created_at: DateTime<Utc>,
}

#[derive(Clone, Serialize)]
pub(crate) struct MessageAliasAttachment {
pub(crate) name: String,
pub(crate) data: Vec<u8>,
pub struct MessageAliasAttachment {
pub name: String,
pub data: Vec<u8>,
}
40 changes: 21 additions & 19 deletions src/bot/auth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use {
crate::bot::{parse_command, ui, BotService, Context, Message},
crate::bot::{parse_command, ui, BotService, Context, Message, Runtime, User},
anyhow::{Context as _, Result},
async_trait::async_trait,
rand::{prelude::StdRng, Rng, SeedableRng},
sequoia_openpgp::{
cert::CertParser,
Expand All @@ -11,7 +10,7 @@ use {
Cert,
},
sha2::Digest,
std::{io::Write, time::Duration},
std::{future::Future, io::Write, time::Duration},
url::{Host, Origin, Url},
};

Expand Down Expand Up @@ -50,28 +49,31 @@ enum SetCommand {
},
}

#[async_trait]
pub(crate) trait GenkaiAuthDatabase: Send + Sync {
async fn register_pgp_key(&self, user_id: u64, cert: &str) -> Result<()>;
async fn get_pgp_key(&self, user_id: u64) -> Result<Option<String>>;

async fn register_token(&self, user_id: u64, hashed_token: &str) -> Result<()>;
async fn revoke_token(&self, user_id: u64) -> Result<()>;
async fn get_token(&self, user_id: u64) -> Result<Option<String>>;
pub trait GenkaiAuthDatabase: Send + Sync {
fn register_pgp_key(&self, user_id: u64, cert: &str)
-> impl Future<Output = Result<()>> + Send;
fn get_pgp_key(&self, user_id: u64) -> impl Future<Output = Result<Option<String>>> + Send;

fn register_token(
&self,
user_id: u64,
hashed_token: &str,
) -> impl Future<Output = Result<()>> + Send;
fn revoke_token(&self, user_id: u64) -> impl Future<Output = Result<()>> + Send;
fn get_token(&self, user_id: u64) -> impl Future<Output = Result<Option<String>>> + Send;
}

pub(crate) struct GenkaiAuthBot<D> {
pub struct GenkaiAuthBot<D> {
db: D,
pgp_pubkey_source_domain_whitelist: Vec<String>,
}

#[async_trait]
impl<D: GenkaiAuthDatabase> BotService for GenkaiAuthBot<D> {
impl<R: Runtime, D: GenkaiAuthDatabase> BotService<R> for GenkaiAuthBot<D> {
fn name(&self) -> &'static str {
NAME
}

async fn on_message(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
async fn on_message(&self, msg: &R::Message, ctx: &R::Context) -> Result<()> {
if !msg.content().starts_with(PREFIX) {
return Ok(());
}
Expand All @@ -93,14 +95,14 @@ impl<D: GenkaiAuthDatabase> BotService for GenkaiAuthBot<D> {
}

impl<D: GenkaiAuthDatabase> GenkaiAuthBot<D> {
pub(crate) fn new(db: D, pubkey_whitelist: Vec<String>) -> Self {
pub fn new(db: D, pubkey_whitelist: Vec<String>) -> Self {
Self {
db,
pgp_pubkey_source_domain_whitelist: pubkey_whitelist,
}
}

async fn set_pgp(&self, msg: &dyn Message, ctx: &dyn Context, url: &str) -> Result<()> {
async fn set_pgp(&self, msg: &impl Message, ctx: &impl Context, url: &str) -> Result<()> {
let verify_result = match self.verify_url(url) {
Ok(_) => download_gpg_key(url).await,
Err(e) => Err(e),
Expand All @@ -127,7 +129,7 @@ impl<D: GenkaiAuthDatabase> GenkaiAuthBot<D> {
Ok(())
}

async fn token(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
async fn token(&self, msg: &impl Message, ctx: &impl Context) -> Result<()> {
let author = msg.author();

if self.db.get_token(author.id()).await?.is_some() {
Expand Down Expand Up @@ -171,7 +173,7 @@ impl<D: GenkaiAuthDatabase> GenkaiAuthBot<D> {
Ok(())
}

async fn revoke(&self, msg: &dyn Message, ctx: &dyn Context) -> Result<()> {
async fn revoke(&self, msg: &impl Message, ctx: &impl Context) -> Result<()> {
self.db
.revoke_token(msg.author().id())
.await
Expand Down
14 changes: 7 additions & 7 deletions src/bot/genkai_point/formula/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use {self::v3::FormulaV3, crate::bot::genkai_point::model::Session};
use {crate::bot::genkai_point::model::Session, v3::FormulaV3};

pub mod v1;
pub mod v2;
pub mod v3;

pub(crate) trait GenkaiPointFormula: Send + Sync + 'static {
pub trait GenkaiPointFormula: Send + Sync + 'static {
fn name(&self) -> &'static str;
fn calc(&self, sessions: &[Session]) -> GenkaiPointFormulaOutput;
}

pub(crate) struct GenkaiPointFormulaOutput {
pub(crate) point: u64,
pub(crate) efficiency: f64,
pub struct GenkaiPointFormulaOutput {
pub point: u64,
pub efficiency: f64,
}

pub(crate) fn default_formula() -> impl GenkaiPointFormula {
pub fn default_formula() -> impl GenkaiPointFormula {
FormulaV3
}

pub(crate) struct DynGenkaiPointFormula(pub Box<dyn GenkaiPointFormula>);
pub struct DynGenkaiPointFormula(pub Box<dyn GenkaiPointFormula>);

impl GenkaiPointFormula for DynGenkaiPointFormula {
fn name(&self) -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/genkai_point/formula/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
chrono_tz::Asia::Tokyo,
};

pub(crate) struct FormulaV1;
pub struct FormulaV1;

impl GenkaiPointFormula for FormulaV1 {
fn name(&self) -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/genkai_point/formula/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
chrono_tz::Asia::Tokyo,
};

pub(crate) struct FormulaV2;
pub struct FormulaV2;

impl GenkaiPointFormula for FormulaV2 {
fn name(&self) -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion src/bot/genkai_point/formula/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use {
chrono_tz::Asia::Tokyo,
};

pub(crate) struct FormulaV3;
pub struct FormulaV3;

impl GenkaiPointFormula for FormulaV3 {
fn name(&self) -> &'static str {
Expand Down
Loading

0 comments on commit 3e9cce0

Please sign in to comment.