Skip to content

Commit

Permalink
fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
AEnterprise committed Oct 11, 2020
1 parent 82cb3cc commit ea0d0e1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
9 changes: 8 additions & 1 deletion src/redis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,22 @@ pub struct TeamInfo {
pub struct TeamMember {
pub username: String,
pub discriminator: String,
pub id: u64,
pub id: String, //string to accomodate javascript
pub avatar: String,
pub socials: TeamSocials,
pub team: String,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TeamSocials {
#[serde(skip_serializing_if = "is_default")]
pub twitter: Option<String>,
#[serde(skip_serializing_if = "is_default")]
pub github: Option<String>,
#[serde(skip_serializing_if = "is_default")]
pub website: Option<String>
}

fn is_default<T: Default + PartialEq>(t: &T) -> bool {
t == &T::default()
}
21 changes: 8 additions & 13 deletions src/redis/redis_link.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
use crate::util::config::ApiConfig;
use crate::util::error::{StartupError, CommunicationError};
use darkredis::{ConnectionPool, Connection};
use std::collections::HashMap;
use uuid::Uuid;
use tokio::sync::watch::{Receiver, channel, Sender};
use tokio::sync::broadcast::{Receiver, channel, Sender};
use crate::redis::{Reply, Request, GearBotRequest, TeamInfo, ReplyData};
use tokio::time::{timeout, Duration};
use futures_util::StreamExt;

pub struct RedisLink {
pool: ConnectionPool,
receiver: Receiver<Reply>,
sender: Sender<Reply>,
}

impl RedisLink {
pub async fn new(config: &ApiConfig) -> Result<Self, StartupError> {
let pool = darkredis::ConnectionPool::create(config.redis.to_string(), None, 5).await?;
let (sender, receiver) = channel(
Reply {
uuid: Uuid::new_v4(),
data: ReplyData::Blank
}
);
let (sender, _) = channel(5);
let connection = pool.spawn("api_connection").await?;
let s = sender.clone();
tokio::spawn(async move {
establish_bot_link(sender, connection).await;
establish_bot_link(s, connection).await;
});

Ok(Self {
pool,
receiver,
sender,
})
}

Expand Down Expand Up @@ -61,7 +56,7 @@ impl RedisLink {
.map_err(|_| CommunicationError::TimeoutError)
}
async fn await_reply(&self, uuid: Uuid) -> Reply {
while let Some(reply) = self.receiver.clone().recv().await {
while let Ok(reply) = self.sender.subscribe().recv().await {
if reply.uuid == uuid {
return reply
}
Expand All @@ -78,7 +73,7 @@ async fn establish_bot_link(sender: Sender<Reply>, connection: Connection) {
.for_each(|message| async {
let m = message;
let reply: Reply = serde_json::from_slice(&m.message).unwrap();
sender.broadcast(reply);
sender.send(reply);
}).await
}

0 comments on commit ea0d0e1

Please sign in to comment.