fix: use non-truncated ULIDs as notification tags and constant notification IDs
This means there is now a zero chance of collision. Signed-off-by: Infi <infi@infi.sh>
This commit is contained in:
parent
73a2d6cf17
commit
168356afdc
|
|
@ -145,29 +145,4 @@ object ULID {
|
||||||
|
|
||||||
return timestamp
|
return timestamp
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Like asTimestamp, but for the entire ULID including the entropy, as an integer.
|
|
||||||
Note that the number you receive will not be reversible to the original ULID, because
|
|
||||||
it will overflow the `Int` type! Used for generating Android notification IDs.
|
|
||||||
*/
|
|
||||||
fun asInteger(ulid: String): Int {
|
|
||||||
if (ulid.length != len) {
|
|
||||||
throw IllegalArgumentException("ULID must be exactly $len characters")
|
|
||||||
}
|
|
||||||
|
|
||||||
var integer = 0
|
|
||||||
|
|
||||||
for (i in 0 until len) {
|
|
||||||
val char = ulid[i]
|
|
||||||
val value = b32chars.indexOf(char)
|
|
||||||
|
|
||||||
if (value == -1) {
|
|
||||||
throw IllegalArgumentException("Invalid character '$char' at position $i")
|
|
||||||
}
|
|
||||||
|
|
||||||
integer = integer or (value shl (len - i - 1) * 5)
|
|
||||||
}
|
|
||||||
|
|
||||||
return integer
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,10 @@ import kotlinx.serialization.json.contentOrNull
|
||||||
import kotlinx.serialization.json.jsonObject
|
import kotlinx.serialization.json.jsonObject
|
||||||
import kotlinx.serialization.json.jsonPrimitive
|
import kotlinx.serialization.json.jsonPrimitive
|
||||||
|
|
||||||
|
object NotificationID {
|
||||||
|
const val NEW_MESSAGE = 0
|
||||||
|
}
|
||||||
|
|
||||||
class HandlerService : FirebaseMessagingService() {
|
class HandlerService : FirebaseMessagingService() {
|
||||||
override fun onNewToken(token: String) {
|
override fun onNewToken(token: String) {
|
||||||
super.onNewToken(token)
|
super.onNewToken(token)
|
||||||
|
|
@ -72,18 +76,13 @@ class HandlerService : FirebaseMessagingService() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val notificationId = message.channel?.let { ULID.asInteger(it) } ?: run {
|
|
||||||
Log.e("HandlerService", "No channel in message, abort")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (authorIcon == null) {
|
if (authorIcon == null) {
|
||||||
authorIcon =
|
authorIcon =
|
||||||
"$REVOLT_BASE/users/${message.author?.ifBlank { "0".repeat(26) }}/default_avatar"
|
"$REVOLT_BASE/users/${message.author?.ifBlank { "0".repeat(26) }}/default_avatar"
|
||||||
}
|
}
|
||||||
|
|
||||||
val db = Database(SqlStorage.driver)
|
val db = Database(SqlStorage.driver)
|
||||||
val channelName = message.channel.let {
|
val channelName = message.channel?.let {
|
||||||
db.channelQueries.findById(it).executeAsOneOrNull()
|
db.channelQueries.findById(it).executeAsOneOrNull()
|
||||||
}?.let {
|
}?.let {
|
||||||
when (it.channelType) {
|
when (it.channelType) {
|
||||||
|
|
@ -123,6 +122,11 @@ class HandlerService : FirebaseMessagingService() {
|
||||||
.setName(user.displayName ?: user.username)
|
.setName(user.displayName ?: user.username)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
if (message.channel == null) {
|
||||||
|
Log.e("HandlerService", "No channel in message, abort")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val remoteInput = RemoteInput.Builder("content").run {
|
val remoteInput = RemoteInput.Builder("content").run {
|
||||||
setLabel(getString(R.string.message_context_sheet_actions_reply))
|
setLabel(getString(R.string.message_context_sheet_actions_reply))
|
||||||
build()
|
build()
|
||||||
|
|
@ -167,7 +171,7 @@ class HandlerService : FirebaseMessagingService() {
|
||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
notify(notificationId, builder.build())
|
notify(message.channel, NotificationID.NEW_MESSAGE, builder.build())
|
||||||
}
|
}
|
||||||
/// END TEMPORARY CODE
|
/// END TEMPORARY CODE
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue