Skip to content

Commit f2e3e9b

Browse files
SessionHero01SessionHero01
authored andcommitted
Profile vs message pro feature
1 parent 5b5f579 commit f2e3e9b

File tree

16 files changed

+124
-156
lines changed

16 files changed

+124
-156
lines changed

library/src/main/cpp/contacts.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static JavaLocalRef<jobject> serialize_contact(JNIEnv *env, const session::confi
3030
(jlong) (info.profile_updated.time_since_epoch().count()),
3131
(jlong) info.priority,
3232
util::serialize_expiry(env, info.exp_mode, info.exp_timer).get(),
33-
(jlong) info.pro_features);
33+
(jlong) info.profile_bitset.data);
3434
return {env, returnObj};
3535
}
3636

@@ -92,7 +92,9 @@ session::config::contact_info deserialize_contact(JNIEnv *env, jobject info, ses
9292
contact_info.priority = env->CallLongMethod(info, class_info.get_priority);
9393
contact_info.exp_mode = expiry_pair.first;
9494
contact_info.exp_timer = std::chrono::seconds(expiry_pair.second);
95-
contact_info.pro_features = env->CallLongMethod(info, class_info.get_pro_features);
95+
contact_info.profile_bitset = {
96+
.data = static_cast<uint64_t>(env->CallLongMethod(info, class_info.get_pro_features))
97+
};
9698

9799
return contact_info;
98100
}
@@ -172,7 +174,7 @@ JavaLocalRef<jobject> serialize_blinded_contact(JNIEnv *env, const session::conf
172174
(jlong) (info.profile_updated.time_since_epoch().count()),
173175
util::serialize_user_pic(env, info.profile_picture).get(),
174176
(jlong) info.priority,
175-
(jlong) info.pro_features
177+
(jlong) info.profile_bitset.data
176178
)};
177179
}
178180

@@ -196,7 +198,7 @@ session::config::blinded_contact_info deserialize_blinded_contact(JNIEnv *env, j
196198
, name_getter(env->GetMethodID(java_class, "getName", "()Ljava/lang/String;"))
197199
, created_epoch_seconds_getter(env->GetMethodID(java_class, "getCreatedEpochSeconds", "()J"))
198200
, profile_updated_epoch_seconds_getter(env->GetMethodID(java_class, "getProfileUpdatedEpochSeconds", "()J"))
199-
, profile_pic_getter(env->GetMethodID(java_class, "getProfilePicture", "()Lnetwork/loki/messenger/libsession_util/util/UserPic;"))
201+
, profile_pic_getter(env->GetMethodID(java_class, "getProfilePic", "()Lnetwork/loki/messenger/libsession_util/util/UserPic;"))
200202
, priority_getter(env->GetMethodID(java_class, "getPriority", "()J"))
201203
, pro_features_getter(env->GetMethodID(java_class, "getProFeaturesRaw", "()J")) {}
202204
};
@@ -219,7 +221,9 @@ session::config::blinded_contact_info deserialize_blinded_contact(JNIEnv *env, j
219221
info.name = JavaStringRef(env, JavaLocalRef(env, (jstring) env->CallObjectMethod(jInfo, class_info.name_getter)).get()).view();
220222
info.profile_updated = std::chrono::sys_seconds{std::chrono::seconds{env->CallLongMethod(jInfo, class_info.profile_updated_epoch_seconds_getter)}};
221223
info.priority = env->CallLongMethod(jInfo, class_info.priority_getter);
222-
info.pro_features = env->CallLongMethod(jInfo, class_info.pro_features_getter);
224+
info.profile_bitset = {
225+
.data = static_cast<uint64_t>(env->CallLongMethod(jInfo, class_info.pro_features_getter))
226+
};
223227

224228
return info;
225229
}

library/src/main/cpp/protocol.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@
88

99
using namespace jni_utils;
1010

11+
static JavaLocalRef<jobject> serializeDecodedPro(JNIEnv *env, const session::DecodedPro &pro) {
12+
static BasicJavaClassInfo class_info(
13+
env,
14+
"network/loki/messenger/libsession_util/protocol/DecodedPro",
15+
"(ILnetwork/loki/messenger/libsession_util/pro/ProProof;JJ)V"
16+
);
1117

18+
return {env, env->NewObject(class_info.java_class, class_info.constructor,
19+
static_cast<jint>(pro.status),
20+
cpp_to_java_proof(env, pro.proof).get(),
21+
static_cast<jlong>(pro.msg_bitset.data),
22+
static_cast<jlong>(pro.profile_bitset.data))
23+
};
24+
}
1225

