2022-02-03 09:52:53 -05:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2018-12-04 15:29:23 -05:00
|
|
|
#[allow(unused)]
|
2018-04-10 09:14:48 -04:00
|
|
|
use serde_json::Value;
|
2018-04-05 20:12:59 -04:00
|
|
|
|
2018-04-07 07:22:50 -04:00
|
|
|
use connection::Manager as ConnectionManager;
|
2022-02-03 09:52:53 -05:00
|
|
|
use error::{Error, Result};
|
2018-03-22 10:35:48 -04:00
|
|
|
#[cfg(feature = "rich_presence")]
|
2018-04-10 09:14:48 -04:00
|
|
|
use models::rich_presence::{
|
2022-02-03 09:52:53 -05:00
|
|
|
Activity, CloseActivityRequestArgs, SendActivityJoinInviteArgs, SetActivityArgs,
|
|
|
|
};
|
|
|
|
use models::{
|
|
|
|
commands::{Subscription, SubscriptionArgs},
|
|
|
|
message::Message,
|
|
|
|
payload::Payload,
|
|
|
|
Command, Event, OpCode,
|
2018-04-10 09:14:48 -04:00
|
|
|
};
|
2018-03-22 10:35:48 -04:00
|
|
|
|
2022-02-03 09:52:53 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
#[repr(transparent)]
|
2018-04-07 07:22:50 -04:00
|
|
|
pub struct Client {
|
2018-12-05 18:58:26 -05:00
|
|
|
connection_manager: ConnectionManager,
|
2018-03-22 10:35:48 -04:00
|
|
|
}
|
|
|
|
|
2018-04-07 07:22:50 -04:00
|
|
|
impl Client {
|
2018-12-05 18:58:26 -05:00
|
|
|
pub fn new(client_id: u64) -> Self {
|
|
|
|
let connection_manager = ConnectionManager::new(client_id);
|
|
|
|
Self { connection_manager }
|
2018-03-22 10:35:48 -04:00
|
|
|
}
|
|
|
|
|
2022-02-03 09:52:53 -05:00
|
|
|
pub fn client_id(&self) -> u64 {
|
|
|
|
self.connection_manager.client_id()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn connect(&mut self) -> Result<()> {
|
|
|
|
self.connection_manager.connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disconnect(&mut self) {
|
|
|
|
self.connection_manager.disconnect()
|
2018-03-22 10:35:48 -04:00
|
|
|
}
|
|
|
|
|
2018-12-05 18:58:26 -05:00
|
|
|
fn execute<A, E>(&mut self, cmd: Command, args: A, evt: Option<Event>) -> Result<Payload<E>>
|
2022-02-03 09:52:53 -05:00
|
|
|
where
|
|
|
|
A: Serialize + Send + Sync,
|
|
|
|
E: Serialize + DeserializeOwned + Send + Sync,
|
2018-04-05 20:12:59 -04:00
|
|
|
{
|
2022-02-03 09:52:53 -05:00
|
|
|
let message = Message::new(
|
|
|
|
OpCode::Frame,
|
|
|
|
Payload::with_nonce(cmd, Some(args), None, evt),
|
|
|
|
);
|
2018-12-05 18:58:26 -05:00
|
|
|
self.connection_manager.send(message)?;
|
|
|
|
let Message { payload, .. } = self.connection_manager.recv()?;
|
|
|
|
let response: Payload<E> = serde_json::from_str(&payload)?;
|
2018-04-05 20:12:59 -04:00
|
|
|
|
|
|
|
match response.evt {
|
|
|
|
Some(Event::Error) => Err(Error::SubscriptionFailed),
|
2022-02-03 09:52:53 -05:00
|
|
|
_ => Ok(response),
|
2018-04-05 20:12:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-22 10:35:48 -04:00
|
|
|
#[cfg(feature = "rich_presence")]
|
2022-02-03 09:52:53 -05:00
|
|
|
pub fn set_activity(&mut self, activity: Activity) -> Result<Payload<Activity>> {
|
|
|
|
self.execute(Command::SetActivity, SetActivityArgs::new(activity), None)
|
2018-04-05 20:12:59 -04:00
|
|
|
}
|
|
|
|
|
2018-04-08 15:07:19 -04:00
|
|
|
#[cfg(feature = "rich_presence")]
|
|
|
|
pub fn clear_activity(&mut self) -> Result<Payload<Activity>> {
|
|
|
|
self.execute(Command::SetActivity, SetActivityArgs::default(), None)
|
|
|
|
}
|
|
|
|
|
2018-04-10 09:14:48 -04:00
|
|
|
// NOTE: Not sure what the actual response values of
|
|
|
|
// SEND_ACTIVITY_JOIN_INVITE and CLOSE_ACTIVITY_REQUEST are,
|
|
|
|
// they are not documented.
|
|
|
|
#[cfg(feature = "rich_presence")]
|
|
|
|
pub fn send_activity_join_invite(&mut self, user_id: u64) -> Result<Payload<Value>> {
|
2022-02-03 09:52:53 -05:00
|
|
|
self.execute(
|
|
|
|
Command::SendActivityJoinInvite,
|
|
|
|
SendActivityJoinInviteArgs::new(user_id),
|
|
|
|
None,
|
|
|
|
)
|
2018-04-10 09:14:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "rich_presence")]
|
|
|
|
pub fn close_activity_request(&mut self, user_id: u64) -> Result<Payload<Value>> {
|
2022-02-03 09:52:53 -05:00
|
|
|
self.execute(
|
|
|
|
Command::CloseActivityRequest,
|
|
|
|
CloseActivityRequestArgs::new(user_id),
|
|
|
|
None,
|
|
|
|
)
|
2018-04-10 09:14:48 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 20:12:59 -04:00
|
|
|
pub fn subscribe<F>(&mut self, evt: Event, f: F) -> Result<Payload<Subscription>>
|
2022-02-03 09:52:53 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(SubscriptionArgs) -> SubscriptionArgs,
|
2018-04-05 20:12:59 -04:00
|
|
|
{
|
|
|
|
self.execute(Command::Subscribe, f(SubscriptionArgs::new()), Some(evt))
|
2018-03-22 10:35:48 -04:00
|
|
|
}
|
|
|
|
|
2018-04-06 07:29:01 -04:00
|
|
|
pub fn unsubscribe<F>(&mut self, evt: Event, f: F) -> Result<Payload<Subscription>>
|
2022-02-03 09:52:53 -05:00
|
|
|
where
|
|
|
|
F: FnOnce(SubscriptionArgs) -> SubscriptionArgs,
|
2018-04-06 07:29:01 -04:00
|
|
|
{
|
|
|
|
self.execute(Command::Unsubscribe, f(SubscriptionArgs::new()), Some(evt))
|
|
|
|
}
|
2018-03-22 10:35:48 -04:00
|
|
|
}
|