Skip to content

Commit 6820f4a

Browse files
committed
feat: memberStatus instead of a bunch of bools for libsession state
1 parent adbb3ce commit 6820f4a

File tree

5 files changed

+102
-41
lines changed

5 files changed

+102
-41
lines changed

src/groups/meta_group_wrapper.hpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,28 @@ struct toJs_impl<member> {
2323
obj["profilePicture"] = toJs(env, info.profile_picture);
2424
obj["removedStatus"] = toJs(env, info.removed_status);
2525

26-
// invites
27-
obj["inviteNotSent"] = toJs(env, info.invite_not_sent());
28-
obj["invitePending"] = toJs(env, info.invite_pending());
29-
obj["inviteAccepted"] = toJs(env, info.invite_status == 0);
30-
obj["inviteFailed"] = toJs(env, info.invite_failed());
31-
32-
// promotions
33-
obj["promotionNotSent"] = toJs(env, info.promotion_not_sent());
34-
obj["promotionPending"] = toJs(env, info.promotion_pending());
35-
obj["promotionFailed"] = toJs(env, info.promotion_failed());
36-
obj["promoted"] = toJs(env, info.promoted());
26+
// promoted() is true as soon as the member is scheduled to be promoted
27+
// Note: this should be part of `libsession-util`, not `libsession-util-nodejs`
28+
if (info.promoted() && !info.promotion_pending()) {
29+
obj["memberStatus"] = toJs(env, "PROMOTION_ACCEPTED");
30+
} else if (info.promotion_pending()) {
31+
obj["memberStatus"] = toJs(env, "PROMOTION_SENT");
32+
} else if (info.promotion_failed()) {
33+
obj["memberStatus"] = toJs(env, "PROMOTION_FAILED");
34+
} else if (info.admin) {
35+
obj["memberStatus"] = toJs(env, "PROMOTION_NOT_SENT");
36+
} else if (info.invite_status == 0) {
37+
obj["memberStatus"] = toJs(env, "INVITE_ACCEPTED");
38+
} else if (info.invite_pending()) {
39+
obj["memberStatus"] = toJs(env, "INVITE_SENT");
40+
} else if (info.invite_failed()) {
41+
obj["memberStatus"] = toJs(env, "INVITE_FAILED");
42+
} else {
43+
// this is probably a bad idea to have a catch-all else, but we have to when we consider
44+
// upgrades of libsession-util
45+
obj["memberStatus"] = toJs(env, "INVITE_NOT_SENT");
46+
}
47+
obj["nominatedAdmin"] = toJs(env, info.admin);
3748

3849
// removed status
3950
obj["isRemoved"] = toJs(env, info.is_removed());

src/user_groups_config.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ struct toJs_impl<group_info> {
7070
obj["authData"] = toJs(env, info.auth_data);
7171
obj["invitePending"] = toJs(env, info.invited);
7272
obj["kicked"] = toJs(env, info.kicked());
73+
obj["destroyed"] = toJs(env, info.isDestroyed());
7374

7475
return obj;
7576
}
@@ -103,6 +104,9 @@ void UserGroupsWrapper::Init(Napi::Env env, Napi::Object exports) {
103104
InstanceMethod("getGroup", &UserGroupsWrapper::getGroup),
104105
InstanceMethod("getAllGroups", &UserGroupsWrapper::getAllGroups),
105106
InstanceMethod("setGroup", &UserGroupsWrapper::setGroup),
107+
InstanceMethod("markGroupKicked", &UserGroupsWrapper::markGroupKicked),
108+
InstanceMethod("markGroupInvited", &UserGroupsWrapper::markGroupInvited),
109+
InstanceMethod("markGroupDestroyed", &UserGroupsWrapper::markGroupDestroyed),
106110
InstanceMethod("eraseGroup", &UserGroupsWrapper::eraseGroup),
107111

108112
});
@@ -316,13 +320,6 @@ Napi::Value UserGroupsWrapper::setGroup(const Napi::CallbackInfo& info) {
316320
group_info.invited = *invited;
317321
}
318322

