2022-01-16 00:40:38 +01:00
|
|
|
package io.heckel.ntfy.msg
|
|
|
|
|
|
|
|
import com.google.gson.Gson
|
2022-04-20 01:20:39 +02:00
|
|
|
import com.google.gson.reflect.TypeToken
|
2022-04-17 02:12:40 +02:00
|
|
|
import io.heckel.ntfy.db.Action
|
2022-01-18 20:28:48 +01:00
|
|
|
import io.heckel.ntfy.db.Attachment
|
|
|
|
import io.heckel.ntfy.db.Notification
|
2022-01-16 00:40:38 +01:00
|
|
|
import io.heckel.ntfy.util.joinTags
|
|
|
|
import io.heckel.ntfy.util.toPriority
|
2022-04-20 01:20:39 +02:00
|
|
|
import java.lang.reflect.Type
|
2022-01-16 00:40:38 +01:00
|
|
|
|
|
|
|
class NotificationParser {
|
|
|
|
private val gson = Gson()
|
|
|
|
|
|
|
|
fun parse(s: String, subscriptionId: Long = 0, notificationId: Int = 0): Notification? {
|
|
|
|
val notificationWithTopic = parseWithTopic(s, subscriptionId = subscriptionId, notificationId = notificationId)
|
|
|
|
return notificationWithTopic?.notification
|
|
|
|
}
|
|
|
|
|
|
|
|
fun parseWithTopic(s: String, subscriptionId: Long = 0, notificationId: Int = 0): NotificationWithTopic? {
|
|
|
|
val message = gson.fromJson(s, Message::class.java)
|
|
|
|
if (message.event != ApiService.EVENT_MESSAGE) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
val attachment = if (message.attachment?.url != null) {
|
|
|
|
Attachment(
|
|
|
|
name = message.attachment.name,
|
|
|
|
type = message.attachment.type,
|
|
|
|
size = message.attachment.size,
|
|
|
|
expires = message.attachment.expires,
|
|
|
|
url = message.attachment.url,
|
|
|
|
)
|
|
|
|
} else null
|
2022-04-17 02:12:40 +02:00
|
|
|
val actions = if (message.actions != null) {
|
2022-04-17 20:29:29 +02:00
|
|
|
message.actions.map { a ->
|
2022-04-23 04:45:33 +02:00
|
|
|
Action(
|
|
|
|
id = a.id,
|
|
|
|
action = a.action,
|
|
|
|
label = a.label,
|
|
|
|
clear = a.clear,
|
|
|
|
url = a.url,
|
|
|
|
method = a.method,
|
|
|
|
headers = a.headers,
|
|
|
|
body = a.body,
|
|
|
|
intent = a.intent,
|
|
|
|
extras = a.extras,
|
|
|
|
progress = null,
|
|
|
|
error = null
|
|
|
|
)
|
2022-04-17 02:12:40 +02:00
|
|
|
}
|
|
|
|
} else null
|
2022-01-16 00:40:38 +01:00
|
|
|
val notification = Notification(
|
|
|
|
id = message.id,
|
|
|
|
subscriptionId = subscriptionId,
|
|
|
|
timestamp = message.time,
|
|
|
|
title = message.title ?: "",
|
2022-03-13 20:58:19 +01:00
|
|
|
message = message.message,
|
|
|
|
encoding = message.encoding ?: "",
|
2022-01-16 00:40:38 +01:00
|
|
|
priority = toPriority(message.priority),
|
|
|
|
tags = joinTags(message.tags),
|
|
|
|
click = message.click ?: "",
|
2022-04-17 02:12:40 +02:00
|
|
|
actions = actions,
|
2022-01-16 00:40:38 +01:00
|
|
|
attachment = attachment,
|
|
|
|
notificationId = notificationId,
|
|
|
|
deleted = false
|
|
|
|
)
|
|
|
|
return NotificationWithTopic(message.topic, notification)
|
|
|
|
}
|
|
|
|
|
2022-04-20 01:20:39 +02:00
|
|
|
/**
|
|
|
|
* Parse JSON array to Action list. The indirection via MessageAction is probably
|
|
|
|
* not necessary, but for "good form".
|
|
|
|
*/
|
|
|
|
fun parseActions(s: String?): List<Action>? {
|
|
|
|
val listType: Type = object : TypeToken<List<MessageAction>?>() {}.type
|
|
|
|
val messageActions: List<MessageAction>? = gson.fromJson(s, listType)
|
|
|
|
return messageActions?.map { a ->
|
2022-04-23 04:45:33 +02:00
|
|
|
Action(
|
|
|
|
id = a.id,
|
|
|
|
action = a.action,
|
|
|
|
label = a.label,
|
|
|
|
clear = a.clear,
|
|
|
|
url = a.url,
|
|
|
|
method = a.method,
|
|
|
|
headers = a.headers,
|
|
|
|
body = a.body,
|
|
|
|
intent = a.intent,
|
|
|
|
extras = a.extras,
|
|
|
|
progress = null,
|
|
|
|
error = null
|
|
|
|
)
|
2022-04-20 01:20:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-16 00:40:38 +01:00
|
|
|
data class NotificationWithTopic(val topic: String, val notification: Notification)
|
|
|
|
}
|