Skip to content

Commit

Permalink
[push] add support for MessagesExportRequested event
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Jan 2, 2025
1 parent eed46c0 commit 141c23e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/src/main/java/me/capcom/smsgateway/modules/push/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ enum class Event {

@SerializedName("WebhooksUpdated")
WebhooksUpdated,

@SerializedName("MessagesExportRequested")
MessagesExportRequested,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.capcom.smsgateway.modules.push.payloads

import com.google.gson.GsonBuilder
import me.capcom.smsgateway.extensions.configure
import java.util.Date

data class MessagesExportRequestedPayload(
val since: Date,
val until: Date,
) {
companion object {
fun from(json: String): MessagesExportRequestedPayload {
val gson = GsonBuilder().configure().create()
return gson.fromJson(json, MessagesExportRequestedPayload::class.java)
}
}
}
18 changes: 17 additions & 1 deletion app/src/main/java/me/capcom/smsgateway/services/PushService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import me.capcom.smsgateway.modules.gateway.workers.RegistrationWorker
import me.capcom.smsgateway.modules.gateway.workers.WebhooksUpdateWorker
import me.capcom.smsgateway.modules.push.Event
import me.capcom.smsgateway.modules.push.events.PushMessageEnqueuedEvent
import me.capcom.smsgateway.modules.push.payloads.MessagesExportRequestedPayload
import me.capcom.smsgateway.modules.receiver.ReceiverService
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
import org.koin.core.component.inject

class PushService : FirebaseMessagingService(), KoinComponent {
private val settingsHelper by lazy { SettingsHelper(this) }
private val settingsHelper by inject<SettingsHelper>()

private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
private val eventBus by inject<EventBus>()
Expand All @@ -38,9 +41,22 @@ class PushService : FirebaseMessagingService(), KoinComponent {
Log.d(this.javaClass.name, message.data.toString())

val event = message.data["event"]?.let { Event.valueOf(it) } ?: Event.MessageEnqueued
val data = message.data["data"]
when (event) {
Event.MessageEnqueued -> scope.launch { eventBus.emit(PushMessageEnqueuedEvent()) }
Event.WebhooksUpdated -> WebhooksUpdateWorker.start(this)
Event.MessagesExportRequested -> data
?.let {
MessagesExportRequestedPayload.from(
data
)
}
?.let { payload ->
get<ReceiverService>().export(
this,
payload.since to payload.until
)
}
}
} catch (e: Throwable) {
e.printStackTrace()
Expand Down

0 comments on commit 141c23e

Please sign in to comment.