privmsg.rs 1.5 KB

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