diff --git a/app/src/main/java/io/aayush/relabs/network/XDAInterface.kt b/app/src/main/java/io/aayush/relabs/network/XDAInterface.kt index 0eb9739..be433aa 100644 --- a/app/src/main/java/io/aayush/relabs/network/XDAInterface.kt +++ b/app/src/main/java/io/aayush/relabs/network/XDAInterface.kt @@ -7,6 +7,10 @@ import io.aayush.relabs.network.data.node.Nodes import io.aayush.relabs.network.data.post.PostInfo import io.aayush.relabs.network.data.post.PostReply import io.aayush.relabs.network.data.react.PostReact +import io.aayush.relabs.network.data.search.Order +import io.aayush.relabs.network.data.search.PostSearch +import io.aayush.relabs.network.data.search.SearchResultNode +import io.aayush.relabs.network.data.search.SearchResultThread import io.aayush.relabs.network.data.thread.ThreadInfo import io.aayush.relabs.network.data.thread.Threads import io.aayush.relabs.network.data.user.Me @@ -105,4 +109,25 @@ interface XDAInterface { @POST("audapp-push-subscriptions") suspend fun postExpoPushToken(@Body body: RequestBody): Response + + @POST("audapp-search") + suspend fun postSearch( + @Query("keywords") query: String, + @Query("search_type") type: String, + @Query("c[container_only]") searchThreadConstraint: Int? = null, + @Query("c[title_only]") searchTitleConstraint: Int? = null, + @Query("order") order: String = Order.RELEVANCE.value, + ): Response + + @POST("audapp-search/{id}") + suspend fun getSearchResultsForThread( + @Path("id") searchID: Int, + @Query("page") page: Int? = null, + ): Response + + @POST("audapp-search/{id}") + suspend fun getSearchResultsForNode( + @Path("id") searchID: Int, + @Query("page") page: Int? = null, + ): Response } diff --git a/app/src/main/java/io/aayush/relabs/network/XDARepository.kt b/app/src/main/java/io/aayush/relabs/network/XDARepository.kt index bc4b1e7..84d2f76 100644 --- a/app/src/main/java/io/aayush/relabs/network/XDARepository.kt +++ b/app/src/main/java/io/aayush/relabs/network/XDARepository.kt @@ -11,12 +11,14 @@ import io.aayush.relabs.network.data.post.PostInfo import io.aayush.relabs.network.data.post.PostReply import io.aayush.relabs.network.data.react.PostReact import io.aayush.relabs.network.data.react.React +import io.aayush.relabs.network.data.search.Type import io.aayush.relabs.network.data.thread.Thread import io.aayush.relabs.network.data.thread.ThreadInfo import io.aayush.relabs.network.data.thread.Threads import io.aayush.relabs.network.data.user.Me import io.aayush.relabs.network.paging.GenericPagingSource.Companion.createPager import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.emptyFlow import java.util.UUID import okhttp3.MultipartBody import retrofit2.Response @@ -136,6 +138,34 @@ class XDARepository @Inject constructor( return safeExecute { xdaInterface.getWatchedNodes() }?.nodes } + fun getSearchResultsForThreads(query: String): Flow> { + return createPager { page -> + val search = safeExecute { xdaInterface.postSearch(query, Type.THREAD.value) }?.search + + if (search != null && search.id != 0) { + safeExecute { + xdaInterface.getSearchResultsForThread(search.id, page) + }?.results.orEmpty() + } else { + emptyList() + } + }.flow + } + + fun getSearchResultsForNodes(query: String): Flow> { + return createPager { page -> + val search = safeExecute { xdaInterface.postSearch(query, Type.NODE.value) }?.search + + if (search != null && search.id != 0) { + safeExecute { + xdaInterface.getSearchResultsForNode(search.id, page) + }?.results.orEmpty() + } else { + emptyList() + } + }.flow + } + private inline fun safeExecute(block: () -> Response): T? { return try { val response = block() diff --git a/app/src/main/java/io/aayush/relabs/network/data/search/Order.kt b/app/src/main/java/io/aayush/relabs/network/data/search/Order.kt new file mode 100644 index 0000000..1121990 --- /dev/null +++ b/app/src/main/java/io/aayush/relabs/network/data/search/Order.kt @@ -0,0 +1,8 @@ +package io.aayush.relabs.network.data.search + +enum class Order(val value: String) { + RELEVANCE("relevance"), + DATE("date"), + MOST_RECENT("last_update"), + MOST_REPLIES("replies") +} diff --git a/app/src/main/java/io/aayush/relabs/network/data/search/PostSearch.kt b/app/src/main/java/io/aayush/relabs/network/data/search/PostSearch.kt new file mode 100644 index 0000000..f33ff7f --- /dev/null +++ b/app/src/main/java/io/aayush/relabs/network/data/search/PostSearch.kt @@ -0,0 +1,5 @@ +package io.aayush.relabs.network.data.search + +data class PostSearch( + val search: Search = Search() +) diff --git a/app/src/main/java/io/aayush/relabs/network/data/search/Search.kt b/app/src/main/java/io/aayush/relabs/network/data/search/Search.kt new file mode 100644 index 0000000..1d39567 --- /dev/null +++ b/app/src/main/java/io/aayush/relabs/network/data/search/Search.kt @@ -0,0 +1,12 @@ +package io.aayush.relabs.network.data.search + +import io.aayush.relabs.network.data.common.DateTime + +data class Search( + val created_at: DateTime = DateTime(), + val id: Int = 0, + val result_count: Int = 0, + val search_constraints: SearchConstraints = SearchConstraints(), + val search_order: String = String(), + val search_type: String = String() +) diff --git a/app/src/main/java/io/aayush/relabs/network/data/search/SearchConstraints.kt b/app/src/main/java/io/aayush/relabs/network/data/search/SearchConstraints.kt new file mode 100644 index 0000000..0e23aff --- /dev/null +++ b/app/src/main/java/io/aayush/relabs/network/data/search/SearchConstraints.kt @@ -0,0 +1,6 @@ +package io.aayush.relabs.network.data.search + +data class SearchConstraints( + val container_only: Int = 0, + val title_only: Int = 0 +) diff --git a/app/src/main/java/io/aayush/relabs/network/data/search/SearchResultNode.kt b/app/src/main/java/io/aayush/relabs/network/data/search/SearchResultNode.kt new file mode 100644 index 0000000..e8aedc7 --- /dev/null +++ b/app/src/main/java/io/aayush/relabs/network/data/search/SearchResultNode.kt @@ -0,0 +1,10 @@ +package io.aayush.relabs.network.data.search + +import io.aayush.relabs.network.data.common.Pagination +import io.aayush.relabs.network.data.node.Node + +data class SearchResultNode( + val search: Search = Search(), + val results: List = emptyList(), + val pagination: Pagination = Pagination() +) diff --git a/app/src/main/java/io/aayush/relabs/network/data/search/SearchResultThread.kt b/app/src/main/java/io/aayush/relabs/network/data/search/SearchResultThread.kt new file mode 100644 index 0000000..d4f17fb --- /dev/null +++ b/app/src/main/java/io/aayush/relabs/network/data/search/SearchResultThread.kt @@ -0,0 +1,10 @@ +package io.aayush.relabs.network.data.search + +import io.aayush.relabs.network.data.common.Pagination +import io.aayush.relabs.network.data.thread.Thread + +data class SearchResultThread( + val search: Search = Search(), + val results: List = emptyList(), + val pagination: Pagination = Pagination() +) diff --git a/app/src/main/java/io/aayush/relabs/network/data/search/Type.kt b/app/src/main/java/io/aayush/relabs/network/data/search/Type.kt new file mode 100644 index 0000000..abcbcaa --- /dev/null +++ b/app/src/main/java/io/aayush/relabs/network/data/search/Type.kt @@ -0,0 +1,6 @@ +package io.aayush.relabs.network.data.search + +enum class Type(val value: String) { + NODE("node"), + THREAD("post") +}