Skip to content

Commit

Permalink
Fix formatting of phone number and update user name on start.
Browse files Browse the repository at this point in the history
  • Loading branch information
boxdot committed Jul 17, 2021
1 parent d23d64b commit c9e73d1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ uuid = "0.8.2"
whoami = "1.1.2"

presage = { git = "https://github.com/whisperfish/presage.git", rev = "20f8be16" }
phonenumber = "0.3.1"

# [patch."https://github.com/whisperfish/presage.git"]
# presage = { path = "../presage" }
Expand Down
10 changes: 8 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use anyhow::{anyhow, Context as _};
use crossterm::event::{KeyCode, KeyEvent, MouseEvent};
use log::error;
use notify_rust::Notification;
use phonenumber::{Mode, PhoneNumber};
use presage::prelude::{
content::{ContentBody, DataMessage, Metadata, SyncMessage},
proto::{
Expand Down Expand Up @@ -254,6 +255,10 @@ impl App {
data = AppData::load(&load_data_path).unwrap_or_default();
}

// ensure that our name is up to date
data.names
.insert(signal_manager.uuid(), config.user.name.clone());

// select the first channel if none is selected
if data.channels.state.selected().is_none() && !data.channels.items.is_empty() {
data.channels.state.select(Some(0));
Expand Down Expand Up @@ -912,14 +917,15 @@ impl App {
&mut self,
uuid: Uuid,
profile_key: Vec<u8>,
fallback_name: impl std::fmt::Display,
phone_number: PhoneNumber,
) -> &str {
if self
.try_ensure_user_is_known(uuid, profile_key)
.await
.is_none()
{
self.data.names.insert(uuid, fallback_name.to_string());
let phone_number_name = phone_number.format().mode(Mode::E164).to_string();
self.data.names.insert(uuid, phone_number_name);
}
self.data.names.get(&uuid).unwrap()
}
Expand Down
12 changes: 6 additions & 6 deletions src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ pub async fn ensure_linked_device(relink: bool) -> anyhow::Result<(Manager, Conf
// get profile
let phone_number = manager
.phone_number()
.expect("no phone number after device was linked");
.expect("no phone number after device was linked")
.format()
.mode(phonenumber::Mode::E164)
.to_string();
let profile = manager
.retrieve_profile()
.await
Expand All @@ -84,15 +87,12 @@ pub async fn ensure_linked_device(relink: bool) -> anyhow::Result<(Manager, Conf

let config = if let Some(config) = config {
// check that config fits the profile
if config.user.phone_number != phone_number.to_string() {
if config.user.phone_number != phone_number {
bail!("Wrong phone number in the config. Please adjust it.");
}
config
} else {
let user = config::User {
name,
phone_number: phone_number.to_string(),
};
let user = config::User { name, phone_number };
let config = config::Config::with_user(user);
config.save_new().context("failed to init config file")?;
config
Expand Down
16 changes: 15 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,19 @@ pub fn utc_now_timestamp_msec() -> u64 {

pub fn is_phone_number(s: impl AsRef<str>) -> bool {
use std::str::FromStr;
PhoneNumber::from_str(s.as_ref()).is_ok()
// Note: previously we formatted phone numbers sometimes incorrectly (not always as E164). So,
// some users might still have them stored with spaces and dashes. So, we strip them here, even
// the formatting now is correct.
let stripped = s.as_ref().replace(&[' ', '-'][..], "");
PhoneNumber::from_str(&stripped).is_ok()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_phone_number() {
assert!(is_phone_number("+1 000-000-0000"));
}
}

0 comments on commit c9e73d1

Please sign in to comment.