1
1
package org.xmtp.android.library
2
2
3
+ import com.google.protobuf.kotlin.toByteString
3
4
import io.grpc.Grpc
4
5
import io.grpc.InsecureChannelCredentials
5
6
import io.grpc.ManagedChannel
@@ -13,14 +14,22 @@ import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryRequest
13
14
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryResponse
14
15
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.Cursor
15
16
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.Envelope
17
+ import org.xmtp.proto.message.api.v1.MessageApiOuterClass.PagingInfo
16
18
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.PublishRequest
17
19
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.PublishResponse
18
20
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
19
21
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryResponse
22
+ import org.xmtp.proto.message.api.v1.MessageApiOuterClass.SortDirection
20
23
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.SubscribeRequest
24
+ import org.xmtp.proto.message.api.v1.queryResponse
25
+ import uniffi.xmtpv3.FfiCursor
21
26
import uniffi.xmtpv3.FfiEnvelope
27
+ import uniffi.xmtpv3.FfiPagingInfo
22
28
import uniffi.xmtpv3.FfiPublishRequest
29
+ import uniffi.xmtpv3.FfiSortDirection
23
30
import uniffi.xmtpv3.FfiV2ApiClient
31
+ import uniffi.xmtpv3.FfiV2QueryRequest
32
+ import uniffi.xmtpv3.FfiV2QueryResponse
24
33
import java.io.Closeable
25
34
import java.util.concurrent.TimeUnit
26
35
@@ -103,16 +112,7 @@ data class GRPCApiClient(
103
112
cursor : Cursor ? ,
104
113
): QueryResponse {
105
114
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)))
116
116
}
117
117
118
118
/* *
@@ -180,10 +180,86 @@ data class GRPCApiClient(
180
180
}
181
181
182
182
private fun envelopeToFFi (envelope : Envelope ): FfiEnvelope {
183
- FfiEnvelope (
183
+ return FfiEnvelope (
184
184
contentTopic = envelope.contentTopic,
185
185
timestampNs = envelope.timestampNs.toULong(),
186
186
message = envelope.message.toByteArray()
187
187
)
188
188
}
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
+ }
189
265
}
0 commit comments