privmsg.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. use async_std::sync::Mutex;
  2. use std::{io, sync::Arc};
  3. use fxhash::FxHashSet;
  4. use darkfi::{
  5. net,
  6. util::serial::{Decodable, Encodable},
  7. Result,
  8. };
  9. pub type PrivMsgId = u32;
  10. #[derive(Debug, Clone)]
  11. pub struct PrivMsg {
  12. pub id: PrivMsgId,
  13. pub nickname: String,
  14. pub channel: String,
  15. pub message: String,
  16. }
  17. impl net::Message for PrivMsg {
  18. fn name() -> &'static str {
  19. "privmsg"
  20. }
  21. }
  22. impl Encodable for PrivMsg {
  23. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  24. let mut len = 0;
  25. len += self.id.encode(&mut s)?;
  26. len += self.nickname.encode(&mut s)?;
  27. len += self.channel.encode(&mut s)?;
  28. len += self.message.encode(&mut s)?;
  29. Ok(len)
  30. }
  31. }
  32. impl Decodable for PrivMsg {
  33. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  34. Ok(Self {
  35. id: Decodable::decode(&mut d)?,
  36. nickname: Decodable::decode(&mut d)?,
  37. channel: Decodable::decode(&mut d)?,
  38. message: Decodable::decode(&mut d)?,
  39. })
  40. }
  41. }
  42. pub struct SeenPrivMsgIds {
  43. privmsg_ids: Mutex<FxHashSet<PrivMsgId>>,
  44. }
  45. pub type SeenPrivMsgIdsPtr = Arc<SeenPrivMsgIds>;
  46. impl SeenPrivMsgIds {
  47. pub fn new() -> Arc<Self> {
  48. Arc::new(Self { privmsg_ids: Mutex::new(FxHashSet::default()) })
  49. }
  50. pub async fn add_seen(&self, id: u32) {
  51. self.privmsg_ids.lock().await.insert(id);
  52. }
  53. pub async fn is_seen(&self, id: u32) -> bool {
  54. self.privmsg_ids.lock().await.contains(&id)
  55. }
  56. }