Skip to main content

steam_client/services/
notifications.rs

1//! Notification handling.
2//!
3//! This module provides functionality to manage Steam notifications
4//! including marking notifications as read and requesting notification counts.
5
6use crate::{error::SteamError, SteamClient};
7
8/// Notification type identifiers.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10#[repr(u32)]
11pub enum NotificationType {
12    /// Trade offer notifications.
13    TradeOffers = 1,
14    /// Community message notifications.
15    CommunityMessages = 3,
16}
17
18impl NotificationType {
19    /// Get the notification type name.
20    pub fn name(&self) -> &'static str {
21        match self {
22            NotificationType::TradeOffers => "tradeOffers",
23            NotificationType::CommunityMessages => "communityMessages",
24        }
25    }
26}
27
28/// A Steam notification.
29#[derive(Debug, Clone)]
30pub struct Notification {
31    /// Notification ID.
32    pub id: String,
33    /// Notification type.
34    pub notification_type: u32,
35    /// Notification targets.
36    pub targets: Vec<u32>,
37    /// Body data (parsed JSON).
38    pub body: Option<serde_json::Value>,
39    /// Whether the notification has been read.
40    pub read: bool,
41    /// Timestamp when the notification was created.
42    pub timestamp: Option<u64>,
43    /// Whether the notification is hidden.
44    pub hidden: bool,
45    /// Expiry timestamp.
46    pub expiry: Option<u64>,
47    /// Timestamp when the notification was viewed.
48    pub viewed: Option<u64>,
49}
50
51impl SteamClient {
52    /// Mark specific notifications as read by their IDs.
53    ///
54    /// # Arguments
55    ///
56    /// * `notification_ids` - The IDs of notifications to mark as read
57    ///
58    /// # Example
59    ///
60    /// ```rust,ignore
61    /// client.mark_notifications_read(vec!["123".to_string(), "456".to_string()]).await?;
62    /// ```
63    pub async fn mark_notifications_read(&mut self, notification_ids: Vec<String>) -> Result<(), SteamError> {
64        if !self.is_logged_in() {
65            return Err(SteamError::NotLoggedOn);
66        }
67
68        let ids: Vec<u64> = notification_ids.iter().filter_map(|s| s.parse().ok()).collect();
69
70        let request = steam_protos::CSteamNotificationMarkNotificationsReadNotification { timestamp: None, notification_type: None, notification_ids: ids, mark_all_read: None };
71
72        // Send unified message
73        self.send_service_method("SteamNotification.MarkNotificationsRead#1", &request).await?;
74
75        Ok(())
76    }
77
78    /// Mark all notifications as read.
79    ///
80    /// # Example
81    ///
82    /// ```rust,ignore
83    /// client.mark_all_notifications_read().await?;
84    /// ```
85    pub async fn mark_all_notifications_read(&mut self) -> Result<(), SteamError> {
86        if !self.is_logged_in() {
87            return Err(SteamError::NotLoggedOn);
88        }
89
90        let request = steam_protos::CSteamNotificationMarkNotificationsReadNotification { timestamp: None, notification_type: None, notification_ids: vec![], mark_all_read: Some(true) };
91
92        // Send unified message
93        self.send_service_method("SteamNotification.MarkNotificationsRead#1", &request).await?;
94
95        Ok(())
96    }
97
98    /// Request notification counts from Steam.
99    ///
100    /// This requests item announcements, comment notifications, and offline
101    /// messages. The responses will arrive via [`poll_event`] as
102    /// notification events.
103    ///
104    /// # Example
105    ///
106    /// ```rust,ignore
107    /// client.request_notifications().await?;
108    /// // Handle NewItems, NewComments events from poll_event
109    /// ```
110    pub async fn request_notifications(&mut self) -> Result<(), SteamError> {
111        if !self.is_logged_in() {
112            return Err(SteamError::NotLoggedOn);
113        }
114
115        // Request item announcements
116        let item_request = steam_protos::CMsgClientRequestItemAnnouncements {};
117        self.send_message(steam_enums::EMsg::ClientRequestItemAnnouncements, &item_request).await?;
118
119        // Request comment notifications
120        let comment_request = steam_protos::CMsgClientRequestCommentNotifications {};
121        self.send_message(steam_enums::EMsg::ClientRequestCommentNotifications, &comment_request).await?;
122
123        // Request offline message count
124        let offline_request = steam_protos::CMsgClientRequestOfflineMessageCount {};
125        self.send_message(steam_enums::EMsg::ClientChatRequestOfflineMessageCount, &offline_request).await?;
126
127        Ok(())
128    }
129}