diff --git a/privmx-endpoint/src/main/cpp/modules/Connection.cpp b/privmx-endpoint/src/main/cpp/modules/Connection.cpp index 3341ccda..bdc4b946 100644 --- a/privmx-endpoint/src/main/cpp/modules/Connection.cpp +++ b/privmx-endpoint/src/main/cpp/modules/Connection.cpp @@ -274,51 +274,6 @@ Java_com_simplito_java_privmx_1endpoint_modules_core_Connection_setUserVerifier( }); } -extern "C" -JNIEXPORT jobject JNICALL -Java_com_simplito_java_privmx_1endpoint_modules_core_Connection_getContextUsers( - JNIEnv *env, - jobject thiz, - jstring context_id -) { - JniContextUtils ctx(env); - if (ctx.nullCheck(context_id, "Context ID")) { - return nullptr; - } - - jobject result; - ctx.callResultEndpointApi( - &result, - [&ctx, &env, &thiz, &context_id]() { - - jclass arrayListCls = env->FindClass("java/util/ArrayList"); - jmethodID initMID = env->GetMethodID(arrayListCls, "", "()V"); - jmethodID addToListMID = env->GetMethodID(arrayListCls, "add", - "(Ljava/lang/Object;)Z"); - jobject array = env->NewObject(arrayListCls, initMID); - - - std::vector users = getConnection( - env, - thiz - )->getContextUsers(ctx.jString2string(context_id)); - - for (auto &user: users) { - env->CallBooleanMethod( - array, - addToListMID, - privmx::wrapper::userInfo2Java(ctx, user) - ); - } - - return array; - }); - if (ctx->ExceptionCheck()) { - return nullptr; - } - return result; -} - extern "C" JNIEXPORT jobject JNICALL Java_com_simplito_java_privmx_1endpoint_modules_core_Connection_listContextUsers( @@ -389,4 +344,118 @@ Java_com_simplito_java_privmx_1endpoint_modules_core_Connection_listContextUsers return nullptr; } return result; +} + +extern "C" +JNIEXPORT jobject JNICALL +Java_com_simplito_java_privmx_1endpoint_modules_core_Connection_subscribeFor( + JNIEnv *env, + jobject thiz, + jobject subscription_queries +) { + JniContextUtils ctx(env); + if (ctx.nullCheck(subscription_queries, "Subscription queries")) { + return nullptr; + } + + jobject result; + ctx.callResultEndpointApi( + &result, + [&ctx, &env, &thiz, &subscription_queries]() { + jclass arrayListCls = env->FindClass("java/util/ArrayList"); + jmethodID initMID = env->GetMethodID(arrayListCls, "", "()V"); + jmethodID addToListMID = env->GetMethodID(arrayListCls, "add", + "(Ljava/lang/Object;)Z"); + + auto subscription_queries_arr = ctx.jObject2jArray(subscription_queries); + auto subscription_queries_c = std::vector(); + + for (int i = 0; i < ctx->GetArrayLength(subscription_queries_arr); i++) { + jobject arrayElement = ctx->GetObjectArrayElement(subscription_queries_arr, i); + if (ctx.nullCheck(arrayElement, "Subscription queries array elements")) { + return nullptr; + } + subscription_queries_c.push_back(ctx.jString2string((jstring) arrayElement)); + } + + auto subscription_ids_c = getConnection(env, thiz)->subscribeFor( + subscription_queries_c); + + jobject array = env->NewObject(arrayListCls, initMID); + for (auto &id: subscription_ids_c) { + ctx->CallBooleanMethod( + array, + addToListMID, + ctx->NewStringUTF(id.c_str()) + ); + } + return array; + } + ); + if (ctx->ExceptionCheck()) { + return nullptr; + } + return result; +} + +extern "C" +JNIEXPORT void JNICALL +Java_com_simplito_java_privmx_1endpoint_modules_core_Connection_unsubscribeFrom( + JNIEnv *env, + jobject thiz, + jobject subscription_ids +) { + JniContextUtils ctx(env); + if (ctx.nullCheck(subscription_ids, "Subscription IDs")) { + return; + } + + ctx.callVoidEndpointApi( + [&ctx, &env, &thiz, &subscription_ids]() { + auto subscription_ids_arr = ctx.jObject2jArray(subscription_ids); + auto subscription_ids_c = std::vector(); + + for (int i = 0; i < ctx->GetArrayLength(subscription_ids_arr); i++) { + jobject arrayElement = ctx->GetObjectArrayElement(subscription_ids_arr, i); + if (ctx.nullCheck(arrayElement, "Subscription ids array elements")) { + return nullptr; + } + subscription_ids_c.push_back(ctx.jString2string((jstring) arrayElement)); + } + + getConnection(env, thiz)->unsubscribeFrom(subscription_ids_c); + } + ); +} + +extern "C" +JNIEXPORT jstring JNICALL +Java_com_simplito_java_privmx_1endpoint_modules_core_Connection_buildSubscriptionQuery( + JNIEnv *env, + jobject thiz, + jlong event_type, + jlong selector_type, + jstring selector_id +) { + JniContextUtils ctx(env); + if (ctx.nullCheck(selector_id, "Selector ID")) { + return nullptr; + } + + jstring result; + ctx.callResultEndpointApi( + &result, + [&ctx, &env, &thiz, &event_type, &selector_type, &selector_id]() { + auto result = getConnection(env, thiz)->buildSubscriptionQuery( + static_cast(event_type), + static_cast(selector_type), + ctx.jString2string(selector_id) + ); + return ctx->NewStringUTF(result.c_str()); + } + ); + if (ctx->ExceptionCheck()) { + return nullptr; + } + return result; } \ No newline at end of file diff --git a/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/model/events/eventSelectorTypes/CoreEventSelectorType.java b/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/model/events/eventSelectorTypes/CoreEventSelectorType.java new file mode 100644 index 00000000..ccc3744b --- /dev/null +++ b/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/model/events/eventSelectorTypes/CoreEventSelectorType.java @@ -0,0 +1,14 @@ +package com.simplito.java.privmx_endpoint.model.events.eventSelectorTypes; + +/** + * Specifies the type of identifier used to select a core event. + * This enum defines the possible types of selectors for the core events. + * + * @category core + */ +public enum CoreEventSelectorType implements EventSelectorType { + /** + * Selects events based on the ID of the context. + */ + CONTEXT_ID +} \ No newline at end of file diff --git a/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/model/events/eventTypes/CoreEventType.java b/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/model/events/eventTypes/CoreEventType.java new file mode 100644 index 00000000..613eae68 --- /dev/null +++ b/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/model/events/eventTypes/CoreEventType.java @@ -0,0 +1,23 @@ +package com.simplito.java.privmx_endpoint.model.events.eventTypes; + +/** + * Defines the types of core events for which a client can subscribe. + * This enum lists the various actions or changes that can happen on core functionalities, + * allowing observers to be notified of specific occurrences. + * + * @category core + */ +public enum CoreEventType implements EventType{ + /** + * Type of event triggered when a new user is added within a context. + */ + USER_ADD, + /** + * Type of event triggered when a user is deleted from context. + */ + USER_REMOVE, + /** + * Type of event triggered when an user status is changed. + */ + USER_STATUS +} \ No newline at end of file diff --git a/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/modules/core/Connection.java b/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/modules/core/Connection.java index 8df426d9..9b9cd261 100644 --- a/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/modules/core/Connection.java +++ b/privmx-endpoint/src/main/java/com/simplito/java/privmx_endpoint/modules/core/Connection.java @@ -15,6 +15,8 @@ import com.simplito.java.privmx_endpoint.model.PKIVerificationOptions; import com.simplito.java.privmx_endpoint.model.PagingList; import com.simplito.java.privmx_endpoint.model.UserInfo; +import com.simplito.java.privmx_endpoint.model.events.eventSelectorTypes.CoreEventSelectorType; +import com.simplito.java.privmx_endpoint.model.events.eventTypes.CoreEventType; import com.simplito.java.privmx_endpoint.model.exceptions.NativeException; import com.simplito.java.privmx_endpoint.model.exceptions.PrivmxException; @@ -273,6 +275,34 @@ public native PagingList listContextUsers( */ public native void setUserVerifier(UserVerifierInterface userVerifier) throws IllegalStateException, PrivmxException, NativeException; + /** + * Subscribe for the Context events on the given subscription query. + * + * @param subscriptionQueries List of queries + * @return List of subscriptionIds in matching order to subscriptionQueries + */ + public native List subscribeFor(List subscriptionQueries); + + /** + * Unsubscribe from events for the given subscriptionId. + * + * @param subscriptionIds List of subscriptionId + */ + public native void unsubscribeFrom(List subscriptionIds); + + /** + * Generate subscription Query for the Context events. + * + * @param eventType Type of event which you listen for + * @param selectorType Scope on which you listen for events + * @param selectorId ID of the selector + */ + public String buildSubscriptionQuery(CoreEventType eventType, CoreEventSelectorType selectorType, String selectorId) { + return buildSubscriptionQuery((long) eventType.ordinal(), (long) selectorType.ordinal(), selectorId); + } + + private native String buildSubscriptionQuery(long eventType, long selectorType, String selectorId); + /** * If there is an active connection then it * disconnects from PrivMX Bridge and frees memory making this instance not reusable.