Skip to content

Commit

Permalink
Merge pull request #21 from Bilb/break-down-set-user-info
Browse files Browse the repository at this point in the history
fix: break down get/set userInfo functions
  • Loading branch information
Bilb authored Sep 3, 2024
2 parents af646ac + 68a5827 commit 126cd0f
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "index.js",
"name": "libsession_util_nodejs",
"description": "Wrappers for the Session Util Library",
"version": "0.3.22",
"version": "0.3.23",
"license": "GPL-3.0",
"author": {
"name": "Oxen Project",
Expand Down
88 changes: 56 additions & 32 deletions src/user_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ void UserConfigWrapper::Init(Napi::Env env, Napi::Object exports) {
exports,
"UserConfigWrapperNode",
{
InstanceMethod("getUserInfo", &UserConfigWrapper::getUserInfo),
InstanceMethod("setUserInfo", &UserConfigWrapper::setUserInfo),
InstanceMethod("getPriority", &UserConfigWrapper::getPriority),
InstanceMethod("getName", &UserConfigWrapper::getName),
InstanceMethod("getProfilePic", &UserConfigWrapper::getProfilePic),
InstanceMethod("setPriority", &UserConfigWrapper::setPriority),
InstanceMethod("setName", &UserConfigWrapper::setName),
InstanceMethod("setNameTruncated", &UserConfigWrapper::setNameTruncated),
InstanceMethod("setProfilePic", &UserConfigWrapper::setProfilePic),
InstanceMethod(
"getEnableBlindedMsgRequest",
&UserConfigWrapper::getEnableBlindedMsgRequest),
Expand All @@ -31,56 +36,75 @@ UserConfigWrapper::UserConfigWrapper(const Napi::CallbackInfo& info) :
ConfigBaseImpl{construct<config::UserProfile>(info, "UserConfig")},
Napi::ObjectWrap<UserConfigWrapper>{info} {}

Napi::Value UserConfigWrapper::getUserInfo(const Napi::CallbackInfo& info) {

Napi::Value UserConfigWrapper::getPriority(const Napi::CallbackInfo& info) {
return wrapResult(info, [&] {
auto env = info.Env();
auto user_info_obj = Napi::Object::New(env);
return config.get_nts_priority();
});
}

auto name = config.get_name();
auto priority = config.get_nts_priority();
Napi::Value UserConfigWrapper::getName(const Napi::CallbackInfo& info) {
return wrapResult(info, [&] {
auto env = info.Env();
return config.get_name();
});
}

user_info_obj["name"] = toJs(env, name);
user_info_obj["priority"] = toJs(env, priority);
Napi::Value UserConfigWrapper::getProfilePic(const Napi::CallbackInfo& info) {
return wrapResult(info, [&] {
auto env = info.Env();
return object_from_profile_pic(env, config.get_profile_pic());
});
}

auto profile_pic_obj = object_from_profile_pic(env, config.get_profile_pic());
if (profile_pic_obj) {
user_info_obj["url"] = profile_pic_obj.Get("url");
user_info_obj["key"] = profile_pic_obj.Get("key");
} else {
user_info_obj["url"] = env.Null();
user_info_obj["key"] = env.Null();
}
void UserConfigWrapper::setPriority(const Napi::CallbackInfo& info) {
return wrapExceptions(info, [&] {
auto env = info.Env();
assertInfoLength(info, 1);
auto priority = info[0];
assertIsNumber(priority);

return user_info_obj;
auto new_priority = toPriority(priority, config.get_nts_priority());
config.set_nts_priority(new_priority);
});
}

Napi::Value UserConfigWrapper::setUserInfo(const Napi::CallbackInfo& info) {
return wrapResult(info, [&] {
assertInfoLength(info, 3);

void UserConfigWrapper::setName(const Napi::CallbackInfo& info) {
return wrapExceptions(info, [&] {
auto env = info.Env();
assertInfoLength(info, 1);
auto name = info[0];
auto priority = info[1];
auto profile_pic_obj = info[2];
assertIsString(name);

assertIsStringOrNull(name);
assertIsNumber(priority);
std::string new_name;
auto new_name = name.As<Napi::String>().Utf8Value();
// this will throw if the name is too long
config.set_name(new_name);
});
}

if (name.IsString())
new_name = name.As<Napi::String>().Utf8Value();
void UserConfigWrapper::setNameTruncated(const Napi::CallbackInfo& info) {
return wrapExceptions(info, [&] {
auto env = info.Env();
assertInfoLength(info, 1);
auto name = info[0];
assertIsString(name);

auto new_name = name.As<Napi::String>().Utf8Value();
// this will truncate silently if the name is too long
config.set_name_truncated(new_name);
});
}

auto new_priority = toPriority(priority, config.get_nts_priority());
config.set_nts_priority(new_priority);
void UserConfigWrapper::setProfilePic(const Napi::CallbackInfo& info) {
return wrapExceptions(info, [&] {
assertInfoLength(info, 1);
auto profile_pic_obj = info[0];

if (!profile_pic_obj.IsNull() && !profile_pic_obj.IsUndefined())
assertIsObject(profile_pic_obj);

config.set_profile_pic(profile_pic_from_object(profile_pic_obj));

return config.get_name();
});
}

Expand Down
9 changes: 7 additions & 2 deletions src/user_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ class UserConfigWrapper : public ConfigBaseImpl, public Napi::ObjectWrap<UserCon
private:
config::UserProfile& config{get_config<config::UserProfile>()};

Napi::Value getUserInfo(const Napi::CallbackInfo& info);
Napi::Value setUserInfo(const Napi::CallbackInfo& info);
Napi::Value getPriority(const Napi::CallbackInfo& info);
Napi::Value getName(const Napi::CallbackInfo& info);
Napi::Value getProfilePic(const Napi::CallbackInfo& info);
void setPriority(const Napi::CallbackInfo& info);
void setName(const Napi::CallbackInfo& info);
void setNameTruncated(const Napi::CallbackInfo& info);
void setProfilePic(const Napi::CallbackInfo& info);

Napi::Value getEnableBlindedMsgRequest(const Napi::CallbackInfo& info);
void setEnableBlindedMsgRequest(const Napi::CallbackInfo& info);
Expand Down
40 changes: 24 additions & 16 deletions user/userconfig.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference path="../shared.d.ts" />

declare module 'libsession_util_nodejs' {
/**
*
Expand All @@ -9,18 +11,14 @@ declare module 'libsession_util_nodejs' {
init: (secretKey: Uint8Array, dump: Uint8Array | null) => void;
/** This function is used to free wrappers from memory only */
free: () => void;
getUserInfo: () => {
name: string;
priority: number;
url: string;
key: Uint8Array;
};
/** if name > CONTACT_MAX_NAME_LENGTH it will be truncated */
setUserInfo: (
name: string,
priority: number,
profilePic: { url: string; key: Uint8Array } | null
) => string;
getPriority: () => number;
getName: () => string | null;
getProfilePic: () => ProfilePicture;
setPriority: (priority: number) => void;
setName: (name: string) => void;
setNameTruncated: (name: string) => void;
setProfilePic: (pic: ProfilePicture) => void;

setEnableBlindedMsgRequest: (msgRequest: boolean) => void;
getEnableBlindedMsgRequest: () => boolean | undefined;
setNoteToSelfExpiry: (expirySeconds: number) => void;
Expand All @@ -37,8 +35,13 @@ declare module 'libsession_util_nodejs' {
*/
export class UserConfigWrapperNode extends BaseConfigWrapperNode {
constructor(secretKey: Uint8Array, dump: Uint8Array | null);
public getUserInfo: UserConfigWrapper['getUserInfo'];
public setUserInfo: UserConfigWrapper['setUserInfo'];
public getPriority: UserConfigWrapper['getPriority'];
public getName: UserConfigWrapper['getName'];
public getProfilePic: UserConfigWrapper['getProfilePic'];
public setPriority: UserConfigWrapper['setPriority'];
public setName: UserConfigWrapper['setName'];
public setNameTruncated: UserConfigWrapper['setNameTruncated'];
public setProfilePic: UserConfigWrapper['setProfilePic'];
public getEnableBlindedMsgRequest: UserConfigWrapper['getEnableBlindedMsgRequest'];
public setEnableBlindedMsgRequest: UserConfigWrapper['setEnableBlindedMsgRequest'];
public getNoteToSelfExpiry: UserConfigWrapper['getNoteToSelfExpiry'];
Expand All @@ -53,8 +56,13 @@ declare module 'libsession_util_nodejs' {
export type UserConfigActionsType =
| ['init', Uint8Array, Uint8Array | null]
| MakeActionCall<UserConfigWrapper, 'free'>
| MakeActionCall<UserConfigWrapper, 'getUserInfo'>
| MakeActionCall<UserConfigWrapper, 'setUserInfo'>
| MakeActionCall<UserConfigWrapper, 'getPriority'>
| MakeActionCall<UserConfigWrapper, 'getName'>
| MakeActionCall<UserConfigWrapper, 'getProfilePic'>
| MakeActionCall<UserConfigWrapper, 'setPriority'>
| MakeActionCall<UserConfigWrapper, 'setName'>
| MakeActionCall<UserConfigWrapper, 'setNameTruncated'>
| MakeActionCall<UserConfigWrapper, 'setProfilePic'>
| MakeActionCall<UserConfigWrapper, 'getEnableBlindedMsgRequest'>
| MakeActionCall<UserConfigWrapper, 'setEnableBlindedMsgRequest'>
| MakeActionCall<UserConfigWrapper, 'getNoteToSelfExpiry'>
Expand Down

0 comments on commit 126cd0f

Please sign in to comment.