Skip to content

Commit 549f9fb

Browse files
authored
Add skeleton for sqlite store implementation (#286)
1 parent b108749 commit 549f9fb

File tree

12 files changed

+640
-10
lines changed

12 files changed

+640
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["presage", "presage-cli", "presage-store-sled", "presage-store-cipher"]
2+
members = ["presage", "presage-cli", "presage-store-sled", "presage-store-cipher", "presage-store-sqlite"]
33
resolver = "2"
44

55
[patch.crates-io]

presage-cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use presage::libsignal_service::ServiceAddress;
2727
use presage::manager::ReceivingMode;
2828
use presage::model::contacts::Contact;
2929
use presage::model::groups::Group;
30+
use presage::model::identity::OnNewIdentity;
3031
use presage::proto::receipt_message;
3132
use presage::proto::EditMessage;
3233
use presage::proto::ReceiptMessage;
@@ -39,7 +40,6 @@ use presage::{
3940
Manager,
4041
};
4142
use presage_store_sled::MigrationConflictStrategy;
42-
use presage_store_sled::OnNewIdentity;
4343
use presage_store_sled::SledStore;
4444
use tempfile::Builder;
4545
use tokio::task;

presage-store-sled/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use presage::{
1616
},
1717
},
1818
manager::RegistrationData,
19+
model::identity::OnNewIdentity,
1920
store::{ContentsStore, StateStore, Store},
2021
};
2122
use protocol::{AciSledStore, PniSledStore, SledProtocolStore, SledTrees};
@@ -101,12 +102,6 @@ impl SchemaVersion {
101102
}
102103
}
103104

