Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/session/config/contacts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ struct contact_info {
// conversation.
notify_mode notifications = notify_mode::defaulted;
int64_t mute_until = 0; // If non-zero, disable notifications until the given unix timestamp
// (overriding whatever the current `notifications` value is until the
// timestamp expires).
// (seconds, overriding whatever the current `notifications` value is
// until the timestamp expires).
expiration_mode exp_mode = expiration_mode::none; // The expiry time; none if not expiring.
std::chrono::seconds exp_timer{0}; // The expiration timer (in seconds)
int64_t created = 0; // Unix timestamp when this contact was added
int64_t created = 0; // Unix timestamp (seconds) when this contact was added

explicit contact_info(std::string sid);

Expand Down
8 changes: 4 additions & 4 deletions include/session/config/groups/info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ class Info : public ConfigBase {
/// the closed group history with a timestamp earlier than this value. Returns nullopt if no
/// delete-before timestamp is set.
///
/// The given value is not checked for sanity (e.g. if you pass milliseconds it will be
/// interpreted as deleting everything for the next 50000+ years). Be careful!
/// The given value is checked for sanity (e.g. if you pass milliseconds it will be
/// interpreted as such)
///
/// Inputs:
/// - `timestamp` -- the new unix timestamp before which clients should delete messages. Pass 0
Expand All @@ -267,8 +267,8 @@ class Info : public ConfigBase {
/// that) from any messages older than the given timestamp. Returns nullopt if no
/// delete-attachments-before timestamp is set.
///
/// The given value is not checked for sanity (e.g. if you pass milliseconds it will be
/// interpreted as deleting all attachments for the next 50000+ years). Be careful!
/// The given value is checked for sanity (e.g. if you pass milliseconds it will be
/// interpreted as such)
///
/// Inputs:
/// - `timestamp` -- the new unix timestamp before which clients should delete attachments. Pass
Expand Down
18 changes: 9 additions & 9 deletions include/session/config/user_groups.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ typedef struct ugroups_legacy_group_info {
int64_t disappearing_timer; // Seconds. 0 == disabled.
int priority; // pinned conversation priority; 0 = unpinned, negative = hidden, positive =
// pinned (with higher meaning pinned higher).
int64_t joined_at; // unix timestamp when joined (or re-joined)
int64_t joined_at; // unix timestamp (seconds) when joined (or re-joined)
CONVO_NOTIFY_MODE notifications; // When the user wants notifications
int64_t mute_until; // Mute notifications until this timestamp (overrides `notifications`
// setting until the timestamp)
int64_t mute_until; // Mute notifications until this timestamp (seconds, overrides
// `notifications` setting until the timestamp)

bool invited; // True if this is in the invite-but-not-accepted state.

Expand All @@ -60,10 +60,10 @@ typedef struct ugroups_group_info {

int priority; // pinned conversation priority; 0 = unpinned, negative = hidden, positive =
// pinned (with higher meaning pinned higher).
int64_t joined_at; // unix timestamp when joined (or re-joined)
int64_t joined_at; // unix timestamp (seconds) when joined (or re-joined)
CONVO_NOTIFY_MODE notifications; // When the user wants notifications
int64_t mute_until; // Mute notifications until this timestamp (overrides `notifications`
// setting until the timestamp)
int64_t mute_until; // Mute notifications until this timestamp (seconds, overrides
// `notifications` setting until the timestamp)

bool invited; // True if this is in the invite-but-not-accepted state.

Expand All @@ -81,10 +81,10 @@ typedef struct ugroups_community_info {

int priority; // pinned conversation priority; 0 = unpinned, negative = hidden, positive =
// pinned (with higher meaning pinned higher).
int64_t joined_at; // unix timestamp when joined (or re-joined)
int64_t joined_at; // unix timestamp (seconds) when joined (or re-joined)
CONVO_NOTIFY_MODE notifications; // When the user wants notifications
int64_t mute_until; // Mute notifications until this timestamp (overrides `notifications`
// setting until the timestamp)
int64_t mute_until; // Mute notifications until this timestamp (seconds, overrides
// `notifications` setting until the timestamp)

bool invited; // True if this is in the invite-but-not-accepted state.

Expand Down
14 changes: 9 additions & 5 deletions include/session/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ inline ustring_view to_unsigned_sv(std::basic_string_view<std::byte> v) {
return {to_unsigned(v.data()), v.size()};
}
inline ustring_view to_unsigned_sv(ustring_view v) {
return v; // no-op, but helps with template metaprogamming
return v; // no-op, but helps with template metaprogramming
}
inline std::string_view from_unsigned_sv(ustring_view v) {
return {from_unsigned(v.data()), v.size()};
Expand All @@ -66,10 +66,6 @@ inline std::basic_string_view<Char> to_sv(const std::array<Char, N>& v) {
return {v.data(), N};
}

inline uint64_t get_timestamp() {
return std::chrono::steady_clock::now().time_since_epoch().count();
}

/// Returns true if the first string is equal to the second string, compared case-insensitively.
inline bool string_iequal(std::string_view s1, std::string_view s2) {
return std::equal(s1.begin(), s1.end(), s2.begin(), s2.end(), [](char a, char b) {
Expand Down Expand Up @@ -205,4 +201,12 @@ inline std::string utf8_truncate(std::string val, size_t n) {
return val;
}

// Helper function to transform a timestamp provided in seconds, milliseconds or microseconds to
// seconds
inline int64_t to_epoch_seconds(int64_t timestamp) {
return timestamp > 9'000'000'000'000 ? timestamp / 1'000'000
: timestamp > 9'000'000'000 ? timestamp / 1'000
: timestamp;
}

} // namespace session
18 changes: 9 additions & 9 deletions src/config/contacts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void contact_info::load(const dict& info_dict) {
} else {
notifications = notify_mode::defaulted;
}
mute_until = maybe_int(info_dict, "!").value_or(0);
mute_until = to_epoch_seconds(maybe_int(info_dict, "!").value_or(0));

int exp_mode_ = maybe_int(info_dict, "e").value_or(0);
if (exp_mode_ >= static_cast<int>(expiration_mode::none) &&
Expand All @@ -118,7 +118,7 @@ void contact_info::load(const dict& info_dict) {
}
}

created = maybe_int(info_dict, "j").value_or(0);
created = to_epoch_seconds(maybe_int(info_dict, "j").value_or(0));
}

void contact_info::into(contacts_contact& c) const {
Expand All @@ -136,12 +136,12 @@ void contact_info::into(contacts_contact& c) const {
c.blocked = blocked;
c.priority = priority;
c.notifications = static_cast<CONVO_NOTIFY_MODE>(notifications);
c.mute_until = mute_until;
c.mute_until = to_epoch_seconds(mute_until);
c.exp_mode = static_cast<CONVO_EXPIRATION_MODE>(exp_mode);
c.exp_seconds = exp_timer.count();
if (c.exp_seconds <= 0 && c.exp_mode != CONVO_EXPIRATION_NONE)
c.exp_mode = CONVO_EXPIRATION_NONE;
c.created = created;
c.created = to_epoch_seconds(created);
}

contact_info::contact_info(const contacts_contact& c) : session_id{c.session_id, 66} {
Expand All @@ -159,12 +159,12 @@ contact_info::contact_info(const contacts_contact& c) : session_id{c.session_id,
blocked = c.blocked;
priority = c.priority;
notifications = static_cast<notify_mode>(c.notifications);
mute_until = c.mute_until;
mute_until = to_epoch_seconds(c.mute_until);
exp_mode = static_cast<expiration_mode>(c.exp_mode);
exp_timer = exp_mode == expiration_mode::none ? 0s : std::chrono::seconds{c.exp_seconds};
if (exp_timer <= 0s && exp_mode != expiration_mode::none)
exp_mode = expiration_mode::none;
created = c.created;
created = to_epoch_seconds(c.created);
}

std::optional<contact_info> Contacts::get(std::string_view pubkey_hex) const {
Expand Down Expand Up @@ -240,7 +240,7 @@ void Contacts::set(const contact_info& contact) {
if (notify == notify_mode::mentions_only)
notify = notify_mode::all;
set_positive_int(info["@"], static_cast<int>(notify));
set_positive_int(info["!"], contact.mute_until);
set_positive_int(info["!"], to_epoch_seconds(contact.mute_until));

set_pair_if(
contact.exp_mode != expiration_mode::none && contact.exp_timer > 0s,
Expand All @@ -249,7 +249,7 @@ void Contacts::set(const contact_info& contact) {
info["E"],
contact.exp_timer.count());

set_positive_int(info["j"], contact.created);
set_positive_int(info["j"], to_epoch_seconds(contact.created));
}

LIBSESSION_C_API void contacts_set(config_object* conf, const contacts_contact* contact) {
Expand Down Expand Up @@ -314,7 +314,7 @@ void Contacts::set_expiry(

void Contacts::set_created(std::string_view session_id, int64_t timestamp) {
auto c = get_or_construct(session_id);
c.created = timestamp;
c.created = to_epoch_seconds(timestamp);
set(c);
}

Expand Down
12 changes: 6 additions & 6 deletions src/config/groups/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,32 @@ void Info::set_expiry_timer(std::chrono::seconds expiration_timer) {
}

void Info::set_created(int64_t timestamp) {
set_positive_int(data["c"], timestamp);
set_positive_int(data["c"], to_epoch_seconds(timestamp));
}

std::optional<int64_t> Info::get_created() const {
if (auto* ts = data["c"].integer())
return *ts;
return to_epoch_seconds(*ts);
return std::nullopt;
}

void Info::set_delete_before(int64_t timestamp) {
set_positive_int(data["d"], timestamp);
set_positive_int(data["d"], to_epoch_seconds(timestamp));
}

std::optional<int64_t> Info::get_delete_before() const {
if (auto* ts = data["d"].integer())
return *ts;
return to_epoch_seconds(*ts);
return std::nullopt;
}

void Info::set_delete_attach_before(int64_t timestamp) {
set_positive_int(data["D"], timestamp);
set_positive_int(data["D"], to_epoch_seconds(timestamp));
}

std::optional<int64_t> Info::get_delete_attach_before() const {
if (auto* ts = data["D"].integer())
return *ts;
return to_epoch_seconds(*ts);
return std::nullopt;
}

Expand Down
16 changes: 8 additions & 8 deletions src/config/user_groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ namespace session::config {
template <typename T>
static void base_into(const base_group_info& self, T& c) {
c.priority = self.priority;
c.joined_at = self.joined_at;
c.joined_at = to_epoch_seconds(self.joined_at);
c.notifications = static_cast<CONVO_NOTIFY_MODE>(self.notifications);
c.mute_until = self.mute_until;
c.mute_until = to_epoch_seconds(self.mute_until);
c.invited = self.invited;
}

template <typename T>
static void base_from(base_group_info& self, const T& c) {
self.priority = c.priority;
self.joined_at = c.joined_at;
self.joined_at = to_epoch_seconds(c.joined_at);
self.notifications = static_cast<notify_mode>(c.notifications);
self.mute_until = c.mute_until;
self.mute_until = to_epoch_seconds(c.mute_until);
self.invited = c.invited;
}

Expand Down Expand Up @@ -129,15 +129,15 @@ void legacy_group_info::into(ugroups_legacy_group_info& c) && {

void base_group_info::load(const dict& info_dict) {
priority = maybe_int(info_dict, "+").value_or(0);
joined_at = std::max<int64_t>(0, maybe_int(info_dict, "j").value_or(0));
joined_at = to_epoch_seconds(std::max<int64_t>(0, maybe_int(info_dict, "j").value_or(0)));

int notify = maybe_int(info_dict, "@").value_or(0);
if (notify >= 0 && notify <= 3)
notifications = static_cast<notify_mode>(notify);
else
notifications = notify_mode::defaulted;

mute_until = maybe_int(info_dict, "!").value_or(0);
mute_until = to_epoch_seconds(maybe_int(info_dict, "!").value_or(0));

invited = maybe_int(info_dict, "i").value_or(0);
}
Expand Down Expand Up @@ -407,9 +407,9 @@ void UserGroups::set(const community_info& c) {

void UserGroups::set_base(const base_group_info& bg, DictFieldProxy& info) const {
set_nonzero_int(info["+"], bg.priority);
set_positive_int(info["j"], bg.joined_at);
set_positive_int(info["j"], to_epoch_seconds(bg.joined_at));
set_positive_int(info["@"], static_cast<int>(bg.notifications));
set_positive_int(info["!"], bg.mute_until);
set_positive_int(info["!"], to_epoch_seconds(bg.mute_until));
set_flag(info["i"], bg.invited);
// We don't set n here because it's subtly different in the three group types
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config_contacts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ TEST_CASE("Contacts", "[config][contacts]") {
c.set_nickname("Joey");
c.approved = true;
c.approved_me = true;
c.created = created_ts;
c.created = created_ts * 1'000;
c.notifications = session::config::notify_mode::all;
c.mute_until = now + 1800;
c.mute_until = (now + 1800) * 1'000'000;

contacts.set(c);

Expand Down
Loading