11
11
#include < vector>
12
12
13
13
#include " session/config/namespaces.hpp"
14
+ #include " session/config/profile_pic.hpp"
14
15
#include " session/types.hpp"
15
16
#include " utilities.hpp"
16
17
@@ -59,20 +60,24 @@ std::vector<unsigned char> toCppBuffer(Napi::Value x, const std::string& identif
59
60
int64_t toCppInteger (Napi::Value x, const std::string& identifier, bool allowUndefined = false );
60
61
std::optional<int64_t > maybeNonemptyInt (Napi::Value x, const std::string& identifier);
61
62
std::optional<bool > maybeNonemptyBoolean (Napi::Value x, const std::string& identifier);
63
+ std::optional<std::chrono::sys_seconds> maybeNonemptySysSeconds (
64
+ Napi::Value x, const std::string& identifier);
65
+
66
+ std::chrono::sys_seconds toCppSysSeconds (Napi::Value x, const std::string& identifier);
62
67
63
68
bool toCppBoolean (Napi::Value x, const std::string& identifier);
64
69
65
- // If the object is null/undef/empty returns nullopt, otherwise if a String returns a std::string of
66
- // the value. Throws if something else.
70
+ // If the object is null/undef/empty returns nullopt, otherwise if a String returns a
71
+ // std::string of the value. Throws if something else.
67
72
std::optional<std::string> maybeNonemptyString (Napi::Value x, const std::string& identifier);
68
73
69
74
// If the object is null/undef/empty returns nullopt, otherwise if a Uint8Array returns a
70
75
// std::vector<unsigned char> of the value. Throws if something else.
71
76
std::optional<std::vector<unsigned char >> maybeNonemptyBuffer (
72
77
Napi::Value x, const std::string& identifier);
73
78
74
- // Implementation struct of toJs(); we add specializations of this for any C++ types we want to be
75
- // able to convert into JS types.
79
+ // Implementation struct of toJs(); we add specializations of this for any C++ types we want to
80
+ // be able to convert into JS types.
76
81
template <typename T, typename SFINAE = void >
77
82
struct toJs_impl {
78
83
// If this gets instantiated it means we're missing a specialization and so fail to compile:
@@ -176,6 +181,30 @@ struct toJs_impl<std::optional<T>> {
176
181
}
177
182
};
178
183
184
+ template <>
185
+ struct toJs_impl <std::chrono::sys_seconds> {
186
+ auto operator ()(const Napi::Env& env, std::chrono::sys_seconds t) const {
187
+ return Napi::Number::New (env, t.time_since_epoch ().count ());
188
+ }
189
+ };
190
+
191
+ // Returns {"url": "...", "key": buffer} object; both values will be Null if the pic is not set.
192
+
193
+ template <>
194
+ struct toJs_impl <config::profile_pic> {
195
+ auto operator ()(const Napi::Env& env, const config::profile_pic& pic) const {
196
+ auto obj = Napi::Object::New (env);
197
+ if (pic) {
198
+ obj[" url" ] = toJs (env, pic.url );
199
+ obj[" key" ] = toJs (env, pic.key );
200
+ } else {
201
+ obj[" url" ] = env.Null ();
202
+ obj[" key" ] = env.Null ();
203
+ }
204
+ return obj;
205
+ }
206
+ };
207
+
179
208
// Helper for various "get_all" functions that copy [it...end) into a Napi::Array via toJs().
180
209
// Throws a Napi::Error on any exception.
181
210
template <typename It, typename EndIt>
@@ -192,8 +221,8 @@ static Napi::Array get_all_impl(const Napi::CallbackInfo& info, size_t size, It
192
221
});
193
222
}
194
223
195
- // Wraps a string in an optional<string_view> which will be nullopt if the input string is empty.
196
- // This is particularly useful with `toJs` to convert empty strings into Null.
224
+ // Wraps a string in an optional<string_view> which will be nullopt if the input string is
225
+ // empty. This is particularly useful with `toJs` to convert empty strings into Null.
197
226
inline std::optional<std::string_view> maybe_string (std::string_view val) {
198
227
if (val.empty ())
199
228
return std::nullopt ;
@@ -231,8 +260,8 @@ auto wrapResult(const Napi::Env& env, Call&& call) {
231
260
}
232
261
}
233
262
234
- // Similar to wrapResult(), but a small shortcut to allow passing `info` instead of `info.Env()` as
235
- // the first argument.
263
+ // Similar to wrapResult(), but a small shortcut to allow passing `info` instead of `info.Env()`
264
+ // as the first argument.
236
265
template <typename Call>
237
266
auto wrapResult (const Napi::CallbackInfo& info, Call&& call) {
238
267
return wrapResult (info.Env (), std::forward<Call>(call));
@@ -262,6 +291,10 @@ std::string printable(std::span<const unsigned char> x);
262
291
* Keep the current priority if a wrapper
263
292
*/
264
293
int64_t toPriority (Napi::Value x, int64_t currentPriority);
294
+ int64_t toPriority (int64_t newPriority, int64_t currentPriority);
295
+
296
+ std::optional<session::config::profile_pic> maybeNonemptyProfilePic (
297
+ Napi::Value x, const std::string& identifier);
265
298
266
299
int64_t unix_timestamp_now ();
267
300
0 commit comments