feat: start to implement group list info classes and wrappers and refactor to use library based hashes

This commit is contained in:
0x330a 2023-02-27 17:31:40 +11:00
parent c06c7eab19
commit c351cd6038
No known key found for this signature in database
GPG Key ID: 267811D6E6A2698C
9 changed files with 142 additions and 38 deletions

View File

@ -121,29 +121,4 @@ class ConfigFactory(private val context: Context,
} }
} }
override fun appendHash(configObject: ConfigBase, hash: String) {
when (configObject) {
is UserProfile -> userHashes.add(hash)
is Contacts -> contactsHashes.add(hash)
is ConversationVolatileConfig -> convoHashes.add(hash)
else -> throw UnsupportedOperationException("Can't support type of ${configObject::class.simpleName} yet")
}
}
override fun getHashesFor(forConfigObject: ConfigBase): List<String> =
when (forConfigObject) {
is UserProfile -> userHashes.toList()
is Contacts -> contactsHashes.toList()
is ConversationVolatileConfig -> convoHashes.toList()
else -> throw UnsupportedOperationException("Can't support type of ${forConfigObject::class.simpleName} yet")
}
override fun removeHashesFor(forConfigObject: ConfigBase, deletedHashes: Set<String>) =
when (forConfigObject) {
is UserProfile -> userHashes.removeAll(deletedHashes)
is Contacts -> contactsHashes.removeAll(deletedHashes)
is ConversationVolatileConfig -> convoHashes.removeAll(deletedHashes)
else -> throw UnsupportedOperationException("Can't support type of ${forConfigObject::class.simpleName} yet")
}
} }

View File

@ -130,7 +130,19 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_removeObsoleteHashes(JNI
for (int i = 0; i < number; i++) { for (int i = 0; i < number; i++) {
auto jElement = (jstring) env->GetObjectArrayElement(to_remove, i); auto jElement = (jstring) env->GetObjectArrayElement(to_remove, i);
auto element_as_string = env->GetStringUTFChars(jElement, nullptr); auto element_as_string = env->GetStringUTFChars(jElement, nullptr);
conf->confirm_removed(element_as_string); // TODO: uncomment when this is re-implemented
// conf->confirm_removed(element_as_string);
env->ReleaseStringUTFChars(jElement, element_as_string); env->ReleaseStringUTFChars(jElement, element_as_string);
} }
} }
extern "C"
JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_ConfigBase_obsoleteHashes(JNIEnv *env, jobject thiz) {
auto conf = ptrToConfigBase(env, thiz);
jclass stack = env->FindClass("java/util/Stack");
jmethodID init = env->GetMethodID(stack, "<init>", "()V");
jobject our_stack = env->NewObject(stack, init);
jmethodID push = env->GetMethodID(stack, "push", "(Ljava/lang/Object;)Ljava/lang/Object;");
// TODO: implement obsoleteHashes()
return our_stack;
}

View File

@ -31,6 +31,60 @@ Java_network_loki_messenger_libsession_1util_UserGroupsConfig_00024Companion_new
jmethodID constructor = env->GetMethodID(contactsClass, "<init>", "(J)V"); jmethodID constructor = env->GetMethodID(contactsClass, "<init>", "(J)V");
jobject newConfig = env->NewObject(contactsClass, constructor, reinterpret_cast<jlong>(user_groups)); jobject newConfig = env->NewObject(contactsClass, constructor, reinterpret_cast<jlong>(user_groups));
user_groups->get_or_construct_legacy_group()
return newConfig; return newConfig;
} }
#pragma clang diagnostic pop #pragma clang diagnostic pop
extern "C"
JNIEXPORT jint JNICALL
Java_network_loki_messenger_libsession_1util_util_GroupInfo_00024LegacyGroupInfo_00024Companion_NAME_1MAX_1LENGTH(
JNIEnv *env, jobject thiz) {
return session::config::legacy_group_info::NAME_MAX_LENGTH;
}
extern "C"
JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_UserGroupsConfig_getCommunityInfo(JNIEnv *env,
jobject thiz,
jstring base_url,
jstring room) {
// TODO: implement getCommunityInfo()
}
extern "C"
JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_UserGroupsConfig_getLegacyGroupInfo(JNIEnv *env,
jobject thiz,
jstring session_id) {
// TODO: implement getLegacyGroupInfo()
}
extern "C"
JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_UserGroupsConfig_getOrConstructCommunityInfo(
JNIEnv *env, jobject thiz, jstring base_url, jstring room, jstring pub_key_hex) {
// TODO: implement getOrConstructCommunityInfo()
}
extern "C"
JNIEXPORT jobject JNICALL
Java_network_loki_messenger_libsession_1util_UserGroupsConfig_getOrConstructLegacyGroupInfo(
JNIEnv *env, jobject thiz, jstring session_id) {
// TODO: implement getOrConstructLegacyGroupInfo()
}
extern "C"
JNIEXPORT void JNICALL
Java_network_loki_messenger_libsession_1util_UserGroupsConfig_set__Lnetwork_loki_messenger_libsession_1util_util_GroupInfo_CommunityInfo_2(
JNIEnv *env, jobject thiz, jobject community_info) {
// TODO: implement set()
}
extern "C"
JNIEXPORT void JNICALL
Java_network_loki_messenger_libsession_1util_UserGroupsConfig_set__Lnetwork_loki_messenger_libsession_1util_util_GroupInfo_LegacyGroupInfo_2(
JNIEnv *env, jobject thiz, jobject legacy_group_info) {
// TODO: implement set()
}

