Skip to content

Commit 437e48a

Browse files
committed
chore: to merge somehoiw
1 parent 799d2bb commit 437e48a

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

include/pro/pro.hpp

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@
1414

1515
namespace session::nodeapi {
1616

17+
template <>
18+
struct toJs_impl<pro_backend::ProRevocationItem> {
19+
auto operator()(const Napi::Env& env, pro_backend::ProRevocationItem i) const {
20+
21+
auto obj = Napi::Object::New(env);
22+
obj["genIndexHashB64"] = toJs(env, to_base64(i.gen_index_hash));
23+
obj["expiryUnixTsMs"] = toJs(env, i.expiry_unix_ts);
24+
25+
return obj;
26+
}
27+
};
28+
29+
template <>
30+
struct toJs_impl<pro_backend::ResponseHeader> {
31+
auto operator()(const Napi::Env& env, pro_backend::ResponseHeader r) const {
32+
33+
auto obj = Napi::Object::New(env);
34+
obj["status"] = toJs(env, r.status);
35+
obj["errors"] = toJs(env, r.errors);
36+
37+
return obj;
38+
}
39+
};
40+
1741
class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
1842

1943
public:
@@ -36,11 +60,15 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
3660
"proProofRequestBody",
3761
static_cast<napi_property_attributes>(
3862
napi_writable | napi_configurable)),
39-
// Pro requests body
63+
// Pro requests
4064
StaticMethod<&ProWrapper::proRevocationRequestBody>(
4165
"proRevocationRequestBody",
4266
static_cast<napi_property_attributes>(
4367
napi_writable | napi_configurable)),
68+
StaticMethod<&ProWrapper::proRevocationParseResponse>(
69+
"proRevocationParseResponse",
70+
static_cast<napi_property_attributes>(
71+
napi_writable | napi_configurable)),
4472
});
4573
}
4674

@@ -176,6 +204,36 @@ class ProWrapper : public Napi::ObjectWrap<ProWrapper> {
176204
return req.to_json();
177205
});
178206
};
207+
208+
static Napi::Value proRevocationParseResponse(const Napi::CallbackInfo& info) {
209+
return wrapResult(info, [&] {
210+
// we expect one argument that matches:
211+
// first: {
212+
// "body": string,
213+
// }
214+
215+
assertInfoLength(info, 1);
216+
assertIsObject(info[0]);
217+
auto env = info.Env();
218+
219+
auto first = info[0].As<Napi::Object>();
220+
221+
if (first.IsEmpty())
222+
throw std::invalid_argument("proRevocationParseResponse first received empty");
223+
224+
assertIsString(first.Get("json"), "proRevocationParseResponse.version");
225+
auto bodyCpp = first.Get("json").As<Napi::String>().Utf8Value();
226+
227+
auto parsed = session::pro_backend::GetProRevocationsResponse::parse(bodyCpp);
228+
229+
auto obj = toJs(env, static_cast<pro_backend::ResponseHeader>(parsed));
230+
// if error is set, the body might not be parsable so don't try to use it
231+
obj["ticket"] = parsed.errors.size() ? env.Null() : toJs(env, parsed.ticket);
232+
obj["items"] = parsed.errors.size() ? env.Null() : toJs(env, parsed.items);
233+
234+
return obj;
235+
});
236+
};
179237
};
180238

181239
}; // namespace session::nodeapi

include/utilities.hpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,14 @@ struct toJs_impl<std::chrono::sys_seconds> {
239239
}
240240
};
241241

242+
template <>
243+
struct toJs_impl<std::chrono::sys_time<std::chrono::milliseconds>> {
244+
auto operator()(
245+
const Napi::Env& env, std::chrono::sys_time<std::chrono::milliseconds> t) const {
246+
return Napi::Number::New(env, t.time_since_epoch().count());
247+
}
248+
};
249+
242250
// Returns {"url": "...", "key": buffer} object; both values will be Null if the pic is not set.
243251

