From c960e8612029df71d52604c8df5b291d20b3c5c4 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Mon, 29 May 2023 23:10:49 -0300 Subject: [PATCH] Add note-to-self expiry timer setting --- include/session/config/user_profile.h | 7 +++++++ include/session/config/user_profile.hpp | 12 ++++++++++++ src/config/user_profile.cpp | 18 ++++++++++++++++++ tests/test_config_userprofile.cpp | 13 ++++++++++--- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/session/config/user_profile.h b/include/session/config/user_profile.h index 36ec4c04..7cea2283 100644 --- a/include/session/config/user_profile.h +++ b/include/session/config/user_profile.h @@ -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 diff --git a/include/session/config/user_profile.hpp b/include/session/config/user_profile.hpp index 2ce3ca69..ca99bfbf 100644 --- a/include/session/config/user_profile.hpp +++ b/include/session/config/user_profile.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -9,6 +10,8 @@ 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 @@ -16,6 +19,7 @@ namespace session::config { /// 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 { @@ -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 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 diff --git a/src/config/user_profile.cpp b/src/config/user_profile.cpp index fb7d0cc4..d8d0bb03 100644 --- a/src/config/user_profile.cpp +++ b/src/config/user_profile.cpp @@ -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(conf)->set_nts_priority(priority); } + +void UserProfile::set_nts_expiry(std::chrono::seconds expiry) { + set_positive_int(data["e"], expiry.count()); +} + +std::optional 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(conf)->get_nts_expiry().value_or(0s).count(); +} + +LIBSESSION_C_API void user_profile_set_nts_expiry(config_object* conf, int expiry) { + unbox(conf)->set_nts_expiry(std::max(0, expiry) * 1s); +} diff --git a/tests/test_config_userprofile.cpp b/tests/test_config_userprofile.cpp index 08bb683d..38a87c06 100644 --- a/tests/test_config_userprofile.cpp +++ b/tests/test_config_userprofile.cpp @@ -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)! @@ -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)); @@ -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");