Skip to content

Commit 5e3fe39

Browse files
committed
Fix fetching of outstanding messages
1 parent 90c48d0 commit 5e3fe39

19 files changed

Lines changed: 173 additions & 36 deletions

File tree

android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ android {
3737
kotlinOptions {
3838
jvmTarget = '1.8'
3939
}
40+
namespace 'com.httpsms'
4041
}
4142

4243
dependencies {

android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
package="com.httpsms">
3+
xmlns:tools="http://schemas.android.com/tools">
54

65

76
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />

android/app/src/main/java/com/httpsms/FirebaseMessagingService.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,12 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
9999

100100
if (!Settings.getActiveStatus(applicationContext)) {
101101
Timber.w("user is not active, stopping processing")
102-
handleFailed(applicationContext, messageID, "MOBILE_APP_INACTIVE")
102+
handleFailed(applicationContext, messageID)
103103
return Result.failure()
104104
}
105105

106106
val message = getMessage(applicationContext, messageID) ?: return Result.failure()
107+
107108
registerReceivers(applicationContext, message.id)
108109

109110
sendMessage(
@@ -126,10 +127,10 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
126127
)
127128
}
128129

129-
private fun handleFailed(context: Context, messageID: String, reason: String) {
130+
private fun handleFailed(context: Context, messageID: String) {
130131
Timber.d("sending failed event for message with ID [${messageID}]")
131132
HttpSmsApiService(Settings.getApiKeyOrDefault(context))
132-
.sendFailedEvent(messageID, ZonedDateTime.now(ZoneOffset.UTC), reason)
133+
.sendFailedEvent(messageID, ZonedDateTime.now(ZoneOffset.UTC), "MOBILE_APP_INACTIVE")
133134
}
134135

135136
private fun getMessage(context: Context, messageID: String): Message? {

android/app/src/main/java/com/httpsms/HttpSmsApiService.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import java.util.logging.Logger.getLogger
1515

1616
class HttpSmsApiService(private val apiKey: String) {
1717
private val apiKeyHeader = "x-api-key"
18-
private val baseURL = URI("https://49b1-145-14-19-43.ngrok.io")
19-
// private val baseURL = URI("https://api.httpsms.com")
18+
// private val baseURL = URI("https://49b1-145-14-19-43.ngrok.io")
19+
private val baseURL = URI("https://api.httpsms.com")
2020
private val jsonMediaType = "application/json; charset=utf-8".toMediaType()
2121
private val client = OkHttpClient()
2222

@@ -32,16 +32,17 @@ class HttpSmsApiService(private val apiKey: String) {
3232

3333
val response = client.newCall(request).execute()
3434
if (response.isSuccessful) {
35-
val payload = ResponseMessage.fromJson(response.body!!.string())?.data
35+
val payload = ResponseMessage.fromJson(response.body!!.string())?.data
3636
if (payload == null) {
3737
Timber.e("cannot decode payload [${response.body}]")
3838
return null
3939
}
40+
Timber.w("response code [${response.code}]")
4041
response.close()
4142
return payload
4243
}
4344

44-
Timber.e("invalid response with code [${response.code}] and payload [${response.body}]")
45+
Timber.e("invalid response with code [${response.code}]")
4546
response.close()
4647
return null
4748
}

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ buildscript {
1212
}
1313

1414
plugins {
15-
id 'com.android.application' version '7.2.2' apply false
16-
id 'com.android.library' version '7.2.2' apply false
15+
id 'com.android.application' version '7.3.0' apply false
16+
id 'com.android.library' version '7.3.0' apply false
1717
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
1818
}
1919

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Thu Jun 23 15:32:32 EEST 2022
22
distributionBase=GRADLE_USER_HOME
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
44
distributionPath=wrapper/dists
55
zipStorePath=wrapper/dists
66
zipStoreBase=GRADLE_USER_HOME

api/pkg/entities/phone.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ func (phone *Phone) MessageExpirationDuration() time.Duration {
2929
return time.Duration(phone.MessageExpirationSeconds) * time.Second
3030
}
3131

32+
// MessageExpirationSecondsSanitized returns the message expiration seconds with default of 1 hour
33+
func (phone *Phone) MessageExpirationSecondsSanitized() uint {
34+
if phone.MessageExpirationSeconds == 0 {
35+
return 60 * 60 // 1 hour
36+
}
37+
return phone.MessageExpirationSeconds
38+
}
39+
3240
// MaxSendAttemptsSanitized returns the max send attempts replacing 0 with 1
3341
func (phone *Phone) MaxSendAttemptsSanitized() uint {
3442
if phone.MaxSendAttempts == 0 {

api/pkg/handlers/message_handler.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,19 @@ func (h *MessageHandler) GetOutstanding(c *fiber.Ctx) error {
128128
}
129129

130130
message, err := h.service.GetOutstanding(ctx, request.ToGetOutstandingParams(c.Path(), h.userIDFomContext(c), timestamp))
131+
if stacktrace.GetCode(err) == repositories.ErrCodeNotFound {
132+
msg := fmt.Sprintf("outstanding message with id [%s] already fetched", request.MessageID)
133+
ctxLogger.Warn(stacktrace.Propagate(err, msg))
134+
return h.responseNotFound(c, "outstanding message already processed")
135+
}
136+
131137
if err != nil {
132-
msg := fmt.Sprintf("cannot get outstnading messgage with ID [%s]", request.MessageID)
138+
msg := fmt.Sprintf("cannot get outstanding messgage with ID [%s]", request.MessageID)
133139
ctxLogger.Error(stacktrace.Propagate(err, msg))
134140
return h.responseInternalServerError(c)
135141
}
136142

143+
ctxLogger.Warn(stacktrace.NewError(fmt.Sprintf("New Message [%s]", spew.Sdump(message))))
137144
return h.responseOK(c, "outstanding message fetched successfully", message)
138145
}
139146

api/pkg/handlers/phone_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ func (h *PhoneHandler) Upsert(c *fiber.Ctx) error {
119119
if errors := h.validator.ValidateUpsert(ctx, request.Sanitize()); len(errors) != 0 {
120120
msg := fmt.Sprintf("validation errors [%s], while fetching phones [%+#v]", spew.Sdump(errors), request)
121121
ctxLogger.Warn(stacktrace.NewError(msg))
122-
return h.responseUnprocessableEntity(c, errors, "validation errors while fetching phones")
122+
return h.responseUnprocessableEntity(c, errors, "validation errors while updating phones")
123123
}
124124

125125
phone, err := h.service.Upsert(ctx, request.ToUpsertParams(h.userFromContext(c), c.OriginalURL()))

api/pkg/repositories/gorm_message_repository.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,20 @@ func (repository *gormMessageRepository) GetOutstanding(ctx context.Context, use
121121
Update("status", entities.MessageStatusSending).Error
122122
},
123123
)
124+
if errors.Is(err, gorm.ErrRecordNotFound) {
125+
msg := fmt.Sprintf("outstanding message with ID [%s] and userID [%s] does not exist", messageID, userID)
126+
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg))
127+
}
128+
124129
if err != nil {
125130
msg := fmt.Sprintf("cannot fetch outstanding message with userID [%s] and messageID [%s]", userID, messageID)
126131
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
127132
}
128133

134+
if message == nil || message.ID == uuid.Nil {
135+
msg := fmt.Sprintf("outstanding message with ID [%s] and userID [%s] does not exist", messageID, userID)
136+
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.NewErrorWithCode(ErrCodeNotFound, msg))
137+
}
138+
129139
return message, nil
130140
}

0 commit comments

Comments
 (0)