feat(ircv3): add client-batches feature
Some checks failed
CI / test (1.71) (pull_request) Has been cancelled
CI / test (stable) (pull_request) Has been cancelled
CI / rustfmt (pull_request) Has been cancelled

Signed-off-by: Raito Bezarius <masterancpp@gmail.com>
This commit is contained in:
Raito Bezarius 2024-10-13 22:23:31 +02:00
parent e85fd07719
commit 30748d7ba3
2 changed files with 21 additions and 1 deletions

View file

@ -71,6 +71,7 @@ tokio-rustls = { version = "0.24.0", features = ["dangerous_configuration"], opt
rustls-pemfile = { version = "1.0.2", optional = true } rustls-pemfile = { version = "1.0.2", optional = true }
tokio-native-tls = { version = "0.3.1", optional = true } tokio-native-tls = { version = "0.3.1", optional = true }
webpki-roots = { version = "0.23.0", optional = true } webpki-roots = { version = "0.23.0", optional = true }
atomic-counter = "1.0.1"
[dev-dependencies] [dev-dependencies]

View file

@ -47,6 +47,7 @@
//! # } //! # }
//! ``` //! ```
use atomic_counter::{AtomicCounter, RelaxedCounter};
#[cfg(feature = "ctcp")] #[cfg(feature = "ctcp")]
use chrono::prelude::*; use chrono::prelude::*;
use futures_util::{ use futures_util::{
@ -82,7 +83,7 @@ use crate::{
Capability, ChannelMode, Command, Capability, ChannelMode, Command,
Command::{ Command::{
ChannelMODE, AUTHENTICATE, CAP, INVITE, JOIN, KICK, KILL, NICK, NICKSERV, NOTICE, OPER, ChannelMODE, AUTHENTICATE, CAP, INVITE, JOIN, KICK, KILL, NICK, NICKSERV, NOTICE, OPER,
PART, PASS, PONG, PRIVMSG, QUIT, SAMODE, SANICK, TOPIC, USER, PART, PASS, PONG, PRIVMSG, QUIT, SAMODE, SANICK, TOPIC, USER, BATCH
}, },
Message, Mode, NegotiationVersion, Response, Message, Mode, NegotiationVersion, Response,
}, },
@ -497,6 +498,8 @@ struct ClientState {
sender: Sender, sender: Sender,
/// The configuration used with this connection. /// The configuration used with this connection.
config: Config, config: Config,
/// A thread-safe counter for batch IDs
batch_id: RelaxedCounter,
/// A thread-safe map of channels to the list of users in them. /// A thread-safe map of channels to the list of users in them.
chanlists: RwLock<HashMap<String, Vec<User>>>, chanlists: RwLock<HashMap<String, Vec<User>>>,
/// A thread-safe map of in-progress batch /// A thread-safe map of in-progress batch
@ -512,6 +515,7 @@ impl ClientState {
ClientState { ClientState {
sender, sender,
config, config,
batch_id: RelaxedCounter::new(0),
inflight_batches: RwLock::new(HashMap::new()), inflight_batches: RwLock::new(HashMap::new()),
chanlists: RwLock::new(HashMap::new()), chanlists: RwLock::new(HashMap::new()),
alt_nick_index: RwLock::new(0), alt_nick_index: RwLock::new(0),
@ -870,6 +874,21 @@ impl ClientState {
} }
pub_state_base!(); pub_state_base!();
/// Sends a client batch with an iterator as the body.
pub fn send_client_batch<I: Iterator<Item=Message>>(&mut self, msg_iterator: I) -> error::Result<()> {
let batch_id = format!("{}", self.batch_id.get());
self.send(BATCH(batch_id.clone(), None, None))?;
// Attach to all message that batch ID.
msg_iterator.for_each(|msg| {
self.send(msg);
});
// Close the batch.
self.send(BATCH(batch_id, None, None))?;
self.batch_id.inc();
Ok(())
}
} }
/// Thread-safe sender that can be used with the client. /// Thread-safe sender that can be used with the client.