Skip to content

Commit a9dc66b

Browse files
committed
Use axiom for logs and fix bug with phone API key
1 parent 94b5859 commit a9dc66b

File tree

8 files changed

+31
-28
lines changed

8 files changed

+31
-28
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,9 @@ class HttpSmsApiService(private val apiKey: String, private val baseURL: URI) {
220220
return Triple(null,"Cannot validate the API key. Check if it is correct and try again.", null)
221221
}
222222

223-
response.close()
224223
Timber.i("FCM token submitted correctly with API key [$apiKey] and server url [$baseURL]" )
225-
226224
val payload = ResponsePhone.fromJson(response.body!!.string())?.data
225+
response.close()
227226
return Triple(payload, null, null)
228227
} catch (ex: Exception) {
229228
return Triple(null, null, ex.message)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,18 @@ class LoginActivity : AppCompatActivity() {
302302

303303
var e164PhoneNumber = formatE164(phoneNumber.text.toString().trim())
304304
var response = service.updateFcmToken(e164PhoneNumber, Constants.SIM1, Settings.getFcmToken(this) ?: "")
305-
if(response.second != null || response.third != null || !SmsManagerService.isDualSIM(this)) {
306-
Timber.e("error updating fcm token [${response.second}]")
305+
if(response.second != null || response.third != null) {
306+
Timber.e("error updating fcm token [${response.second}], third [${response.third}]")
307307
liveData.postValue(Pair(response.second, response.third))
308308
return@Thread
309309
}
310310

311+
if (!SmsManagerService.isDualSIM(this)) {
312+
Timber.d("single sim detected, no need to update sim2")
313+
liveData.postValue(Pair(null, null))
314+
return@Thread
315+
}
316+
311317
e164PhoneNumber = formatE164(phoneNumberSIM2.text.toString().trim())
312318
response = service.updateFcmToken(e164PhoneNumber, Constants.SIM2, Settings.getFcmToken(this) ?: "")
313319

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ class LogzTree(val context: Context): Timber.DebugTree() {
3535
t
3636
)
3737

38-
val body = Klaxon().toJsonString(logEntry).toRequestBody("application/x-www-form-urlencoded".toMediaType())
38+
val body = Klaxon().toJsonString(listOf(logEntry)).toRequestBody("application/json".toMediaType())
3939
val request: Request = Request.Builder()
40-
.url("https://listener.logz.io:8071?token=xPDQiZOOfemERsCaVsJXtMbhKfWdVyNk&type=http-bulk")
40+
.url("https://api.axiom.co/v1/datasets/production/ingest")
4141
.post(body)
42-
.header("Content-Type", "application/x-www-form-urlencoded")
42+
.header("Content-Type", "application/json")
43+
.header("Authorization", "Bearer xaat-2a2e0b73-3702-4971-a80f-be3956934950")
4344
.build()
4445

4546
Thread {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ class MainActivity : AppCompatActivity() {
335335
if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
336336
phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
337337
}
338-
Timber.w("numbers = [${phoneNumbers.joinToString()}]")
339338
val isStored = HttpSmsApiService.create(context).storeHeartbeat(phoneNumbers.toTypedArray(), charging)
340339
if (!isStored) {
341340
error = "Could not send heartbeat make sure the phone is connected to the internet"

api/pkg/di/container.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,6 @@ func (container *Container) PhoneAPIKeyRepository() (repository repositories.Pho
699699
container.Logger(),
700700
container.Tracer(),
701701
container.DB(),
702-
container.UserRistrettoCache(),
703702
)
704703
}
705704

api/pkg/repositories/gorm_phone_api_key_repository.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ func (repository *gormPhoneAPIKeyRepository) RemovePhoneByID(ctx context.Context
4444
defer span.End()
4545

4646
query := `
47-
UPDATE ?
47+
UPDATE phone_api_keys
4848
SET phone_ids = array_remove(phone_ids, ?),
4949
phone_numbers = array_remove(phone_numbers, ?)
5050
WHERE user_id = ? AND array_position(phone_ids, ?) IS NOT NULL;
5151
`
5252
err := repository.db.WithContext(ctx).
53-
Raw(query, (entities.PhoneAPIKey{}).TableName(), phoneID, phoneNumber, userID, phoneID).
53+
Exec(query, phoneID, phoneNumber, userID, phoneID).
5454
Error
5555
if err != nil {
5656
msg := fmt.Sprintf("cannot remove phone with ID [%s] and number [%s] for user with ID [%s] ", phoneID, phoneNumber, userID)
@@ -103,7 +103,7 @@ func (repository *gormPhoneAPIKeyRepository) LoadAuthContext(ctx context.Context
103103
}
104104

105105
phoneAPIKey := new(entities.PhoneAPIKey)
106-
err := repository.db.WithContext(ctx).Where("api_key = ?", phoneAPIKey).First(apiKey).Error
106+
err := repository.db.WithContext(ctx).Where("api_key = ?", apiKey).First(phoneAPIKey).Error
107107
if errors.Is(err, gorm.ErrRecordNotFound) {
108108
msg := fmt.Sprintf("phone api key [%s] does not exist", apiKey)
109109
return entities.AuthContext{}, repository.tracer.WrapErrorSpan(span, stacktrace.PropagateWithCode(err, ErrCodeNotFound, msg))
@@ -121,7 +121,7 @@ func (repository *gormPhoneAPIKeyRepository) LoadAuthContext(ctx context.Context
121121
PhoneNumbers: phoneAPIKey.PhoneNumbers,
122122
}
123123

124-
if result := repository.cache.SetWithTTL(apiKey, authUser, 1, 1*time.Hour); !result {
124+
if result := repository.cache.SetWithTTL(apiKey, authUser, 1, 15*time.Second); !result {
125125
msg := fmt.Sprintf("cannot cache [%T] with ID [%s] and result [%t]", authUser, phoneAPIKey.ID, result)
126126
ctxLogger.Error(repository.tracer.WrapErrorSpan(span, stacktrace.NewError(msg)))
127127
}
@@ -168,32 +168,33 @@ func (repository *gormPhoneAPIKeyRepository) AddPhone(ctx context.Context, phone
168168

169169
err := crdbgorm.ExecuteTx(ctx, repository.db, nil, func(tx *gorm.DB) error {
170170
query := `
171-
UPDATE ?
171+
UPDATE phone_api_keys
172172
SET phone_ids = array_remove(phone_ids, ?),
173173
phone_numbers = array_remove(phone_numbers, ?)
174174
WHERE user_id = ?;
175175
`
176176
err := tx.WithContext(ctx).
177-
Raw(query, phoneAPIKey.TableName(), phone.ID, phone.PhoneNumber, phone.UserID).
177+
Exec(query, phone.ID, phone.PhoneNumber, phone.UserID).
178178
Error
179179
if err != nil {
180180
msg := fmt.Sprintf("cannot remove phone with ID [%s] from API Key with ID [%s] for user with ID [%s]", phone.ID, phoneAPIKey.ID, phone.UserID)
181181
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
182182
}
183183

184184
query = `
185-
UPDATE ?
185+
UPDATE phone_api_keys
186186
SET phone_ids = array_append(phone_ids, ?),
187187
phone_numbers = array_append(phone_numbers, ?)
188188
WHERE array_position(phone_ids, ?) IS NULL AND id = ?;
189189
`
190-
err = repository.db.WithContext(ctx).
191-
Raw(query, phoneAPIKey.TableName(), phone.ID, phone.PhoneNumber, phoneAPIKey.ID).
190+
err = tx.WithContext(ctx).
191+
Exec(query, phone.ID, phone.PhoneNumber, phone.ID, phoneAPIKey.ID).
192192
Error
193193
if err != nil {
194194
msg := fmt.Sprintf("cannot add [%T] with ID [%s] from API Key with ID [%s] for user with ID [%s]", phone, phone.ID, phoneAPIKey.ID, phone.UserID)
195195
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
196196
}
197+
197198
return nil
198199
})
199200
if err != nil {
@@ -209,21 +210,19 @@ func (repository *gormPhoneAPIKeyRepository) RemovePhone(ctx context.Context, ph
209210
defer span.End()
210211

211212
query := `
212-
UPDATE ?
213+
UPDATE phone_api_keys
213214
SET phone_ids = array_remove(phone_ids, ?),
214215
phone_numbers = array_remove(phone_numbers, ?)
215216
WHERE id = ?;
216217
`
217218
err := repository.db.WithContext(ctx).
218-
Raw(query, phoneAPIKey.TableName(), phone.ID, phone.PhoneNumber, phoneAPIKey.ID).
219+
Exec(query, phone.ID, phone.PhoneNumber, phoneAPIKey.ID).
219220
Error
220221
if err != nil {
221222
msg := fmt.Sprintf("cannot remove phone with ID [%s] from phone API key with ID [%s]", phone.ID, phoneAPIKey.ID)
222223
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
223224
}
224225

225-
repository.cache.Del(phoneAPIKey.APIKey)
226-
227226
return nil
228227
}
229228

web/pages/phone-api-keys/index.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
</td>
120120
<td>{{ phoneApiKey.created_at | timestamp }}</td>
121121
<td>
122-
<ul v-if="phoneApiKey.phone_numbers" class="ml-n3">
122+
<ul v-if="phoneApiKey.phone_numbers.length" class="ml-n3">
123123
<li
124124
v-for="phoneNumber in phoneApiKey.phone_numbers"
125125
:key="phoneNumber"
@@ -141,7 +141,7 @@
141141
</v-btn>
142142
</li>
143143
</ul>
144-
<span v-else>-</span>
144+
<span v-else class="text--secondary">-</span>
145145
</td>
146146
<td>
147147
<v-btn
@@ -405,7 +405,7 @@ export default Vue.extend({
405405
this.loading = true
406406
this.$store
407407
.dispatch('indexPhoneApiKeys')
408-
.then((phoneApiKeys) => {
408+
.then((phoneApiKeys: Array<EntitiesPhoneAPIKey>) => {
409409
this.phoneApiKeys = phoneApiKeys
410410
})
411411
.finally(() => {
@@ -423,7 +423,7 @@ export default Vue.extend({
423423
)?.id,
424424
})
425425
.then(() => {
426-
this.deleteApiKeyDialog = false
426+
this.removePhoneFromApiKeyDialog = false
427427
this.loadPhoneApiKeys()
428428
})
429429
.finally(() => {

web/store/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,12 @@ export const actions = {
518518

519519
deletePhoneFromPhoneApiKey(
520520
context: ActionContext<State, State>,
521-
payload: { phoneAPIKeyID: string; phoneID: string },
521+
payload: { phoneApiKeyId: string; phoneId: string },
522522
) {
523523
return new Promise<void>((resolve, reject) => {
524524
axios
525525
.delete<ResponsesNoContent>(
526-
`/v1/api-keys/${payload.phoneAPIKeyID}/phones/${payload.phoneID}`,
526+
`/v1/api-keys/${payload.phoneApiKeyId}/phones/${payload.phoneId}`,
527527
)
528528
.then(async (response: AxiosResponse<ResponsesNoContent>) => {
529529
await context.dispatch('addNotification', {

0 commit comments

Comments
 (0)