push(core): F7PushRegistrar.unregisterBlocking — снятие устройства f7push при logout (DELETE devices/{id}, контракт mail/110); compile+detekt green
This commit is contained in:
@@ -81,6 +81,28 @@ object F7PushRegistrar {
|
||||
return code == 0 || code >= 500 || code == 408 || code == 429
|
||||
}
|
||||
|
||||
/**
|
||||
* Снимает устройство с регистрации в f7push (при logout) ДО очистки локальной сессии.
|
||||
* Контракт сервера (f7push v0.2.4, подтверждён integrator-b, mail/110):
|
||||
* `DELETE /ocs/v2.php/apps/f7push/api/v1/devices/{deviceId}`, та же Basic-авторизация +
|
||||
* `OCS-APIRequest`; идемпотентен (200 `success:true` даже если строки нет). Best-effort:
|
||||
* ошибку глотаем (сеть могла отвалиться при выходе), возвращаем HTTP-код или 0.
|
||||
*/
|
||||
fun unregisterBlocking(
|
||||
context: Context,
|
||||
session: AuthSession,
|
||||
): Int {
|
||||
val deviceId = getDeviceId(context)
|
||||
val endpoint = session.serverUrl.trimEnd('/') +
|
||||
"/ocs/v2.php/apps/f7push/api/v1/devices/$deviceId"
|
||||
return try {
|
||||
deleteRequest(endpoint, session)
|
||||
} catch (t: Throwable) {
|
||||
Log.w(TAG, "unregister failed", t)
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
private fun postJson(endpoint: String, session: AuthSession, json: String): Int {
|
||||
val conn = URL(endpoint).openConnection() as HttpURLConnection
|
||||
conn.connectTimeout = 15000
|
||||
@@ -108,6 +130,25 @@ object F7PushRegistrar {
|
||||
return code
|
||||
}
|
||||
|
||||
private fun deleteRequest(endpoint: String, session: AuthSession): Int {
|
||||
val conn = URL(endpoint).openConnection() as HttpURLConnection
|
||||
conn.connectTimeout = 15000
|
||||
conn.readTimeout = 15000
|
||||
conn.requestMethod = "DELETE"
|
||||
conn.setRequestProperty("Accept", "application/json")
|
||||
conn.setRequestProperty("OCS-APIRequest", "true")
|
||||
val basic = android.util.Base64.encodeToString(
|
||||
"${session.username}:${session.appPassword}".toByteArray(StandardCharsets.UTF_8),
|
||||
android.util.Base64.NO_WRAP
|
||||
)
|
||||
conn.setRequestProperty("Authorization", "Basic $basic")
|
||||
|
||||
val code = conn.responseCode
|
||||
drainQuietly(if (code >= 400) conn.errorStream else conn.inputStream)
|
||||
conn.disconnect()
|
||||
return code
|
||||
}
|
||||
|
||||
private fun drainQuietly(stream: InputStream?) {
|
||||
if (stream == null) return
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user