Skip to content

Commit

Permalink
Merge pull request #46 from jagerman/blinded-msg-requests
Browse files Browse the repository at this point in the history
Add blinded msg requests option to UserProfile
  • Loading branch information
jagerman authored Aug 22, 2023
2 parents ad36b42 + e3ccf29 commit 9895586
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()


project(libsession-util
VERSION 1.0.90
VERSION 1.1.0
DESCRIPTION "Session client utility library"
LANGUAGES ${LANGS})

Expand Down
2 changes: 1 addition & 1 deletion external/oxen-encoding
41 changes: 41 additions & 0 deletions include/session/config/user_profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,47 @@ LIBSESSION_EXPORT int user_profile_get_nts_expiry(const config_object* conf);
/// - `expiry` -- [in] Integer of the expiry timer in seconds
LIBSESSION_EXPORT void user_profile_set_nts_expiry(config_object* conf, int expiry);

/// API: user_profile/user_profile_get_blinded_msgreqs
///
/// Returns true if blinded message requests should be retrieved (from SOGS servers), false if they
/// should be ignored.
///
/// Declaration:
/// ```cpp
/// INT user_profile_get_blinded_msgreqs(
/// [in] const config_object* conf
/// );
/// ```
///
/// Inputs:
/// - `conf` -- [in] Pointer to the config object
///
/// Outputs:
/// - `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. 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] int enabled
/// );
/// ```
///
/// Inputs:
/// - `conf` -- [in] Pointer to the config object
/// - `enabled` -- [in] true if they should be enabled, false if disabled
///
/// Outputs:
/// - `void` -- Returns Nothing
LIBSESSION_EXPORT void user_profile_set_blinded_msgreqs(config_object* conf, int enabled);

#ifdef __cplusplus
} // extern "C"
#endif
33 changes: 33 additions & 0 deletions include/session/config/user_profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

#include "base.hpp"
Expand All @@ -20,6 +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 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 @@ -154,6 +158,35 @@ class UserProfile final : public ConfigBase {
/// Inputs:
/// - `timer` -- Default to 0 seconds, will set the expiry timer
void set_nts_expiry(std::chrono::seconds timer = 0s);

/// API: user_profile/UserProfile::get_blinded_msgreqs
///
/// 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:
/// - `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. 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 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
26 changes: 26 additions & 0 deletions src/config/user_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,29 @@ LIBSESSION_C_API int user_profile_get_nts_expiry(const config_object* conf) {
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);
}

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

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 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, int enabled) {
std::optional<bool> val;
if (enabled >= 0)
val = static_cast<bool>(enabled);
unbox<UserProfile>(conf)->set_blinded_msgreqs(std::move(val));
}
2 changes: 1 addition & 1 deletion tests/Catch2
Submodule Catch2 updated 460 files
10 changes: 10 additions & 0 deletions tests/test_config_userprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ 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) == -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));
CHECK(config_needs_push(conf2));
Expand Down Expand Up @@ -318,6 +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(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 9895586

Please sign in to comment.