mirror of
https://codeberg.org/steam-vent/chat.git
synced 2026-11-30 20:50:52 +01:00
[Feat] Add invite/other events, bbcode flag, drop self-typing
This commit is contained in:
parent
c21ff738a8
commit
2acc87d6f0
5 changed files with 137 additions and 10 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -122,7 +122,7 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0"
|
|||
|
||||
[[package]]
|
||||
name = "aurelia-steam-vent-chat"
|
||||
version = "0.5.0"
|
||||
version = "0.6.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"steam-vent",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "aurelia-steam-vent-chat"
|
||||
version = "0.5.0"
|
||||
version = "0.6.0"
|
||||
authors = ["Robin Appelman <robin@icewind.nl>"]
|
||||
edition = "2024"
|
||||
description = "Steam chat client library (Aurelia fork of steam-vent-chat, ported to steam-vent 0.5)"
|
||||
|
|
|
|||
|
|
@ -18,6 +18,12 @@ upstream in these ways:
|
|||
3 has no default `SteamID` to fall back to.
|
||||
- Incoming messages are delivered. Upstream checks `local_echo()` on both arms,
|
||||
so it drops every message from a friend.
|
||||
- Typing notifications from your own other devices are dropped, so they no
|
||||
longer show up as the friend typing.
|
||||
- `send_message` sets `contains_bbcode`, as the official clients do.
|
||||
- Game invites arrive as `ChatEvent::GameInvite`. Every other entry type
|
||||
arrives as `ChatEvent::Other` with its raw value. `ChatEvent` is
|
||||
`#[non_exhaustive]`, so a `match` on it needs a wildcard arm.
|
||||
|
||||
## Usage
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use aurelia_steam_vent_chat::{ChatClient, ChatEvent};
|
||||
use clap::Parser;
|
||||
use std::error::Error;
|
||||
use std::io::stdin;
|
||||
|
|
@ -7,7 +8,6 @@ use steam_vent::auth::{
|
|||
FileGuardDataStore, SharedSecretAuthConfirmationHandler,
|
||||
};
|
||||
use steam_vent::{Connection, ServerList};
|
||||
use aurelia_steam_vent_chat::{ChatClient, ChatEvent};
|
||||
use steamid_ng::SteamID;
|
||||
use tokio::spawn;
|
||||
use tokio_stream::StreamExt;
|
||||
|
|
@ -64,8 +64,18 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||
while let Some(Ok(event)) = events.next().await {
|
||||
match event {
|
||||
ChatEvent::Typing(event) => println!("{} is tying...", event.source.steam64()),
|
||||
ChatEvent::Message(event) => println!("{}: {}", event.source.steam64(), event.message_no_bbcode.unwrap_or(event.message)),
|
||||
ChatEvent::EchoMessage(event) => println!("me: {}", event.message_no_bbcode.unwrap_or(event.message)),
|
||||
ChatEvent::Message(event) => println!(
|
||||
"{}: {}",
|
||||
event.source.steam64(),
|
||||
event.message_no_bbcode.unwrap_or(event.message)
|
||||
),
|
||||
ChatEvent::EchoMessage(event) => {
|
||||
println!("me: {}", event.message_no_bbcode.unwrap_or(event.message))
|
||||
}
|
||||
ChatEvent::GameInvite(event) => {
|
||||
println!("{} sent a game invite", event.source.steam64())
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
119
src/lib.rs
119
src/lib.rs
|
|
@ -1,7 +1,7 @@
|
|||
use steam_vent::{Connection, ConnectionTrait, NetworkError};
|
||||
use steam_vent_proto::steammessages_friendmessages_steamclient::{
|
||||
CFriendMessages_IncomingMessage_Notification, CFriendMessages_SendMessage_Request,
|
||||
};
|
||||
use steam_vent::{Connection, ConnectionTrait, NetworkError};
|
||||
use steamid_ng::SteamID;
|
||||
use tokio_stream::{Stream, StreamExt};
|
||||
|
||||
|
|
@ -27,12 +27,19 @@ impl ChatClient {
|
|||
})
|
||||
}
|
||||
|
||||
/// Send a chat message to a user
|
||||
pub async fn send_message(&self, target: SteamID, message: String) -> Result<MessageResult, NetworkError> {
|
||||
/// Send a chat message to a user.
|
||||
///
|
||||
/// Flags text as BBCode, like Steam.
|
||||
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),
|
||||
chat_entry_type: Some(MessageType::Chat as i32),
|
||||
contains_bbcode: Some(true),
|
||||
..CFriendMessages_SendMessage_Request::default()
|
||||
};
|
||||
let result = self.connection.service_method(req).await?;
|
||||
|
|
@ -46,6 +53,7 @@ impl ChatClient {
|
|||
|
||||
/// Incoming chat event
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum ChatEvent {
|
||||
/// Another user sent a message
|
||||
Message(MessageEvent),
|
||||
|
|
@ -53,6 +61,10 @@ pub enum ChatEvent {
|
|||
EchoMessage(MessageEvent),
|
||||
/// Another user is typing
|
||||
Typing(TypingEvent),
|
||||
/// Another user sent a game invite
|
||||
GameInvite(MessageEvent),
|
||||
/// Any other entry type
|
||||
Other(OtherEvent),
|
||||
}
|
||||
|
||||
impl TryFrom<CFriendMessages_IncomingMessage_Notification> for ChatEvent {
|
||||
|
|
@ -83,11 +95,27 @@ impl TryFrom<CFriendMessages_IncomingMessage_Notification> for ChatEvent {
|
|||
message: notification.message.unwrap_or_default(),
|
||||
message_no_bbcode: notification.message_no_bbcode,
|
||||
}),
|
||||
// Own typing elsewhere is noise.
|
||||
MessageType::Typing if notification.local_echo() => return Err(()),
|
||||
MessageType::Typing => ChatEvent::Typing(TypingEvent {
|
||||
source,
|
||||
server_timestamp: notification.rtime32_server_timestamp() as u64,
|
||||
}),
|
||||
_ => return Err(()),
|
||||
MessageType::GameInvite if !notification.local_echo() => {
|
||||
ChatEvent::GameInvite(MessageEvent {
|
||||
source,
|
||||
server_timestamp: notification.rtime32_server_timestamp() as u64,
|
||||
message: notification.message.unwrap_or_default(),
|
||||
message_no_bbcode: notification.message_no_bbcode,
|
||||
})
|
||||
}
|
||||
_ => ChatEvent::Other(OtherEvent {
|
||||
source,
|
||||
entry_type: notification.chat_entry_type(),
|
||||
local_echo: notification.local_echo(),
|
||||
server_timestamp: notification.rtime32_server_timestamp() as u64,
|
||||
message: notification.message,
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -105,6 +133,21 @@ pub struct MessageEvent {
|
|||
pub server_timestamp: u64,
|
||||
}
|
||||
|
||||
/// Entry type without its own variant
|
||||
#[derive(Debug)]
|
||||
pub struct OtherEvent {
|
||||
/// SteamID of the conversation partner
|
||||
pub source: SteamID,
|
||||
/// Raw `EChatEntryType` value
|
||||
pub entry_type: i32,
|
||||
/// Sent by the local user elsewhere
|
||||
pub local_echo: bool,
|
||||
/// Service side time of the event
|
||||
pub server_timestamp: u64,
|
||||
/// Raw message contents, if any
|
||||
pub message: Option<String>,
|
||||
}
|
||||
|
||||
/// Incoming typing event
|
||||
#[derive(Debug)]
|
||||
pub struct TypingEvent {
|
||||
|
|
@ -158,3 +201,71 @@ pub struct MessageResult {
|
|||
pub modified_message: Option<String>,
|
||||
pub server_timestamp: u64,
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const FRIEND: u64 = 76_561_198_000_000_001;
|
||||
|
||||
fn note(entry_type: i32, local_echo: bool) -> CFriendMessages_IncomingMessage_Notification {
|
||||
CFriendMessages_IncomingMessage_Notification {
|
||||
steamid_friend: Some(FRIEND),
|
||||
chat_entry_type: Some(entry_type),
|
||||
local_echo: Some(local_echo),
|
||||
message: Some("hi".into()),
|
||||
rtime32_server_timestamp: Some(42),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn friend_message_is_message() {
|
||||
assert!(matches!(
|
||||
ChatEvent::try_from(note(1, false)),
|
||||
Ok(ChatEvent::Message(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn own_message_is_echo() {
|
||||
assert!(matches!(
|
||||
ChatEvent::try_from(note(1, true)),
|
||||
Ok(ChatEvent::EchoMessage(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn friend_typing_is_typing() {
|
||||
assert!(matches!(
|
||||
ChatEvent::try_from(note(2, false)),
|
||||
Ok(ChatEvent::Typing(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn own_typing_is_dropped() {
|
||||
assert!(ChatEvent::try_from(note(2, true)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn game_invite_is_delivered() {
|
||||
match ChatEvent::try_from(note(3, false)) {
|
||||
Ok(ChatEvent::GameInvite(e)) => {
|
||||
assert_eq!(u64::from(e.source), FRIEND);
|
||||
assert_eq!(e.message, "hi");
|
||||
}
|
||||
other => panic!("expected GameInvite, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_type_is_other_with_raw_value() {
|
||||
match ChatEvent::try_from(note(99, false)) {
|
||||
Ok(ChatEvent::Other(e)) => {
|
||||
assert_eq!(e.entry_type, 99);
|
||||
assert_eq!(e.server_timestamp, 42);
|
||||
}
|
||||
other => panic!("expected Other, got {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue