Use footer as a contact status

This commit is contained in:
link2xt
2021-02-11 08:43:50 +03:00
committed by link2xt
parent 59f9fc7cbf
commit 0601b05cb7
10 changed files with 167 additions and 33 deletions

View File

@@ -71,6 +71,9 @@ pub struct Contact {
/// Parameters as Param::ProfileImage
pub param: Params,
/// Last seen message signature for this contact, to be displayed in the profile.
status: String,
}
/// Possible origins of a contact.
@@ -172,7 +175,7 @@ impl Contact {
let mut res = context
.sql
.query_row(
"SELECT c.name, c.addr, c.origin, c.blocked, c.authname, c.param
"SELECT c.name, c.addr, c.origin, c.blocked, c.authname, c.param, c.status
FROM contacts c
WHERE c.id=?;",
paramsv![contact_id as i32],
@@ -185,6 +188,7 @@ impl Contact {
blocked: row.get::<_, Option<i32>>(3)?.unwrap_or_default() != 0,
origin: row.get(2)?,
param: row.get::<_, String>(5)?.parse().unwrap_or_default(),
status: row.get(6).unwrap_or_default(),
};
Ok(contact)
},
@@ -196,6 +200,10 @@ impl Contact {
.get_config(Config::ConfiguredAddr)
.await
.unwrap_or_default();
res.status = context
.get_config(Config::Selfstatus)
.await
.unwrap_or_default();
} else if contact_id == DC_CONTACT_ID_DEVICE {
res.name = context
.stock_str(StockMessage::DeviceMessages)
@@ -841,7 +849,8 @@ impl Contact {
Ok(contact)
}
pub async fn update_param(&mut self, context: &Context) -> Result<()> {
/// Updates `param` column in the database.
pub async fn update_param(&self, context: &Context) -> Result<()> {
context
.sql
.execute(
@@ -852,6 +861,18 @@ impl Contact {
Ok(())
}
/// Updates `status` column in the database.
pub async fn update_status(&self, context: &Context) -> Result<()> {
context
.sql
.execute(
"UPDATE contacts SET status=? WHERE id=?",
paramsv![self.status, self.id as i32],
)
.await?;
Ok(())
}
/// Get the ID of the contact.
pub fn get_id(&self) -> u32 {
self.id
@@ -932,6 +953,13 @@ impl Contact {
dc_str_to_color(&self.addr)
}
/// Gets the contact's status.
///
/// Status is the last signature received in a message from this contact.
pub fn get_status(&self) -> &str {
self.status.as_str()
}
/// Check if a contact was verified. E.g. by a secure-join QR code scan
/// and if the key has not changed since this verification.
///
@@ -1163,6 +1191,18 @@ pub(crate) async fn set_profile_image(
Ok(())
}
/// Sets contact status.
pub(crate) async fn set_status(context: &Context, contact_id: u32, status: String) -> Result<()> {
let mut contact = Contact::load_from_db(context, contact_id).await?;
if contact.status != status {
contact.status = status;
contact.update_status(context).await?;
context.emit_event(EventType::ContactsChanged(Some(contact_id)));
}
Ok(())
}
/// Normalize a name.
///
/// - Remove quotes (come from some bad MUA implementations)