diff --git a/app/src/main/java/me/capcom/smsgateway/data/dao/MessagesDao.kt b/app/src/main/java/me/capcom/smsgateway/data/dao/MessagesDao.kt index b15e693..36b5172 100644 --- a/app/src/main/java/me/capcom/smsgateway/data/dao/MessagesDao.kt +++ b/app/src/main/java/me/capcom/smsgateway/data/dao/MessagesDao.kt @@ -144,6 +144,12 @@ interface MessagesDao { _insertRecipientStatesByMessage(id, state) } + @Query("UPDATE message SET simNumber = :simNumber WHERE id = :id") + fun updateSimNumber( + id: String, + simNumber: Int + ) + @Query("DELETE FROM message WHERE createdAt < :until AND state <> 'Pending'") suspend fun truncateLog(until: Long) } \ No newline at end of file diff --git a/app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt b/app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt index 5854b6d..07ab5de 100644 --- a/app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt +++ b/app/src/main/java/me/capcom/smsgateway/modules/messages/MessagesService.kt @@ -236,6 +236,7 @@ class MessagesService( msg.message.source, phone?.let { setOf(it) } ?: msg.recipients.map { it.phoneNumber }.toSet(), state, + msg.message.simNumber, error ) ) @@ -262,7 +263,12 @@ class MessagesService( val message = request.message val id = message.id - val smsManager: SmsManager = getSmsManager(selectSimNumber(request)) + val simNumber = selectSimNumber(request) + val smsManager: SmsManager = getSmsManager(simNumber) + + if (request.message.simNumber == null && simNumber != null) { + dao.updateSimNumber(id, simNumber + 1) + } @Suppress("NAME_SHADOWING") val messageText = when (message.isEncrypted) { diff --git a/app/src/main/java/me/capcom/smsgateway/modules/messages/events/MessageStateChangedEvent.kt b/app/src/main/java/me/capcom/smsgateway/modules/messages/events/MessageStateChangedEvent.kt index a1ab05b..aa932c5 100644 --- a/app/src/main/java/me/capcom/smsgateway/modules/messages/events/MessageStateChangedEvent.kt +++ b/app/src/main/java/me/capcom/smsgateway/modules/messages/events/MessageStateChangedEvent.kt @@ -9,6 +9,7 @@ class MessageStateChangedEvent( val source: EntitySource, val phoneNumbers: Set, val state: ProcessingState, + val simNumber: Int?, val error: String? ): AppEvent(NAME) { diff --git a/app/src/main/java/me/capcom/smsgateway/modules/webhooks/EventsReceiver.kt b/app/src/main/java/me/capcom/smsgateway/modules/webhooks/EventsReceiver.kt index b1045a1..04f4dc3 100644 --- a/app/src/main/java/me/capcom/smsgateway/modules/webhooks/EventsReceiver.kt +++ b/app/src/main/java/me/capcom/smsgateway/modules/webhooks/EventsReceiver.kt @@ -40,18 +40,21 @@ class EventsReceiver : EventsReceiver() { WebHookEvent.SmsSent -> SmsEventPayload.SmsSent( messageId = event.id, phoneNumber = phoneNumber, + event.simNumber, sentAt = Date(), ) WebHookEvent.SmsDelivered -> SmsEventPayload.SmsDelivered( messageId = event.id, phoneNumber = phoneNumber, + event.simNumber, deliveredAt = Date(), ) WebHookEvent.SmsFailed -> SmsEventPayload.SmsFailed( messageId = event.id, phoneNumber = phoneNumber, + event.simNumber, failedAt = Date(), reason = event.error ?: "Unknown", ) diff --git a/app/src/main/java/me/capcom/smsgateway/modules/webhooks/payload/SmsEventPayload.kt b/app/src/main/java/me/capcom/smsgateway/modules/webhooks/payload/SmsEventPayload.kt index 4d36852..92ec291 100644 --- a/app/src/main/java/me/capcom/smsgateway/modules/webhooks/payload/SmsEventPayload.kt +++ b/app/src/main/java/me/capcom/smsgateway/modules/webhooks/payload/SmsEventPayload.kt @@ -5,23 +5,27 @@ import java.util.Date sealed class SmsEventPayload( val messageId: String, val phoneNumber: String, + val simNumber: Int?, ) { class SmsSent( messageId: String, phoneNumber: String, - val sentAt: Date - ) : SmsEventPayload(messageId, phoneNumber) + simNumber: Int?, + val sentAt: Date, + ) : SmsEventPayload(messageId, phoneNumber, simNumber) class SmsDelivered( messageId: String, phoneNumber: String, - val deliveredAt: Date - ) : SmsEventPayload(messageId, phoneNumber) + simNumber: Int?, + val deliveredAt: Date, + ) : SmsEventPayload(messageId, phoneNumber, simNumber) class SmsFailed( messageId: String, phoneNumber: String, + simNumber: Int?, val failedAt: Date, val reason: String, - ) : SmsEventPayload(messageId, phoneNumber) + ) : SmsEventPayload(messageId, phoneNumber, simNumber) } \ No newline at end of file