104-
#[derive(Debug, Clone)]
105-
pub enum OnNewIdentity {
106-
Reject,
107-
Trust,
108-
}
109-
110105
impl SledStore {
111106
#[allow(unused_variables)]
112107
fn new(

presage-store-sqlite/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "presage-store-sqlite"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
async-trait = "0.1.83"
8+
chrono = "0.4.38"
9+
presage = { path = "../presage" }
10+
presage-store-cipher = { path = "../presage-store-cipher", optional = true }
11+
12+
sqlx = { version = "0.8.2", features = ["sqlite"] }
13+
thiserror = "1.0.65"

presage-store-sqlite/src/content.rs

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
use std::marker::PhantomData;
2+
3+
use presage::{
4+
libsignal_service::{prelude::Content, zkgroup::GroupMasterKeyBytes},
5+
model::{contacts::Contact, groups::Group},
6+
store::{ContentsStore, StickerPack},
7+
};
8+
9+
use crate::{SqliteStore, SqliteStoreError};
10+
11+
impl ContentsStore for SqliteStore {
12+
type ContentsStoreError = SqliteStoreError;
13+
14+
type ContactsIter = DummyIter<Result<Contact, Self::ContentsStoreError>>;
15+
16+
type GroupsIter = DummyIter<Result<(GroupMasterKeyBytes, Group), Self::ContentsStoreError>>;
17+
18+
type MessagesIter = DummyIter<Result<Content, Self::ContentsStoreError>>;
19+
20+
type StickerPacksIter = DummyIter<Result<StickerPack, Self::ContentsStoreError>>;
21+
22+
async fn clear_profiles(&mut self) -> Result<(), Self::ContentsStoreError> {
23+
todo!()
24+
}
25+
26+
async fn clear_contents(&mut self) -> Result<(), Self::ContentsStoreError> {
27+
todo!()
28+
}
29+
30+
async fn clear_messages(&mut self) -> Result<(), Self::ContentsStoreError> {
31+
todo!()
32+
}
33+
34+
async fn clear_thread(
35+
&mut self,
36+
thread: &presage::store::Thread,
37+
) -> Result<(), Self::ContentsStoreError> {
38+
todo!()
39+
}
40+
41+
async fn save_message(
42+
&self,
43+
thread: &presage::store::Thread,
44+
message: presage::libsignal_service::prelude::Content,
45+
) -> Result<(), Self::ContentsStoreError> {
46+
todo!()
47+
}
48+
49+
async fn delete_message(
50+
&mut self,
51+
thread: &presage::store::Thread,
52+
timestamp: u64,
53+
) -> Result<bool, Self::ContentsStoreError> {
54+
todo!()
55+
}
56+
57+
async fn message(
58+
&self,
59+
thread: &presage::store::Thread,
60+
timestamp: u64,
61+
) -> Result<Option<presage::libsignal_service::prelude::Content>, Self::ContentsStoreError>
62+
{
63+
todo!()
64+
}
65+
66+
async fn messages(
67+
&self,
68+
thread: &presage::store::Thread,
69+
range: impl std::ops::RangeBounds<u64>,
70+
) -> Result<Self::MessagesIter, Self::ContentsStoreError> {
71+
todo!()
72+
}
73+
74+
async fn clear_contacts(&mut self) -> Result<(), Self::ContentsStoreError> {
75+
todo!()
76+
}
77+
78+
async fn save_contact(
79+
&mut self,
80+
contacts: &presage::model::contacts::Contact,
81+
) -> Result<(), Self::ContentsStoreError> {
82+
todo!()
83+
}
84+
85+
async fn contacts(&self) -> Result<Self::ContactsIter, Self::ContentsStoreError> {
86+
todo!()
87+
}
88+
89+
async fn contact_by_id(
90+
&self,
91+
id: &presage::libsignal_service::prelude::Uuid,
92+
) -> Result<Option<presage::model::contacts::Contact>, Self::ContentsStoreError> {
93+
todo!()
94+
}
95+
96+
async fn clear_groups(&mut self) -> Result<(), Self::ContentsStoreError> {
97+
todo!()
98+
}
99+
100+
async fn save_group(
101+
&self,
102+
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
103+
group: impl Into<presage::model::groups::Group>,
104+
) -> Result<(), Self::ContentsStoreError> {
105+
todo!()
106+
}
107+
108+
async fn groups(&self) -> Result<Self::GroupsIter, Self::ContentsStoreError> {
109+
todo!()
110+
}
111+
112+
async fn group(
113+
&self,
114+
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
115+
) -> Result<Option<presage::model::groups::Group>, Self::ContentsStoreError> {
116+
todo!()
117+
}
118+
119+
async fn save_group_avatar(
120+
&self,
121+
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
122+
avatar: &presage::AvatarBytes,
123+
) -> Result<(), Self::ContentsStoreError> {
124+
todo!()
125+
}
126+
127+
async fn group_avatar(
128+
&self,
129+
master_key: presage::libsignal_service::zkgroup::GroupMasterKeyBytes,
130+
) -> Result<Option<presage::AvatarBytes>, Self::ContentsStoreError> {
131+
todo!()
132+
}
133+
134+
async fn upsert_profile_key(
135+
&mut self,
136+
uuid: &presage::libsignal_service::prelude::Uuid,
137+
key: presage::libsignal_service::prelude::ProfileKey,
138+
) -> Result<bool, Self::ContentsStoreError> {
139+
todo!()
140+
}
141+
142+
async fn profile_key(
143+
&self,
144+
uuid: &presage::libsignal_service::prelude::Uuid,
145+
) -> Result<Option<presage::libsignal_service::prelude::ProfileKey>, Self::ContentsStoreError>
146+
{
147+
todo!()
148+
}
149+
150+
async fn save_profile(
151+
&mut self,
152+
uuid: presage::libsignal_service::prelude::Uuid,
153+
key: presage::libsignal_service::prelude::ProfileKey,
154+
profile: presage::libsignal_service::Profile,
155+
) -> Result<(), Self::ContentsStoreError> {
156+
todo!()
157+
}
158+
159+
async fn profile(
160+
&self,
161+
uuid: presage::libsignal_service::prelude::Uuid,
162+
key: presage::libsignal_service::prelude::ProfileKey,
163+
) -> Result<Option<presage::libsignal_service::Profile>, Self::ContentsStoreError> {
164+
todo!()
165+
}
166+
167+
async fn save_profile_avatar(
168+
&mut self,
169+
uuid: presage::libsignal_service::prelude::Uuid,
170+
key: presage::libsignal_service::prelude::ProfileKey,
171+
profile: &presage::AvatarBytes,
172+
) -> Result<(), Self::ContentsStoreError> {
173+
todo!()
174+
}
175+
176+
async fn profile_avatar(
177+
&self,
178+
uuid: presage::libsignal_service::prelude::Uuid,
179+
key: presage::libsignal_service::prelude::ProfileKey,
180+
) -> Result<Option<presage::AvatarBytes>, Self::ContentsStoreError> {
181+
todo!()
182+
}
183+
184+
async fn add_sticker_pack(
185+
&mut self,
186+
pack: &presage::store::StickerPack,
187+
) -> Result<(), Self::ContentsStoreError> {
188+
todo!()
189+
}
190+
191+
async fn sticker_pack(
192+
&self,
193+
id: &[u8],
194+
) -> Result<Option<presage::store::StickerPack>, Self::ContentsStoreError> {
195+
todo!()
196+
}
197+
198+
async fn remove_sticker_pack(&mut self, id: &[u8]) -> Result<bool, Self::ContentsStoreError> {
199+
todo!()
200+
}
201+
202+
async fn sticker_packs(&self) -> Result<Self::StickerPacksIter, Self::ContentsStoreError> {
203+
todo!()
204+
}
205+
}
206+
207+
pub struct DummyIter<T> {
208+
_data: PhantomData<T>,
209+
}
210+
211+
impl<T> Iterator for DummyIter<T> {
212+
type Item = T;
213+
214+
fn next(&mut self) -> Option<Self::Item> {
215+
todo!()
216+
}
217+
}

presage-store-sqlite/src/error.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use presage::store::StoreError;
2+
3+
#[derive(Debug, thiserror::Error)]
4+
pub enum SqliteStoreError {
5+
#[error("database migration is not supported")]
6+
MigrationConflict,
7+
#[error("data store error: {0}")]
8+
Db(#[from] sqlx::Error),
9+
}
10+
11+
impl StoreError for SqliteStoreError {}

presage-store-sqlite/src/lib.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#![allow(warnings)]
2+
3+
use std::path::Path;
4+
5+
use presage::{
6+
model::identity::OnNewIdentity,
7+
store::{StateStore, Store},
8+
};
9+
use protocol::SqliteProtocolStore;
10+
use sqlx::{sqlite::SqliteConnectOptions, SqlitePool};
11+
12+
mod content;
13+
mod error;
14+
mod protocol;
15+
16+
pub use error::SqliteStoreError;
17+
18+
#[derive(Debug, Clone)]
19+
pub struct SqliteStore {
20+
db: SqlitePool,
21+
/// Whether to trust new identities automatically (for instance, when a somebody's phone has changed)
22+
trust_new_identities: OnNewIdentity,
23+
}
24+
25+
impl SqliteStore {
26+
pub async fn open(
27+
db_path: impl AsRef<Path>,
28+
trust_new_identities: OnNewIdentity,
29+
) -> Result<Self, SqliteStoreError> {
30+
let connect_options = SqliteConnectOptions::new().filename(db_path);
31+
let pool = SqlitePool::connect_with(connect_options).await?;
32+
33+
Ok(Self {
34+
db: pool,
35+
trust_new_identities,
36+
})
37+
}
38+
}
39+
40+
impl Store for SqliteStore {
41+
type Error = SqliteStoreError;
42+
43+
type AciStore = SqliteProtocolStore;
44+
45+
type PniStore = SqliteProtocolStore;
46+
47+
async fn clear(&mut self) -> Result<(), SqliteStoreError> {
48+
todo!()
49+
}
50+
51+
fn aci_protocol_store(&self) -> Self::AciStore {
52+
SqliteProtocolStore {
53+
store: self.clone(),
54+
}
55+
}
56+
57+
fn pni_protocol_store(&self) -> Self::PniStore {
58+
SqliteProtocolStore {
59+
store: self.clone(),
60+
}
61+
}
62+
}
63+
64+
impl StateStore for SqliteStore {
65+
type StateStoreError = SqliteStoreError;
66+
67+
async fn load_registration_data(
68+
&self,
69+
) -> Result<Option<presage::manager::RegistrationData>, Self::StateStoreError> {
70+
todo!()
71+
}
72+
73+
async fn set_aci_identity_key_pair(
74+
&self,
75+
key_pair: presage::libsignal_service::protocol::IdentityKeyPair,
76+
) -> Result<(), Self::StateStoreError> {
77+
todo!()
78+
}
79+
80+
async fn set_pni_identity_key_pair(
81+
&self,
82+
key_pair: presage::libsignal_service::protocol::IdentityKeyPair,
83+
) -> Result<(), Self::StateStoreError> {
84+
todo!()
85+
}
86+
87+
async fn save_registration_data(
88+
&mut self,
89+
state: &presage::manager::RegistrationData,
90+
) -> Result<(), Self::StateStoreError> {
91+
todo!()
92+
}
93+
94+
async fn is_registered(&self) -> bool {
95+
todo!()
96+
}
97+
98+
async fn clear_registration(&mut self) -> Result<(), Self::StateStoreError> {
99+
todo!()
100+
}
101+
}

0 commit comments

Comments
 (0)