Skip to content

Commit

Permalink
Add skeleton for sqlite store implementation (whisperfish#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon authored Oct 24, 2024
1 parent b108749 commit 549f9fb
Show file tree
Hide file tree
Showing 12 changed files with 640 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["presage", "presage-cli", "presage-store-sled", "presage-store-cipher"]
members = ["presage", "presage-cli", "presage-store-sled", "presage-store-cipher", "presage-store-sqlite"]
resolver = "2"

[patch.crates-io]
Expand Down
2 changes: 1 addition & 1 deletion presage-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use presage::libsignal_service::ServiceAddress;
use presage::manager::ReceivingMode;
use presage::model::contacts::Contact;
use presage::model::groups::Group;
use presage::model::identity::OnNewIdentity;
use presage::proto::receipt_message;
use presage::proto::EditMessage;
use presage::proto::ReceiptMessage;
Expand All @@ -39,7 +40,6 @@ use presage::{
Manager,
};
use presage_store_sled::MigrationConflictStrategy;
use presage_store_sled::OnNewIdentity;
use presage_store_sled::SledStore;
use tempfile::Builder;
use tokio::task;
Expand Down
7 changes: 1 addition & 6 deletions presage-store-sled/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use presage::{
},
},
manager::RegistrationData,
model::identity::OnNewIdentity,
store::{ContentsStore, StateStore, Store},
};
use protocol::{AciSledStore, PniSledStore, SledProtocolStore, SledTrees};
Expand Down Expand Up @@ -101,12 +102,6 @@ impl SchemaVersion {
}
}

#[derive(Debug, Clone)]
pub enum OnNewIdentity {
Reject,
Trust,
}