319-
if (auto kicked =
320-
maybeNonemptyBoolean(obj.Get("kicked"), "UserGroupsWrapper::setGroup kicked")) {
321-
if (*kicked) {
322-
group_info.setKicked();
323-
}
324-
}
325-
326323
if (auto secretKey = maybeNonemptyBuffer(
327324
obj.Get("secretKey"), "UserGroupsWrapper::setGroup secretKey")) {
328325
group_info.secretkey = *secretKey;
@@ -343,6 +340,45 @@ Napi::Value UserGroupsWrapper::setGroup(const Napi::CallbackInfo& info) {
343340
});
344341
}
345342

343+
Napi::Value UserGroupsWrapper::markGroupKicked(const Napi::CallbackInfo& info) {
344+
return wrapResult(info, [&] {
345+
auto groupPk = getStringArgs<1>(info);
346+
347+
auto group = config.get_group(groupPk);
348+
if (group) {
349+
group->markKicked();
350+
config.set(*group);
351+
}
352+
return config.get_or_construct_group(groupPk);
353+
});
354+
}
355+
356+
Napi::Value UserGroupsWrapper::markGroupInvited(const Napi::CallbackInfo& info) {
357+
return wrapResult(info, [&] {
358+
auto groupPk = getStringArgs<1>(info);
359+
360+
auto group = config.get_group(groupPk);
361+
if (group) {
362+
group->markInvited();
363+
config.set(*group);
364+
}
365+
return config.get_or_construct_group(groupPk);
366+
});
367+
}
368+
369+
Napi::Value UserGroupsWrapper::markGroupDestroyed(const Napi::CallbackInfo& info) {
370+
return wrapResult(info, [&] {
371+
auto groupPk = getStringArgs<1>(info);
372+
373+
auto group = config.get_group(groupPk);
374+
if (group) {
375+
group->markDestroyed();
376+
config.set(*group);
377+
}
378+
return config.get_or_construct_group(groupPk);
379+
});
380+
}
381+
346382
Napi::Value UserGroupsWrapper::eraseGroup(const Napi::CallbackInfo& info) {
347383
return wrapResult(info, [&] { return config.erase_group(getStringArgs<1>(info)); });
348384
}

src/user_groups_config.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ class UserGroupsWrapper : public ConfigBaseImpl, public Napi::ObjectWrap<UserGro
3434
Napi::Value getGroup(const Napi::CallbackInfo& info);
3535
Napi::Value getAllGroups(const Napi::CallbackInfo& info);
3636
Napi::Value setGroup(const Napi::CallbackInfo& info);
37+
Napi::Value markGroupKicked(const Napi::CallbackInfo& info);
38+
Napi::Value markGroupInvited(const Napi::CallbackInfo& info);
39+
Napi::Value markGroupDestroyed(const Napi::CallbackInfo& info);
3740
Napi::Value eraseGroup(const Napi::CallbackInfo& info);
3841
};
3942

types/groups/groupmembers.d.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@ declare module 'libsession_util_nodejs' {
1212
profilePicture: ProfilePicture | null;
1313
};
1414

