ntfy-android/app/src/main/java/io/heckel/ntfy/msg/NotificationParser.kt

58 lines
2.1 KiB
Kotlin
Raw Normal View History

2022-01-16 12:40:38 +13:00
package io.heckel.ntfy.msg
import android.util.Base64
2022-01-16 12:40:38 +13:00
import com.google.gson.Gson
2022-04-17 12:12:40 +12:00
import io.heckel.ntfy.db.Action
2022-01-19 08:28:48 +13:00
import io.heckel.ntfy.db.Attachment
import io.heckel.ntfy.db.Notification
2022-01-16 12:40:38 +13:00
import io.heckel.ntfy.util.joinTags
import io.heckel.ntfy.util.toPriority
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 12:12:40 +12:00
val actions = if (message.actions != null) {
message.actions.map { action ->
Action(action.action, action.label, action.url)
}
} else null
2022-01-16 12:40:38 +13:00
val notification = Notification(
id = message.id,
subscriptionId = subscriptionId,
timestamp = message.time,
title = message.title ?: "",
message = message.message,
encoding = message.encoding ?: "",
2022-01-16 12:40:38 +13:00
priority = toPriority(message.priority),
tags = joinTags(message.tags),
click = message.click ?: "",
2022-04-17 12:12:40 +12:00
actions = actions,
2022-01-16 12:40:38 +13:00
attachment = attachment,
notificationId = notificationId,
deleted = false
)
return NotificationWithTopic(message.topic, notification)
}
data class NotificationWithTopic(val topic: String, val notification: Notification)
}