From d79f871807752f94b799ce174429e3bfe9b01af4 Mon Sep 17 00:00:00 2001
From: Koen <koen@pop-os.localdomain>
Date: Thu, 15 Jun 2023 11:38:31 +0200
Subject: [PATCH] Implemented updated QueryReferences API.

---
 .../polycentric/core/ProcessHandleTests.kt    | 15 +++----
 .../com/futo/polycentric/core/ApiMethods.kt   |  8 +++-
 .../futo/polycentric/protos/protocol.proto    | 39 ++++++++++++-------
 3 files changed, 39 insertions(+), 23 deletions(-)

diff --git a/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt b/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt
index 8b60157..714f843 100644
--- a/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt
+++ b/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt
@@ -10,10 +10,8 @@ import org.junit.Assert.*
 import org.junit.Test
 import org.junit.runner.RunWith
 import userpackage.Protocol
-import userpackage.Protocol.QueryReferencesResponseItem
 import java.io.ByteArrayOutputStream
 
-
 @RunWith(AndroidJUnit4::class)
 class ProcessHandleTests {
     @Test
@@ -232,16 +230,19 @@ class ProcessHandleTests {
         babbage.fullyBackfillServers()
         turing.fullyBackfillServers()
 
-        val queryReferences = ApiMethods.getQueryReferences(SERVER, subject, ContentType.POST.value, null,
-            arrayListOf(Protocol.CountLWWElementReferences.newBuilder()
+        val queryReferences = ApiMethods.getQueryReferences(SERVER, subject, null,
+            Protocol.QueryReferencesRequestEvents.newBuilder()
+                .setFromType(ContentType.POST.value)
+                .build(),
+            arrayListOf(Protocol.QueryReferencesRequestCountLWWElementReferences.newBuilder()
                 .setFromType(ContentType.OPINION.value)
                 .setValue(ByteString.copyFrom(Opinion.like.data))
                 .build(),
-                Protocol.CountLWWElementReferences.newBuilder()
+                Protocol.QueryReferencesRequestCountLWWElementReferences.newBuilder()
                     .setFromType(ContentType.OPINION.value)
                     .setValue(ByteString.copyFrom(Opinion.dislike.data))
                     .build()),
-            arrayListOf(Protocol.CountReferences.newBuilder()
+            arrayListOf(Protocol.QueryReferencesRequestCountReferences.newBuilder()
                 .setFromType(ContentType.POST.value)
                 .build())
         )
@@ -249,7 +250,7 @@ class ProcessHandleTests {
         assertEquals(rootPosts.count(), queryReferences.itemsCount)
 
         val pointerToString = { pointer: Pointer -> pointer.toProto().toByteArray().toBase64() }
-        val pointerToItem: HashMap<String, QueryReferencesResponseItem> = hashMapOf()
+        val pointerToItem: HashMap<String, Protocol.QueryReferencesResponseEventItem> = hashMapOf()
 
         for (item in queryReferences.itemsList) {
             assertTrue(item.hasEvent())
diff --git a/app/src/main/java/com/futo/polycentric/core/ApiMethods.kt b/app/src/main/java/com/futo/polycentric/core/ApiMethods.kt
index 30b5aa4..d56b13e 100644
--- a/app/src/main/java/com/futo/polycentric/core/ApiMethods.kt
+++ b/app/src/main/java/com/futo/polycentric/core/ApiMethods.kt
@@ -99,9 +99,13 @@ class ApiMethods {
             return Protocol.Events.parseFrom(result.get())
         }
 
-        fun getQueryReferences(server: String, reference: Protocol.Reference, fromType: Long? = null, cursor: ByteArray? = null, countLwwElementReferences: List<Protocol.CountLWWElementReferences>? = null, countReferences: List<Protocol.CountReferences>? = null): Protocol.QueryReferencesResponse {
+        fun getQueryReferences(server: String, reference: Protocol.Reference, cursor: ByteArray? = null,
+           requestEvents: Protocol.QueryReferencesRequestEvents? = null,
+           countLwwElementReferences: List<Protocol.QueryReferencesRequestCountLWWElementReferences>? = null,
+           countReferences: List<Protocol.QueryReferencesRequestCountReferences>? = null): Protocol.QueryReferencesResponse
+        {
             val builder = Protocol.QueryReferencesRequest.newBuilder().setReference(reference)
-            if (fromType != null) builder.fromType = fromType
+            if (requestEvents != null) builder.requestEvents = requestEvents
             if (cursor != null) builder.cursor = ByteString.copyFrom(cursor)
             if (countLwwElementReferences != null) builder.addAllCountLwwElementReferences(countLwwElementReferences)
             if (countReferences != null) builder.addAllCountReferences(countReferences)
diff --git a/app/src/main/proto/com/futo/polycentric/protos/protocol.proto b/app/src/main/proto/com/futo/polycentric/protos/protocol.proto
index f6a28da..c8edbb8 100644
--- a/app/src/main/proto/com/futo/polycentric/protos/protocol.proto
+++ b/app/src/main/proto/com/futo/polycentric/protos/protocol.proto
@@ -195,34 +195,45 @@ message RepeatedUInt64 {
     repeated uint64 numbers = 1;
 }
 
-message CountLWWElementReferences {
+// start /query_references API
+
+message QueryReferencesRequest {
+             Reference                                       reference                    = 1;
+    optional bytes                                           cursor                       = 2;
+    optional QueryReferencesRequestEvents                    request_events               = 3;
+    repeated QueryReferencesRequestCountLWWElementReferences count_lww_element_references = 4;
+    repeated QueryReferencesRequestCountReferences           count_references             = 5;
+}
+
+message QueryReferencesRequestEvents {
+    optional uint64                                          from_type                    = 1;
+    repeated QueryReferencesRequestCountLWWElementReferences count_lww_element_references = 2;
+    repeated QueryReferencesRequestCountReferences           count_references             = 3;
+}
+
+message QueryReferencesRequestCountLWWElementReferences {
              bytes  value     = 1;
     optional uint64 from_type = 2;
 }
 
-message CountReferences {
+message QueryReferencesRequestCountReferences {
     optional uint64 from_type = 1;
 }
 
-message QueryReferencesRequest {
-             Reference                 reference                    = 1;
-    optional bytes                     cursor                       = 2;
-    optional uint64                    from_type                    = 3;
-    repeated CountLWWElementReferences count_lww_element_references = 4;
-    repeated CountReferences           count_references             = 5;
-}
-
-message QueryReferencesResponseItem {
+message QueryReferencesResponseEventItem {
              SignedEvent event  = 1;
     repeated uint64      counts = 2;
 }
 
 message QueryReferencesResponse {
-    repeated QueryReferencesResponseItem items          = 1;
-    repeated SignedEvent                 related_events = 2;
-    optional bytes                       cursor         = 3;
+    repeated QueryReferencesResponseEventItem items          = 1;
+    repeated SignedEvent                      related_events = 2;
+    optional bytes                            cursor         = 3;
+    repeated uint64                           counts         = 4;
 }
 
+// end /query_references API
+
 message URLInfo {
     // url_type 1 = URLInfoSystemLink
     // url_type 2 = URLInfoEventLink
-- 
GitLab