Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use robius-directories crate to support persistence on Android #163

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ metadata.makepad-auto-version = "zqpv-Yj-K7WNVK2I8h5Okhho46Q="

[dependencies]
makepad-widgets = { git = "https://github.com/makepad/makepad", branch = "rik" }
directories = "5.0.1"
## Including this crate automatically configures all `robius-*` crates to work with Makepad.
robius-use-makepad = "0.1.0"
robius-open = "0.1.0"
## A fork of the `directories` crate that adds support for Android by using our `robius-android-env` crate.
robius-directories = { git = "https://github.com/project-robius/robius-directories", branch = "robius"}

anyhow = "1.0"
chrono = "0.4"
Expand Down
4 changes: 3 additions & 1 deletion login.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
username = " "
password = " "
homeserver = " "

### Please fill in your username and password above.
### The homeserver name/URL field is optional, but defaults to "matrix.org".
###
### Robrix does not yet have a login splash screen.
### Thus, on desktop platforms, we allow the user to
Expand All @@ -12,7 +14,7 @@ password = " "
### Instead, we enable the mobile apps to read them from a copy
### of this file that is included in the app package files.
###
### Only the two username and password fields are supported.
### Only the username, password, and homeserver fields are supported.
###
### Note that for obvious reasons you should not commit this file
### or upload it to a public repo with your actual username and password in it.
Expand Down
5 changes: 5 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ impl LiveHook for App { }

impl MatchEvent for App {
fn handle_startup(&mut self, _cx: &mut Cx) {
// Initialize the project directory here from the main UI thread
// such that background threads/tasks will be able to can access it.
let _app_data_dir = crate::app_data_dir();
log!("App::handle_startup(): app_data_dir: {:?}", _app_data_dir);

log!("App::handle_startup(): starting matrix sdk loop");
crate::sliding_sync::start_matrix_tokio().unwrap();
}
Expand Down
2 changes: 1 addition & 1 deletion src/event_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub fn text_preview_of_other_state(
HistoryVisibility::Shared => "joined users, for all of time.",
HistoryVisibility::WorldReadable | _ => "anyone for all time.",
};
Some(format!("set this room's history to be visible by {}.", visibility))
Some(format!("set this room's history to be visible by {}", visibility))
}
AnyOtherFullStateEventContent::RoomJoinRules(FullStateEventContent::Original { content, .. }) => {
Some(match content.join_rule {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{path::Path, sync::OnceLock};

use directories::ProjectDirs;
use robius_directories::ProjectDirs;

pub use makepad_widgets;
pub mod app;
Expand Down
4 changes: 2 additions & 2 deletions src/persistent_state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Handles app persistence by saving and restoring client session data to/from the filesystem.

use std::{
path::{Path, PathBuf},
path::PathBuf,
};
use anyhow::{anyhow, bail};
use makepad_widgets::log;
Expand Down Expand Up @@ -110,7 +110,7 @@ pub async fn restore_session(

// Build the client with the previous settings from the session.
let client = Client::builder()
.homeserver_url(client_session.homeserver)
.server_name_or_homeserver_url(client_session.homeserver)
.sqlite_store(client_session.db_path, Some(&client_session.passphrase))
.simplified_sliding_sync(false)
.build()
Expand Down
35 changes: 21 additions & 14 deletions src/sliding_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ use tokio::{
};
use unicode_segmentation::UnicodeSegmentation;
use std::{cmp::{max, min}, collections::{BTreeMap, BTreeSet}, ops::Range, path:: Path, sync::{Arc, Mutex, OnceLock}};
use url::Url;

use crate::{
app_data_dir, avatar_cache::AvatarUpdate, event_preview::text_preview_of_timeline_item, home::{
room_screen::TimelineUpdate,
rooms_list::{self, enqueue_rooms_list_update, RoomPreviewAvatar, RoomPreviewEntry, RoomsListUpdate},
room_screen::TimelineUpdate, rooms_list::{self, enqueue_rooms_list_update, RoomPreviewAvatar, RoomPreviewEntry, RoomsListUpdate}
}, media_cache::MediaCacheEntry, persistent_state::{self, ClientSessionPersisted}, profile::{
user_profile::{AvatarState, UserProfile},
user_profile_cache::{enqueue_user_profile_update, UserProfileUpdate},
Expand All @@ -41,11 +39,11 @@ struct Cli {

/// The homeserver to connect to.
#[clap(value_parser)]
homeserver: Option<Url>,
homeserver: Option<String>,

/// Set the proxy that should be used for the connection.
#[clap(short, long)]
proxy: Option<Url>,
proxy: Option<String>,

/// Enable verbose logging output.
#[clap(short, long, action)]
Expand Down Expand Up @@ -74,13 +72,12 @@ async fn build_client(
.collect()
};

let homeserver_url = cli.homeserver.as_ref()
.map(|h| h.as_str())
let homeserver_url = cli.homeserver.as_deref()
.unwrap_or("https://matrix-client.matrix.org/");
// .unwrap_or("https://matrix.org/");

let mut builder = Client::builder()
.homeserver_url(homeserver_url)
.server_name_or_homeserver_url(homeserver_url)
// Use a sqlite database to persist the client's encryption setup.
.sqlite_store(&db_path, Some(&passphrase))
// The matrix homeserver's sliding sync proxy doesn't support Simplified MSC3575.
Expand Down Expand Up @@ -763,10 +760,12 @@ async fn async_main_loop() -> Result<()> {
let start = std::time::Instant::now();

let cli = Cli::try_parse().ok().or_else(|| {
// Quickly try to parse the username and password fields from "login.toml".
// Quickly try to parse the username, password, and homeserver fields from "login.toml".
let login_file = std::include_str!("../login.toml");
let mut username = None;
let mut password = None;
let mut homeserver = None;
let mut homeserver_found = false;
for line in login_file.lines() {
if line.starts_with("username") {
username = line.find('=')
Expand All @@ -778,19 +777,28 @@ async fn async_main_loop() -> Result<()> {
.and_then(|i| line.get((i + 1) ..))
.map(|s| s.trim().trim_matches('"').trim().to_string());
}
if username.is_some() && password.is_some() {
if line.starts_with("homeserver") {
homeserver_found = true;
homeserver = line.find('=')
.and_then(|i| line.get((i + 1) ..))
.map(|s| s.trim().trim_matches('"').trim().to_string());
if homeserver.as_ref().is_some_and(|h| h.is_empty()) {
homeserver = None;
}
}
if username.is_some() && password.is_some() && homeserver_found {
break;
}
}
if let (Some(username), Some(password)) = (username, password) {
if username.is_empty() || password.is_empty() {
None
} else {
log!("Parsed username: {username:?} and password.");
log!("Parsed username and password from 'login.toml': {username:?}, homeserver: {homeserver:?}");
Some(Cli {
username,
password,
homeserver: None,
homeserver,
proxy: None,
verbose: false,
})
Expand All @@ -815,8 +823,7 @@ async fn async_main_loop() -> Result<()> {

let specified_username: Option<OwnedUserId> = cli.username.to_string().try_into().ok()
.or_else(|| {
let homeserver_url = cli.homeserver.as_ref()
.and_then(|u| u.host_str())
let homeserver_url = cli.homeserver.as_deref()
.unwrap_or("matrix.org");
let user_id_str = if cli.username.starts_with("@") {
format!("{}:{}", cli.username, homeserver_url)
Expand Down