forked from whisperfish/presage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add skeleton for sqlite store implementation (whisperfish#286)
- Loading branch information
Showing
12 changed files
with
640 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!() | ||
} | ||
} |
Oops, something went wrong.