Skip to content

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

Fix the deleting of phone notifications

Acho Arnold10af367c953a
diff --git a/api/pkg/listeners/phone_notification_listener.go b/api/pkg/listeners/phone_notification_listener.go
index bcb15612..95333ef7 100644
--- a/api/pkg/listeners/phone_notification_listener.go
+++ b/api/pkg/listeners/phone_notification_listener.go
@@ -38,6 +38,7 @@ func NewNotificationListener(
events.EventTypeMessageNotificationSend: l.onMessageNotificationSend,
events.PhoneHeartbeatMissed: l.onPhoneHeartbeatMissed,
events.UserAccountDeleted: l.onUserAccountDeleted,
+ events.MessageAPIDeleted: l.onMessageAPIDeleted,
}
}
@@ -167,3 +168,22 @@ func (listener *PhoneNotificationListener) onUserAccountDeleted(ctx context.Cont
return nil
}
+
+// onMessageAPIDeleted handles the events.MessageAPIDeleted event
+func (listener *PhoneNotificationListener) onMessageAPIDeleted(ctx context.Context, event cloudevents.Event) error {
+ ctx, span := listener.tracer.Start(ctx)
+ defer span.End()
+
+ var payload events.MessageAPIDeletedPayload
+ if err := event.DataAs(&payload); err != nil {
+ msg := fmt.Sprintf("cannot decode [%s] into [%T]", event.Data(), payload)
+ return listener.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
+ }
+
+ if err := listener.service.DeleteByMessageID(ctx, payload.UserID, payload.MessageID); err != nil {
+ msg := fmt.Sprintf("cannot delete [entities.PhoneNotification] for user [%s] and message [%s] on [%s] event with ID [%s]", payload.UserID, payload.MessageID, event.Type(), event.ID())
+ return listener.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
+ }
+
+ return nil
+}
diff --git a/api/pkg/repositories/gorm_phone_notification_repository.go b/api/pkg/repositories/gorm_phone_notification_repository.go
index fc8d1be6..e1136415 100644
--- a/api/pkg/repositories/gorm_phone_notification_repository.go
+++ b/api/pkg/repositories/gorm_phone_notification_repository.go
@@ -60,12 +60,26 @@ func (repository *gormPhoneNotificationRepository) DeleteAllForUser(
return nil
}
+// DeleteByMessageID deletes all entities.PhoneNotification for a user and message ID.
+func (repository *gormPhoneNotificationRepository) DeleteByMessageID(ctx context.Context, userID entities.UserID, messageID uuid.UUID) error {
+ ctx, span := repository.tracer.Start(ctx)
+ defer span.End()
+
+ err := repository.db.WithContext(ctx).
+ Where("user_id = ? AND message_id = ?", userID, messageID).
+ Delete(&entities.PhoneNotification{}).Error
+ if err != nil {
+ msg := fmt.Sprintf("cannot delete [%T] for user [%s] and message with ID [%s]", &entities.PhoneNotification{}, userID, messageID)
+ return repository.tracer.WrapErrorSpan(span,
+ stacktrace.Propagate(err, msg),
+ )
+ }
+
+ return nil
+}
+
// UpdateStatus updates the status of a phone notification.
-func (repository *gormPhoneNotificationRepository) UpdateStatus(
- ctx context.Context,
- notificationID uuid.UUID,
- status entities.PhoneNotificationStatus,
-) error {
+func (repository *gormPhoneNotificationRepository) UpdateStatus(ctx context.Context, notificationID uuid.UUID, status entities.PhoneNotificationStatus) error {
ctx, span := repository.tracer.Start(ctx)
defer span.End()
diff --git a/api/pkg/repositories/phone_notification_repository.go b/api/pkg/repositories/phone_notification_repository.go
index e8bedfe4..2abc67f9 100644
--- a/api/pkg/repositories/phone_notification_repository.go
+++ b/api/pkg/repositories/phone_notification_repository.go
@@ -22,4 +22,7 @@ type PhoneNotificationRepository interface {
// DeleteAllForUser deletes all entities.PhoneNotification for a user
DeleteAllForUser(ctx context.Context, userID entities.UserID) error
+
+ // DeleteByMessageID deletes entities.PhoneNotification for a message and user
+ DeleteByMessageID(ctx context.Context, userID entities.UserID, messageID uuid.UUID) error
}
diff --git a/api/pkg/services/phone_notification_service.go b/api/pkg/services/phone_notification_service.go
index 5da01f04..4b0bf893 100644
--- a/api/pkg/services/phone_notification_service.go
+++ b/api/pkg/services/phone_notification_service.go
@@ -65,6 +65,20 @@ func (service *PhoneNotificationService) DeleteAllForUser(ctx context.Context, u
return nil
}
+// DeleteByMessageID deletes all entities.PhoneNotification for a user and message ID.
+func (service *PhoneNotificationService) DeleteByMessageID(ctx context.Context, userID entities.UserID, messageID uuid.UUID) error {
+ ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
+ defer span.End()
+
+ if err := service.phoneNotificationRepository.DeleteByMessageID(ctx, userID, messageID); err != nil {
+ msg := fmt.Sprintf("could not delete [entities.PhoneNotification] for user [%s] and message with ID [%s]", userID, messageID)
+ return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
+ }
+
+ ctxLogger.Info(fmt.Sprintf("deleted [entities.PhoneNotification] for user [%s] and message with ID [%s]", userID, messageID))
+ return nil
+}
+
// SendHeartbeatFCM sends a heartbeat message so the phone can request a heartbeat
func (service *PhoneNotificationService) SendHeartbeatFCM(ctx context.Context, payload *events.PhoneHeartbeatMissedPayload) error {
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)