Skip to content

Commit 0bcc986

Browse files
committed
blinded_contact_info: type-safe timestamp
1 parent 5c47b52 commit 0bcc986

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

include/session/config/contacts.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ struct blinded_contact_info {
125125
std::string name;
126126
profile_pic profile_picture;
127127
bool legacy_blinding;
128-
int64_t created = 0; // Unix timestamp (seconds) when this contact was added
128+
std::chrono::sys_seconds created{}; // Unix timestamp (seconds) when this contact was added
129129

130130
blinded_contact_info() = default;
131131
explicit blinded_contact_info(

src/config/contacts.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ blinded_contact_info::blinded_contact_info(
317317
}
318318

319319
void blinded_contact_info::load(const dict& info_dict) {
320-
name = maybe_string(info_dict, "n").value_or("");
320+
name = string_or_empty(info_dict, "n");
321321

322322
auto url = maybe_string(info_dict, "p");
323323
auto key = maybe_vector(info_dict, "q");
@@ -327,8 +327,8 @@ void blinded_contact_info::load(const dict& info_dict) {
327327
} else {
328328
profile_picture.clear();
329329
}
330-
legacy_blinding = maybe_int(info_dict, "y").value_or(0);
331-
created = to_epoch_seconds(maybe_int(info_dict, "j").value_or(0));
330+
legacy_blinding = int_or_0(info_dict, "y");
331+
created = ts_or_epoch(info_dict, "j");
332332
}
333333

334334
void blinded_contact_info::into(contacts_blinded_contact& c) const {
@@ -346,7 +346,7 @@ void blinded_contact_info::into(contacts_blinded_contact& c) const {
346346
copy_c_str(c.profile_pic.url, "");
347347
}
348348
c.legacy_blinding = legacy_blinding;
349-
c.created = to_epoch_seconds(created);
349+
c.created = created.time_since_epoch().count();
350350
}
351351

352352
blinded_contact_info::blinded_contact_info(const contacts_blinded_contact& c) {
@@ -359,7 +359,7 @@ blinded_contact_info::blinded_contact_info(const contacts_blinded_contact& c) {
359359
profile_picture.key.assign(c.profile_pic.key, c.profile_pic.key + 32);
360360
}
361361
legacy_blinding = c.legacy_blinding;
362-
created = to_epoch_seconds(c.created);
362+
created = to_sys_seconds(c.created);
363363
}
364364

365365
const std::string blinded_contact_info::session_id() const {
@@ -472,7 +472,7 @@ void Contacts::set_blinded(const blinded_contact_info& bc) {
472472
bc.profile_picture.key);
473473

474474
set_positive_int(info["y"], bc.legacy_blinding);
475-
set_positive_int(info["j"], to_epoch_seconds(bc.created));
475+
set_ts(info["j"], bc.created);
476476
}
477477

478478
bool Contacts::erase_blinded(
@@ -717,4 +717,4 @@ LIBSESSION_C_API void contacts_iterator_advance(contacts_iterator* it) {
717717
++*static_cast<Contacts::iterator*>(it->_internals);
718718
}
719719

720-
} // extern "C"
720+
} // extern "C"

tests/test_config_contacts.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -928,14 +928,14 @@ TEST_CASE("Contacts", "[config][blinded_contacts]") {
928928
CHECK(c.name.empty());
929929
CHECK_FALSE(c.profile_picture);
930930
CHECK(c.legacy_blinding);
931-
CHECK(c.created == 0);
931+
CHECK(c.created.time_since_epoch() == 0s);
932932

933933
CHECK_FALSE(contacts.needs_push());
934934
CHECK_FALSE(contacts.needs_dump());
935935
CHECK(std::get<seqno_t>(contacts.push()) == 0);
936936

937937
c.set_name("Joe");
938-
c.created = created_ts * 1'000;
938+
c.created = session::to_sys_seconds(created_ts * 1'000);
939939
contacts.set_blinded(c);
940940

941941
REQUIRE(contacts.get_blinded(definitely_real_id, true).has_value());
@@ -970,7 +970,7 @@ TEST_CASE("Contacts", "[config][blinded_contacts]") {
970970
REQUIRE(x);
971971
CHECK(x->name == "Joe");
972972
CHECK_FALSE(x->profile_picture);
973-
CHECK(x->created == created_ts);
973+
CHECK(x->created.time_since_epoch() == created_ts * 1s);
974974
CHECK(x->legacy_blinding == true);
975975

976976
auto another_id = "251111111111111111111111111111111111111111111111111111111111111111"sv;
@@ -1094,4 +1094,4 @@ TEST_CASE("Contacts", "[config][blinded_contacts]") {
10941094
CHECK(names[1] == "Name 3");
10951095
CHECK_FALSE(legacy_blindings[0]);
10961096
CHECK(legacy_blindings[1]);
1097-
}
1097+
}

0 commit comments

Comments
 (0)