Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Add expired message status

Acho Arnold2701ee3c430b
diff --git a/api/pkg/entities/message.go b/api/pkg/entities/message.go
index db7db213..cb8b1df8 100644
--- a/api/pkg/entities/message.go
+++ b/api/pkg/entities/message.go
@@ -38,6 +38,9 @@ const (
// MessageStatusDelivered means the mobile phone has delivered the message
MessageStatusDelivered = "delivered"
+
+ // MessageStatusExpired means the message could not be sent by the mobile phone after 5 minutes
+ MessageStatusExpired = "expired"
)
// MessageEventName is the type of event generated by the mobile phone for a message
@@ -78,12 +81,17 @@ type Message struct {
}
// IsSending determines if a message is being sent
-func (message Message) IsSending() bool {
+func (message *Message) IsSending() bool {
return message.Status == MessageStatusSending
}
+// IsExpired checks if a message is expired
+func (message *Message) IsExpired() bool {
+ return message.Status == MessageStatusExpired
+}
+
// IsSent determines if a message has been sent
-func (message Message) IsSent() bool {
+func (message *Message) IsSent() bool {
return message.Status == MessageStatusSent
}
diff --git a/api/pkg/events/message_notification_failed_event.go b/api/pkg/events/message_notification_failed_event.go
new file mode 100644
index 00000000..c39174f4
--- /dev/null
+++ b/api/pkg/events/message_notification_failed_event.go
@@ -0,0 +1,22 @@
+package events
+
+import (
+ "time"
+
+ "github.com/NdoleStudio/httpsms/pkg/entities"
+
+ "github.com/google/uuid"
+)
+
+// EventTypeMessageNotificationFailed is emitted when a new message notification is failed
+const EventTypeMessageNotificationFailed = "message.notification.failed"
+
+// MessageNotificationFailedPayload is the payload of the EventTypeMessageNotificationFailed event
+type MessageNotificationFailedPayload struct {
+ MessageID uuid.UUID `json:"message_id"`
+ UserID entities.UserID `json:"user_id"`
+ NotificationID uuid.UUID `json:"notification_id"`
+ PhoneID uuid.UUID `json:"phone_id"`
+ ErrorMessage string `json:"error_message"`
+ NotificationFailedAt time.Time `json:"notification_failed_at"`
+}
diff --git a/api/pkg/events/message_notification_scheduled_event.go b/api/pkg/events/message_notification_scheduled_event.go
index a068bd16..aa571928 100644
--- a/api/pkg/events/message_notification_scheduled_event.go
+++ b/api/pkg/events/message_notification_scheduled_event.go
@@ -11,7 +11,7 @@ import (
// EventTypeMessageNotificationScheduled is emitted when a new message notification is scheduled
const EventTypeMessageNotificationScheduled = "message.notification.scheduled"
-// MessageNotificationScheduledPayload is the payload of the MessageNotificationScheduledPayload event
+// MessageNotificationScheduledPayload is the payload of the EventTypeMessageNotificationScheduled event
type MessageNotificationScheduledPayload struct {
MessageID uuid.UUID `json:"id"`
UserID entities.UserID `json:"user_id"`
diff --git a/api/pkg/events/message_notification_sent_event.go b/api/pkg/events/message_notification_sent_event.go
new file mode 100644
index 00000000..a10c8948
--- /dev/null
+++ b/api/pkg/events/message_notification_sent_event.go
@@ -0,0 +1,23 @@
+package events
+
+import (
+ "time"
+
+ "github.com/NdoleStudio/httpsms/pkg/entities"
+
+ "github.com/google/uuid"
+)
+
+// EventTypeMessageNotificationSent is emitted when a new message notification is scheduled
+const EventTypeMessageNotificationSent = "message.notification.sent"
+
+// MessageNotificationSentPayload is the payload of the EventTypeMessageNotificationSent event
+type MessageNotificationSentPayload struct {
+ MessageID uuid.UUID `json:"message_id"`
+ UserID entities.UserID `json:"user_id"`
+ PhoneID uuid.UUID `json:"phone_id"`
+ ScheduledAt time.Time `json:"scheduled_at"`
+ FcmMessageID string `json:"fcm_message_id"`
+ NotificationSentAt time.Time `json:"notification_sent_at"`
+ NotificationID uuid.UUID `json:"notification_id"`
+}
diff --git a/api/pkg/listeners/notification_listener.go b/api/pkg/listeners/notification_listener.go
index 404cdfe6..4c24c884 100644
--- a/api/pkg/listeners/notification_listener.go
+++ b/api/pkg/listeners/notification_listener.go
@@ -78,6 +78,8 @@ func (listener *NotificationListener) onMessageNotificationScheduled(ctx context
scheduleParams := &services.NotificationSendParams{
UserID: payload.UserID,
PhoneID: payload.PhoneID,
+ Source: event.Source(),
+ ScheduledAt: payload.ScheduledAt,
PhoneNotificationID: payload.NotificationID,
MessageID: payload.MessageID,
}
diff --git a/api/pkg/services/message_service.go b/api/pkg/services/message_service.go
index be63b4a2..6d206f4d 100644
--- a/api/pkg/services/message_service.go
+++ b/api/pkg/services/message_service.go
@@ -462,8 +462,8 @@ func (service *MessageService) HandleMessageSent(ctx context.Context, params Han
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
- if !message.IsSending() {
- msg := fmt.Sprintf("message has wrong status [%s]. expected %s", message.Status, entities.MessageStatusSending)
+ if !message.IsSending() && !message.IsExpired() {
+ msg := fmt.Sprintf("message has wrong status [%s]. expected [%s, %s]", message.Status, entities.MessageStatusSending, entities.MessageStatusExpired)
return service.tracer.WrapErrorSpan(span, stacktrace.NewError(msg))
}
@@ -489,8 +489,8 @@ func (service *MessageService) HandleMessageFailed(ctx context.Context, params H
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
- if !message.IsSent() && !message.IsSending() {
- msg := fmt.Sprintf("message has wrong status [%s]. expected [%s,%s]", message.Status, entities.MessageStatusSending, entities.MessageStatusSent)
+ if !message.IsSent() && !message.IsSending() && !message.IsExpired() {
+ msg := fmt.Sprintf("message has wrong status [%s]. expected [%s, %s, %s]", message.Status, entities.MessageStatusSending, entities.MessageStatusSent, entities.MessageStatusExpired)
return service.tracer.WrapErrorSpan(span, stacktrace.NewError(msg))
}
@@ -516,8 +516,8 @@ func (service *MessageService) HandleMessageDelivered(ctx context.Context, param
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
- if !message.IsSent() && !message.IsSending() {
- msg := fmt.Sprintf("message has wrong status [%s]. expected [%s, %s]", message.Status, entities.MessageStatusSent, entities.MessageStatusSending)
+ if !message.IsSent() && !message.IsSending() && !message.IsExpired() {
+ msg := fmt.Sprintf("message has wrong status [%s]. expected [%s, %s, %s]", message.Status, entities.MessageStatusSent, entities.MessageStatusSending, entities.MessageStatusExpired)
return service.tracer.WrapErrorSpan(span, stacktrace.NewError(msg))
}
diff --git a/api/pkg/services/notification_service.go b/api/pkg/services/notification_service.go
index 802d109d..b5bad368 100644
--- a/api/pkg/services/notification_service.go
+++ b/api/pkg/services/notification_service.go
@@ -2,6 +2,7 @@ package services
import (
"context"
+ "errors"
"fmt"
"time"
@@ -50,6 +51,8 @@ type NotificationSendParams struct {
UserID entities.UserID
PhoneID uuid.UUID
PhoneNotificationID uuid.UUID
+ Source string
+ ScheduledAt time.Time
MessageID uuid.UUID
}
@@ -58,19 +61,15 @@ func (service *NotificationService) Send(ctx context.Context, params *Notificati
ctx, span := service.tracer.Start(ctx)
defer span.End()
- ctxLogger := service.tracer.CtxLogger(service.logger, span)
-
phone, err := service.phoneRepository.LoadByID(ctx, params.PhoneID)
if err != nil {
- service.updateStatus(ctx, params.PhoneNotificationID, entities.PhoneNotificationStatusFailed)
msg := fmt.Sprintf("cannot load phone with userID [%s] and phoneID [%s]", params.UserID, params.PhoneID)
- return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
+ return service.handleNotificationFailed(ctx, errors.New(msg), params)
}
if phone.FcmToken == nil {
- service.updateStatus(ctx, params.PhoneNotificationID, entities.PhoneNotificationStatusFailed)
msg := fmt.Sprintf("phone with id [%s] has no FCM token", phone.ID)
- return service.tracer.WrapErrorSpan(span, stacktrace.NewError(msg))
+ return service.handleNotificationFailed(ctx, errors.New(msg), params)
}
result, err := service.messagingClient.Send(ctx, &messaging.Message{
@@ -80,14 +79,9 @@ func (service *NotificationService) Send(ctx context.Context, params *Notificati
Token: *phone.FcmToken,
})
if err != nil {
- service.updateStatus(ctx, params.PhoneNotificationID, entities.PhoneNotificationStatusFailed)
- msg := fmt.Sprintf("cannot send notification for message [%s] to phone [%s]", params.MessageID, phone.ID)
- return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
+ return service.handleNotificationFailed(ctx, err, params)
}
-
- ctxLogger.Info(fmt.Sprintf("sent notification [%s] for message [%s] to phone [%s]", result, params.MessageID, phone.ID))
- service.updateStatus(ctx, params.PhoneNotificationID, entities.PhoneNotificationStatusSent)
- return nil
+ return service.handleNotificationSent(ctx, result, params)
}
// NotificationScheduleParams are parameters for sending a notification
@@ -140,6 +134,49 @@ func (service *NotificationService) Schedule(ctx context.Context, params *Notifi
return nil
}
+func (service *NotificationService) handleNotificationFailed(ctx context.Context, err error, params *NotificationSendParams) error {
+ ctx, span := service.tracer.Start(ctx)
+ defer span.End()
+
+ ctxLogger := service.tracer.CtxLogger(service.logger, span)
+
+ msg := fmt.Sprintf("cannot send notification for message [%s] to phone [%s]", params.MessageID, params.PhoneNotificationID)
+ ctxLogger.Warn(stacktrace.Propagate(err, msg))
+
+ event, err := service.createMessageNotificationFailedEvent(params.Source, err.Error(), params)
+ if err != nil {
+ return stacktrace.Propagate(err, fmt.Sprintf("cannot create [%s] event for notification [%s]", events.EventTypeMessageNotificationFailed, params.PhoneNotificationID))
+ }
+
+ if err = service.eventDispatcher.Dispatch(ctx, event); err != nil {
+ return stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch event [%s] for notification [%s]", event.Type(), params.PhoneNotificationID))
+ }
+
+ service.updateStatus(ctx, params.PhoneNotificationID, entities.PhoneNotificationStatusFailed)
+ return nil
+}
+
+func (service *NotificationService) handleNotificationSent(ctx context.Context, result string, params *NotificationSendParams) error {
+ ctx, span := service.tracer.Start(ctx)
+ defer span.End()
+
+ ctxLogger := service.tracer.CtxLogger(service.logger, span)
+
+ ctxLogger.Info(fmt.Sprintf("sent notification [%s] for message [%s] to phone [%s]", result, params.MessageID, params.PhoneID))
+
+ event, err := service.createMessageNotificationSentEvent(params.Source, result, params)
+ if err != nil {
+ return stacktrace.Propagate(err, fmt.Sprintf("cannot create [%s] event for notification [%s]", events.EventTypeMessageNotificationSent, params.PhoneNotificationID))
+ }
+
+ if err = service.eventDispatcher.Dispatch(ctx, event); err != nil {
+ return stacktrace.Propagate(err, fmt.Sprintf("cannot dispatch event [%s] for notification [%s]", event.Type(), params.PhoneNotificationID))
+ }
+
+ service.updateStatus(ctx, params.PhoneNotificationID, entities.PhoneNotificationStatusSent)
+ return nil
+}
+
func (service *NotificationService) createEvent(source string, notification *entities.PhoneNotification) (cloudevents.Event, error) {
event := cloudevents.NewEvent()
@@ -164,6 +201,57 @@ func (service *NotificationService) createEvent(source string, notification *ent
return event, nil
}
+func (service *NotificationService) createMessageNotificationSentEvent(source string, fcmMessageID string, params *NotificationSendParams) (cloudevents.Event, error) {
+ event := cloudevents.NewEvent()
+
+ event.SetSource(source)
+ event.SetType(events.EventTypeMessageNotificationSent)
+ event.SetTime(time.Now().UTC())
+ event.SetID(uuid.New().String())
+
+ payload := events.MessageNotificationSentPayload{
+ MessageID: params.MessageID,
+ UserID: params.UserID,
+ PhoneID: params.PhoneID,
+ ScheduledAt: params.ScheduledAt,
+ FcmMessageID: fcmMessageID,
+ NotificationSentAt: time.Now().UTC(),
+ NotificationID: params.PhoneNotificationID,
+ }
+
+ if err := event.SetData(cloudevents.ApplicationJSON, payload); err != nil {
+ msg := fmt.Sprintf("cannot encode %T [%#+v] as JSON", payload, payload)
+ return event, stacktrace.Propagate(err, msg)
+ }
+
+ return event, nil
+}
+
+func (service *NotificationService) createMessageNotificationFailedEvent(source string, errorMessage string, params *NotificationSendParams) (cloudevents.Event, error) {
+ event := cloudevents.NewEvent()
+
+ event.SetSource(source)
+ event.SetType(events.EventTypeMessageNotificationFailed)
+ event.SetTime(time.Now().UTC())
+ event.SetID(uuid.New().String())
+
+ payload := events.MessageNotificationFailedPayload{
+ MessageID: params.MessageID,
+ UserID: params.UserID,
+ PhoneID: params.PhoneID,
+ ErrorMessage: errorMessage,
+ NotificationFailedAt: time.Now().UTC(),
+ NotificationID: params.PhoneNotificationID,
+ }
+
+ if err := event.SetData(cloudevents.ApplicationJSON, payload); err != nil {
+ msg := fmt.Sprintf("cannot encode %T [%#+v] as JSON", payload, payload)
+ return event, stacktrace.Propagate(err, msg)
+ }
+
+ return event, nil
+}
+
func (service *NotificationService) updateStatus(ctx context.Context, notificationID uuid.UUID, status entities.PhoneNotificationStatus) {
ctx, span := service.tracer.Start(ctx)
defer span.End()
@@ -174,7 +262,6 @@ func (service *NotificationService) updateStatus(ctx context.Context, notificati
if err != nil {
msg := fmt.Sprintf("cannot update status of notificaiton with id [%s] to [%s]", notificationID, status)
ctxLogger.Error(stacktrace.Propagate(err, msg))
- return
}
ctxLogger.Info(fmt.Sprintf("updated status of notificaiton with id [%s] to [%s]", notificationID, status))