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:
Infi 2025-07-07 23:33:55 +02:00
parent 73a2d6cf17
commit 168356afdc
2 changed files with 11 additions and 32 deletions

View File

@ -145,29 +145,4 @@ object ULID {
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
}
}

View File

@ -29,6 +29,10 @@ import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
object NotificationID {
const val NEW_MESSAGE = 0
}
class HandlerService : FirebaseMessagingService() {
override fun onNewToken(token: String) {
super.onNewToken(token)
@ -72,18 +76,13 @@ class HandlerService : FirebaseMessagingService() {
return
}
val notificationId = message.channel?.let { ULID.asInteger(it) } ?: run {
Log.e("HandlerService", "No channel in message, abort")
return
}
if (authorIcon == null) {
authorIcon =
"$REVOLT_BASE/users/${message.author?.ifBlank { "0".repeat(26) }}/default_avatar"
}
val db = Database(SqlStorage.driver)
val channelName = message.channel.let {
val channelName = message.channel?.let {
db.channelQueries.findById(it).executeAsOneOrNull()
}?.let {
when (it.channelType) {
@ -123,6 +122,11 @@ class HandlerService : FirebaseMessagingService() {
.setName(user.displayName ?: user.username)
.build()
if (message.channel == null) {
Log.e("HandlerService", "No channel in message, abort")
return
}
val remoteInput = RemoteInput.Builder("content").run {
setLabel(getString(R.string.message_context_sheet_actions_reply))
build()
@ -167,7 +171,7 @@ class HandlerService : FirebaseMessagingService() {
) {
return
}
notify(notificationId, builder.build())
notify(message.channel, NotificationID.NEW_MESSAGE, builder.build())
}
/// END TEMPORARY CODE
}