Skip to content

Commit

Permalink
[webhooks] add simNumber field to payload
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Dec 24, 2024
1 parent 587c972 commit 02452a3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ class MessagesService(
msg.message.source,
phone?.let { setOf(it) } ?: msg.recipients.map { it.phoneNumber }.toSet(),
state,
msg.message.simNumber,
error
)
)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class MessageStateChangedEvent(
val source: EntitySource,
val phoneNumbers: Set<String>,
val state: ProcessingState,
val simNumber: Int?,
val error: String?
): AppEvent(NAME) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 02452a3

Please sign in to comment.