diff --git a/src/chat.rs b/src/chat.rs index 6e58d1d2d..3610762d0 100644 --- a/src/chat.rs +++ b/src/chat.rs @@ -75,6 +75,29 @@ pub enum ProtectionStatus { Protected = 1, } +#[derive( + Debug, + Display, + Clone, + Copy, + PartialEq, + Eq, + FromPrimitive, + ToPrimitive, + FromSql, + ToSql, + IntoStaticStr, + Serialize, + Deserialize, +)] +#[repr(u32)] +pub enum EncryptionModus { + Opportunistic = 0, + ForcePlaintext = 1, + ForceEncrypted = 2, + ForceVerified = 3, +} + impl Default for ProtectionStatus { fn default() -> Self { ProtectionStatus::Unprotected @@ -898,6 +921,33 @@ impl ChatId { Ok(ret.trim().to_string()) } + /// This sets a protection modus for the chat and enforces that messages are only send if they + /// meet the encryption modus (ForcePlaintext, Opportunistic, ForceEncrypted, ForceVerified) + pub async fn set_encryption_modus(self, context: &Context, modus: &EncryptionModus) -> Result<()> { + let encryption_modus: u32 = context + .sql + .query_get_value( + "SELECT encryption_modus FROM chats WHERE id=?;", + paramsv![self], + ) + .await??; + Ok(encryption_modus.unwrap_or_default()) + } + + /// This sets a protection modus for the chat and enforces that messages are only send if they + /// meet the encryption modus (ForcePlaintext, Opportunistic, ForceEncrypted, ForceVerified) + pub async fn get_encryption_modus(self, context: &Context) -> Result { + let encryption_modus: u32 = context + .sql + .query_get_value( + "SELECT encryption_modus FROM chats WHERE id=?;", + paramsv![self], + ) + .await??; + Ok(encryption_modus.into()) + } + + /// Bad evil escape hatch. /// /// Avoid using this, eventually types should be cleaned up enough diff --git a/src/sql/tables.sql b/src/sql/tables.sql index c52c29823..9daf778b2 100644 --- a/src/sql/tables.sql +++ b/src/sql/tables.sql @@ -38,7 +38,8 @@ CREATE TABLE chats ( locations_last_sent INTEGER DEFAULT 0, created_timestamp INTEGER DEFAULT 0, muted_until INTEGER DEFAULT 0, - ephemeral_timer INTEGER + ephemeral_timer INTEGER, + encryption_modus INTEGER DEFAULT 0 ); CREATE INDEX chats_index1 ON chats (grpid); CREATE INDEX chats_index2 ON chats (archived);