Skip to content

Commit b6e4646

Browse files
committed
do it all for query
1 parent cb2986d commit b6e4646

File tree

1 file changed

+87
-11
lines changed

1 file changed

+87
-11
lines changed

library/src/main/java/org/xmtp/android/library/ApiClient.kt

+87-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.xmtp.android.library
22

3+
import com.google.protobuf.kotlin.toByteString
34
import io.grpc.Grpc
45
import io.grpc.InsecureChannelCredentials
56
import io.grpc.ManagedChannel
@@ -13,14 +14,22 @@ import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryRequest
1314
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryResponse
1415
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.Cursor
1516
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.Envelope
17+
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.PagingInfo
1618
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.PublishRequest
1719
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.PublishResponse
1820
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
1921
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryResponse
22+
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.SortDirection
2023
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.SubscribeRequest
24+
import org.xmtp.proto.message.api.v1.queryResponse
25+
import uniffi.xmtpv3.FfiCursor
2126
import uniffi.xmtpv3.FfiEnvelope
27+
import uniffi.xmtpv3.FfiPagingInfo
2228
import uniffi.xmtpv3.FfiPublishRequest
29+
import uniffi.xmtpv3.FfiSortDirection
2330
import uniffi.xmtpv3.FfiV2ApiClient
31+
import uniffi.xmtpv3.FfiV2QueryRequest
32+
import uniffi.xmtpv3.FfiV2QueryResponse
2433
import java.io.Closeable
2534
import java.util.concurrent.TimeUnit
2635

@@ -103,16 +112,7 @@ data class GRPCApiClient(
103112
cursor: Cursor?,
104113
): QueryResponse {
105114
val request = makeQueryRequest(topic, pagination, cursor)
106-
val headers = Metadata()
107-
108-
authToken?.let { token ->
109-
headers.put(AUTHORIZATION_HEADER_KEY, "Bearer $token")
110-
}
111-
headers.put(CLIENT_VERSION_HEADER_KEY, Constants.VERSION)
112-
if (appVersion != null) {
113-
headers.put(APP_VERSION_HEADER_KEY, appVersion)
114-
}
115-
return rustV2Client.query(request, headers = headers)
115+
return queryResponseFromFFi(rustV2Client.query(queryRequestToFFi(request)))
116116
}
117117

118118
/**
@@ -180,10 +180,86 @@ data class GRPCApiClient(
180180
}
181181

182182
private fun envelopeToFFi(envelope: Envelope): FfiEnvelope {
183-
FfiEnvelope(
183+
return FfiEnvelope(
184184
contentTopic = envelope.contentTopic,
185185
timestampNs = envelope.timestampNs.toULong(),
186186
message = envelope.message.toByteArray()
187187
)
188188
}
189+
190+
private fun envelopeFromFFi(envelope: FfiEnvelope): Envelope {
191+
return Envelope.newBuilder().also {
192+
it.contentTopic = envelope.contentTopic,
193+
it.timestampNs = envelope.timestampNs.toLong()
194+
it.message = envelope.message.toByteString()
195+
}.build()
196+
}
197+
198+
private fun queryRequestToFFi(request: QueryRequest): FfiV2QueryRequest {
199+
return FfiV2QueryRequest(
200+
contentTopics = request.contentTopicsList,
201+
startTimeNs = request.startTimeNs.toULong(),
202+
endTimeNs = request.endTimeNs.toULong(),
203+
pagingInfo = pagingInfoToFFi(request.pagingInfo)
204+
)
205+
}
206+
207+
private fun queryResponseFromFFi(response: FfiV2QueryResponse): QueryResponse {
208+
return QueryResponse.newBuilder().also { queryResponse ->
209+
queryResponse.addAllEnvelopes(response.envelopes.map { envelopeFromFFi(it) })
210+
response.pagingInfo?.let {
211+
queryResponse.pagingInfo = pagingInfoFromFFi(it)
212+
}
213+
}.build()
214+
}
215+
216+
private fun pagingInfoFromFFi(info: FfiPagingInfo): PagingInfo {
217+
return PagingInfo.newBuilder().also {
218+
it.limit = info.limit.toInt()
219+
info.cursor?.let { cursor ->
220+
it.cursor = cursorFromFFi(cursor)
221+
}
222+
it.direction = directionFromFfi(info.direction)
223+
}.build()
224+
}
225+
226+
private fun pagingInfoToFFi(info: PagingInfo): FfiPagingInfo {
227+
return FfiPagingInfo(
228+
limit = info.limit.toUInt(),
229+
cursor = cursorToFFi(info.cursor),
230+
direction = directionToFfi(info.direction)
231+
)
232+
}
233+
234+
private fun directionToFfi(direction: SortDirection): FfiSortDirection {
235+
return when (direction) {
236+
SortDirection.SORT_DIRECTION_ASCENDING -> FfiSortDirection.ASCENDING
237+
SortDirection.SORT_DIRECTION_DESCENDING -> FfiSortDirection.DESCENDING
238+
else -> FfiSortDirection.UNSPECIFIED
239+
}
240+
}
241+
242+
private fun directionFromFfi(direction: FfiSortDirection): SortDirection {
243+
return when (direction) {
244+
FfiSortDirection.ASCENDING -> SortDirection.SORT_DIRECTION_ASCENDING
245+
FfiSortDirection.DESCENDING -> SortDirection.SORT_DIRECTION_DESCENDING
246+
else -> SortDirection.SORT_DIRECTION_UNSPECIFIED
247+
}
248+
}
249+
250+
private fun cursorToFFi(cursor: Cursor): FfiCursor {
251+
return FfiCursor(
252+
digest = cursor.index.digest.toByteArray(),
253+
senderTimeNs = cursor.index.senderTimeNs.toULong()
254+
)
255+
}
256+
257+
private fun cursorFromFFi(cursor: FfiCursor): Cursor {
258+
return Cursor.newBuilder().also {
259+
it.index.toBuilder().also { index ->
260+
index.digest = cursor.digest.toByteString()
261+
index.senderTimeNs = cursor.senderTimeNs.toLong()
262+
}.build()
263+
}.build()
264+
}
189265
}

0 commit comments

Comments
 (0)