Skip to content

Commit

Permalink
feat(model/client): make api credentials runtime configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
craigcabrey committed Jun 14, 2024
1 parent 387d934 commit 6e756ed
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
38 changes: 38 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod strings;
mod types;
mod utils;

use std::borrow::Cow;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::OnceLock;
Expand Down Expand Up @@ -77,6 +78,21 @@ fn main() -> glib::ExitCode {
application_opts.test_dc = true;
}

let maybe_client_id = dict.lookup::<String>("client-id").unwrap();
let maybe_client_secret = dict.lookup::<String>("client-secret").unwrap();

match (maybe_client_id, maybe_client_secret) {
(Some(client_id), Some(client_secret)) => {
application_opts.client_id = client_id.parse::<i32>().unwrap();
application_opts.client_secret = Cow::Owned(client_secret);
},
(None, None) => (),
_ => {
log::error!("Both client-id and client-secret must be set together");
return -1
},
};

APPLICATION_OPTS.set(application_opts).unwrap();

-1
Expand All @@ -103,12 +119,16 @@ fn main() -> glib::ExitCode {
pub(crate) struct ApplicationOptions {
pub(crate) data_dir: PathBuf,
pub(crate) test_dc: bool,
pub(crate) client_id: i32,
pub(crate) client_secret: Cow<'static, str>,
}
impl Default for ApplicationOptions {
fn default() -> Self {
Self {
data_dir: PathBuf::from(glib::user_data_dir().to_str().unwrap()).join("paper-plane"),
test_dc: Default::default(),
client_id: config::TG_API_ID,
client_secret: Cow::Owned(config::TG_API_HASH.to_string()),
}
}
}
Expand All @@ -132,6 +152,24 @@ fn setup_cli<A: IsA<gio::Application>>(app: A) -> A {
Some("error|warn|info|debug|trace"),
);

app.add_main_option(
"client-id",
b'c'.into(),
glib::OptionFlags::NONE,
glib::OptionArg::String,
&gettext("Override the builtin client id"),
None,
);

app.add_main_option(
"client-secret",
b's'.into(),
glib::OptionFlags::NONE,
glib::OptionArg::String,
&gettext("Override the builtin client secret"),
None,
);

app.add_main_option(
"test-dc",
b't'.into(),
Expand Down
7 changes: 5 additions & 2 deletions src/model/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;

use crate::APPLICATION_OPTS;
use crate::config;
use crate::model;
use crate::types::ClientId;
Expand Down Expand Up @@ -169,6 +170,8 @@ impl Client {
.expect("Data directory path is not a valid unicode string")
.into();

let application_opts = APPLICATION_OPTS.get().unwrap();

tdlib::functions::set_tdlib_parameters(
database_info.use_test_dc,
database_directory,
Expand All @@ -178,8 +181,8 @@ impl Client {
true,
true,
true,
config::TG_API_ID,
config::TG_API_HASH.into(),
application_opts.client_id,
application_opts.client_secret.to_string(),
system_language_code,
"Desktop".into(),
String::new(),
Expand Down

0 comments on commit 6e756ed

Please sign in to comment.