Skip to content

Commit 3e72afd

Browse files
committed
Implement test server setup flow
1 parent 710c226 commit 3e72afd

File tree

2 files changed

+108
-17
lines changed

2 files changed

+108
-17
lines changed

app/src/controllers/application.rs

+75-17
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use crate::ui;
2222
use std::cell::RefCell;
2323
use std::rc::Rc;
2424

25+
//@TODO find a better place for this
26+
const TEST_SERVER_EMAIL: &str = "[email protected]";
27+
2528
#[derive(Debug)]
2629
pub enum ApplicationMessage {
2730
Setup {},
@@ -90,6 +93,23 @@ impl ApplicationMessage {
9093
gmail_refresh_token: response_token.refresh_token,
9194
}
9295
}
96+
97+
pub fn save_identity_message_for_test_server() -> ApplicationMessage {
98+
ApplicationMessage::SaveIdentity {
99+
email_address: TEST_SERVER_EMAIL.to_string(),
100+
full_name: "Full name".to_string(),
101+
account_name: "Test".to_string(),
102+
identity_type: models::IdentityType::Imap,
103+
expires_at: DateTime::<Utc>::MAX_UTC,
104+
imap_server_hostname: "127.0.0.1".to_string(),
105+
imap_server_port: 3993,
106+
imap_password: TEST_SERVER_EMAIL.to_string(),
107+
imap_use_tls: true,
108+
imap_use_starttls: false,
109+
gmail_access_token: String::new(),
110+
gmail_refresh_token: String::new(),
111+
}
112+
}
93113
}
94114

95115
fn send_notification(notifications_email_count: &Rc<RefCell<i32>>, new_messages: Vec<NewMessage>, application_obj: &Application) {
@@ -132,6 +152,7 @@ mod imp {
132152
pub application_message_sender: RefCell<Option<glib::Sender<ApplicationMessage>>>,
133153
pub store: RefCell<Option<Rc<services::Store>>>,
134154
pub notifications_email_count: Rc<RefCell<i32>>,
155+
pub test_server_enabled: Rc<RefCell<bool>>,
135156
}
136157

137158
#[glib::object_subclass]
@@ -147,29 +168,27 @@ mod imp {
147168
fn activate(&self) {
148169
debug!("Application activate");
149170

150-
{
171+
let mut application_message;
172+
if self.should_set_up_test_server() {
173+
debug!("Test server setup not found. Configuring");
174+
175+
application_message = ApplicationMessage::save_identity_message_for_test_server();
176+
} else {
151177
let store_borrow = self.store.borrow();
152178
let store = store_borrow.as_ref().expect("Unable to access store");
153-
match store.initialize_database() {
154-
Ok(_) => {
155-
let application_message = match store.is_account_setup_needed() {
179+
180+
application_message = match store.is_account_setup_needed() {
156181
true => ApplicationMessage::Setup {},
157182
false => ApplicationMessage::LoadIdentities { initialize: false },
158-
};
183+
}
184+
}
159185

160186
self.application_message_sender
161187
.borrow()
162188
.as_ref()
163189
.expect("Unable to access application message sender")
164190
.send(application_message)
165191
.expect("Unable to send application message");
166-
}
167-
Err(e) => {
168-
//@TODO show an error dialog
169-
error!("Error encountered when initializing the database: {}", &e);
170-
}
171-
}
172-
}
173192

174193
self.parent_activate();
175194

@@ -184,16 +203,27 @@ mod imp {
184203
fn startup(&self) {
185204
debug!("Application startup");
186205
self.parent_startup();
187-
let app = self.obj();
188206

189-
// Set icons for shell
190207
gtk::Window::set_default_icon_name(APP_ID);
191208

192-
app.setup_css();
193-
app.setup_gactions();
194-
app.setup_accels();
209+
let obj = self.obj();
210+
obj.setup_css();
211+
obj.setup_gactions();
212+
obj.setup_accels();
195213

196214
self.run();
215+
self.setup_database();
216+
}
217+
218+
fn command_line(&self, command_line: &gio::ApplicationCommandLine) -> glib::ExitCode {
219+
if command_line.options_dict().contains(&"with-test-server") {
220+
debug!("Enabling test server setup");
221+
self.enable_test_server_setup();
222+
}
223+
224+
self.obj().activate();
225+
226+
glib::ExitCode::SUCCESS
197227
}
198228
}
199229

@@ -569,6 +599,34 @@ mod imp {
569599
});
570600
application_obj.add_action(&action_show_conversation_for_email_id);
571601
}
602+
603+
fn setup_database(&self) {
604+
let store_borrow = self.store.borrow();
605+
let store = store_borrow.as_ref().expect("Unable to access store");
606+
607+
store.initialize_database();
608+
}
609+
610+
fn enable_test_server_setup(&self) {
611+
self.test_server_enabled.replace(true);
612+
}
613+
614+
fn should_set_up_test_server(&self) -> bool {
615+
*self.test_server_enabled.borrow() && !self.is_test_server_set_up_already()
616+
}
617+
618+
fn is_test_server_set_up_already(&self) -> bool {
619+
let store_borrow = self.store.borrow();
620+
let store = store_borrow.as_ref().expect("Unable to access store");
621+
622+
store
623+
.get_bare_identities()
624+
.expect("Unable to fetch identities")
625+
.into_iter()
626+
.filter(|identity| &identity.email_address == TEST_SERVER_EMAIL)
627+
.count()
628+
!= 0
629+
}
572630
}
573631
}
574632

docs/application_states.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Application states
2+
3+
The following plantuml diagram shows the different application states and the
4+
different application messages that can be delivered and which state
5+
transitions they can lead to.
6+
7+
As can be seen, application states are not very well defined: once the
8+
application reaches the "Running" state, the state remains largely the same.
9+
But the diagram clarifies well the different startup flows.
10+
11+
```plantuml
12+
@startuml
13+
state "Google Authentication" as google_authentication
14+
state "Welcome Dialog" as welcome_dialog
15+
state "Running" as running
16+
state "Identity saved" as identity_saved
17+
state "Fetching conversation content" as fetching_conversation_contect
18+
19+
[*] --> running : LoadIdentities(initialize: false)
20+
[*] --> identity_saved : SaveIdentity(TestServerAccount)
21+
[*] --> welcome_dialog : Setup
22+
welcome_dialog --> google_authentication : OpenGoogleAuthentication
23+
google_authentication -> identity_saved : SaveIdentity(GmailAccount)
24+
identity_saved -> running : LoadIdentities(initialize: true)
25+
running -> running : ShowFolder(folder)
26+
running -> running : ShowConversation(conversation)
27+
running -> running : NewMessages(...)
28+
running -> running : ShowConversationContainingEmail(...)
29+
running -> fetching_conversation_contect : ShowConversation(...)
30+
fetching_conversation_contect -> running : ConversationContentLoadFinished(conversation)
31+
running --> [*]
32+
@enduml
33+
```

0 commit comments

Comments
 (0)