|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <napi.h> |
| 4 | +#include <oxenc/base64.h> |
| 5 | +#include <oxenc/hex.h> |
| 6 | + |
| 7 | +#include <algorithm> |
| 8 | +#include <span> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +#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" |
| 17 | +#include "session/session_protocol.hpp" |
| 18 | + |
| 19 | +namespace session::nodeapi { |
| 20 | + |
| 21 | +class ProWrapper : public Napi::ObjectWrap<ProWrapper> { |
| 22 | + public: |
| 23 | + ProWrapper(const Napi::CallbackInfo& info) : Napi::ObjectWrap<ProWrapper>{info} { |
| 24 | + throw std::invalid_argument("ProWrapper is static and doesn't need to be constructed"); |
| 25 | + } |
| 26 | + |
| 27 | + static void Init(Napi::Env env, Napi::Object exports) { |
| 28 | + MetaBaseWrapper::NoBaseClassInitHelper<ProWrapper>( |
| 29 | + env, |
| 30 | + exports, |
| 31 | + "ProWrapperNode", |
| 32 | + { |
| 33 | + // Pro features |
| 34 | + StaticMethod<&ProWrapper::proFeaturesForMessage>( |
| 35 | + "proFeaturesForMessage", |
| 36 | + static_cast<napi_property_attributes>( |
| 37 | + napi_writable | napi_configurable)), |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + private: |
| 42 | + static Napi::Value proFeaturesForMessage(const Napi::CallbackInfo& info) { |
| 43 | + return wrapResult(info, [&] { |
| 44 | + // we expect two arguments that match: |
| 45 | + // first: { |
| 46 | + // "utf16": string, |
| 47 | + // "proFeatures": Array<ProFeature>, |
| 48 | + // } |
| 49 | + |
| 50 | + assertInfoLength(info, 1); |
| 51 | + assertIsObject(info[0]); |
| 52 | + |
| 53 | + auto first = info[0].As<Napi::Object>(); |
| 54 | + |
| 55 | + if (first.IsEmpty()) |
| 56 | + throw std::invalid_argument("proFeaturesForMessage first received empty"); |
| 57 | + |
| 58 | + assertIsArray(first.Get("proFeatures"), "proFeaturesForMessage.proFeatures"); |
| 59 | + auto proFeaturesJS = first.Get("proFeatures").As<Napi::Array>(); |
| 60 | + std::vector<std::string> proFeatures; |
| 61 | + proFeatures.reserve(proFeaturesJS.Length()); |
| 62 | + for (uint32_t i = 0; i < proFeaturesJS.Length(); i++) { |
| 63 | + auto itemValue = proFeaturesJS.Get(i); |
| 64 | + assertIsString(itemValue, "proFeaturesForMessage.proFeatures.itemValue"); |
| 65 | + std::string item = |
| 66 | + toCppString(itemValue, "proFeaturesForMessage.proFeatures.itemValue"); |
| 67 | + proFeatures.push_back(item); |
| 68 | + } |
| 69 | + |
| 70 | + SESSION_PROTOCOL_PRO_EXTRA_FEATURES flags; |
| 71 | + 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; |
| 76 | + } else if (feature == "ANIMATED_AVATAR") { |
| 77 | + flags |= SESSION_PROTOCOL_PRO_FEATURES_ANIMATED_AVATAR; |
| 78 | + } |
| 79 | + } |
| 80 | + assertIsString(first.Get("utf16"), "proFeaturesForMessage.utf16"); |
| 81 | + std::u16string utf16 = first.Get("utf16").As<Napi::String>().Utf16Value(); |
| 82 | + return session::pro_features_for_utf16((utf16.data()), utf16.length(), flags); |
| 83 | + }); |
| 84 | + }; |
| 85 | +}; |
| 86 | +}; // namespace session::nodeapi |
0 commit comments