Skip to content

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

Add ability to store multiple heartbeats in 1 request

Acho Arnold98fd2d753d9a
diff --git a/api/docs/docs.go b/api/docs/docs.go
index fa55086e..273b0eb5 100644
--- a/api/docs/docs.go
+++ b/api/docs/docs.go
@@ -1,5 +1,5 @@
-// Code generated by swaggo/swag. DO NOT EDIT.
-
+// Package docs GENERATED BY SWAG; DO NOT EDIT
+// This file was generated by swaggo/swag
package docs
import "github.com/swaggo/swag"
@@ -1913,6 +1913,44 @@ const docTemplate = `{
}
}
}
+ },
+ "delete": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "Deletes the currently authenticated user together with all their data.",
+ "consumes": [
+ "application/json"
+ ],
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "Users"
+ ],
+ "summary": "Delete a user",
+ "responses": {
+ "201": {
+ "description": "Created",
+ "schema": {
+ "$ref": "#/definitions/responses.NoContent"
+ }
+ },
+ "401": {
+ "description": "Unauthorized",
+ "schema": {
+ "$ref": "#/definitions/responses.Unauthorized"
+ }
+ },
+ "500": {
+ "description": "Internal Server Error",
+ "schema": {
+ "$ref": "#/definitions/responses.InternalServerError"
+ }
+ }
+ }
}
},
"/users/subscription": {
@@ -2669,11 +2707,7 @@ const docTemplate = `{
},
"sim": {
"description": "SIM is the SIM card to use to send the message\n* SMS1: use the SIM card in slot 1\n* SMS2: use the SIM card in slot 2\n* DEFAULT: used the default communication SIM card",
- "allOf": [
- {
- "$ref": "#/definitions/entities.SIM"
- }
- ],
+ "type": "string",
"example": "DEFAULT"
},
"status": {
@@ -2811,7 +2845,8 @@ const docTemplate = `{
"example": "+18005550199"
},
"sim": {
- "$ref": "#/definitions/entities.SIM"
+ "description": "SIM card that received the message",
+ "type": "string"
},
"updated_at": {
"type": "string",
@@ -2823,48 +2858,6 @@ const docTemplate = `{
}
}
},
- "entities.SIM": {
- "type": "string",
- "enum": [
- "SIM1",
- "SIM2"
- ],
- "x-enum-varnames": [
- "SIM1",
- "SIM2"
- ]
- },
- "entities.SubscriptionName": {
- "type": "string",
- "enum": [
- "free",
- "pro-monthly",
- "pro-yearly",
- "ultra-monthly",
- "ultra-yearly",
- "pro-lifetime",
- "20k-monthly",
- "100k-monthly",
- "50k-monthly",
- "20k-yearly",
- "100k-yearly",
- "50k-yearly"
- ],
- "x-enum-varnames": [
- "SubscriptionNameFree",
- "SubscriptionNameProMonthly",
- "SubscriptionNameProYearly",
- "SubscriptionNameUltraMonthly",
- "SubscriptionNameUltraYearly",
- "SubscriptionNameProLifetime",
- "SubscriptionName20KMonthly",
- "SubscriptionName100KMonthly",
- "SubscriptionName50KMonthly",
- "SubscriptionName20KYearly",
- "SubscriptionName100KYearly",
- "SubscriptionName50KYearly"
- ]
- },
"entities.User": {
"type": "object",
"required": [
@@ -2926,11 +2919,7 @@ const docTemplate = `{
"example": "8f9c71b8-b84e-4417-8408-a62274f65a08"
},
"subscription_name": {
- "allOf": [
- {
- "$ref": "#/definitions/entities.SubscriptionName"
- }
- ],
+ "type": "string",
"example": "free"
},
"subscription_renews_at": {
@@ -3051,14 +3040,17 @@ const docTemplate = `{
"type": "object",
"required": [
"charging",
- "owner"
+ "phone_numbers"
],
"properties": {
"charging": {
"type": "boolean"
},
- "owner": {
- "type": "string"
+ "phone_numbers": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
}
}
},
@@ -3178,11 +3170,7 @@ const docTemplate = `{
},
"sim": {
"description": "SIM card that received the message",
- "allOf": [
- {
- "$ref": "#/definitions/entities.SIM"
- }
- ],
+ "type": "string",
"example": "SIM1"
},
"timestamp": {
@@ -3874,8 +3862,6 @@ var SwaggerInfo = &swag.Spec{
Description: "API to send SMS messages using android [SmsManager](https://developer.android.com/reference/android/telephony/SmsManager) via HTTP",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
- LeftDelim: "{{",
- RightDelim: "}}",
}
func init() {
diff --git a/api/docs/swagger.json b/api/docs/swagger.json
index 16182844..cd167e5a 100644
--- a/api/docs/swagger.json
+++ b/api/docs/swagger.json
@@ -1733,6 +1733,38 @@
}
}
}
+ },
+ "delete": {
+ "security": [
+ {
+ "ApiKeyAuth": []
+ }
+ ],
+ "description": "Deletes the currently authenticated user together with all their data.",
+ "consumes": ["application/json"],
+ "produces": ["application/json"],
+ "tags": ["Users"],
+ "summary": "Delete a user",
+ "responses": {
+ "201": {
+ "description": "Created",
+ "schema": {
+ "$ref": "#/definitions/responses.NoContent"
+ }
+ },
+ "401": {
+ "description": "Unauthorized",
+ "schema": {
+ "$ref": "#/definitions/responses.Unauthorized"
+ }
+ },
+ "500": {
+ "description": "Internal Server Error",
+ "schema": {
+ "$ref": "#/definitions/responses.InternalServerError"
+ }
+ }
+ }
}
},
"/users/subscription": {
@@ -2445,11 +2477,7 @@
},
"sim": {
"description": "SIM is the SIM card to use to send the message\n* SMS1: use the SIM card in slot 1\n* SMS2: use the SIM card in slot 2\n* DEFAULT: used the default communication SIM card",
- "allOf": [
- {
- "$ref": "#/definitions/entities.SIM"
- }
- ],
+ "type": "string",
"example": "DEFAULT"
},
"status": {
@@ -2587,7 +2615,8 @@
"example": "+18005550199"
},
"sim": {
- "$ref": "#/definitions/entities.SIM"
+ "description": "SIM card that received the message",
+ "type": "string"
},
"updated_at": {
"type": "string",
@@ -2599,42 +2628,6 @@
}
}
},
- "entities.SIM": {
- "type": "string",
- "enum": ["SIM1", "SIM2"],
- "x-enum-varnames": ["SIM1", "SIM2"]
- },
- "entities.SubscriptionName": {
- "type": "string",
- "enum": [
- "free",
- "pro-monthly",
- "pro-yearly",
- "ultra-monthly",
- "ultra-yearly",
- "pro-lifetime",
- "20k-monthly",
- "100k-monthly",
- "50k-monthly",
- "20k-yearly",
- "100k-yearly",
- "50k-yearly"
- ],
- "x-enum-varnames": [
- "SubscriptionNameFree",
- "SubscriptionNameProMonthly",
- "SubscriptionNameProYearly",
- "SubscriptionNameUltraMonthly",
- "SubscriptionNameUltraYearly",
- "SubscriptionNameProLifetime",
- "SubscriptionName20KMonthly",
- "SubscriptionName100KMonthly",
- "SubscriptionName50KMonthly",
- "SubscriptionName20KYearly",
- "SubscriptionName100KYearly",
- "SubscriptionName50KYearly"
- ]
- },
"entities.User": {
"type": "object",
"required": [
@@ -2696,11 +2689,7 @@
"example": "8f9c71b8-b84e-4417-8408-a62274f65a08"
},
"subscription_name": {
- "allOf": [
- {
- "$ref": "#/definitions/entities.SubscriptionName"
- }
- ],
+ "type": "string",
"example": "free"
},
"subscription_renews_at": {
@@ -2806,13 +2795,16 @@
},
"requests.HeartbeatStore": {
"type": "object",
- "required": ["charging", "owner"],
+ "required": ["charging", "phone_numbers"],
"properties": {
"charging": {
"type": "boolean"
},
- "owner": {
- "type": "string"
+ "phone_numbers": {
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
}
}
},
@@ -2908,11 +2900,7 @@
},
"sim": {
"description": "SIM card that received the message",
- "allOf": [
- {
- "$ref": "#/definitions/entities.SIM"
- }
- ],
+ "type": "string",
"example": "SIM1"
},
"timestamp": {
diff --git a/api/docs/swagger.yaml b/api/docs/swagger.yaml
index 86853745..0b5db182 100644
--- a/api/docs/swagger.yaml
+++ b/api/docs/swagger.yaml
@@ -172,14 +172,13 @@ definitions:
example: "2022-06-05T14:26:09.527976+03:00"
type: string
sim:
- allOf:
- - $ref: "#/definitions/entities.SIM"
description: |-
SIM is the SIM card to use to send the message
* SMS1: use the SIM card in slot 1
* SMS2: use the SIM card in slot 2
* DEFAULT: used the default communication SIM card
example: DEFAULT
+ type: string
status:
example: pending
type: string
@@ -305,7 +304,8 @@ definitions:
example: "+18005550199"
type: string
sim:
- $ref: "#/definitions/entities.SIM"
+ description: SIM card that received the message
+ type: string
updated_at:
example: "2022-06-05T14:26:10.303278+03:00"
type: string
@@ -325,42 +325,6 @@ definitions:
- updated_at
- user_id
type: object
- entities.SIM:
- enum:
- - SIM1
- - SIM2
- type: string
- x-enum-varnames:
- - SIM1
- - SIM2
- entities.SubscriptionName:
- enum:
- - free
- - pro-monthly
- - pro-yearly
- - ultra-monthly
- - ultra-yearly
- - pro-lifetime
- - 20k-monthly
- - 100k-monthly
- - 50k-monthly
- - 20k-yearly
- - 100k-yearly
- - 50k-yearly
- type: string
- x-enum-varnames:
- - SubscriptionNameFree
- - SubscriptionNameProMonthly
- - SubscriptionNameProYearly
- - SubscriptionNameUltraMonthly
- - SubscriptionNameUltraYearly
- - SubscriptionNameProLifetime
- - SubscriptionName20KMonthly
- - SubscriptionName100KMonthly
- - SubscriptionName50KMonthly
- - SubscriptionName20KYearly
- - SubscriptionName100KYearly
- - SubscriptionName50KYearly
entities.User:
properties:
active_phone_id:
@@ -394,9 +358,8 @@ definitions:
example: 8f9c71b8-b84e-4417-8408-a62274f65a08
type: string
subscription_name:
- allOf:
- - $ref: "#/definitions/entities.SubscriptionName"
example: free
+ type: string
subscription_renews_at:
example: "2022-06-05T14:26:02.302718+03:00"
type: string
@@ -499,11 +462,13 @@ definitions:
properties:
charging:
type: boolean
- owner:
- type: string
+ phone_numbers:
+ items:
+ type: string
+ type: array
required:
- charging
- - owner
+ - phone_numbers
type: object
requests.MessageBulkSend:
properties:
@@ -597,10 +562,9 @@ definitions:
example: "+18005550199"
type: string
sim:
- allOf:
- - $ref: "#/definitions/entities.SIM"
description: SIM card that received the message
example: SIM1
+ type: string
timestamp:
description:
Timestamp is the time when the event was emitted, Please send
@@ -2384,6 +2348,32 @@ paths:
tags:
- Users
/users/me:
+ delete:
+ consumes:
+ - application/json
+ description:
+ Deletes the currently authenticated user together with all their
+ data.
+ produces:
+ - application/json
+ responses:
+ "201":
+ description: Created
+ schema:
+ $ref: "#/definitions/responses.NoContent"
+ "401":
+ description: Unauthorized
+ schema:
+ $ref: "#/definitions/responses.Unauthorized"
+ "500":
+ description: Internal Server Error
+ schema:
+ $ref: "#/definitions/responses.InternalServerError"
+ security:
+ - ApiKeyAuth: []
+ summary: Delete a user
+ tags:
+ - Users
get:
consumes:
- application/json
diff --git a/api/pkg/handlers/heartbeat_handler.go b/api/pkg/handlers/heartbeat_handler.go
index dbddc09b..dc87d39d 100644
--- a/api/pkg/handlers/heartbeat_handler.go
+++ b/api/pkg/handlers/heartbeat_handler.go
@@ -2,6 +2,9 @@ package handlers
import (
"fmt"
+ "sync"
+
+ "github.com/NdoleStudio/httpsms/pkg/entities"
"github.com/NdoleStudio/httpsms/pkg/requests"
"github.com/NdoleStudio/httpsms/pkg/services"
@@ -121,12 +124,23 @@ func (h *HeartbeatHandler) Store(c *fiber.Ctx) error {
return h.responseUnprocessableEntity(c, errors, "validation errors while storing heartbeat")
}
- heartbeat, err := h.service.Store(ctx, request.ToStoreParams(h.userFromContext(c), c.OriginalURL(), c.Get("X-Client-Version")))
- if err != nil {
- msg := fmt.Sprintf("cannot store heartbeat with params [%+#v]", request)
- ctxLogger.Error(stacktrace.Propagate(err, msg))
- return h.responseInternalServerError(c)
+ params := request.ToStoreParams(h.userFromContext(c), c.OriginalURL(), c.Get("X-Client-Version"))
+
+ wg := sync.WaitGroup{}
+ responses := make([]*entities.Heartbeat, len(params))
+ for index, value := range params {
+ wg.Add(1)
+ go func(input services.HeartbeatStoreParams, index int) {
+ response, err := h.service.Store(ctx, input)
+ if err != nil {
+ msg := fmt.Sprintf("cannot store heartbeat with params [%+#v]", request)
+ ctxLogger.Error(stacktrace.Propagate(err, msg))
+ }
+ responses[index] = response
+ wg.Done()
+ }(value, index)
}
- return h.responseCreated(c, "heartbeat created successfully", heartbeat)
+ wg.Wait()
+ return h.responseCreated(c, fmt.Sprintf("[%d] heartbeats received successfully", len(responses)), responses)
}
diff --git a/api/pkg/requests/heartbeat_store_request.go b/api/pkg/requests/heartbeat_store_request.go
index 8f792612..9be48c26 100644
--- a/api/pkg/requests/heartbeat_store_request.go
+++ b/api/pkg/requests/heartbeat_store_request.go
@@ -10,24 +10,35 @@ import (
// HeartbeatStore is the payload for fetching entities.Heartbeat of a phone number
type HeartbeatStore struct {
request
- Charging bool `json:"charging"`
- Owner string `json:"owner"`
+ Owner string `json:"owner" swaggerignore:"true"`
+ Charging bool `json:"charging"`
+ PhoneNumbers []string `json:"phone_numbers"`
}
// Sanitize sets defaults to MessageOutstanding
func (input *HeartbeatStore) Sanitize() HeartbeatStore {
input.Owner = input.sanitizeAddress(input.Owner)
+
+ input.PhoneNumbers = input.sanitizeAddresses(input.PhoneNumbers)
+ if len(input.PhoneNumbers) == 0 {
+ input.PhoneNumbers = append(input.PhoneNumbers, input.Owner)
+ }
+
return *input
}
// ToStoreParams converts HeartbeatIndex to repositories.IndexParams
-func (input *HeartbeatStore) ToStoreParams(user entities.AuthUser, source string, version string) services.HeartbeatStoreParams {
- return services.HeartbeatStoreParams{
- Owner: input.Owner,
- Version: version,
- Source: source,
- Charging: input.Charging,
- Timestamp: time.Now().UTC(),
- UserID: user.ID,
+func (input *HeartbeatStore) ToStoreParams(user entities.AuthUser, source string, version string) []services.HeartbeatStoreParams {
+ var params []services.HeartbeatStoreParams
+ for _, phoneNumber := range input.PhoneNumbers {
+ params = append(params, services.HeartbeatStoreParams{
+ Owner: phoneNumber,
+ Charging: input.Charging,
+ Source: source,
+ Version: version,
+ UserID: user.ID,
+ Timestamp: time.Now(),
+ })
}
+ return params
}
diff --git a/api/pkg/requests/request.go b/api/pkg/requests/request.go
index e570d2ac..851137d1 100644
--- a/api/pkg/requests/request.go
+++ b/api/pkg/requests/request.go
@@ -13,6 +13,14 @@ import (
type request struct{}
+func (input *request) sanitizeAddresses(value []string) []string {
+ var result []string
+ for _, address := range value {
+ result = append(result, input.sanitizeAddress(address))
+ }
+ return result
+}
+
func (input *request) sanitizeAddress(value string) string {
value = strings.TrimSpace(value)
if !strings.HasPrefix(value, "+") && input.isDigits(value) && len(value) > 9 {
diff --git a/api/pkg/validators/heartbeat_handler_validator.go b/api/pkg/validators/heartbeat_handler_validator.go
index 6811dfd0..0472a475 100644
--- a/api/pkg/validators/heartbeat_handler_validator.go
+++ b/api/pkg/validators/heartbeat_handler_validator.go
@@ -62,9 +62,11 @@ func (validator *HeartbeatHandlerValidator) ValidateStore(_ context.Context, req
v := govalidator.New(govalidator.Options{
Data: &request,
Rules: govalidator.MapData{
- "owner": []string{
+ "phone_numbers": []string{
"required",
- phoneNumberRule,
+ "max:2",
+ "min:1",
+ multiplePhoneNumberRule,
},
},
})
diff --git a/api/pkg/validators/validator.go b/api/pkg/validators/validator.go
index 12a1a015..0436a480 100644
--- a/api/pkg/validators/validator.go
+++ b/api/pkg/validators/validator.go
@@ -17,6 +17,7 @@ type validator struct{}
const (
phoneNumberRule = "phoneNumber"
+ multiplePhoneNumberRule = "multiplePhoneNumber"
contactPhoneNumberRule = "contactPhoneNumber"
multipleContactPhoneNumberRule = "multipleContactPhoneNumber"
multipleInRule = "multipleIn"
@@ -40,6 +41,22 @@ func init() {
return nil
})
+ govalidator.AddCustomRule(multiplePhoneNumberRule, func(field string, rule string, message string, value interface{}) error {
+ phoneNumbers, ok := value.([]string)
+ if !ok {
+ return fmt.Errorf("The %s field must be an array of valid phone numbers", field)
+ }
+
+ for index, number := range phoneNumbers {
+ _, err := phonenumbers.Parse(number, phonenumbers.UNKNOWN_REGION)
+ if err != nil {
+ return fmt.Errorf("The %s field in index [%s] must be a valid E.164 phone number: https://en.wikipedia.org/wiki/E.164", field, index)
+ }
+ }
+
+ return nil
+ })
+
// custom rules to take fixed length word.
// e.g: max_word:5 will throw error if the field contains more than 5 words
govalidator.AddCustomRule(contactPhoneNumberRule, func(field string, rule string, message string, value interface{}) error {