FeedbackRepository.kt

2.5 KB · raw

1package com.peachpasswords.feedback23import com.peachpasswords.BuildConfig4import kotlinx.coroutines.Dispatchers5import kotlinx.coroutines.withContext6import okhttp3.MediaType.Companion.toMediaType7import okhttp3.OkHttpClient8import okhttp3.Request9import okhttp3.RequestBody.Companion.toRequestBody10import org.json.JSONObject11import javax.inject.Inject12import javax.inject.Singleton1314/**15 * Submits user feedback to the Peach feedback endpoint. Payload shape mirrors16 * the browser extension's `SUBMIT_FEEDBACK` handler exactly17 * (`src/background/messages.ts`): a JSON body of18 * `{ category, message, version, browser, source }`, POSTed as19 * `application/json`.20 *21 * As in the browser, [FEEDBACK_ENDPOINT] is empty by default: when unset the22 * submission is a no-op that reports success, so the UI flow works end-to-end23 * without a live backend. The orchestrator fills in the real URL once it exists24 * (see INTEGRATION.md). The `source` discriminator is `"peach-android"` here25 * (the browser sends `"peach-extension"`).26 */27@Singleton28class FeedbackRepository @Inject constructor() {2930    private val client: OkHttpClient by lazy { OkHttpClient() }3132    suspend fun submit(type: String, message: String) {33        if (message.isBlank()) throw IllegalArgumentException("message_empty")3435        // No-op success when no endpoint is configured (matches browser behaviour).36        if (FEEDBACK_ENDPOINT.isBlank()) return3738        withContext(Dispatchers.IO) {39            val payload = JSONObject().apply {40                put("category", type)41                put("message", message)42                put("version", BuildConfig.VERSION_NAME)43                put("browser", "android")44                put("source", "peach-android")45            }46            val body = payload.toString().toRequestBody(JSON)47            val request = Request.Builder()48                .url(FEEDBACK_ENDPOINT)49                .post(body)50                .build()51            client.newCall(request).execute().use { res ->52                if (!res.isSuccessful) throw java.io.IOException("HTTP ${res.code}")53            }54        }55    }5657    companion object {58        // TODO(orchestrator): set to the real Peach feedback endpoint when it59        // exists. The browser source ships this empty (FEEDBACK_ENDPOINT = "")60        // and treats empty as a no-op success. Keep that contract.61        const val FEEDBACK_ENDPOINT: String = ""6263        private val JSON = "application/json; charset=utf-8".toMediaType()64    }65}