impl SledStore {
#[allow(unused_variables)]
fn new(
Expand Down
13 changes: 13 additions & 0 deletions presage-store-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "presage-store-sqlite"
version = "0.1.0"
edition = "2021"

[dependencies]
async-trait = "0.1.83"
chrono = "0.4.38"
presage = { path = "../presage" }
presage-store-cipher = { path = "../presage-store-cipher", optional = true }

sqlx = { version = "0.8.2", features = ["sqlite"] }
thiserror = "1.0.65"
217 changes: 217 additions & 0 deletions presage-store-sqlite/src/content.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
use std::marker::PhantomData;

use presage::{
libsignal_service::{prelude::Content, zkgroup::GroupMasterKeyBytes},
model::{contacts::Contact, groups::Group},
store::{ContentsStore, StickerPack},
};

use crate::{SqliteStore, SqliteStoreError};

impl ContentsStore for SqliteStore {
type ContentsStoreError = SqliteStoreError;

type ContactsIter = DummyIter<Result<Contact, Self::ContentsStoreError>>;

type GroupsIter = DummyIter<Result<(GroupMasterKeyBytes, Group), Self::ContentsStoreError>>;

type MessagesIter = DummyIter<Result<Content, Self::ContentsStoreError>>;

type StickerPacksIter = DummyIter<Result<StickerPack, Self::ContentsStoreError>>;

async fn clear_profiles(&mut self) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn clear_contents(&mut self) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn clear_messages(&mut self) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn clear_thread(
&mut self,
thread: &presage::store::Thread,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn save_message(
&self,
thread: &presage::store::Thread,
message: presage::libsignal_service::prelude::Content,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn delete_message(
&mut self,
thread: &presage::store::Thread,
timestamp: u64,
) -> Result<bool, Self::ContentsStoreError> {
todo!()
}

async fn message(
&self,
thread: &presage::store::Thread,
timestamp: u64,
) -> Result<Option<presage::libsignal_service::prelude::Content>, Self::ContentsStoreError>
{
todo!()
}

async fn messages(
&self,
thread: &presage::store::Thread,
range: impl std::ops::RangeBounds<u64>,
) -> Result<Self::MessagesIter, Self::ContentsStoreError> {
todo!()
}

async fn clear_contacts(&mut self) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn save_contact(
&mut self,
contacts: &presage::model::contacts::Contact,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn contacts(&self) -> Result<Self::ContactsIter, Self::ContentsStoreError> {
todo!()
}

async fn contact_by_id(
&self,
id: &presage::libsignal_service::prelude::Uuid,
) -> Result<Option<presage::model::contacts::Contact>, Self::ContentsStoreError> {
todo!()
}

async fn clear_groups(&mut self) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn save_group(
&self,
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
group: impl Into<presage::model::groups::Group>,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn groups(&self) -> Result<Self::GroupsIter, Self::ContentsStoreError> {
todo!()
}

async fn group(
&self,
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
) -> Result<Option<presage::model::groups::Group>, Self::ContentsStoreError> {
todo!()
}

async fn save_group_avatar(
&self,
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
avatar: &presage::AvatarBytes,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn group_avatar(
&self,
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
) -> Result<Option<presage::AvatarBytes>, Self::ContentsStoreError> {
todo!()
}

async fn upsert_profile_key(
&mut self,
uuid: &presage::libsignal_service::prelude::Uuid,
key: presage::libsignal_service::prelude::ProfileKey,
) -> Result<bool, Self::ContentsStoreError> {
todo!()
}

async fn profile_key(
&self,
uuid: &presage::libsignal_service::prelude::Uuid,
) -> Result<Option<presage::libsignal_service::prelude::ProfileKey>, Self::ContentsStoreError>
{
todo!()
}

async fn save_profile(
&mut self,
uuid: presage::libsignal_service::prelude::Uuid,
key: presage::libsignal_service::prelude::ProfileKey,
profile: presage::libsignal_service::Profile,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn profile(
&self,
uuid: presage::libsignal_service::prelude::Uuid,
key: presage::libsignal_service::prelude::ProfileKey,
) -> Result<Option<presage::libsignal_service::Profile>, Self::ContentsStoreError> {
todo!()
}

async fn save_profile_avatar(
&mut self,
uuid: presage::libsignal_service::prelude::Uuid,
key: presage::libsignal_service::prelude::ProfileKey,
profile: &presage::AvatarBytes,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn profile_avatar(
&self,
uuid: presage::libsignal_service::prelude::Uuid,
key: presage::libsignal_service::prelude::ProfileKey,
) -> Result<Option<presage::AvatarBytes>, Self::ContentsStoreError> {
todo!()
}

async fn add_sticker_pack(
&mut self,
pack: &presage::store::StickerPack,
) -> Result<(), Self::ContentsStoreError> {
todo!()
}

async fn sticker_pack(
&self,
id: &[u8],
) -> Result<Option<presage::store::StickerPack>, Self::ContentsStoreError> {
todo!()
}

async fn remove_sticker_pack(&mut self, id: &[u8]) -> Result<bool, Self::ContentsStoreError> {
todo!()
}

async fn sticker_packs(&self) -> Result<Self::StickerPacksIter, Self::ContentsStoreError> {
todo!()
}
}

pub struct DummyIter<T> {
_data: PhantomData<T>,
}

impl<T> Iterator for DummyIter<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
todo!()
}
}
11 changes: 11 additions & 0 deletions presage-store-sqlite/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use presage::store::StoreError;

#[derive(Debug, thiserror::Error)]
pub enum SqliteStoreError {
#[error("database migration is not supported")]
MigrationConflict,
#[error("data store error: {0}")]
Db(#[from] sqlx::Error),
}

impl StoreError for SqliteStoreError {}
101 changes: 101 additions & 0 deletions presage-store-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#![allow(warnings)]

use std::path::Path;

use presage::{
model::identity::OnNewIdentity,
store::{StateStore, Store},
};
use protocol::SqliteProtocolStore;
use sqlx::{sqlite::SqliteConnectOptions, SqlitePool};

mod content;
mod error;
mod protocol;

pub use error::SqliteStoreError;

#[derive(Debug, Clone)]
pub struct SqliteStore {
db: SqlitePool,
/// Whether to trust new identities automatically (for instance, when a somebody's phone has changed)
trust_new_identities: OnNewIdentity,
}

impl SqliteStore {
pub async fn open(
db_path: impl AsRef<Path>,
trust_new_identities: OnNewIdentity,
) -> Result<Self, SqliteStoreError> {
let connect_options = SqliteConnectOptions::new().filename(db_path);
let pool = SqlitePool::connect_with(connect_options).await?;

Ok(Self {
db: pool,
trust_new_identities,
})
}
}

impl Store for SqliteStore {
type Error = SqliteStoreError;

type AciStore = SqliteProtocolStore;

type PniStore = SqliteProtocolStore;

async fn clear(&mut self) -> Result<(), SqliteStoreError> {
todo!()
}

fn aci_protocol_store(&self) -> Self::AciStore {
SqliteProtocolStore {
store: self.clone(),
}
}

fn pni_protocol_store(&self) -> Self::PniStore {
SqliteProtocolStore {
store: self.clone(),
}
}
}

impl StateStore for SqliteStore {
type StateStoreError = SqliteStoreError;

async fn load_registration_data(
&self,
) -> Result<Option<presage::manager::RegistrationData>, Self::StateStoreError> {
todo!()
}

async fn set_aci_identity_key_pair(
&self,
key_pair: presage::libsignal_service::protocol::IdentityKeyPair,
) -> Result<(), Self::StateStoreError> {
todo!()
}

async fn set_pni_identity_key_pair(
&self,
key_pair: presage::libsignal_service::protocol::IdentityKeyPair,
) -> Result<(), Self::StateStoreError> {
todo!()
}

async fn save_registration_data(
&mut self,
state: &presage::manager::RegistrationData,
) -> Result<(), Self::StateStoreError> {
todo!()
}

async fn is_registered(&self) -> bool {
todo!()
}

async fn clear_registration(&mut self) -> Result<(), Self::StateStoreError> {
todo!()
}
}
Loading

0 comments on commit 549f9fb

Please sign in to comment.