1326
static JavaLocalRef<jobject> serializeEnvelop(JNIEnv *env, const session::Envelope &envelope) {
1427
static BasicJavaClassInfo class_info(
@@ -32,7 +45,7 @@ static JavaLocalRef<jobject> serializeDecodedEnvelope(JNIEnv *env, const session
3245
static BasicJavaClassInfo class_info(
3346
env,
3447
"network/loki/messenger/libsession_util/protocol/DecodedEnvelope",
35-
"(Lnetwork/loki/messenger/libsession_util/protocol/Envelope;ILnetwork/loki/messenger/libsession_util/pro/ProProof;J[B[B[BJ)V"
48+
"(Lnetwork/loki/messenger/libsession_util/protocol/Envelope;Lnetwork/loki/messenger/libsession_util/protocol/DecodedPro;[B[B[BJ)V"
3649
);
3750

3851
JavaLocalRef sender_ed25519 = util::bytes_from_span(env, envelop.sender_ed25519_pubkey);
@@ -41,10 +54,7 @@ static JavaLocalRef<jobject> serializeDecodedEnvelope(JNIEnv *env, const session
4154

4255
return {env, env->NewObject(class_info.java_class, class_info.constructor,
4356
serializeEnvelop(env, envelop.envelope).get(),
44-
envelop.pro ? static_cast<jint>(envelop.pro->status)
45-
: static_cast<jint>(-1),
46-
envelop.pro ? cpp_to_java_proof(env, envelop.pro->proof).get() : nullptr,
47-
static_cast<jlong>(envelop.pro ? envelop.pro->features : 0),
57+
envelop.pro ? nullptr : serializeDecodedPro(env, *envelop.pro).get(),
4858
content.get(),
4959
sender_ed25519.get(),
5060
sender_x25519.get(),
@@ -144,21 +154,16 @@ Java_network_loki_messenger_libsession_1util_protocol_SessionProtocol_decodeForC
144154
*java_to_cpp_array<32>(env, pro_backend_pub_key)
145155
);
146156

147-
JavaLocalRef envelopClass(env, env->FindClass(
148-
"network/loki/messenger/libsession_util/protocol/DecodedCommunityMessage"));
149-
jmethodID init = env->GetMethodID(
150-
envelopClass.get(),
151-
"<init>",
152-
"(ILnetwork/loki/messenger/libsession_util/pro/ProProof;J[B)V"
157+
static BasicJavaClassInfo class_info(
158+
env,
159+
"network/loki/messenger/libsession_util/protocol/DecodedCommunityMessage",
160+
"(Lnetwork/loki/messenger/libsession_util/protocol/DecodedPro;[B)V"
153161
);
154162

155163
return env->NewObject(
156-
envelopClass.get(),
157-
init,
158-
decoded.pro ? static_cast<jint>(decoded.pro->status)
159-
: static_cast<jint>(-1),
160-
decoded.pro ? cpp_to_java_proof(env, decoded.pro->proof).get() : nullptr,
161-
static_cast<jlong>(decoded.pro ? decoded.pro->features : 0),
164+
class_info.java_class,
165+
class_info.constructor,
166+
decoded.pro ? serializeDecodedPro(env, *decoded.pro).get() : nullptr,
162167
util::bytes_from_vector(env, decoded.content_plaintext).get()
163168
);
164169
});
@@ -250,14 +255,13 @@ Java_network_loki_messenger_libsession_1util_protocol_SessionProtocol_decodeForG
250255
extern "C"
251256
JNIEXPORT jobject JNICALL
252257
Java_network_loki_messenger_libsession_1util_protocol_SessionProtocol_proFeaturesForMessage(
253-
JNIEnv *env, jobject thiz, jstring message_body, jlong proposed_features) {
258+
JNIEnv *env, jobject thiz, jstring message_body) {
254259
return run_catching_cxx_exception_or_throws<jobject>(env, [=] {
255260
JavaCharsRef message_ref(env, message_body);
256261

257262
auto features = session::pro_features_for_utf16(
258263
reinterpret_cast<const char16_t *>(message_ref.chars()),
259-
message_ref.size(),
260-
static_cast<SESSION_PROTOCOL_PRO_FEATURES>(proposed_features)
264+
message_ref.size()
261265
);
262266

263267
static BasicJavaClassInfo class_info(
@@ -271,7 +275,7 @@ Java_network_loki_messenger_libsession_1util_protocol_SessionProtocol_proFeature
271275
class_info.constructor,
272276
static_cast<jint>(features.status),
273277
features.error.empty() ? nullptr : env->NewStringUTF(std::string(features.error).c_str()),
274-
static_cast<jlong>(features.features),
278+
static_cast<jlong>(features.bitset.data),
275279
static_cast<jint>(features.codepoint_count)
276280
);
277281
});

library/src/main/cpp/user_profile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ extern "C"
184184
JNIEXPORT jlong JNICALL
185185
Java_network_loki_messenger_libsession_1util_UserProfile_getProFeaturesRaw(JNIEnv *env,
186186
jobject thiz) {
187-
return static_cast<jlong>(ptrToProfile(env, thiz)->get_pro_features());
187+
return static_cast<jlong>(ptrToProfile(env, thiz)->get_profile_bitset().data);
188188
}
189189

190190
extern "C"

library/src/main/java/network/loki/messenger/libsession_util/Config.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package network.loki.messenger.libsession_util
22

33
import network.loki.messenger.libsession_util.pro.ProConfig
4-
import network.loki.messenger.libsession_util.pro.ProProof
5-
import network.loki.messenger.libsession_util.protocol.ProFeatures
4+
import network.loki.messenger.libsession_util.protocol.ProProfileFeatures
65
import network.loki.messenger.libsession_util.util.BaseCommunityInfo
76
import network.loki.messenger.libsession_util.util.BlindedContact
87
import network.loki.messenger.libsession_util.util.ConfigPush
@@ -12,7 +11,6 @@ import network.loki.messenger.libsession_util.util.ExpiryMode
1211
import network.loki.messenger.libsession_util.util.GroupInfo
1312
import network.loki.messenger.libsession_util.util.GroupMember
1413
import network.loki.messenger.libsession_util.util.UserPic
15-
import java.time.Instant
1614

1715
typealias ConversationPriority = Long
1816

@@ -85,7 +83,7 @@ interface ReadableUserProfile: ReadableConfig {
8583
fun getCommunityMessageRequests(): Boolean
8684
fun isBlockCommunityMessageRequestsSet(): Boolean
8785

88-
fun getProFeatures(): ProFeatures
86+
fun getProFeatures(): ProProfileFeatures
8987
fun getProConfig(): ProConfig?
9088
fun getProAccessExpiryMs(): Long?
9189
}

library/src/main/java/network/loki/messenger/libsession_util/UserProfile.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package network.loki.messenger.libsession_util
22

33
import network.loki.messenger.libsession_util.pro.ProConfig
44
import network.loki.messenger.libsession_util.pro.ProProof
5-
import network.loki.messenger.libsession_util.protocol.ProFeatures
5+
import network.loki.messenger.libsession_util.protocol.ProProfileFeatures
66
import network.loki.messenger.libsession_util.util.ExpiryMode
77
import network.loki.messenger.libsession_util.util.UserPic
88

@@ -48,7 +48,7 @@ class UserProfile private constructor(pointer: Long) : ConfigBase(pointer), Muta
4848
external override fun setProAccessExpiryMs(epochMills: Long)
4949
external override fun removeProAccessExpiry()
5050
private external fun getProFeaturesRaw(): Long
51-
override fun getProFeatures(): ProFeatures = ProFeatures(getProFeaturesRaw())
51+
override fun getProFeatures(): ProProfileFeatures = ProProfileFeatures(getProFeaturesRaw())
5252
external override fun getProConfig(): ProConfig?
5353

5454
private external fun getProAccessExpiryMsOrZero(): Long

library/src/main/java/network/loki/messenger/libsession_util/pro/ProProof.kt

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import kotlinx.serialization.SerialName
55
import kotlinx.serialization.Serializable
66
import java.time.Instant
77

8+
typealias ProProofStatus = Int
9+
810
/**
911
* Represents a proof of Pro. This class is marked as @Serializable to represent the JSON structure
1012
* received from the Pro Backend.
@@ -51,24 +53,6 @@ data class ProProof(
5153
}
5254
}
5355

54-
enum class Status(internal val nativeValue: Int) {
55-
InvalidProBackendSignature(1),
56-
InvalidUserSignature(2),
57-
Valid(3),
58-
Expired(4),
59-
60-
;
61-
companion object {
62-
internal fun fromNativeValue(value: Int): Status {
63-
return entries.first { it.nativeValue == value }
64-
}
65-
66-
internal fun fromNativeValueOrNull(value: Int): Status? {
67-
return entries.firstOrNull { it.nativeValue == value }
68-
}
69-
}
70-
}
71-
7256
class ProSignedMessage(
7357
val data: ByteArray,
7458
val signature: ByteArray,
@@ -85,17 +69,15 @@ data class ProProof(
8569
senderED25519PubKey: ByteArray,
8670
now: Instant,
8771
signedMessage: ProSignedMessage? = null,
88-
): Status {
72+
): ProProofStatus {
8973
val signedMessageData = signedMessage?.data
9074
val signedMessageSignature = signedMessage?.signature
91-
val statusValue = nativeStatus(
75+
return nativeStatus(
9276
nowUnixTs = now.toEpochMilli(),
9377
verifyPubKey = senderED25519PubKey,
9478
signedMessageData = signedMessageData,
9579
signedMessageSignature = signedMessageSignature
9680
)
97-
98-
return Status.fromNativeValue(statusValue)
9981
}
10082

10183
private external fun nativeStatus(
@@ -104,4 +86,11 @@ data class ProProof(
10486
signedMessageData: ByteArray?,
10587
signedMessageSignature: ByteArray?
10688
): Int
89+
90+
companion object {
91+
const val STATUS_INVALID_PRO_BACKEND_SIGNATURE: ProProofStatus = 1
92+
const val STATUS_INVALID_USER_SIGNATURE: ProProofStatus = 2
93+
const val STATUS_VALID: ProProofStatus = 3
94+
const val STATUS_EXPIRED: ProProofStatus = 4
95+
}
10796
}
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,18 @@
11
package network.loki.messenger.libsession_util.protocol
22

33
import androidx.annotation.Keep
4-
import network.loki.messenger.libsession_util.pro.ProProof
54
import network.loki.messenger.libsession_util.util.Bytes
6-
import java.util.EnumSet
75

86
data class DecodedCommunityMessage(
9-
val proStatus: ProProof.Status?,
10-
val proProof: ProProof?,
11-
val proFeatures: ProFeatures,
7+
val decodedPro: DecodedPro?,
128
val contentPlainText: Bytes,
139
) {
1410
@Keep
1511
constructor(
16-
status: Int,
17-
proProof: ProProof?,
18-
proFeatures: Long,
12+
decodedPro: DecodedPro?,
1913
contentPlainText: ByteArray,
2014
): this(
21-
proStatus = ProProof.Status.fromNativeValueOrNull(status),
22-
proProof = proProof,
23-
proFeatures = ProFeatures(proFeatures),
15+
decodedPro = decodedPro,
2416
contentPlainText = Bytes(contentPlainText),
2517
)
26-
27-
init {
28-
check(proProof == null || proStatus in EnumSet.of(ProProof.Status.Expired, ProProof.Status.Valid)) {
29-
"proProof must be null unless proStatus is Expired or Valid"
30-
}
31-
}
32-
}
18+
}
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package network.loki.messenger.libsession_util.protocol
22

33
import androidx.annotation.Keep
4-
import network.loki.messenger.libsession_util.pro.ProProof
54
import network.loki.messenger.libsession_util.util.Bytes
65
import java.time.Instant
7-
import java.util.EnumSet
86

97
data class DecodedEnvelope(
108
val envelope: Envelope,
11-
val proStatus: ProProof.Status?,
12-
val proProof: ProProof?,
13-
val proFeatures: ProFeatures,
9+
val decodedPro: DecodedPro?,
1410
val contentPlainText: Bytes,
1511
val senderEd25519PubKey: Bytes,
1612
val senderX25519PubKey: Bytes,
@@ -19,27 +15,17 @@ data class DecodedEnvelope(
1915
@Keep
2016
constructor(
2117
envelope: Envelope,
22-
proStatus: Int,
23-
proProof: ProProof?,
24-
proFeatures: Long,
18+
decodedPro: DecodedPro?,
2519
contentPlainText: ByteArray,
2620
senderEd25519PubKey: ByteArray,
2721
senderX25519PubKey: ByteArray,
2822
timestampEpochMills: Long
2923
): this(
3024
envelope = envelope,
31-
proStatus = ProProof.Status.fromNativeValueOrNull(proStatus),
32-
proProof = proProof,
33-
proFeatures = ProFeatures(proFeatures),
3425
contentPlainText = Bytes(contentPlainText),
3526
senderEd25519PubKey = Bytes(senderEd25519PubKey),
3627
senderX25519PubKey = Bytes(senderX25519PubKey),
37-
timestamp = Instant.ofEpochMilli(timestampEpochMills)
28+
timestamp = Instant.ofEpochMilli(timestampEpochMills),
29+
decodedPro = decodedPro
3830
)
39-
40-
init {
41-
check(proProof == null || proStatus in EnumSet.of(ProProof.Status.Expired, ProProof.Status.Valid)) {
42-
"proProof must be null unless proStatus is Expired or Valid"
43-
}
44-
}
4531
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package network.loki.messenger.libsession_util.protocol
2+
3+
import network.loki.messenger.libsession_util.pro.ProProof
4+
import network.loki.messenger.libsession_util.pro.ProProofStatus
5+
6+
data class DecodedPro(
7+
val status: ProProofStatus,
8+
val proof: ProProof?,
9+
val proMessageFeatures: ProMessageFeatures,
10+
val proProfileFeatures: ProProfileFeatures,
11+
)

0 commit comments

Comments
 (0)