package com.peachpasswords.feedback import com.peachpasswords.BuildConfig import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import org.json.JSONObject import javax.inject.Inject import javax.inject.Singleton /** * Submits user feedback to the Peach feedback endpoint. Payload shape mirrors * the browser extension's `SUBMIT_FEEDBACK` handler exactly * (`src/background/messages.ts`): a JSON body of * `{ category, message, version, browser, source }`, POSTed as * `application/json`. * * As in the browser, [FEEDBACK_ENDPOINT] is empty by default: when unset the * submission is a no-op that reports success, so the UI flow works end-to-end * without a live backend. The orchestrator fills in the real URL once it exists * (see INTEGRATION.md). The `source` discriminator is `"peach-android"` here * (the browser sends `"peach-extension"`). */ @Singleton class FeedbackRepository @Inject constructor() { private val client: OkHttpClient by lazy { OkHttpClient() } suspend fun submit(type: String, message: String) { if (message.isBlank()) throw IllegalArgumentException("message_empty") // No-op success when no endpoint is configured (matches browser behaviour). if (FEEDBACK_ENDPOINT.isBlank()) return withContext(Dispatchers.IO) { val payload = JSONObject().apply { put("category", type) put("message", message) put("version", BuildConfig.VERSION_NAME) put("browser", "android") put("source", "peach-android") } val body = payload.toString().toRequestBody(JSON) val request = Request.Builder() .url(FEEDBACK_ENDPOINT) .post(body) .build() client.newCall(request).execute().use { res -> if (!res.isSuccessful) throw java.io.IOException("HTTP ${res.code}") } } } companion object { // TODO(orchestrator): set to the real Peach feedback endpoint when it // exists. The browser source ships this empty (FEEDBACK_ENDPOINT = "") // and treats empty as a no-op success. Keep that contract. const val FEEDBACK_ENDPOINT: String = "" private val JSON = "application/json; charset=utf-8".toMediaType() } }