15-
export type GroupMemberGet = GroupMemberShared & {
16-
/** Default state, before we try sending the invite */
17-
inviteNotSent: boolean;
18-
/** We did send the invite to the user */
19-
invitePending: boolean;
20-
/** The invite was accepted by the user */
21-
inviteAccepted: boolean;
22-
23-
/** We failed to send the invite to the user */
24-
inviteFailed: boolean;
25-
26-
/** Default state, before we try sending the promotion */
27-
promotionNotSent: boolean;
28-
/** We did send the promotion, not accepted yet */
29-
promotionPending: boolean;
30-
/** We tried to send the promotion but failed */
31-
promotionFailed: boolean;
32-
/** The user is already an admin *or* has a pending promotion */
33-
promoted: boolean;
15+
type MemberStateGroupV2 =
16+
| 'INVITE_NOT_SENT' // as soon as we've scheduled that guy to be invited, but before we've tried sending the invite message
17+
| 'INVITE_FAILED'
18+
| 'INVITE_SENT'
19+
| 'INVITE_ACCEPTED' // regular member
20+
| 'PROMOTION_NOT_SENT' // as soon as we've scheduled that guy to be an admin, but before we've tried sending the promotion message
21+
| 'PROMOTION_FAILED'
22+
| 'PROMOTION_SENT'
23+
| 'PROMOTION_ACCEPTED'; // regular admin
3424

25+
export type GroupMemberGet = GroupMemberShared & {
26+
memberStatus: MemberStateGroupV2;
27+
/**
28+
* True if the member is scheduled to get the keys (.admin field of libsession).
29+
* This is equivalent of memberStatus being one of:
30+
* - PROMOTION_NOT_SENT
31+
* - PROMOTION_FAILED
32+
* - PROMOTION_SENT
33+
* - PROMOTION_ACCEPTED
34+
*/
35+
nominatedAdmin: boolean;
3536
/** True if the user should be removed from the group */
3637
isRemoved: boolean;
3738
/** True if the user and his messages should be removed from the group */

types/user/usergroups.d.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ declare module 'libsession_util_nodejs' {
4646
authData: Uint8ArrayLen100 | null; // len 100
4747
name: string | null;
4848
invitePending: boolean; // tracks `db.approved`. libsession allows this field for all groups (including communities, but we don't need it for more)
49-
kicked: boolean; // Note: setting this to true will erase authData and admin secretKey, permanently
49+
kicked: boolean; // Note: if the group was `destroyed` this will be false, but `destroyed` will be true
50+
destroyed: boolean;
5051
};
5152

5253
/**
53-
* We can set anything on a UserGroup and can omit fields by explicitely setting them to null.
54-
* The only one which cannot be omited is the pubkeyHex
54+
* We can set anything on a UserGroup and can omit fields by explicitly setting them to null.
55+
* The only one which cannot be omitted is the pubkeyHex
5556
*/
5657
type UserGroupsSet = Pick<UserGroupsGet, 'pubkeyHex'> &
57-
AllFieldsNullable<Omit<UserGroupsGet, 'pubkeyHex'>>;
58+
AllFieldsNullable<Omit<UserGroupsGet, 'pubkeyHex' | 'kicked' | 'destroyed'>>;
5859

5960
type UserGroupsWrapper = BaseConfigWrapper & {
6061
init: (secretKey: Uint8Array, dump: Uint8Array | null) => void;
@@ -90,6 +91,9 @@ declare module 'libsession_util_nodejs' {
9091
getGroup: (pubkeyHex: GroupPubkeyType) => UserGroupsGet | null;
9192
getAllGroups: () => Array<UserGroupsGet>;
9293
setGroup: (info: UserGroupsSet) => UserGroupsGet;
94+
markGroupKicked: (pubkeyHex: GroupPubkeyType) => UserGroupsGet;
95+
markGroupInvited: (pubkeyHex: GroupPubkeyType) => UserGroupsGet;
96+
markGroupDestroyed: (pubkeyHex: GroupPubkeyType) => UserGroupsGet;
9397
eraseGroup: (pubkeyHex: GroupPubkeyType) => boolean;
9498
};
9599

@@ -115,6 +119,9 @@ declare module 'libsession_util_nodejs' {
115119
public getGroup: UserGroupsWrapper['getGroup'];
116120
public getAllGroups: UserGroupsWrapper['getAllGroups'];
117121
public setGroup: UserGroupsWrapper['setGroup'];
122+
public markGroupKicked: UserGroupsWrapper['markGroupKicked'];
123+
public markGroupInvited: UserGroupsWrapper['markGroupInvited'];
124+
public markGroupDestroyed: UserGroupsWrapper['markGroupDestroyed'];
118125
public eraseGroup: UserGroupsWrapper['eraseGroup'];
119126
}
120127

@@ -134,5 +141,8 @@ declare module 'libsession_util_nodejs' {
134141
| MakeActionCall<UserGroupsWrapper, 'getGroup'>
135142
| MakeActionCall<UserGroupsWrapper, 'getAllGroups'>
136143
| MakeActionCall<UserGroupsWrapper, 'setGroup'>
144+
| MakeActionCall<UserGroupsWrapper, 'markGroupKicked'>
145+
| MakeActionCall<UserGroupsWrapper, 'markGroupInvited'>
146+
| MakeActionCall<UserGroupsWrapper, 'markGroupDestroyed'>
137147
| MakeActionCall<UserGroupsWrapper, 'eraseGroup'>;
138148
}

0 commit comments

Comments
 (0)