From: n1c00o <34602094+n1c00o@users.noreply.github.com> Date: Sat, 16 Oct 2021 20:26:09 +0000 (+0200) Subject: Merge branch 'main' into nats-structs-discord-gateway X-Git-Tag: v0.1~55^2~3 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=e58e816ceb8caa3c77dd98a952761b7e7f05b6cb;p=matthieu%2Fnova.git Merge branch 'main' into nats-structs-discord-gateway --- e58e816ceb8caa3c77dd98a952761b7e7f05b6cb diff --cc Cargo.lock index e6a2de2,c25dbfe..e8b87c7 --- a/Cargo.lock +++ b/Cargo.lock @@@ -222,8 -210,9 +211,10 @@@ dependencies = "nats", "pretty_env_logger", "prometheus", + "redis", "serde 1.0.130", + "serde_repr", + "testcontainers", "tokio", ] diff --cc common/rust/Cargo.toml index 453f7b8,d54eea9..234f1b1 --- a/common/rust/Cargo.toml +++ b/common/rust/Cargo.toml @@@ -11,6 -10,10 +11,11 @@@ serde_repr = "0.1 config = "0.11" hyper = { version = "0.14", features = ["full"] } tokio = { version = "1", features = ["full"] } - prometheus = "0.12.0" - nats = "0.15.2" +enumflags2 = { version = "0.7.1", features = ["serde"] } + prometheus = { version = "0.12.0", features = ["process"] } + nats = "0.15.2" + testcontainers = "0.12.0" + + [dependencies.redis] + version = "*" + features = ["cluster", "connection-manager", "tokio-comp"] diff --cc common/rust/src/config.rs index bd12350,e4dbade..327f6f8 --- a/common/rust/src/config.rs +++ b/common/rust/src/config.rs @@@ -17,7 -22,11 +22,12 @@@ impl Settings + std::default::Default + Clone, { ++ + /// Initializes a new configuration like the other components of nova + /// And starts the prometheus metrics server if needed. pub fn new(service_name: &str) -> Result, ConfigError> { + pretty_env_logger::init(); + let mut default = Config::default(); // this file my be shared with all the components default.merge(File::with_name("config/default"))?; @@@ -32,8 -42,7 +43,9 @@@ let mut config: Settings = default.clone().try_into().unwrap(); // try to load the config - config.config = default.get::(&service_name).unwrap(); + config.config = default.get::(service_name).unwrap(); - pretty_env_logger::init(); ++ // todo(MatthieuCodder): the following line was not present in the conflict ++ // pretty_env_logger::init(); // start the monitoring system if needed crate::monitoring::start_monitoring(&config.monitoring); diff --cc common/rust/src/lib.rs index a021e97,f4e27fc..5418d0a --- a/common/rust/src/lib.rs +++ b/common/rust/src/lib.rs @@@ -1,8 -1,16 +1,18 @@@ - /// This crate contains shared code in all the rust projects - /// It contains utilities such as monitoring, logging and more. + /// This crate is all the utilities shared by the nova rust projects + /// It includes loging, config and protocols. pub mod config; +pub mod discord_models; +pub mod error; pub mod monitoring; pub mod nats; pub mod payloads; + pub mod error; + pub mod redis; + + pub use log as log; + pub use serde as serde; + pub use ::config as config_crate; + pub use prometheus as prometheus; + pub use ::nats as nats_crate; + pub use testcontainers as testcontainers; -pub use ::redis as redis_crate; ++pub use ::redis as redis_crate; diff --cc common/rust/src/nats.rs index 1a4d349,c61aa4c..a001dc9 --- a/common/rust/src/nats.rs +++ b/common/rust/src/nats.rs @@@ -2,30 -2,29 +2,31 @@@ use nats::{Connection, Options} use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] - struct NatsConfigurationClientCert { - cert: String, - key: String, + pub struct NatsConfigurationClientCert { + pub cert: String, + pub key: String, } ++ #[derive(Clone, Debug, Deserialize)] - struct NatsConfigurationTls { - mtu: Option, + pub struct NatsConfigurationTls { + pub mtu: Option, } #[derive(Clone, Debug, Deserialize)] pub struct NatsConfiguration { - client_cert: Option, - root_cert: Option>, - jetstream_api_prefix: Option, - max_reconnects: Option, - reconnect_buffer_size: Option, - tls: Option, - client_name: Option, - tls_required: Option, - host: String, + pub client_cert: Option, + pub root_cert: Option>, + pub jetstream_api_prefix: Option, + pub max_reconnects: Option, + pub reconnect_buffer_size: Option, + pub tls: Option, + pub client_name: Option, + pub tls_required: Option, + pub host: String, } +// todo: Prefer From since it automatically gives a free Into implementation - /// + // Allows the configuration to directly create a nats connection impl Into for NatsConfiguration { fn into(self) -> Connection { let mut options = Options::new(); diff --cc common/rust/src/payloads.rs index 4f70b45,c97ac1d..d8d06f2 --- a/common/rust/src/payloads.rs +++ b/common/rust/src/payloads.rs @@@ -1,268 -1,24 +1,269 @@@ use serde::{Deserialize, Serialize}; + use std::fmt::Debug; -/// Data structure sent to the cache component -/// by the gateway & webhook. -#[derive(Serialize, Deserialize, Debug)] -#[serde(bound(deserialize = "T: Deserialize<'de> + Debug"))] -pub struct CachePayload { +use crate::discord_models::{ + application::Application, + channel::{Channel, Message, ThreadMember}, + emoji::Emoji, + gateway::PresenceUpdate, + guild::{Guild, GuildMember, Integration}, + invite::InviteTargetTypes, + permissions::Role, + slash_commands::{ApplicationCommand, Interaction}, + stage_instance::StageInstance, + user::User, + voice::VoiceState, +}; - #[serde(rename = "tr")] +/// Payload send to the nova cache queues +#[derive(Serialize, Deserialize, Debug, Clone)] +// #[serde(bound(deserialize = "T: Deserialize<'de> + std::default::Default + Clone"))] +pub struct CachePayload { pub tracing: Tracing, - - #[serde(rename = "d")] - pub data: T, - - #[serde(rename = "o")] - pub operation: String, + pub data: CacheData, } - #[derive(Serialize, Deserialize, Debug, Clone)] + #[derive(Serialize, Deserialize, Debug)] pub struct Tracing { pub node_id: String, - pub span: Option + pub span: Option, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum CacheData { + Ready { + version: u8, + user: User, + guilds: Vec, + session_id: String, + shard: Option>, + application: Application, + }, + ApplicationCommandCreate { + guild_id: Option, + command: ApplicationCommand, + }, + ApplicationCommandUpdate { + guild_id: Option, + command: ApplicationCommand, + }, + ApplicationCommandDelete { + guild_id: Option, + command: ApplicationCommand, + }, + ChannelCreate { + channel: Channel, + }, + ChannelUpdate { + channel: Channel, + }, + ChannelDelete { + channel: Channel, + }, + ThreadCreate { + channel: Channel, + }, + ThreadUpdate { + channel: Channel, + }, + ThreadDelete { + channel: Channel, + }, + ThreadListSync { + guild_id: String, + channel_ids: Option>, + threads: Vec, + members: Vec, + }, + ThreadMemberUpdate { + member: ThreadMember, + }, + ThreadMembersUpdate { + id: String, + guild_id: String, + member_count: i64, + added_members: Option>, + removed_member_ids: Option>, + }, + ChannelPinsUpdate { + guild_id: Option, + channel_id: String, + last_pin_timestamp: Option, + }, + GuildCreate { + guild: Guild, + }, + GuildUpdate { + guild: Guild, + }, + GuildDelete { + guild: Guild, + }, + GuildBanAdd { + guild_id: String, + user: User, + }, + GuildBanRemove { + guild_id: String, + user: User, + }, + GuildEmojisUpdate { + guild_id: String, + emojis: Vec, + }, + GuildIntegrationsUpdate { + guild_id: String, + }, + GuildMemberAdd { + guild_id: String, + member: GuildMember, + }, + GuildMemberRemove { + guild_id: String, + user: User, + }, + GuildMemberUpdate { + guild_id: String, + roles: Vec, + user: User, + nick: Option, + joined_at: Option, + premium_since: Option, + deaf: Option, + mute: Option, + pending: Option, + }, + GuildMembersChunk { + guild_id: String, + members: Vec, + chunk_index: i64, + chunk_count: i64, + not_found: Option>, + presences: Option>, + nonce: Option, + }, + GuildRoleCreate { + guild_id: String, + role: Role, + }, + GuildRoleUpdate { + guild_id: String, + role: Role, + }, + GuildRoleDelete { + guild_id: String, + role_id: String, + }, + IntegrationCreate { + guild_id: String, + integration: Integration, + }, + IntegrationUpdate { + guild_id: String, + integration: Integration, + }, + IntegrationDelete { + id: String, + guild_id: String, + application_id: Option, + }, + InviteCreate { + channel_id: String, + code: String, + created_at: String, + guild_id: Option, + inviter: Option, + max_age: i64, + max_uses: i64, + target_type: Option, + target_user: Option, + target_application: Option, + temporary: bool, + uses: i64, + }, + InviteDelete { + channel_id: String, + guild_id: Option, + code: String, + }, + InteractionCreate { + // boxed to avoid a large difference size between variants (https://rust-lang.github.io/rust-clippy/master/index.html#large_enum_variant) + interaction: Box, + }, + MessageCreate { + message: Message, + }, + MessageUpdate { + message: Message, + }, + MessageDelete { + id: String, + channel_id: String, + guild_id: Option, + }, + MessageDeleteBulk { + ids: Vec, + channel_id: String, + guild_id: Option, + }, + MessageReactionAdd { + user_id: String, + channel_id: String, + message_id: String, + guild_id: Option, + member: Option, + emoji: Emoji, + }, + MessageReactionRemove { + user_id: String, + channel_id: String, + message_id: String, + guild_id: Option, + emoji: Emoji, + }, + MessageReactionRemoveAll { + channel_id: String, + message_id: String, + guild_id: Option, + }, + MessageReactionRemoveEmoji { + channel_id: String, + message_id: String, + guild_id: Option, + emoji: Emoji, + }, + PresenceUpdate { + presence: PresenceUpdate, + }, + TypingStart { + channel_id: String, + guild_id: Option, + user_id: String, + timestamp: i64, + member: Option, + }, + UserUpdate { + user: User, + }, + VoiceStateUpdate { + state: VoiceState, + }, + VoiceServerUpdate { + token: String, + guild_id: String, + endpoint: Option, + }, + WebhookUpdate { + guild_id: String, + channel_id: String, + }, + StageInstanceCreate { + instance: StageInstance, + }, + StageInstanceUpdate { + instance: StageInstance, + }, + StageInstanceDelete { + instance: StageInstance, + }, }