2021-11-07 19:13:32 +01:00
|
|
|
package io.heckel.ntfy.msg
|
|
|
|
|
|
|
|
import android.util.Log
|
2021-11-17 05:01:23 +01:00
|
|
|
import androidx.annotation.Keep
|
2021-11-07 19:13:32 +01:00
|
|
|
import com.google.gson.Gson
|
2021-11-12 01:41:29 +01:00
|
|
|
import io.heckel.ntfy.data.Notification
|
2021-11-27 22:18:09 +01:00
|
|
|
import io.heckel.ntfy.util.topicUrl
|
|
|
|
import io.heckel.ntfy.util.topicUrlJson
|
|
|
|
import io.heckel.ntfy.util.topicUrlJsonPoll
|
|
|
|
import io.heckel.ntfy.util.toPriority
|
|
|
|
import io.heckel.ntfy.util.joinTags
|
2021-11-14 01:26:37 +01:00
|
|
|
import okhttp3.*
|
2021-11-12 01:41:29 +01:00
|
|
|
import okhttp3.RequestBody.Companion.toRequestBody
|
2021-11-14 01:26:37 +01:00
|
|
|
import java.io.IOException
|
2021-11-12 01:41:29 +01:00
|
|
|
import java.util.concurrent.TimeUnit
|
2021-11-15 22:24:31 +01:00
|
|
|
import kotlin.random.Random
|
2021-11-12 01:41:29 +01:00
|
|
|
|
|
|
|
class ApiService {
|
|
|
|
private val gson = Gson()
|
|
|
|
private val client = OkHttpClient.Builder()
|
2021-11-14 01:26:37 +01:00
|
|
|
.callTimeout(15, TimeUnit.SECONDS) // Total timeout for entire request
|
|
|
|
.connectTimeout(15, TimeUnit.SECONDS)
|
|
|
|
.readTimeout(15, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(15, TimeUnit.SECONDS)
|
|
|
|
.build()
|
|
|
|
|
|
|
|
private val subscriberClient = OkHttpClient.Builder()
|
|
|
|
.readTimeout(77, TimeUnit.SECONDS) // Assuming that keepalive messages are more frequent than this
|
2021-11-12 01:41:29 +01:00
|
|
|
.build()
|
|
|
|
|
2021-11-27 22:18:09 +01:00
|
|
|
fun publish(baseUrl: String, topic: String, message: String, title: String, priority: Int, tags: List<String>) {
|
2021-11-07 19:13:32 +01:00
|
|
|
val url = topicUrl(baseUrl, topic)
|
2021-11-12 01:41:29 +01:00
|
|
|
Log.d(TAG, "Publishing to $url")
|
|
|
|
|
2021-11-27 22:18:09 +01:00
|
|
|
var builder = Request.Builder()
|
|
|
|
.url(url)
|
|
|
|
.addHeader("X-Priority", priority.toString())
|
|
|
|
.put(message.toRequestBody())
|
|
|
|
if (tags.isNotEmpty()) {
|
|
|
|
builder = builder.addHeader("X-Tags", tags.joinToString(","))
|
|
|
|
}
|
|
|
|
if (title.isNotEmpty()) {
|
|
|
|
builder = builder.addHeader("X-Title", title)
|
|
|
|
}
|
|
|
|
client.newCall(builder.build()).execute().use { response ->
|
2021-11-12 01:41:29 +01:00
|
|
|
if (!response.isSuccessful) {
|
|
|
|
throw Exception("Unexpected response ${response.code} when publishing to $url")
|
2021-11-07 19:13:32 +01:00
|
|
|
}
|
2021-11-12 01:41:29 +01:00
|
|
|
Log.d(TAG, "Successfully published to $url")
|
2021-11-07 19:13:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
fun poll(subscriptionId: Long, baseUrl: String, topic: String): List<Notification> {
|
2021-11-07 19:13:32 +01:00
|
|
|
val url = topicUrlJsonPoll(baseUrl, topic)
|
2021-11-12 01:41:29 +01:00
|
|
|
Log.d(TAG, "Polling topic $url")
|
|
|
|
|
|
|
|
val request = Request.Builder().url(url).build();
|
|
|
|
client.newCall(request).execute().use { response ->
|
|
|
|
if (!response.isSuccessful) {
|
|
|
|
throw Exception("Unexpected response ${response.code} when polling topic $url")
|
2021-11-07 19:13:32 +01:00
|
|
|
}
|
2021-11-12 01:41:29 +01:00
|
|
|
val body = response.body?.string()?.trim()
|
|
|
|
if (body == null || body.isEmpty()) return emptyList()
|
|
|
|
val notifications = body.lines().map { line ->
|
|
|
|
fromString(subscriptionId, line)
|
|
|
|
}
|
|
|
|
Log.d(TAG, "Notifications: $notifications")
|
|
|
|
return notifications
|
2021-11-07 19:13:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 20:08:52 +01:00
|
|
|
fun subscribe(
|
|
|
|
baseUrl: String,
|
|
|
|
topics: String,
|
|
|
|
since: Long,
|
|
|
|
notify: (topic: String, Notification) -> Unit,
|
|
|
|
fail: (Exception) -> Unit
|
|
|
|
): Call {
|
2021-11-14 01:26:37 +01:00
|
|
|
val sinceVal = if (since == 0L) "all" else since.toString()
|
2021-11-16 20:08:52 +01:00
|
|
|
val url = topicUrlJson(baseUrl, topics, sinceVal)
|
2021-11-14 01:26:37 +01:00
|
|
|
Log.d(TAG, "Opening subscription connection to $url")
|
|
|
|
|
|
|
|
val request = Request.Builder().url(url).build()
|
|
|
|
val call = subscriberClient.newCall(request)
|
|
|
|
call.enqueue(object : Callback {
|
|
|
|
override fun onResponse(call: Call, response: Response) {
|
|
|
|
try {
|
|
|
|
if (!response.isSuccessful) {
|
|
|
|
throw Exception("Unexpected response ${response.code} when subscribing to topic $url")
|
|
|
|
}
|
|
|
|
val source = response.body?.source() ?: throw Exception("Unexpected response for $url: body is empty")
|
|
|
|
while (!source.exhausted()) {
|
|
|
|
val line = source.readUtf8Line() ?: throw Exception("Unexpected response for $url: line is null")
|
|
|
|
val message = gson.fromJson(line, Message::class.java)
|
|
|
|
if (message.event == EVENT_MESSAGE) {
|
2021-11-16 20:08:52 +01:00
|
|
|
val topic = message.topic
|
2021-11-15 22:24:31 +01:00
|
|
|
val notification = Notification(
|
|
|
|
id = message.id,
|
2021-11-16 20:08:52 +01:00
|
|
|
subscriptionId = 0, // TO BE SET downstream
|
2021-11-15 22:24:31 +01:00
|
|
|
timestamp = message.time,
|
2021-11-27 22:18:09 +01:00
|
|
|
title = message.title ?: "",
|
2021-11-15 22:24:31 +01:00
|
|
|
message = message.message,
|
2021-11-27 22:18:09 +01:00
|
|
|
priority = toPriority(message.priority),
|
|
|
|
tags = joinTags(message.tags),
|
2021-11-15 22:24:31 +01:00
|
|
|
notificationId = Random.nextInt(),
|
|
|
|
deleted = false
|
|
|
|
)
|
2021-11-16 20:08:52 +01:00
|
|
|
notify(topic, notification)
|
2021-11-14 01:26:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
2021-11-14 19:54:48 +01:00
|
|
|
Log.e(TAG, "Connection to $url failed (1): ${e.message}", e)
|
2021-11-14 01:26:37 +01:00
|
|
|
fail(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onFailure(call: Call, e: IOException) {
|
2021-11-14 19:54:48 +01:00
|
|
|
Log.e(TAG, "Connection to $url failed (2): ${e.message}", e)
|
2021-11-14 01:26:37 +01:00
|
|
|
fail(e)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return call
|
|
|
|
}
|
|
|
|
|
2021-11-12 01:41:29 +01:00
|
|
|
private fun fromString(subscriptionId: Long, s: String): Notification {
|
2021-11-17 05:01:23 +01:00
|
|
|
val message = gson.fromJson(s, Message::class.java)
|
2021-11-27 22:18:09 +01:00
|
|
|
return Notification(
|
|
|
|
id = message.id,
|
|
|
|
subscriptionId = subscriptionId,
|
|
|
|
timestamp = message.time,
|
|
|
|
title = message.title ?: "",
|
|
|
|
message = message.message,
|
|
|
|
priority = toPriority(message.priority),
|
|
|
|
tags = joinTags(message.tags),
|
|
|
|
notificationId = 0,
|
|
|
|
deleted = false
|
|
|
|
)
|
2021-11-12 01:41:29 +01:00
|
|
|
}
|
|
|
|
|
2021-11-17 05:01:23 +01:00
|
|
|
/* This annotation ensures that proguard still works in production builds,
|
|
|
|
* see https://stackoverflow.com/a/62753300/1440785 */
|
|
|
|
@Keep
|
2021-11-14 01:26:37 +01:00
|
|
|
private data class Message(
|
2021-11-12 01:41:29 +01:00
|
|
|
val id: String,
|
|
|
|
val time: Long,
|
2021-11-14 01:26:37 +01:00
|
|
|
val event: String,
|
2021-11-16 20:08:52 +01:00
|
|
|
val topic: String,
|
2021-11-27 22:18:09 +01:00
|
|
|
val priority: Int?,
|
|
|
|
val tags: List<String>?,
|
|
|
|
val title: String?,
|
2021-11-12 01:41:29 +01:00
|
|
|
val message: String
|
|
|
|
)
|
|
|
|
|
2021-11-07 19:13:32 +01:00
|
|
|
companion object {
|
|
|
|
private const val TAG = "NtfyApiService"
|
2021-11-14 01:26:37 +01:00
|
|
|
private const val EVENT_MESSAGE = "message"
|
2021-11-07 19:13:32 +01:00
|
|
|
}
|
|
|
|
}
|