Last active
May 28, 2022 03:06
-
-
Save jayhuang75/87a709d62a066f2178784d946bd88223 to your computer and use it in GitHub Desktop.
nextjs-rust-twitter-websocket-model.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crate::ClientStore; | |
use uuid::Uuid; | |
use warp::ws::Message; | |
use tokio::sync::{mpsc}; | |
use serde::{Deserialize, Serialize}; | |
#[derive(Debug, Clone)] | |
pub struct Client { | |
pub user_id: Uuid, | |
pub topic: String, | |
pub sender: Option<mpsc::UnboundedSender<std::result::Result<Message, warp::Error>>>, | |
} | |
impl Client { | |
pub fn new(user_id: Uuid, topic: String) -> Self { | |
Client { | |
user_id: user_id, | |
topic: topic, | |
sender: None, | |
} | |
} | |
pub async fn register(&self, clients: ClientStore) -> String { | |
let uuid = self.user_id.to_string(); | |
clients.write().await.insert( | |
uuid.clone(), | |
Client { | |
user_id: self.user_id, | |
topic: self.topic.clone(), | |
sender: None, | |
}, | |
); | |
uuid | |
} | |
} | |
#[derive(Deserialize, Debug)] | |
pub struct RegisterRequest { | |
pub user_id: String, | |
pub topic: String, | |
} | |
#[derive(Serialize, Debug)] | |
pub struct RegisterResponse { | |
pub user_uuid: String, | |
} | |
#[derive(Deserialize)] | |
pub struct TopicRequest { | |
pub topic: String, | |
} | |
#[derive(Debug, Deserialize)] | |
#[serde(untagged)] | |
pub enum StreamMessage { | |
Tweet(Tweet), | |
Other(serde::de::IgnoredAny), | |
} | |
#[derive(Debug, Deserialize, Serialize)] | |
pub struct Tweet { | |
pub id: u64, | |
pub user: User, | |
pub text: String, | |
pub entities: Option<Entities>, | |
pub lang: String, | |
pub possibly_sensitive: bool, | |
} | |
#[derive(Debug, Deserialize, Serialize)] | |
pub struct User { | |
pub screen_name: String, | |
pub location: String, | |
} | |
#[derive(Debug, Deserialize, Serialize)] | |
pub struct Entities { | |
pub hashtags: Vec<Hashtag>, | |
} | |
#[derive(Debug, Deserialize, Serialize)] | |
pub struct Hashtag { | |
pub text: String, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment