chat: resultify parent_is_encrypted and don't ignore the error

This should prevent accidental sending of unencrypted messages when
parent_is_encrypted returns an error. For example, if the database is
busy due to other thread activity, parent_is_encrypted should not return
false. Instead, message sending job should retry later.
This commit is contained in:
Alexander Krotov
2020-01-31 21:58:16 +03:00
parent 627b54f712
commit c7eca8deb3

View File

@@ -583,23 +583,19 @@ impl Chat {
sql.query_row(&query, params, collect).ok() sql.query_row(&query, params, collect).ok()
} }
fn parent_is_encrypted(&self, context: &Context) -> bool { fn parent_is_encrypted(&self, context: &Context) -> Result<bool, Error> {
let sql = &context.sql; let sql = &context.sql;
let params = params![self.id]; let params = params![self.id];
let query = Self::parent_query("param"); let query = Self::parent_query("param");
let packed: Option<String> = sql.query_get_value(context, &query, params); let packed: Option<String> = sql.query_get_value_result(&query, params)?;
if let Some(ref packed) = packed { if let Some(ref packed) = packed {
match packed.parse::<Params>() { let param = packed.parse::<Params>()?;
Ok(param) => param.exists(Param::GuaranteeE2ee), Ok(param.exists(Param::GuaranteeE2ee))
Err(err) => {
error!(context, "invalid params stored: '{}', {:?}", packed, err);
false
}
}
} else { } else {
false // No messages
Ok(false)
} }
} }
@@ -801,7 +797,7 @@ impl Chat {
} }
} }
if can_encrypt && (all_mutual || self.parent_is_encrypted(context)) { if can_encrypt && (all_mutual || self.parent_is_encrypted(context)?) {
msg.param.set_int(Param::GuaranteeE2ee, 1); msg.param.set_int(Param::GuaranteeE2ee, 1);
} }
} }