Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,32 @@ pub enum BotBuilderError {
Other(#[from] anyhow::Error),
}

/// `message` is `Arc` so cloning the context across spawned tasks only bumps a
/// refcount, matching the pattern used by serenity's `Context` and matrix-sdk's
/// `Room`/`Client`.
#[derive(Clone)]
pub struct MessageContext {
pub message: Box<wa::Message>,
pub message: Arc<wa::Message>,
pub info: MessageInfo,
pub client: Arc<Client>,
}

impl MessageContext {
pub fn from_parts(message: &wa::Message, info: &MessageInfo, client: Arc<Client>) -> Self {
Self::from_arc(Arc::new(message.clone()), info, client)
}

pub fn from_arc(message: Arc<wa::Message>, info: &MessageInfo, client: Arc<Client>) -> Self {
Self {
message: Box::new(message.clone()),
message,
info: info.clone(),
client,
}
}

pub fn from_event(event: &Event, client: Arc<Client>) -> Option<Self> {
let (msg, info) = event.as_message()?;
Some(Self::from_parts(msg, info, client))
Some(Self::from_arc(Arc::clone(msg), info, client))
}

pub async fn send_message(
Expand Down Expand Up @@ -1070,4 +1078,28 @@ mod tests {

assert!(!bot.client().skip_history_sync_enabled());
}

#[tokio::test]
async fn from_arc_does_not_deep_clone() {
let backend = create_test_sqlite_backend().await;
let bot = Bot::builder()
.with_backend(backend)
.with_transport_factory(TokioWebSocketTransportFactory::new())
.with_http_client(MockHttpClient)
.with_runtime(TokioRuntime)
.build()
.await
.expect("Failed to build bot");

let original = Arc::new(wa::Message {
conversation: Some("ping".to_string()),
..Default::default()
});
let original_ptr = Arc::as_ptr(&original);

let ctx =
MessageContext::from_arc(Arc::clone(&original), &MessageInfo::default(), bot.client());

assert!(std::ptr::eq(Arc::as_ptr(&ctx.message), original_ptr));
}
Comment thread
jlucaso1 marked this conversation as resolved.
}
2 changes: 1 addition & 1 deletion src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl Client {

self.core
.event_bus
.dispatch(Event::Message(Box::new(msg), info));
.dispatch(Event::Message(Arc::new(msg), info));
}

/// Handles a newsletter plaintext message.
Expand Down
2 changes: 1 addition & 1 deletion src/pdo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ impl Client {
self.core
.event_bus
.dispatch(wacore::types::events::Event::Message(
Box::new(message),
Arc::new(message),
message_info,
));
}
Expand Down
4 changes: 2 additions & 2 deletions wacore/src/types/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ pub enum Event {
QrScannedWithoutMultidevice(QrScannedWithoutMultidevice),
ClientOutdated(ClientOutdated),

Message(Box<wa::Message>, Arc<MessageInfo>),
Message(Arc<wa::Message>, Arc<MessageInfo>),
Receipt(Receipt),
UndecryptableMessage(UndecryptableMessage),
#[serde(skip)]
Expand Down Expand Up @@ -466,7 +466,7 @@ pub struct MexNotification {
}

impl Event {
pub fn as_message(&self) -> Option<(&wa::Message, &MessageInfo)> {
pub fn as_message(&self) -> Option<(&Arc<wa::Message>, &MessageInfo)> {
if let Event::Message(msg, info) = self {
Some((msg, &**info))
} else {
Expand Down
Loading