View File

@ -11,4 +11,44 @@ inline session::config::UserGroups* ptrToUserGroups(JNIEnv *env, jobject obj) {
return (session::config::UserGroups*) env->GetLongField(obj, pointerField); return (session::config::UserGroups*) env->GetLongField(obj, pointerField);
} }
/**
val sessionId: String,
val name: String,
val members: Map<String, Boolean>,
val hidden: Boolean,
val encPubKey: String,
val encSecKey: String,
val priority: Int
*/
inline session::config::legacy_group_info deserialize_legacy_group_info(JNIEnv *env, jobject info) {
auto clazz = env->FindClass("network/loki/messenger/libsession_util/util/GroupInfo$LegacyGroupInfo");
auto id_field = env->GetFieldID(clazz, "sessionId", "Ljava/lang/String;");
auto name_field = env->GetFieldID(clazz, "name", "Ljava/lang/String;");
auto members_field = env->GetFieldID(clazz, "members", "Ljava/util/Map;");
auto hidden_field = env->GetFieldID(clazz, "hidden", "Z");
auto enc_pub_key_field = env->GetFieldID(clazz, "encPubKey", "Ljava/lang/String;");
auto enc_sec_key_field = env->GetFieldID(clazz, "encSecKey", "Ljava/lang/String;");
auto priority_field = env->GetFieldID(clazz, "priority", "I");
jstring id = static_cast<jstring>(env->GetObjectField(info, id_field));
jstring name = static_cast<jstring>(env->GetObjectField(info, name_field));
jobject members_map = env->GetObjectField(info, members_field);
bool hidden = env->GetBooleanField(info, hidden_field);
jstring enc_pub_key = static_cast<jstring>(env->GetObjectField(info, enc_pub_key_field));
jstring enc_sec_key = static_cast<jstring>(env->GetObjectField(info, enc_sec_key_field));
int priority = env->GetIntField(info, priority_field);
}
inline std::map<std::string, bool> deserialize_members(JNIEnv *env, jobject members_map) {
}
inline jobject serialize_legacy_group_info(JNIEnv *env, session::config::legacy_group_info info) {
return nullptr;
}
inline jobject serialize_members(JNIEnv *env, std::map<std::string, bool> members_map) {
return nullptr;
}
#endif //SESSION_ANDROID_USER_GROUPS_H #endif //SESSION_ANDROID_USER_GROUPS_H

View File

@ -3,6 +3,7 @@ package network.loki.messenger.libsession_util
import network.loki.messenger.libsession_util.util.ConfigWithSeqNo import network.loki.messenger.libsession_util.util.ConfigWithSeqNo
import network.loki.messenger.libsession_util.util.Contact import network.loki.messenger.libsession_util.util.Contact
import network.loki.messenger.libsession_util.util.Conversation import network.loki.messenger.libsession_util.util.Conversation
import network.loki.messenger.libsession_util.util.GroupInfo
import network.loki.messenger.libsession_util.util.UserPic import network.loki.messenger.libsession_util.util.UserPic
import org.session.libsignal.protos.SignalServiceProtos.SharedConfigMessage.Kind import org.session.libsignal.protos.SignalServiceProtos.SharedConfigMessage.Kind
@ -33,6 +34,7 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
external fun encryptionDomain(): String external fun encryptionDomain(): String
external fun confirmPushed(seqNo: Long, newHash: String) external fun confirmPushed(seqNo: Long, newHash: String)
external fun merge(toMerge: Array<Pair<String,ByteArray>>): Int external fun merge(toMerge: Array<Pair<String,ByteArray>>): Int
external fun obsoleteHashes(): List<String>
external fun removeObsoleteHashes(toRemove: Array<String>) external fun removeObsoleteHashes(toRemove: Array<String>)
external fun configNamespace(): Int external fun configNamespace(): Int
@ -153,6 +155,11 @@ class UserGroupsConfig(pointer: Long): ConfigBase(pointer) {
external fun newInstance(ed25519SecretKey: ByteArray, initialDump: ByteArray): UserGroupsConfig external fun newInstance(ed25519SecretKey: ByteArray, initialDump: ByteArray): UserGroupsConfig
} }
external fun getCommunityInfo(baseUrl: String, room: String): GroupInfo.CommunityInfo?
external fun getLegacyGroupInfo(sessionId: String): GroupInfo.LegacyGroupInfo?
external fun getOrConstructCommunityInfo(baseUrl: String, room: String, pubKeyHex: String): GroupInfo.CommunityInfo
external fun getOrConstructLegacyGroupInfo(sessionId: String): GroupInfo.LegacyGroupInfo
external fun set(communityInfo: GroupInfo.CommunityInfo)
external fun set(legacyGroupInfo: GroupInfo.LegacyGroupInfo)
} }

