Skip to content

Commit 61e0d1a

Browse files
authored
Merge pull request #37 from session-foundation/bilb/feat-add-pro-backend-fns
fix: add proFeaturesForMessage linking
2 parents fa8daef + 56a0f57 commit 61e0d1a

File tree

14 files changed

+219
-133
lines changed

14 files changed

+219
-133
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ set(ENABLE_ONIONREQ OFF)
3737
# as it is not part of the archive. We actually don't care about it on session-desktop
3838
set(SUBMODULE_CHECK OFF)
3939

40-
file(GLOB SOURCE_FILES src/*.cpp src/groups/*.cpp src/multi_encrypt/*.cpp)
40+
file(GLOB SOURCE_FILES src/*.cpp src/groups/*.cpp )
4141

4242
add_subdirectory(libsession-util)
4343

@@ -47,7 +47,7 @@ if(MSVC)
4747
endif()
4848

4949
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
50-
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "include/" "node_modules/node-addon-api" "../../node_modules/node-addon-api" "node_modules/node-api-headers/include" "../../node_modules/node-api-headers/include")
50+
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "include/" "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/node-addon-api" "${CMAKE_CURRENT_SOURCE_DIR}/node_modules" "${CMAKE_CURRENT_SOURCE_DIR}/node_modules/node-api-headers/include")
5151

5252
set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
5353
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} ${LIBSESSION_STATIC_BUNDLE_LIBS})

include/groups/meta_group.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <napi.h>
4+
45
#include <vector>
56

67
#include "session/config/groups/info.hpp"
@@ -35,7 +36,10 @@ class MetaGroup {
3536
shared_ptr<config::groups::Keys> keys,
3637
std::vector<unsigned char> edGroupPubKey,
3738
std::optional<std::vector<unsigned char>> edGroupSecKey) :
38-
info{info}, members{members}, keys{keys}, edGroupPubKey{oxenc::to_hex(edGroupPubKey.begin(), edGroupPubKey.end())} {
39+
info{info},
40+
members{members},
41+
keys{keys},
42+
edGroupPubKey{oxenc::to_hex(edGroupPubKey.begin(), edGroupPubKey.end())} {
3943

4044
if (edGroupSecKey.has_value()) {
4145
this->edGroupSecKey = oxenc::to_hex(edGroupSecKey->begin(), edGroupSecKey->end());

include/meta/meta_base_wrapper.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <napi.h>
44

55
#include <optional>
6-
#include <span>
76
#include <vector>
87

98
#include "../base_config.hpp"
@@ -13,7 +12,7 @@ namespace session::nodeapi {
1312
class MetaBaseWrapper {
1413

1514
public:
16-
explicit MetaBaseWrapper(){};
15+
explicit MetaBaseWrapper() {};
1716

1817
virtual ~MetaBaseWrapper() = default;
1918

include/multi_encrypt/multi_encrypt.hpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ namespace session::nodeapi {
2020

2121
namespace log = oxen::log;
2222

23-
auto cat = log::Cat("multi_encrypt");
24-
2523
inline std::vector<unsigned char> extractPlaintext(
2624
const Napi::Object& obj, const std::string identifier) {
2725

@@ -718,9 +716,8 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
718716

719717
auto contentOrEnvelope = extractContentOrEnvelope(
720718
obj, "decryptForCommunity.obj.contentOrEnvelope");
721-
decrypted.push_back(
722-
session::decode_for_community(
723-
contentOrEnvelope, nowMs, proBackendPubkeyHex));
719+
decrypted.push_back(session::decode_for_community(
720+
contentOrEnvelope, nowMs, proBackendPubkeyHex));
724721
decryptedServerIds.push_back(serverId);
725722

726723
} catch (const std::exception& e) {
@@ -819,9 +816,8 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
819816

820817
auto envelopePayload =
821818
extractEnvelopePayload(obj, "decryptFor1o1.obj.envelopePayload");
822-
decrypted.push_back(
823-
session::decode_envelope(
824-
keys, envelopePayload, nowMs, proBackendPubkeyHex));
819+
decrypted.push_back(session::decode_envelope(
820+
keys, envelopePayload, nowMs, proBackendPubkeyHex));
825821
decryptedMessageHashes.push_back(messageHash);
826822
} catch (const std::exception& e) {
827823
log::warning(
@@ -919,9 +915,8 @@ class MultiEncryptWrapper : public Napi::ObjectWrap<MultiEncryptWrapper> {
919915

920916
auto envelopePayload =
921917
extractEnvelopePayload(obj, "decryptForGroup.obj.envelopePayload");
922-
decrypted.push_back(
923-
session::decode_envelope(
924-
keys, envelopePayload, nowMs, proBackendPubkeyHex));
918+
decrypted.push_back(session::decode_envelope(
919+
keys, envelopePayload, nowMs, proBackendPubkeyHex));
925920
decryptedMessageHashes.push_back(messageHash);
926921
} catch (const std::exception& e) {
927922
log::warning(

include/pro/pro.hpp

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,18 @@
44
#include <oxenc/base64.h>
55
#include <oxenc/hex.h>
66

7-
#include <algorithm>
8-
#include <span>
97
#include <vector>
108

9+
#include "../meta/meta_base_wrapper.hpp"
1110
#include "../utilities.hpp"
12-
#include "oxen/log.hpp"
13-
#include "pro/types.hpp"
14-
#include "session/attachments.hpp"
15-
#include "session/config/user_profile.hpp"
16-
#include "session/random.hpp"
11+
#include "meta/meta_base_wrapper.hpp"
1712
#include "session/session_protocol.hpp"
1813

1914
namespace session::nodeapi {
15+
namespace log = oxen::log;
2016

2117
class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
18+
2219
public:
2320
ProWrapper(const Napi::CallbackInfo& info) : Napi::ObjectWrap<ProWrapper>{info} {
2421
throw std::invalid_argument("ProWrapper is static and doesn't need to be constructed");
@@ -49,6 +46,7 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
4946

5047
assertInfoLength(info, 1);
5148
assertIsObject(info[0]);
49+
auto env = info.Env();
5250

5351
auto first = info[0].As<Napi::Object>();
5452

@@ -67,20 +65,31 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
6765
proFeatures.push_back(item);
6866
}
6967

70-
SESSION_PROTOCOL_PRO_EXTRA_FEATURES flags;
68+
SESSION_PROTOCOL_PRO_EXTRA_FEATURES flags = 0;
7169
for (std::string& feature : proFeatures) {
72-
if (feature == "10K_CHARACTER_LIMIT") {
73-
flags |= SESSION_PROTOCOL_PRO_FEATURES_10K_CHARACTER_LIMIT;
74-
} else if (feature == "PRO_BADGE") {
75-
flags |= SESSION_PROTOCOL_PRO_FEATURES_PRO_BADGE;
70+
// Note: 10K_CHARACTER_LIMIT cannot be requested by the caller
71+
if (feature == "PRO_BADGE") {
72+
flags |= SESSION_PROTOCOL_PRO_EXTRA_FEATURES_PRO_BADGE;
7673
} else if (feature == "ANIMATED_AVATAR") {
77-
flags |= SESSION_PROTOCOL_PRO_FEATURES_ANIMATED_AVATAR;
74+
flags |= SESSION_PROTOCOL_PRO_EXTRA_FEATURES_ANIMATED_AVATAR;
7875
}
7976
}
8077
assertIsString(first.Get("utf16"), "proFeaturesForMessage.utf16");
8178
std::u16string utf16 = first.Get("utf16").As<Napi::String>().Utf16Value();
82-
return session::pro_features_for_utf16((utf16.data()), utf16.length(), flags);
79+
auto pro_features_msg =
80+
session::pro_features_for_utf16((utf16.data()), utf16.length(), flags);
81+
82+
auto obj = Napi::Object::New(env);
83+
84+
obj["success"] = toJs(env, pro_features_msg.success);
85+
obj["error"] =
86+
pro_features_msg.error.size() ? toJs(env, pro_features_msg.error) : env.Null();
87+
obj["codepointCount"] = toJs(env, pro_features_msg.codepoint_count);
88+
obj["proFeatures"] = proFeaturesToJs(env, pro_features_msg.features);
89+
90+
return obj;
8391
});
8492
};
8593
};
94+
8695
}; // namespace session::nodeapi

include/pro/types.hpp

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@
44
#include <oxenc/base64.h>
55
#include <oxenc/hex.h>
66

7-
#include <algorithm>
8-
#include <span>
9-
#include <vector>
10-
11-
#include "../utilities.hpp"
12-
#include "oxen/log.hpp"
13-
#include "session/attachments.hpp"
14-
#include "session/config/user_profile.hpp"
15-
#include "session/multi_encrypt.hpp"
16-
#include "session/random.hpp"
7+
#include "session/config/pro.hpp"
8+
#include "session/session_protocol.hpp"
9+
#include "utilities.hpp"
1710

1811
namespace session::nodeapi {
1912

@@ -72,32 +65,6 @@ struct toJs_impl<session::DecodedEnvelope> {
7265
}
7366
};
7467

75-
template <>
76-
struct toJs_impl<SESSION_PROTOCOL_PRO_FEATURES> {
77-
Napi::Object operator()(const Napi::Env& env, const SESSION_PROTOCOL_PRO_FEATURES bitset) {
78-
Napi::Array arr = Napi::Array::New(env);
79-
uint32_t index = 0;
80-
81-
if (bitset == SESSION_PROTOCOL_PRO_FEATURES_NIL) {
82-
return arr;
83-
}
84-
85-
if (bitset & (SESSION_PROTOCOL_PRO_FEATURES_10K_CHARACTER_LIMIT)) {
86-
arr[index] = Napi::String::New(env, "10K_CHARACTER_LIMIT");
87-
index++;
88-
}
89-
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_PRO_BADGE) {
90-
arr[index++] = Napi::String::New(env, "PRO_BADGE");
91-
index++;
92-
}
93-
if (bitset & SESSION_PROTOCOL_PRO_FEATURES_ANIMATED_AVATAR) {
94-
arr[index++] = Napi::String::New(env, "ANIMATED_AVATAR");
95-
index++;
96-
}
97-
return arr;
98-
}
99-
};
100-
10168
template <>
10269
struct toJs_impl<session::DecodedPro> {
10370
Napi::Object operator()(const Napi::Env& env, const session::DecodedPro decoded_pro) {
@@ -110,22 +77,7 @@ struct toJs_impl<session::DecodedPro> {
11077
: decoded_pro.status == ProStatus::Valid ? "Valid"
11178
: "Expired");
11279
obj["proProof"] = toJs(env, decoded_pro.proof);
113-
obj["proFeatures"] = toJs(env, decoded_pro.features);
114-
115-
return obj;
116-
}
117-
};
118-
119-
template <>
120-
struct toJs_impl<session::ProFeaturesForMsg> {
121-
Napi::Object operator()(
122-
const Napi::Env& env, const session::ProFeaturesForMsg pro_features_msg) {
123-
auto obj = Napi::Object::New(env);
124-
125-
obj["success"] = toJs(env, pro_features_msg.success);
126-
obj["error"] = toJs(env, pro_features_msg.error);
127-
obj["codepointCount"] = toJs(env, pro_features_msg.codepoint_count);
128-
obj["proFeatures"] = toJs(env, pro_features_msg.features);
80+
obj["proFeatures"] = proFeaturesToJs(env, decoded_pro.features);
12981

13082
return obj;
13183
}

include/profile_pic.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
namespace session::nodeapi {
77

8-
98
// Constructs a profile_pic from a Napi::Value which must be either Null or an Object; if an
109
// Object then it *must* contain "url" (string or null) and "key" (uint8array of size 32 or
1110
// null) keys; if either is empty or Null then you get back an empty (i.e. clearing)

include/utilities.hpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include <unordered_set>
1111
#include <vector>
1212

13+
#include "oxen/log.hpp"
1314
#include "oxenc/hex.h"
1415
#include "session/config/namespaces.hpp"
1516
#include "session/config/profile_pic.hpp"
17+
#include "session/session_protocol.hpp"
1618
#include "session/types.h"
1719
#include "session/types.hpp"
1820
#include "utilities.hpp"
@@ -21,6 +23,8 @@ namespace session::nodeapi {
2123

2224
using namespace std::literals;
2325

26+
inline auto cat = oxen::log::Cat("nodeapi");
27+
2428
static void checkOrThrow(bool condition, const char* msg) {
2529
if (!condition)
2630
throw std::invalid_argument{msg};
@@ -119,6 +123,11 @@ struct toJs_impl<session::config::Namespace> {
119123
}
120124
};
121125

126+
template <>
127+
struct toJs_impl<size_t> {
128+
auto operator()(const Napi::Env& env, size_t b) const { return Napi::Number::New(env, (b)); }
129+
};
130+
122131
template <typename T>
123132
struct toJs_impl<T, std::enable_if_t<std::is_arithmetic_v<T>>> {
124133
auto operator()(const Napi::Env& env, T n) const { return Napi::Number::New(env, n); }
@@ -294,7 +303,7 @@ auto wrapResult(const Napi::Env& env, Call&& call) {
294303
if constexpr (std::is_base_of_v<Napi::Value, Result>)
295304
return res;
296305
else
297-
return toJs(env, std::move(res));
306+
return toJs<Result>(env, std::move(res));
298307
}
299308
} catch (const std::exception& e) {
300309
throw Napi::Error::New(env, e.what());
@@ -362,6 +371,8 @@ Napi::Object decrypt_result_to_JS(
362371

363372
confirm_pushed_entry_t confirm_pushed_entry_from_JS(const Napi::Env& env, const Napi::Object& obj);
364373

374+
Napi::Object proFeaturesToJs(const Napi::Env& env, const SESSION_PROTOCOL_PRO_FEATURES bitset);
375+
365376
std::span<const uint8_t> from_hex_to_span(std::string_view x);
366377

367378
template <std::size_t N>
@@ -371,11 +382,10 @@ template <std::size_t N>
371382
std::array<uint8_t, N> from_hex_to_array(std::string x) {
372383
std::string as_hex = oxenc::from_hex(x);
373384
if (as_hex.size() != N) {
374-
throw std::invalid_argument(
375-
std::format(
376-
"from_hex_to_array: Decoded hex size mismatch: expected {}, got {}",
377-
N,
378-
as_hex.size()));
385+
throw std::invalid_argument(std::format(
386+
"from_hex_to_array: Decoded hex size mismatch: expected {}, got {}",
387+
N,
388+
as_hex.size()));
379389
}
380390

381391
std::array<uint8_t, N> result;
@@ -386,21 +396,21 @@ std::array<uint8_t, N> from_hex_to_array(std::string x) {
386396
std::vector<unsigned char> from_hex_to_vector(std::string_view x);
387397

388398
std::span<const uint8_t> from_base64_to_span(std::string_view x);
389-
std::vector<unsigned char> from_base64_to_vector(std::string_view x) ;
390-
399+
std::vector<unsigned char> from_base64_to_vector(std::string_view x);
391400

392401
// Concept to match containers with a size() method
393402
template <typename T>
394403
concept HasSize = requires(T t) {
395-
{t.size()}->std::convertible_to<size_t>;
404+
{
405+
t.size()
406+
} -> std::convertible_to<size_t>;
396407
};
397408

398409
template <HasSize T>
399410
void assert_length(const T& x, size_t n, std::string_view base_identifier) {
400411
if (x.size() != n) {
401-
throw std::invalid_argument(
402-
std::format(
403-
"assert_length: expected {}, got {} for {}", n, x.size(), base_identifier));
412+
throw std::invalid_argument(std::format(
413+
"assert_length: expected {}, got {} for {}", n, x.size(), base_identifier));
404414
}
405415
}
406416

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"update_version": "sh update_version.sh",
1313
"clean": "rimraf .cache build",
1414
"lint:cpp": "cppcheck --std=c++20 -j8 --quiet src libsession-util/src",
15+
"lint": "find src include -name '*.cpp' -o -name '*.hpp' | xargs clang-format-19 -i",
1516
"install": "cmake-js build --runtime=electron --runtime-version=34.2.0 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF --CDWITH_TESTS=OFF",
1617
"prepare_release": "sh prepare_release.sh",
1718
"dedup": "npx --yes yarn-deduplicate yarn.lock"

0 commit comments

Comments
 (0)