steam-vent-chat 0.5.0

This commit is contained in:
Robin Appelman 2026-06-13 18:32:51 +02:00
commit b31706e11a
5 changed files with 994 additions and 726 deletions

View file

@ -1,6 +1,9 @@
use steam_vent::proto::steammessages_friendmessages_steamclient::{CFriendMessages_IncomingMessage_Notification, CFriendMessages_SendMessage_Request};
use steam_vent::{Connection, ConnectionTrait, NetworkError};
use steam_vent_proto::steammessages_friendmessages_steamclient::{
CFriendMessages_IncomingMessage_Notification, CFriendMessages_SendMessage_Request,
};
use steamid_ng::SteamID;
use thiserror::Error;
use tokio_stream::{Stream, StreamExt};
/// High level wrapper around steam-vent's [`Connection`] for implementing a chat client
@ -20,13 +23,17 @@ impl ChatClient {
.on_notification::<CFriendMessages_IncomingMessage_Notification>()
.filter_map(|notification| {
notification
.map(|notification| ChatEvent::try_from(notification).ok())
.map(|notification| parse_chat_event(notification).ok().flatten())
.transpose()
})
}
/// Send a chat message to a user
pub async fn send_message(&self, target: SteamID, message: String) -> Result<MessageResult, NetworkError> {
pub async fn send_message(
&self,
target: SteamID,
message: String,
) -> Result<MessageResult, NetworkError> {
let req = CFriendMessages_SendMessage_Request {
steamid: Some(target.into()),
message: Some(message),
@ -53,35 +60,39 @@ pub enum ChatEvent {
Typing(TypingEvent),
}
impl TryFrom<CFriendMessages_IncomingMessage_Notification> for ChatEvent {
type Error = ();
#[derive(Debug, Error)]
pub enum InvalidChatEvent {
#[error("Invalid steam id {0}")]
InvalidSteamId(u64),
}
fn try_from(
notification: CFriendMessages_IncomingMessage_Notification,
) -> Result<Self, Self::Error> {
let source = SteamID::try_from(notification.steamid_friend()).unwrap_or_default();
let message_type =
MessageType::try_from(notification.chat_entry_type()).unwrap_or_default();
Ok(match message_type {
MessageType::Chat if notification.local_echo() => ChatEvent::Message(MessageEvent {
fn parse_chat_event(
notification: CFriendMessages_IncomingMessage_Notification,
) -> Result<Option<ChatEvent>, InvalidChatEvent> {
let source = SteamID::try_from(notification.steamid_friend())
.map_err(|_| InvalidChatEvent::InvalidSteamId(notification.steamid_friend()))?;
let message_type = MessageType::try_from(notification.chat_entry_type()).unwrap_or_default();
Ok(match message_type {
MessageType::Chat if notification.local_echo() => Some(ChatEvent::Message(MessageEvent {
source,
server_timestamp: notification.rtime32_server_timestamp() as u64,
message: notification.message.unwrap_or_default(),
message_no_bbcode: notification.message_no_bbcode,
})),
MessageType::Chat if notification.local_echo() => {
Some(ChatEvent::EchoMessage(MessageEvent {
source,
server_timestamp: notification.rtime32_server_timestamp() as u64,
message: notification.message.unwrap_or_default(),
message_no_bbcode: notification.message_no_bbcode,
}),
MessageType::Chat if notification.local_echo() => ChatEvent::EchoMessage(MessageEvent {
source,
server_timestamp: notification.rtime32_server_timestamp() as u64,
message: notification.message.unwrap_or_default(),
message_no_bbcode: notification.message_no_bbcode,
}),
MessageType::Typing => ChatEvent::Typing(TypingEvent {
source,
server_timestamp: notification.rtime32_server_timestamp() as u64,
}),
_ => return Err(()),
})
}
}))
}
MessageType::Typing => Some(ChatEvent::Typing(TypingEvent {
source,
server_timestamp: notification.rtime32_server_timestamp() as u64,
})),
_ => None,
})
}
/// Incoming chat message
@ -107,7 +118,7 @@ pub struct TypingEvent {
}
#[repr(i32)]
#[derive(Default)]
#[derive(Default, Debug)]
enum MessageType {
#[default]
Invalid,
@ -149,4 +160,4 @@ pub struct MessageResult {
/// Chat message with bbcode added
pub modified_message: Option<String>,
pub server_timestamp: u64,
}
}