Skip to content

Commit

Permalink
Make blinded_msgreqs flag tri-state
Browse files Browse the repository at this point in the history
Can be on, off, or unset.  This allows Session clients to distinguish
between default or explicitly set (and thus avoids edge cases on setting
a default during upgrade).
  • Loading branch information
jagerman committed Aug 11, 2023
1 parent 6a71c96 commit e3ccf29
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
14 changes: 8 additions & 6 deletions include/session/config/user_profile.h
Expand Up @@ -215,7 +215,7 @@ LIBSESSION_EXPORT void user_profile_set_nts_expiry(config_object* conf, int expi
///
/// Declaration:
/// ```cpp
/// BOOL user_profile_get_blinded_msgreqs(
/// INT user_profile_get_blinded_msgreqs(
/// [in] const config_object* conf
/// );
/// ```
Expand All @@ -224,18 +224,20 @@ LIBSESSION_EXPORT void user_profile_set_nts_expiry(config_object* conf, int expi
/// - `conf` -- [in] Pointer to the config object
///
/// Outputs:
/// - `bool` -- Returns true if blinded message requests are enabled, false otherwise.
LIBSESSION_EXPORT bool user_profile_get_blinded_msgreqs(const config_object* conf);
/// - `int` -- Will be -1 if the config does not have the value explicitly set, 0 if the setting is
/// explicitly disabled, and 1 if the setting is explicitly enabled.
LIBSESSION_EXPORT int user_profile_get_blinded_msgreqs(const config_object* conf);

/// API: user_profile/user_profile_set_blinded_msgreqs
///
/// Sets whether blinded message requests should be retrieved from SOGS servers.
/// Sets whether blinded message requests should be retrieved from SOGS servers. Set to 1 (or any
/// positive value) to enable; 0 to disable; and -1 to clear the setting.
///
/// Declaration:
/// ```cpp
/// VOID user_profile_set_blinded_msgreqs(
/// [in] config_object* conf,
/// [in] bool enabled
/// [in] int enabled
/// );
/// ```
///
Expand All @@ -245,7 +247,7 @@ LIBSESSION_EXPORT bool user_profile_get_blinded_msgreqs(const config_object* con
///
/// Outputs:
/// - `void` -- Returns Nothing
LIBSESSION_EXPORT void user_profile_set_blinded_msgreqs(config_object* conf, bool enabled);
LIBSESSION_EXPORT void user_profile_set_blinded_msgreqs(config_object* conf, int enabled);

#ifdef __cplusplus
} // extern "C"
Expand Down
29 changes: 20 additions & 9 deletions include/session/config/user_profile.hpp
Expand Up @@ -2,6 +2,7 @@

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

#include "base.hpp"
Expand All @@ -20,7 +21,9 @@ using namespace std::literals;
/// + - 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.
/// M - set to 1 if blinded message request retrieval is *disabled*, omitted if enabled.
/// M - set to 1 if blinded message request retrieval is enabled, 0 if retrieval is *disabled*, and
/// omitted if the setting has not been explicitly set (or has been explicitly cleared for some
/// reason).

class UserProfile final : public ConfigBase {

Expand Down Expand Up @@ -158,24 +161,32 @@ class UserProfile final : public ConfigBase {

/// API: user_profile/UserProfile::get_blinded_msgreqs
///
/// Returns true if blinded message requests (i.e. from SOGS servers you are connected to) are
/// enabled; if disabled then the Session client should not poll for such incoming message
/// requests.
/// Accesses whether or not blinded message requests are enabled for the client. Can have three
/// values:
///
/// - std::nullopt -- the value has not been given an explicit value so the client should use
/// its default.
/// - true -- the value is explicitly enabled (i.e. user wants blinded message requests)
/// - false -- the value is explicitly disabled (i.e. user disabled blinded message requests)
///
/// Inputs: None
///
/// Outputs:
/// - `bool` - true if blinded message requests are enabled, false otherwise.
bool get_blinded_msgreqs() const;
/// - `std::optional<bool>` - true/false if blinded message requests are enabled or disabled;
/// `std::nullopt` if the option has not been set either way.
std::optional<bool> get_blinded_msgreqs() const;

/// API: user_profile/UserProfile::set_blinded_msgreqs
///
/// Sets whether blinded message requests (i.e. from SOGS servers you are connected to) should
/// be enabled or not.
/// be enabled or not. This is typically invoked with either `true` or `false`, but can also be
/// called with `std::nullopt` to explicitly clear the value.
///
/// Inputs:
/// - `enabled` -- true if blinded message requests should be retrieved, false otherwise.
void set_blinded_msgreqs(bool enabled);
/// - `enabled` -- true if blinded message requests should be retrieved, false if they should
/// not, and `std::nullopt` to drop the setting from the config (and thus use the client's
/// default).
void set_blinded_msgreqs(std::optional<bool> enabled);
};

} // namespace session::config
28 changes: 18 additions & 10 deletions src/config/user_profile.cpp
Expand Up @@ -127,20 +127,28 @@ LIBSESSION_C_API void user_profile_set_nts_expiry(config_object* conf, int expir
unbox<UserProfile>(conf)->set_nts_expiry(std::max(0, expiry) * 1s);
}

void UserProfile::set_blinded_msgreqs(bool enabled) {
set_flag(data["M"], !enabled);
void UserProfile::set_blinded_msgreqs(std::optional<bool> value) {
if (!value)
data["M"].erase();
else
data["M"] = static_cast<int>(*value);
}

bool UserProfile::get_blinded_msgreqs() const {
if (auto* M = data["M"].integer(); M && *M > 0)
return false;
return true;
std::optional<bool> UserProfile::get_blinded_msgreqs() const {
if (auto* M = data["M"].integer(); M)
return static_cast<bool>(*M);
return std::nullopt;
}

LIBSESSION_C_API bool user_profile_get_blinded_msgreqs(const config_object* conf) {
return unbox<UserProfile>(conf)->get_blinded_msgreqs();
LIBSESSION_C_API int user_profile_get_blinded_msgreqs(const config_object* conf) {
if (auto opt = unbox<UserProfile>(conf)->get_blinded_msgreqs())
return static_cast<int>(*opt);
return -1;
}

LIBSESSION_C_API void user_profile_set_blinded_msgreqs(config_object* conf, bool enabled) {
unbox<UserProfile>(conf)->set_blinded_msgreqs(enabled);
LIBSESSION_C_API void user_profile_set_blinded_msgreqs(config_object* conf, int enabled) {
std::optional<bool> val;
if (enabled >= 0)
val = static_cast<bool>(enabled);
unbox<UserProfile>(conf)->set_blinded_msgreqs(std::move(val));
}
14 changes: 9 additions & 5 deletions tests/test_config_userprofile.cpp
Expand Up @@ -245,9 +245,13 @@ TEST_CASE("user profile C API", "[config][user_profile][c]") {
user_profile_set_nts_expiry(conf2, 86400);
CHECK(user_profile_get_nts_expiry(conf2) == 86400);

CHECK(user_profile_get_blinded_msgreqs(conf2));
user_profile_set_blinded_msgreqs(conf2, false);
CHECK_FALSE(user_profile_get_blinded_msgreqs(conf2));
CHECK(user_profile_get_blinded_msgreqs(conf2) == -1);
user_profile_set_blinded_msgreqs(conf2, 0);
CHECK(user_profile_get_blinded_msgreqs(conf2) == 0);
user_profile_set_blinded_msgreqs(conf2, -1);
CHECK(user_profile_get_blinded_msgreqs(conf2) == -1);
user_profile_set_blinded_msgreqs(conf2, 1);
CHECK(user_profile_get_blinded_msgreqs(conf2) == 1);

// Both have changes, so push need a push
CHECK(config_needs_push(conf));
Expand Down Expand Up @@ -322,8 +326,8 @@ TEST_CASE("user profile C API", "[config][user_profile][c]") {
CHECK(user_profile_get_nts_priority(conf2) == 9);
CHECK(user_profile_get_nts_expiry(conf) == 86400);
CHECK(user_profile_get_nts_expiry(conf2) == 86400);
CHECK_FALSE(user_profile_get_blinded_msgreqs(conf));
CHECK_FALSE(user_profile_get_blinded_msgreqs(conf2));
CHECK(user_profile_get_blinded_msgreqs(conf) == 1);
CHECK(user_profile_get_blinded_msgreqs(conf2) == 1);

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

0 comments on commit e3ccf29

Please sign in to comment.