mirror of
https://github.com/chatmail/core.git
synced 2026-05-03 21:36:29 +03:00
Ensure that Peerstate has an address set on the type level
This commit is contained in:
@@ -617,18 +617,18 @@ impl Contact {
|
||||
.peek_key(PeerstateVerifiedStatus::Unverified)
|
||||
.map(|k| k.formatted_fingerprint())
|
||||
.unwrap_or_default();
|
||||
if peerstate.addr.is_some() && &loginparam.addr < peerstate.addr.as_ref().unwrap() {
|
||||
if loginparam.addr < peerstate.addr {
|
||||
cat_fingerprint(&mut ret, &loginparam.addr, &fingerprint_self, "");
|
||||
cat_fingerprint(
|
||||
&mut ret,
|
||||
peerstate.addr.as_ref().unwrap(),
|
||||
peerstate.addr.clone(),
|
||||
&fingerprint_other_verified,
|
||||
&fingerprint_other_unverified,
|
||||
);
|
||||
} else {
|
||||
cat_fingerprint(
|
||||
&mut ret,
|
||||
peerstate.addr.as_ref().unwrap(),
|
||||
peerstate.addr.clone(),
|
||||
&fingerprint_other_verified,
|
||||
&fingerprint_other_unverified,
|
||||
);
|
||||
|
||||
@@ -26,7 +26,7 @@ pub enum PeerstateVerifiedStatus {
|
||||
/// Peerstate represents the state of an Autocrypt peer.
|
||||
pub struct Peerstate<'a> {
|
||||
pub context: &'a Context,
|
||||
pub addr: Option<String>,
|
||||
pub addr: String,
|
||||
pub last_seen: i64,
|
||||
pub last_seen_autocrypt: i64,
|
||||
pub prefer_encrypt: EncryptPreference,
|
||||
@@ -98,10 +98,10 @@ pub enum DegradeEvent {
|
||||
}
|
||||
|
||||
impl<'a> Peerstate<'a> {
|
||||
pub fn new(context: &'a Context) -> Self {
|
||||
pub fn new(context: &'a Context, addr: String) -> Self {
|
||||
Peerstate {
|
||||
context,
|
||||
addr: None,
|
||||
addr,
|
||||
last_seen: 0,
|
||||
last_seen_autocrypt: 0,
|
||||
prefer_encrypt: Default::default(),
|
||||
@@ -118,9 +118,8 @@ impl<'a> Peerstate<'a> {
|
||||
}
|
||||
|
||||
pub fn from_header(context: &'a Context, header: &Aheader, message_time: i64) -> Self {
|
||||
let mut res = Self::new(context);
|
||||
let mut res = Self::new(context, header.addr.clone());
|
||||
|
||||
res.addr = Some(header.addr.clone());
|
||||
res.last_seen = message_time;
|
||||
res.last_seen_autocrypt = message_time;
|
||||
res.to_save = Some(ToSave::All);
|
||||
@@ -132,9 +131,8 @@ impl<'a> Peerstate<'a> {
|
||||
}
|
||||
|
||||
pub fn from_gossip(context: &'a Context, gossip_header: &Aheader, message_time: i64) -> Self {
|
||||
let mut res = Self::new(context);
|
||||
let mut res = Self::new(context, gossip_header.addr.clone());
|
||||
|
||||
res.addr = Some(gossip_header.addr.clone());
|
||||
res.gossip_timestamp = message_time;
|
||||
res.to_save = Some(ToSave::All);
|
||||
res.gossip_key = Some(gossip_header.public_key.clone());
|
||||
@@ -177,9 +175,8 @@ impl<'a> Peerstate<'a> {
|
||||
public_key, gossip_timestamp, gossip_key, public_key_fingerprint,
|
||||
gossip_key_fingerprint, verified_key, verified_key_fingerprint
|
||||
*/
|
||||
let mut res = Self::new(context);
|
||||
let mut res = Self::new(context, row.get(0)?);
|
||||
|
||||
res.addr = Some(row.get(0)?);
|
||||
res.last_seen = row.get(1)?;
|
||||
res.last_seen_autocrypt = row.get(2)?;
|
||||
res.prefer_encrypt = EncryptPreference::from_i32(row.get(3)?).unwrap_or_default();
|
||||
@@ -273,9 +270,7 @@ impl<'a> Peerstate<'a> {
|
||||
}
|
||||
|
||||
pub fn apply_header(&mut self, header: &Aheader, message_time: i64) {
|
||||
if self.addr.is_none()
|
||||
|| self.addr.as_ref().unwrap().to_lowercase() != header.addr.to_lowercase()
|
||||
{
|
||||
if self.addr.to_lowercase() != header.addr.to_lowercase() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -305,9 +300,7 @@ impl<'a> Peerstate<'a> {
|
||||
}
|
||||
|
||||
pub fn apply_gossip(&mut self, gossip_header: &Aheader, message_time: i64) {
|
||||
if self.addr.is_none()
|
||||
|| self.addr.as_ref().unwrap().to_lowercase() != gossip_header.addr.to_lowercase()
|
||||
{
|
||||
if self.addr.to_lowercase() != gossip_header.addr.to_lowercase() {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -323,19 +316,17 @@ impl<'a> Peerstate<'a> {
|
||||
}
|
||||
|
||||
pub fn render_gossip_header(&self, min_verified: PeerstateVerifiedStatus) -> Option<String> {
|
||||
if let Some(ref addr) = self.addr {
|
||||
if let Some(key) = self.peek_key(min_verified) {
|
||||
// TODO: avoid cloning
|
||||
let header = Aheader::new(
|
||||
addr.to_string(),
|
||||
key.clone(),
|
||||
EncryptPreference::NoPreference,
|
||||
);
|
||||
return Some(header.to_string());
|
||||
}
|
||||
if let Some(key) = self.peek_key(min_verified) {
|
||||
// TODO: avoid cloning
|
||||
let header = Aheader::new(
|
||||
self.addr.clone(),
|
||||
key.clone(),
|
||||
EncryptPreference::NoPreference,
|
||||
);
|
||||
Some(header.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn peek_key(&self, min_verified: PeerstateVerifiedStatus) -> Option<&Key> {
|
||||
@@ -387,13 +378,12 @@ impl<'a> Peerstate<'a> {
|
||||
}
|
||||
|
||||
pub fn save_to_db(&self, sql: &Sql, create: bool) -> Result<()> {
|
||||
ensure!(!self.addr.is_none(), "self.addr is not configured");
|
||||
if create {
|
||||
sql::execute(
|
||||
self.context,
|
||||
sql,
|
||||
"INSERT INTO acpeerstates (addr) VALUES(?);",
|
||||
params![self.addr.as_ref().unwrap()],
|
||||
params![self.addr],
|
||||
)?;
|
||||
}
|
||||
|
||||
@@ -471,7 +461,7 @@ mod tests {
|
||||
|
||||
let mut peerstate = Peerstate {
|
||||
context: &ctx.ctx,
|
||||
addr: Some(addr.into()),
|
||||
addr: addr.into(),
|
||||
last_seen: 10,
|
||||
last_seen_autocrypt: 11,
|
||||
prefer_encrypt: EncryptPreference::Mutual,
|
||||
@@ -516,7 +506,7 @@ mod tests {
|
||||
|
||||
let peerstate = Peerstate {
|
||||
context: &ctx.ctx,
|
||||
addr: Some(addr.into()),
|
||||
addr: addr.into(),
|
||||
last_seen: 10,
|
||||
last_seen_autocrypt: 11,
|
||||
prefer_encrypt: EncryptPreference::Mutual,
|
||||
@@ -554,7 +544,7 @@ mod tests {
|
||||
|
||||
let mut peerstate = Peerstate {
|
||||
context: &ctx.ctx,
|
||||
addr: Some(addr.into()),
|
||||
addr: addr.into(),
|
||||
last_seen: 10,
|
||||
last_seen_autocrypt: 11,
|
||||
prefer_encrypt: EncryptPreference::Mutual,
|
||||
|
||||
22
src/qr.rs
22
src/qr.rs
@@ -139,24 +139,20 @@ fn decode_openpgp(context: &Context, qr: &str) -> Lot {
|
||||
if invitenumber.is_none() || auth.is_none() {
|
||||
if let Some(peerstate) = peerstate {
|
||||
lot.state = LotState::QrFprOk;
|
||||
let addr = peerstate
|
||||
.addr
|
||||
.as_ref()
|
||||
.map(|s| s.as_str())
|
||||
.unwrap_or_else(|| "");
|
||||
|
||||
lot.id = Contact::add_or_lookup(context, name, addr, Origin::UnhandledQrScan)
|
||||
.map(|(id, _)| id)
|
||||
.unwrap_or_default();
|
||||
lot.id = Contact::add_or_lookup(
|
||||
context,
|
||||
name,
|
||||
peerstate.addr.clone(),
|
||||
Origin::UnhandledQrScan,
|
||||
)
|
||||
.map(|(id, _)| id)
|
||||
.unwrap_or_default();
|
||||
|
||||
let (id, _) = chat::create_or_lookup_by_contact_id(context, lot.id, Blocked::Deaddrop)
|
||||
.unwrap_or_default();
|
||||
|
||||
chat::add_info_msg(
|
||||
context,
|
||||
id,
|
||||
format!("{} verified.", peerstate.addr.unwrap_or_default()),
|
||||
);
|
||||
chat::add_info_msg(context, id, format!("{} verified.", peerstate.addr));
|
||||
} else {
|
||||
lot.state = LotState::QrFprWithoutAddr;
|
||||
lot.text1 = Some(dc_format_fingerprint(&fingerprint));
|
||||
|
||||
@@ -761,11 +761,8 @@ pub fn handle_degrade_event(context: &Context, peerstate: &Peerstate) -> Result<
|
||||
chat::create_or_lookup_by_contact_id(context, contact_id as u32, Blocked::Deaddrop)
|
||||
.unwrap_or_default();
|
||||
|
||||
let peeraddr: &str = match peerstate.addr {
|
||||
Some(ref addr) => &addr,
|
||||
None => "",
|
||||
};
|
||||
let msg = context.stock_string_repl_str(StockMessage::ContactSetupChanged, peeraddr);
|
||||
let msg = context
|
||||
.stock_string_repl_str(StockMessage::ContactSetupChanged, peerstate.addr.clone());
|
||||
|
||||
chat::add_info_msg(context, contact_chat_id, msg);
|
||||
emit_event!(context, Event::ChatModified(contact_chat_id));
|
||||
|
||||
Reference in New Issue
Block a user