244252
template <>
@@ -383,10 +391,11 @@ template <std::size_t N>
383391
std::array<uint8_t, N> from_hex_to_array(std::string x) {
384392
std::string as_hex = oxenc::from_hex(x);
385393
if (as_hex.size() != N) {
386-
throw std::invalid_argument(fmt::format(
387-
"from_hex_to_array: Decoded hex size mismatch: expected {}, got {}",
388-
N,
389-
as_hex.size()));
394+
throw std::invalid_argument(
395+
fmt::format(
396+
"from_hex_to_array: Decoded hex size mismatch: expected {}, got {}",
397+
N,
398+
as_hex.size()));
390399
}
391400

392401
std::array<uint8_t, N> result;
@@ -402,16 +411,15 @@ std::vector<unsigned char> from_base64_to_vector(std::string_view x);
402411
// Concept to match containers with a size() method
403412
template <typename T>
404413
concept HasSize = requires(T t) {
405-
{
406-
t.size()
407-
} -> std::convertible_to<size_t>;
414+
{t.size()}->std::convertible_to<size_t>;
408415
};
409416

410417
template <HasSize T>
411418
void assert_length(const T& x, size_t n, std::string_view base_identifier) {
412419
if (x.size() != n) {
413-
throw std::invalid_argument(fmt::format(
414-
"assert_length: expected {}, got {} for {}", n, x.size(), base_identifier));
420+
throw std::invalid_argument(
421+
fmt::format(
422+
"assert_length: expected {}, got {} for {}", n, x.size(), base_identifier));
415423
}
416424
}
417425

src/pro/pro.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#include "pro/pro.hpp"
1+
#include "pro/pro.hpp"

types/pro/pro.d.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ declare module 'libsession_util_nodejs' {
6565
support_url: string;
6666
};
6767

68+
type ProRevocationItem = {
69+
genIndexHashB64: string;
70+
expiryUnixTsMs: number;
71+
};
72+
6873
type ProWrapper = {
6974
proFeaturesForMessage: (args: {
7075
utf16: string;
@@ -83,11 +88,18 @@ declare module 'libsession_util_nodejs' {
8388
/**
8489
*
8590
* @param version: Request version. The latest accepted version is 0
86-
* @param ticket: 4-byte monotonic integer for the caller's revocation list iteration. Set to 0 if unknown; otherwise, use the latest known `ticket` from a prior `GetProRevocationResponse` to allow
91+
* @param ticket: 4-byte monotonic integer for the caller's revocation list iteration. Set to 0 if unknown; otherwise, use the latest known `ticket` from a prior `GetProRevocationsResponse` to allow
8792
/// the Session Pro Backend to omit the revocation list if it has not changed.
8893
* @returns the stringified body to include in the request
8994
*/
9095
proRevocationRequestBody: ({ version, ticket }: { version: number; ticket: number }) => string;
96+
97+
proRevocationParseResponse: ({ json }: { json: string }) => {
98+
status: number;
99+
errors: Array<string>;
100+
ticket: number | null;
101+
items: Array<ProRevocationItem> | null;
102+
};
91103
};
92104

93105
export type ProActionsCalls = MakeWrapperActionCalls<ProWrapper>;
@@ -99,6 +111,7 @@ declare module 'libsession_util_nodejs' {
99111
public static proFeaturesForMessage: ProWrapper['proFeaturesForMessage'];
100112
public static proProofRequestBody: ProWrapper['proProofRequestBody'];
101113
public static proRevocationRequestBody: ProWrapper['proRevocationRequestBody'];
114+
public static proRevocationParseResponse: ProWrapper['proRevocationParseResponse'];
102115
}
103116

104117
/**
@@ -109,5 +122,6 @@ declare module 'libsession_util_nodejs' {
109122
export type ProActionsType =
110123
| MakeActionCall<ProWrapper, 'proFeaturesForMessage'>
111124
| MakeActionCall<ProWrapper, 'proProofRequestBody'>
112-
| MakeActionCall<ProWrapper, 'proRevocationRequestBody'>;
125+
| MakeActionCall<ProWrapper, 'proRevocationRequestBody'>
126+
| MakeActionCall<ProWrapper, 'proRevocationParseResponse'>;
113127
}

0 commit comments

Comments
 (0)