Skip to content

Commit

Permalink
Merge pull request #2493 from bonomat/feat/store-os-for-user
Browse files Browse the repository at this point in the history
feat: store os in db
  • Loading branch information
bonomat authored Apr 30, 2024
2 parents bbb5bd2 + 31fb86e commit 0d8157b
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE users
DROP COLUMN os;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE users
ADD COLUMN os TEXT;
8 changes: 8 additions & 0 deletions coordinator/src/db/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct User {
pub referral_code: String,
/// The referral code referred by
pub used_referral_code: Option<String>,
pub os: Option<String>,
}

#[derive(Insertable, Debug, Clone, Serialize, Deserialize)]
Expand All @@ -49,6 +50,7 @@ pub struct NewUser {
pub version: Option<String>,
/// This user was referred by this code
pub used_referral_code: Option<String>,
pub os: Option<String>,
}

impl From<RegisterParams> for User {
Expand All @@ -67,6 +69,7 @@ impl From<RegisterParams> for User {
// TODO: this is not ideal, we shouldn't need to do this as it's autogenerated in the db
// However, this is needed here because we convert from `RegisteredUser` to `User`. We
// should not do this anymore.
os: value.os,
referral_code,
used_referral_code: value.referral_code,
}
Expand Down Expand Up @@ -102,6 +105,7 @@ pub fn upsert_user(
contact: Option<String>,
nickname: Option<String>,
version: Option<String>,
os: Option<String>,
used_referral_code: Option<String>,
) -> QueryResult<User> {
// If no name or contact has been provided we default to empty string
Expand All @@ -119,6 +123,7 @@ pub fn upsert_user(
fcm_token: "".to_owned(),
last_login: timestamp,
version: version.clone(),
os: os.clone(),
used_referral_code: used_referral_code.clone(),
})
.on_conflict(schema::users::pubkey)
Expand Down Expand Up @@ -182,6 +187,7 @@ pub fn login_user(
trader_id: PublicKey,
token: String,
version: Option<String>,
os: Option<String>,
) -> Result<()> {
tracing::debug!(%trader_id, token, "Updating token for client.");
let last_login = OffsetDateTime::now_utc();
Expand All @@ -194,6 +200,7 @@ pub fn login_user(
timestamp: OffsetDateTime::now_utc(),
fcm_token: token.clone(),
version: version.clone(),
os: os.clone(),
last_login,
// TODO: this breaks the used referral code
used_referral_code: None,
Expand All @@ -204,6 +211,7 @@ pub fn login_user(
users::fcm_token.eq(&token),
users::last_login.eq(last_login),
users::version.eq(version),
users::os.eq(os),
))
.execute(conn)?;

Expand Down
11 changes: 10 additions & 1 deletion coordinator/src/orderbook/tests/registration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async fn registered_user_is_stored_in_db() {
let nickname = Some("dummy_user".to_string());
let fcm_token = "just_a_token".to_string();
let version = Some("1.9.0".to_string());
let os = Some("linux".to_string());

let user = user::upsert_user(
&mut conn,
Expand All @@ -31,10 +32,18 @@ async fn registered_user_is_stored_in_db() {
nickname.clone(),
version.clone(),
Some("code1".to_string()),
os.clone(),
)
.unwrap();
assert!(user.id.is_some(), "Id should be filled in by diesel");
user::login_user(&mut conn, dummy_pubkey, fcm_token.clone(), version.clone()).unwrap();
user::login_user(
&mut conn,
dummy_pubkey,
fcm_token.clone(),
version.clone(),
os,
)
.unwrap();

let users = user::all(&mut conn).unwrap();
assert_eq!(users.len(), 1);
Expand Down
5 changes: 4 additions & 1 deletion coordinator/src/orderbook/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub async fn websocket_connection(stream: WebSocket, state: Arc<AppState>) {
Ok(OrderbookRequest::Authenticate {
fcm_token,
version,
os,
signature,
}) => {
let msg = create_sign_message(AUTH_SIGN_MESSAGE.to_vec());
Expand Down Expand Up @@ -249,7 +250,9 @@ pub async fn websocket_connection(stream: WebSocket, state: Arc<AppState>) {
}

let token = fcm_token.unwrap_or("unavailable".to_string());
if let Err(e) = user::login_user(&mut conn, trader_id, token, version) {
if let Err(e) =
user::login_user(&mut conn, trader_id, token, version, os)
{
tracing::error!(%trader_id, "Failed to update logged in user. Error: {e:#}")
}

Expand Down
1 change: 1 addition & 0 deletions coordinator/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ pub async fn post_register(
register_params.nickname.clone(),
register_params.version.clone(),
register_params.referral_code,
register_params.os,
)
.map_err(|e| AppError::InternalServerError(format!("Could not upsert user: {e:#}")))?;

Expand Down
1 change: 1 addition & 0 deletions coordinator/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ diesel::table! {
version -> Nullable<Text>,
referral_code -> Text,
used_referral_code -> Nullable<Text>,
os -> Nullable<Text>,
}
}

Expand Down
11 changes: 8 additions & 3 deletions crates/orderbook-client/examples/authenticated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@ async fn main() -> Result<Never> {
};

loop {
let (_, mut stream) =
orderbook_client::subscribe_with_authentication(url.clone(), &authenticate, None, None)
.await?;
let (_, mut stream) = orderbook_client::subscribe_with_authentication(
url.clone(),
&authenticate,
None,
None,
None,
)
.await?;

loop {
match stream.try_next().await {
Expand Down
7 changes: 5 additions & 2 deletions crates/orderbook-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub async fn subscribe(
SplitSink<WebSocketStream, tungstenite::Message>,
impl Stream<Item = Result<String, anyhow::Error>> + Unpin,
)> {
subscribe_impl(None, url, None, None).await
subscribe_impl(None, url, None, None, None).await
}

/// Connects to the orderbook WebSocket API with authentication.
Expand All @@ -34,12 +34,13 @@ pub async fn subscribe_with_authentication(
authenticate: impl Fn(Message) -> Signature,
fcm_token: Option<String>,
version: Option<String>,
os: Option<String>,
) -> Result<(
SplitSink<WebSocketStream, tungstenite::Message>,
impl Stream<Item = Result<String, anyhow::Error>> + Unpin,
)> {
let signature = create_auth_message_signature(authenticate);
subscribe_impl(Some(signature), url, fcm_token, version).await
subscribe_impl(Some(signature), url, fcm_token, version, os).await
}

pub fn create_auth_message_signature(authenticate: impl Fn(Message) -> Signature) -> Signature {
Expand All @@ -52,6 +53,7 @@ async fn subscribe_impl(
url: String,
fcm_token: Option<String>,
version: Option<String>,
os: Option<String>,
) -> Result<(
SplitSink<WebSocketStream, tungstenite::Message>,
impl Stream<Item = Result<String>> + Unpin,
Expand All @@ -71,6 +73,7 @@ async fn subscribe_impl(
fcm_token,
version,
signature,
os,
},
)?)
.await;
Expand Down
1 change: 1 addition & 0 deletions crates/xxi-node/src/commons/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub enum OrderbookRequest {
Authenticate {
fcm_token: Option<String>,
version: Option<String>,
os: Option<String>,
signature: Signature,
},
InsertOrder(NewLimitOrder),
Expand Down
1 change: 1 addition & 0 deletions crates/xxi-node/src/commons/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct RegisterParams {
pub contact: Option<String>,
pub nickname: Option<String>,
pub version: Option<String>,
pub os: Option<String>,
/// Entered referral code, i.e. this user was revered by using this referral code
pub referral_code: Option<String>,
}
Expand Down
2 changes: 2 additions & 0 deletions mobile/native/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,13 @@ pub fn run_in_flutter(seed_dir: String, fcm_token: String) -> Result<()> {
});

let version = env!("CARGO_PKG_VERSION").to_string();
let os = std::env::consts::OS.to_string();
let runtime = crate::state::get_or_create_tokio_runtime()?;
runtime.block_on(async {
tx_websocket.send(OrderbookRequest::Authenticate {
fcm_token: Some(fcm_token),
version: Some(version),
os: Some(os),
signature,
})
})?;
Expand Down
3 changes: 2 additions & 1 deletion mobile/native/src/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn subscribe(
let url = url.clone();
let fcm_token = fcm_token.clone();
let version = env!("CARGO_PKG_VERSION").to_string();
match orderbook_client::subscribe_with_authentication(url, authenticate, fcm_token, Some(version))
let os = std::env::consts::OS.to_string();
match orderbook_client::subscribe_with_authentication(url, authenticate, fcm_token, Some(version), Some(os))
.await
{
Ok((mut sink, mut stream)) => {
Expand Down
1 change: 1 addition & 0 deletions mobile/native/src/trade/users/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub async fn register_beta(
contact: Some(contact),
nickname: Some(name),
version: Some(version.clone()),
os: Some(std::env::consts::OS.to_string()),
referral_code,
};

Expand Down

0 comments on commit 0d8157b

Please sign in to comment.