~nicohman/signal-rs

e0c56196e0f457d964f9e1843c0713aaeff8875b — nicohman 7 months ago 5fae6c5
Name resolution work
7 files changed, 89 insertions(+), 36 deletions(-)

M Cargo.lock
M Cargo.toml
M qml/ChatList.qml
M qml/MessageUI.qml
M src/main.rs
M src/scli.rs
M src/signal.rs
M Cargo.lock => Cargo.lock +19 -21
@@ 753,9 753,9 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"

[[package]]
name = "futures"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "309f13e3f4be6d5917178c84db67c0b9a09177ac16d4f9a7313a767a68adaa77"
checksum = "7f55667319111d593ba876406af7c409c0ebb44dc4be6132a783ccf163ea14c1"
dependencies = [
 "futures-channel",
 "futures-core",


@@ 768,9 768,9 @@ dependencies = [

[[package]]
name = "futures-channel"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a3b03bd32f6ec7885edeb99acd1e47e20e34fd4dfd3c6deed6fcac8a9d28f6a"
checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939"
dependencies = [
 "futures-core",
 "futures-sink",


@@ 778,15 778,15 @@ dependencies = [

[[package]]
name = "futures-core"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed8aeae2b6ab243ebabe6f54cd4cf53054d98883d5d326128af7d57a9ca5cd3d"
checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94"

[[package]]
name = "futures-executor"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f7836b36b7533d16fd5937311d98ba8965ab81030de8b0024c299dd5d51fb9b"
checksum = "891a4b7b96d84d5940084b2a37632dd65deeae662c114ceaa2c879629c9c0ad1"
dependencies = [
 "futures-core",
 "futures-task",


@@ 796,15 796,15 @@ dependencies = [

[[package]]
name = "futures-io"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d41234e71d5e8ca73d01563974ef6f50e516d71e18f1a2f1184742e31f5d469f"
checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59"

[[package]]
name = "futures-macro"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3520e0eb4e704e88d771b92d51273ee212997f0d8282f17f5d8ff1cb39104e42"
checksum = "ea405816a5139fb39af82c2beb921d52143f556038378d6db21183a5c37fbfb7"
dependencies = [
 "proc-macro-hack",
 "proc-macro2",


@@ 814,24 814,21 @@ dependencies = [

[[package]]
name = "futures-sink"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c72d188479368953c6c8c7140e40d7a4401674ab3b98a41e60e515d6cbdbe5de"
checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3"

[[package]]
name = "futures-task"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "08944cea9021170d383287169859c0ca8147d9ec285978393109954448f33cc7"
dependencies = [
 "once_cell 1.5.2",
]
checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80"

[[package]]
name = "futures-util"
version = "0.3.10"
version = "0.3.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3dd206efbe2ca683b2ce138ccdf61e1b0a63f5816dcedc9d8654c500ba0cea6"
checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1"
dependencies = [
 "futures-channel",
 "futures-core",


@@ 2365,6 2362,7 @@ dependencies = [
 "dbus",
 "dbus-tokio",
 "enum_variant_type",
 "futures",
 "futures-util",
 "gettext-rs",
 "glib",

M Cargo.toml => Cargo.toml +1 -0
@@ 33,6 33,7 @@ dbus = "0.9.0"
home = "0.5.3"	
pallet = "0.6.1"
anyhow  = "1.0"
futures = "0.3.13"
tempfile = "3.1.0"
base64 = "0.13.0"
cpp_macros = "0.4"

M qml/ChatList.qml => qml/ChatList.qml +1 -1
@@ 37,7 37,7 @@ Page {
                }
                Column {
                    Label {
                        text: name
                        text: signal.resolve_name(model.tel)
                        color: theme.text
                    }
                    Label {

M qml/MessageUI.qml => qml/MessageUI.qml +1 -1
@@ 130,7 130,7 @@ ItemDelegate {
                	leftPadding: 5
                	Label {
                		color: theme.highlightedText
                		text: signalState.contact(message.message.source).Name || message.message.source
                		text: signal.resolve_name(message.message.source)//signalState.contact(message.message.source).Name || message.message.source
                		visible: signalState.currentChat && signalState.currentChat.is_group
                	}
                }

M src/main.rs => src/main.rs +35 -12
@@ 14,6 14,7 @@
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#![feature(type_alias_impl_trait)]
#![feature(async_closure)]
#[macro_use]
extern crate cstr;
#[macro_use]


@@ 166,8 167,8 @@ impl SignalUI {
            .send(SignalRequest::SendMessage { to, message })
            .expect("Couldn't send SendMessage");
    }
    fn resolve_name(&self, source: String) -> String {
        self.state.as_ref().unwrap().resolve_name(source)
    fn resolve_name(&mut self, source: String) -> String {
        self.state.as_mut().unwrap().resolve_name(source)
    }
    fn send_group_message(&self, chat_id: String, message: String) {
        self.state


@@ 259,6 260,31 @@ impl SignalUI {
                    }
                }
                if let SignalResponse::Groups(_) = res {
                    let ids = slf
                        .state
                        .as_mut()
                        .unwrap()
                        .sbackend
                        .msg_store
                        .all()
                        .unwrap()
                        .into_iter()
                        .map(|x| {
                            return x.inner.source;
                        })
                        .collect::<Vec<String>>();
                    let mut done: HashMap<String, bool> = HashMap::new();
                    for id in ids {
                        if !done.contains_key(id.as_str()) {
                            done.insert(id.clone(), true);
                            slf.state
                                .as_mut()
                                .unwrap()
                                .sbackend
                                .resolve_name(&id)
                                .unwrap();
                        }
                    }
                    println!("Finished initialization");
                    slf.show_view("chats".to_string());
                    slf.ready();


@@ 285,7 311,6 @@ impl SignalUI {
                    conn.clone(),
                );
                state.user_tel = config.number.clone();
                state.show_view("chats".to_string());
                self.state = Some(state);
                res_receiver.attach(None, move |value| {
                    process_res(value);


@@ 354,14 379,16 @@ impl SignalState {
    ) -> SignalState {
        SignalState {
            current: None,
            req_sender,
            req_sender: req_sender.clone(),
            last_id: Rc::new(RefCell::new(None)),
            res_sender: res_sender.clone(),
            current_chat: Rc::new(RefCell::new(None)),
            user_tel: None,
            backend: config.backend,
            config: Rc::new(RefCell::new(config.clone())),
            sbackend: std::boxed::Box::new(scli::SCLISession::new(res_sender, config, conn)),
            sbackend: std::boxed::Box::new(scli::SCLISession::new(
                res_sender, config, conn, req_sender,
            )),
        }
    }
    /// Shows the given view in the app_box


@@ 431,7 458,7 @@ impl SignalState {
                let latest = msgs.values().max_by_key(|x| x.sent_at).unwrap();
                chats.push(Chat {
                    ID: (chats.len() + i) as i32,
                    name: id.clone(),
                    name: self.resolve_name(id.clone()),
                    tel: id,
                    is_group: false,
                    last: latest.message.clone(),


@@ 445,12 472,8 @@ impl SignalState {
            .send(SignalResponse::ChatList(chats))
            .expect("Couldn't send ChatList");
    }
    pub fn resolve_name(&self, source: String) -> String {
        if let Some(contact) = self.sbackend.contact(source.as_str()).ok() {
            return contact.name;
        } else {
            return source;
        }
    pub fn resolve_name(&mut self, source: String) -> String {
        return self.sbackend.resolve_name(source.as_str()).unwrap();
    }
    pub fn update_contacts(&self) {
        let mut contacts: Vec<Contact> = self.sbackend.contacts().values().cloned().collect();

M src/scli.rs => src/scli.rs +29 -1
@@ 34,6 34,8 @@ pub struct SCLISession {
    pub res_sender: glib::Sender<SignalResponse>,
    pub groups: HashMap<String, Group>,
    pub proxy: nonblock::Proxy<'static, Arc<nonblock::SyncConnection>>,
    pub resolved_names: HashMap<String, Option<String>>,
    pub req_sender: UnboundedSender<SignalRequest>,
}
impl SCLISession {
    pub fn merge_ids(&self, from: String, to: String) {


@@ 247,7 249,11 @@ impl SCLISession {
                                    ID: 0,
                                    reactions: vec![]
                                })).expect("Couldnt send IDLessMessage");
                            }
                            },
                            SignalRequest::GetContactName { chat_id } => {
                                let (name,): (String,) = proxy.method_call("org.asamk.Signal", "getContactName", (chat_id.clone(), )).await.unwrap();
                    res_sender.send(SignalResponse::ContactList(vec![Contact {name, tel: chat_id}]));
                            },
                            SignalRequest::AddContact{ name, phone} => {
                                res_sender.send(SignalResponse::ContactList(vec![Contact {name, tel: phone}])).expect("Couldn't send ContactList");
                            },


@@ 376,6 382,7 @@ impl SCLISession {
        res_sender: glib::Sender<SignalResponse>,
        config: Config,
        conn: Arc<nonblock::SyncConnection>,
        req_sender: UnboundedSender<SignalRequest>,
    ) -> SCLISession {
        let scli_config = config.scli.expect("No configured SCLI settings");
        let data_dir = scli_config.data_dir.clone().replace(


@@ 418,6 425,8 @@ impl SCLISession {
            contact_store,
            groups: HashMap::new(),
            proxy,
            resolved_names: HashMap::new(),
            req_sender,
        };
        session
    }


@@ 434,6 443,25 @@ impl SCLISession {
            })
            .collect())
    }
    pub fn resolve_name(&mut self, chat_id: &str) -> Result<String> {
        if let Some(contact) = self.contact(chat_id.clone()).ok() {
            return Ok(contact.name);
        } else if let Some(group) = self.groups().get(chat_id) {
            return Ok(group.name.clone());
        } else if let Some(name) = self.resolved_names.get(chat_id) {
            if let Some(name) = name {
                return Ok(name.to_string());
            } else {
                return Ok(chat_id.to_string());
            }
        } else {
            self.req_sender.send(SignalRequest::GetContactName {
                chat_id: chat_id.to_string(),
            });
            self.resolved_names.insert(chat_id.to_string(), None);
            return Ok(chat_id.to_string());
        }
    }
    pub fn messages<'a>(
        &'a self,
    ) -> Result<BTreeMap<String, BTreeMap<i64, SignalMessage>>, anyhow::Error> {

M src/signal.rs => src/signal.rs +3 -0
@@ 101,6 101,9 @@ pub enum SignalRequest {
        to: String,
        message: String,
    },
    GetContactName {
        chat_id: String,
    },
    SendGroupMessage {
        chat_id: String,
        message: String,