Skip to content

Commit

Permalink
Merge pull request #33 from jagerman/nts-expiry
Browse files Browse the repository at this point in the history
Add note-to-self expiry timer setting
  • Loading branch information
jagerman authored May 31, 2023
2 parents 97084c6 + c960e86 commit 9777b37
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
7 changes: 7 additions & 0 deletions include/session/config/user_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ LIBSESSION_EXPORT int user_profile_get_nts_priority(const config_object* conf);
// higher priority in the conversation list.
LIBSESSION_EXPORT void user_profile_set_nts_priority(config_object* conf, int priority);

// Gets the Note-to-self message expiry timer (seconds). Returns 0 if not set.
LIBSESSION_EXPORT int user_profile_get_nts_expiry(const config_object* conf);

// Sets the Note-to-self message expiry timer (seconds). Setting 0 (or negative) will clear the
// current timer.
LIBSESSION_EXPORT void user_profile_set_nts_expiry(config_object* conf, int expiry);

#ifdef __cplusplus
} // extern "C"
#endif
12 changes: 12 additions & 0 deletions include/session/config/user_profile.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <chrono>
#include <memory>
#include <session/config.hpp>

Expand All @@ -9,13 +10,16 @@

namespace session::config {

using namespace std::literals;

/// keys used in this config, either currently or in the past (so that we don't reuse):
///
/// n - user profile name
/// p - user profile url
/// q - user profile decryption key (binary)
/// + - the priority value for the "Note to Self" pseudo-conversation (higher = higher in the
/// conversation list). Omitted when 0. -1 means hidden.
/// e - the expiry timer (in seconds) for the "Note to Self" pseudo-conversation. Omitted when 0.

class UserProfile final : public ConfigBase {

Expand Down Expand Up @@ -62,6 +66,14 @@ class UserProfile final : public ConfigBase {
/// Sets the Note-to-self conversation priority. -1 for hidden, 0 for unpinned, higher for
/// pinned higher.
void set_nts_priority(int priority);

/// Returns the current Note-to-self message expiry timer, if set, or std::nullopt if there is
/// no current expiry timer set.
std::optional<std::chrono::seconds> get_nts_expiry() const;

/// Sets the Note-to-self message expiry timer. Call without arguments (or pass a zero time) to
/// disable the expiry timer.
void set_nts_expiry(std::chrono::seconds timer = 0s);
};

} // namespace session::config
18 changes: 18 additions & 0 deletions src/config/user_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,21 @@ LIBSESSION_C_API int user_profile_get_nts_priority(const config_object* conf) {
LIBSESSION_C_API void user_profile_set_nts_priority(config_object* conf, int priority) {
unbox<UserProfile>(conf)->set_nts_priority(priority);
}

void UserProfile::set_nts_expiry(std::chrono::seconds expiry) {
set_positive_int(data["e"], expiry.count());
}

std::optional<std::chrono::seconds> UserProfile::get_nts_expiry() const {
if (auto* e = data["e"].integer(); e && *e > 0)
return std::chrono::seconds{*e};
return std::nullopt;
}

LIBSESSION_C_API int user_profile_get_nts_expiry(const config_object* conf) {
return unbox<UserProfile>(conf)->get_nts_expiry().value_or(0s).count();
}

LIBSESSION_C_API void user_profile_set_nts_expiry(config_object* conf, int expiry) {
unbox<UserProfile>(conf)->set_nts_expiry(std::max(0, expiry) * 1s);
}
13 changes: 10 additions & 3 deletions tests/test_config_userprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ TEST_CASE("user profile C API", "[config][user_profile][c]") {
free(to_push);
free(to_push_decrypted);

// This should also be unset:
// These should also be unset:
auto pic = user_profile_get_pic(conf);
CHECK(strlen(pic.url) == 0);
CHECK(user_profile_get_nts_priority(conf) == 0);
CHECK(user_profile_get_nts_expiry(conf) == 0);

// Now let's go set a profile name and picture:
// Now let's go set them:
CHECK(0 == user_profile_set_name(conf, "Kallie"));
user_profile_pic p;
strcpy(p.url, "http://example.org/omg-pic-123.bmp"); // NB: length must be < sizeof(p.url)!
Expand Down Expand Up @@ -235,11 +237,14 @@ TEST_CASE("user profile C API", "[config][user_profile][c]") {
user_profile_set_name(conf, "Nibbler");
user_profile_set_name(conf2, "Raz");

// And, on conf2, we're also going to change the profile pic:
// And, on conf2, we're also going to change some other things:
strcpy(p.url, "http://new.example.com/pic");
memcpy(p.key, "qwert\0yuio1234567890123456789012", 32);
user_profile_set_pic(conf2, p);

user_profile_set_nts_expiry(conf2, 86400);
CHECK(user_profile_get_nts_expiry(conf2) == 86400);

// Both have changes, so push need a push
CHECK(config_needs_push(conf));
CHECK(config_needs_push(conf2));
Expand Down Expand Up @@ -311,6 +316,8 @@ TEST_CASE("user profile C API", "[config][user_profile][c]") {

CHECK(user_profile_get_nts_priority(conf) == 9);
CHECK(user_profile_get_nts_priority(conf2) == 9);
CHECK(user_profile_get_nts_expiry(conf) == 86400);
CHECK(user_profile_get_nts_expiry(conf2) == 86400);

config_confirm_pushed(conf, to_push->seqno, "fakehash4");
config_confirm_pushed(conf2, to_push2->seqno, "fakehash4");
Expand Down

0 comments on commit 9777b37

Please sign in to comment.