View File

@ -0,0 +1,22 @@
package network.loki.messenger.libsession_util.util
sealed class GroupInfo {
data class CommunityInfo(val community: Conversation.Community, val priority: Int) : GroupInfo()
data class LegacyGroupInfo(
val sessionId: String,
val name: String,
val members: Map<String, Boolean>,
val hidden: Boolean,
val encPubKey: String,
val encSecKey: String,
val priority: Int
): GroupInfo() {
companion object {
@Suppress("FunctionName")
external fun NAME_MAX_LENGTH(): Int
}
}
}

View File

@ -73,7 +73,7 @@ data class ConfigurationSyncJob(val destination: Destination): Job {
} }
val toDeleteRequest = configsRequiringPush.map { base -> val toDeleteRequest = configsRequiringPush.map { base ->
configFactory.getHashesFor(base) base.obsoleteHashes()
// accumulate by adding together // accumulate by adding together
}.reduce(List<String>::plus).let { toDeleteFromAllNamespaces -> }.reduce(List<String>::plus).let { toDeleteFromAllNamespaces ->
if (toDeleteFromAllNamespaces.isEmpty()) null if (toDeleteFromAllNamespaces.isEmpty()) null
@ -137,11 +137,9 @@ data class ConfigurationSyncJob(val destination: Destination): Job {
// confirm pushed seqno // confirm pushed seqno
val thisSeqNo = toPushMessage.seqNo val thisSeqNo = toPushMessage.seqNo
config.confirmPushed(thisSeqNo, insertHash) config.confirmPushed(thisSeqNo, insertHash)
config.
// wipe any of the existing hashes which we deleted (they may or may not be in this namespace) // wipe any of the existing hashes which we deleted (they may or may not be in this namespace)
if (configFactory.removeHashesFor(config, deletedHashes.toSet())) { config.removeObsoleteHashes(deletedHashes.toTypedArray())
Log.d(TAG, "Successfully removed the deleted hashes from ${config.javaClass.simpleName}") Log.d(TAG, "Successfully removed the deleted hashes from ${config.javaClass.simpleName}")
}
// dump and write config after successful // dump and write config after successful
if (config.needsDump()) { // usually this will be true? if (config.needsDump()) { // usually this will be true?
configFactory.persist(config) configFactory.persist(config)

View File

@ -137,7 +137,7 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
namespace, namespace,
updateLatestHash = false, updateLatestHash = false,
updateStoredHashes = false, updateStoredHashes = false,
).filter { (_, hash) -> !configFactory.getHashesFor(forConfigObject).contains(hash) } ).filter { (_, hash) -> !forConfigObject.obsoleteHashes().contains(hash) }
if (messages.isEmpty()) { if (messages.isEmpty()) {
// no new messages to process // no new messages to process
@ -153,8 +153,7 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
return@forEach return@forEach
} }
Log.d("Loki-DBG", "Merging config of kind ${message.kind} into ${forConfigObject.javaClass.simpleName}") Log.d("Loki-DBG", "Merging config of kind ${message.kind} into ${forConfigObject.javaClass.simpleName}")
forConfigObject.merge(message.data) forConfigObject.merge(hash!! to message.data)
configFactory.appendHash(forConfigObject, hash!!)
} catch (e: Exception) { } catch (e: Exception) {
Log.e("Loki", e) Log.e("Loki", e)
} }

View File

@ -10,9 +10,6 @@ interface ConfigFactoryProtocol {
val contacts: Contacts? val contacts: Contacts?
val convoVolatile: ConversationVolatileConfig? val convoVolatile: ConversationVolatileConfig?
fun persist(forConfigObject: ConfigBase) fun persist(forConfigObject: ConfigBase)
fun appendHash(configObject: ConfigBase, hash: String)
fun getHashesFor(forConfigObject: ConfigBase): List<String>
fun removeHashesFor(config: ConfigBase, deletedHashes: Set<String>): Boolean
} }
interface ConfigFactoryUpdateListener { interface ConfigFactoryUpdateListener {