Skip to content

Commit b42905c

Browse files
mpretty-cyrojagerman
authored andcommitted
Updated blinded_contact_info to not inherit from community
1 parent fb95468 commit b42905c

2 files changed

Lines changed: 37 additions & 13 deletions

File tree

include/session/config/contacts.hpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,16 @@ struct contact_info {
118118
void load(const dict& info_dict);
119119
};
120120

121-
struct blinded_contact_info : community {
122-
using community::community;
121+
struct blinded_contact_info {
122+
community comm;
123123

124124
const std::string session_id() const; // in hex
125125
std::string name;
126126
profile_pic profile_picture;
127127
bool legacy_blinding;
128128
int64_t created = 0; // Unix timestamp (seconds) when this contact was added
129129

130+
blinded_contact_info() = default;
130131
explicit blinded_contact_info(
131132
std::string_view base_url,
132133
std::string_view blinded_id,
@@ -153,6 +154,13 @@ struct blinded_contact_info : community {
153154
/// - `name` -- Name to assign to the contact
154155
void set_name(std::string name);
155156

157+
/// These functions are here so we can use the `comm_iterator_helper` for loading data
158+
/// into this struct
159+
void set_base_url(std::string_view base_url);
160+
void set_room(std::string_view room);
161+
void set_pubkey(std::span<const unsigned char> pubkey);
162+
void set_pubkey(std::string_view pubkey);
163+
156164
private:
157165
friend class Contacts;
158166
friend struct session::config::comm_iterator_helper;

src/config/contacts.cpp

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ blinded_contact_info::blinded_contact_info(
169169
std::string_view blinded_id,
170170
std::span<const unsigned char> pubkey,
171171
bool legacy_blinding) :
172-
legacy_blinding{legacy_blinding},
173-
community(std::move(base_url), blinded_id.substr(2), std::move(pubkey)) {
172+
comm{community(std::move(base_url), blinded_id.substr(2), std::move(pubkey))},
173+
legacy_blinding{legacy_blinding} {
174174
check_session_id(blinded_id, legacy_blinding ? "15" : "25");
175175
}
176176

177177
const std::string blinded_contact_info::session_id() const {
178-
return "{}{}"_format(legacy_blinding ? "15" : "25", room());
178+
return "{}{}"_format(legacy_blinding ? "15" : "25", comm.room());
179179
}
180180

181181
void blinded_contact_info::set_name(std::string n) {
@@ -185,6 +185,22 @@ void blinded_contact_info::set_name(std::string n) {
185185
name = std::move(n);
186186
}
187187

188+
void blinded_contact_info::set_base_url(std::string_view base_url) {
189+
comm.set_base_url(base_url);
190+
}
191+
192+
void blinded_contact_info::set_room(std::string_view room) {
193+
comm.set_room(room);
194+
}
195+
196+
void blinded_contact_info::set_pubkey(std::span<const unsigned char> pubkey) {
197+
comm.set_pubkey(pubkey);
198+
}
199+
200+
void blinded_contact_info::set_pubkey(std::string_view pubkey) {
201+
comm.set_pubkey(pubkey);
202+
}
203+
188204
void blinded_contact_info::load(const dict& info_dict) {
189205
name = maybe_string(info_dict, "n").value_or("");
190206

@@ -201,12 +217,12 @@ void blinded_contact_info::load(const dict& info_dict) {
201217
}
202218

203219
void blinded_contact_info::into(contacts_blinded_contact& c) const {
204-
copy_c_str(c.base_url, base_url());
220+
copy_c_str(c.base_url, comm.base_url());
205221
c.session_id[0] = (legacy_blinding ? '1' : '2');
206222
c.session_id[1] = '5';
207223
std::memcpy(c.session_id + 2, session_id().data(), 64);
208224
c.session_id[66] = '\0';
209-
std::memcpy(c.pubkey, pubkey().data(), 32);
225+
std::memcpy(c.pubkey, comm.pubkey().data(), 32);
210226
copy_c_str(c.name, name);
211227
if (profile_picture) {
212228
copy_c_str(c.profile_pic.url, profile_picture.url);
@@ -218,8 +234,8 @@ void blinded_contact_info::into(contacts_blinded_contact& c) const {
218234
c.created = to_epoch_seconds(created);
219235
}
220236

221-
blinded_contact_info::blinded_contact_info(const contacts_blinded_contact& c) :
222-
community(c.base_url, {c.session_id + 2, 64}, c.pubkey) {
237+
blinded_contact_info::blinded_contact_info(const contacts_blinded_contact& c) {
238+
comm = community(c.base_url, {c.session_id + 2, 64}, c.pubkey);
223239
assert(std::strlen(c.name) <= contact_info::MAX_NAME_LENGTH);
224240
name = c.name;
225241
assert(std::strlen(c.profile_pic.url) <= profile_pic::MAX_URL_LENGTH);
@@ -374,14 +390,14 @@ size_t Contacts::size() const {
374390

375391
ConfigBase::DictFieldProxy Contacts::blinded_contact_field(
376392
const blinded_contact_info& bc, std::span<const unsigned char>* get_pubkey) const {
377-
auto record = data["b"][bc.base_url()];
393+
auto record = data["b"][bc.comm.base_url()];
378394
if (get_pubkey) {
379395
auto pkrec = record["#"];
380396
if (auto pk = pkrec.string_view_or(""); pk.size() == 32)
381397
*get_pubkey = std::span<const unsigned char>{
382398
reinterpret_cast<const unsigned char*>(pk.data()), pk.size()};
383399
}
384-
return record["R"][bc.room()]; // The `room` value is the blinded id without the prefix
400+
return record["R"][bc.comm.room()]; // The `room` value is the blinded id without the prefix
385401
}
386402

387403
using any_blinded_contact = std::variant<blinded_contact_info>;
@@ -395,7 +411,7 @@ std::optional<blinded_contact_info> Contacts::get_blinded(
395411
std::shared_ptr<any_blinded_contact> val;
396412

397413
while (!comm.done()) {
398-
if (comm.load<blinded_contact_info>(val)) // TODO: This is untested
414+
if (comm.load<blinded_contact_info>(val))
399415
if (auto* ptr = std::get_if<blinded_contact_info>(val.get());
400416
ptr && ptr->session_id() == pubkey_hex)
401417
return *ptr;
@@ -425,7 +441,7 @@ std::vector<blinded_contact_info> Contacts::blinded_contacts() const {
425441
}
426442

427443
bool Contacts::set_blinded_contact(const blinded_contact_info& bc) {
428-
data["b"][bc.base_url()]["#"] = bc.pubkey();
444+
data["b"][bc.comm.base_url()]["#"] = bc.comm.pubkey();
429445
auto info = blinded_contact_field(bc); // data["b"][base]["R"][bc_session_id_without_prefix]
430446

431447
// Always set the name, even if empty, to keep the dict from getting pruned if there are no

0 commit comments

Comments
 (0)