Skip to main content

steam_client/services/
friends_handle.rs

1//! `FriendsHandle` — ergonomic handle for the friends API.
2//!
3//! Forwards to the existing `&mut SteamClient` methods. Constructed via
4//! [`SteamClient::friends`]. Same pattern as the [`CSGOClient`] handle —
5//! organizes the API by topic while keeping the existing inherent methods
6//! on `SteamClient` for backwards compatibility.
7//!
8//! This is the Phase-5 handle pattern: once Phase 4 fully lands and the
9//! underlying methods take `&self`, the handle can also borrow shared instead
10//! of mutable, enabling concurrent calls on different handles.
11//!
12//! [`CSGOClient`]: crate::services::CSGOClient
13
14use steamid::SteamID;
15
16use crate::client::SteamClient;
17use crate::error::SteamError;
18use crate::services::friends::AddFriendResult;
19
20/// Handle for friends-related operations. Borrows the client mutably for now.
21pub struct FriendsHandle<'a> {
22    pub(crate) client: &'a mut SteamClient,
23}
24
25impl<'a> FriendsHandle<'a> {
26    pub(crate) fn new(client: &'a mut SteamClient) -> Self {
27        Self { client }
28    }
29
30    /// Request the friends list from Steam.
31    pub async fn request(&mut self) -> Result<(), SteamError> {
32        self.client.request_friends().await
33    }
34
35    /// Add a friend by SteamID.
36    pub async fn add(&mut self, steam_id: SteamID) -> Result<AddFriendResult, SteamError> {
37        self.client.add_friend(steam_id).await
38    }
39
40    /// Remove a friend by SteamID.
41    pub async fn remove(&mut self, steam_id: SteamID) -> Result<(), SteamError> {
42        self.client.remove_friend(steam_id).await
43    }
44
45    /// Get Steam levels for a list of SteamIDs.
46    pub async fn steam_levels(&mut self, steam_ids: Vec<SteamID>) -> Result<std::collections::HashMap<SteamID, u32>, SteamError> {
47        self.client.get_steam_levels(steam_ids).await
48    }
49
50    /// Set a nickname for a friend.
51    pub async fn set_nickname(&mut self, steam_id: SteamID, nickname: &str) -> Result<(), SteamError> {
52        self.client.set_nickname(steam_id, nickname).await
53    }
54
55    /// Get all friend nicknames.
56    pub async fn nicknames(&mut self) -> Result<std::collections::HashMap<SteamID, String>, SteamError> {
57        self.client.get_nicknames().await
58    }
59}
60
61impl SteamClient {
62    /// Friends-service handle. Provides a topic-organized view of the
63    /// friends API.
64    ///
65    /// ```rust,no_run
66    /// # use steam_client::SteamClient;
67    /// # use steamid::SteamID;
68    /// # async fn demo(client: &mut SteamClient) -> Result<(), Box<dyn std::error::Error>> {
69    /// let friend = SteamID::from_steam_id64(76561198000000000);
70    /// client.friends().add(friend).await?;
71    /// let levels = client.friends().steam_levels(&[friend]).await?;
72    /// # Ok(())
73    /// # }
74    /// ```
75    pub fn friends(&mut self) -> FriendsHandle<'_> {
76        FriendsHandle::new(self)
77    }
78}