mirror of
https://github.com/makayabou/asg-server.git
synced 2026-05-02 17:43:36 +02:00
[push] add support for multiple push types
This commit is contained in:
parent
4d4f1e0bcd
commit
d8af7e0c83
2
go.mod
2
go.mod
@ -4,7 +4,7 @@ go 1.22.0
|
||||
|
||||
require (
|
||||
firebase.google.com/go/v4 v4.12.1
|
||||
github.com/android-sms-gateway/client-go v1.0.0
|
||||
github.com/android-sms-gateway/client-go v1.0.1-0.20240610220902-94dc5641aa00
|
||||
github.com/capcom6/go-helpers v0.0.0-20240521035030-5f57bddeecee
|
||||
github.com/capcom6/go-infra-fx v0.0.2
|
||||
github.com/go-playground/validator/v10 v10.16.0
|
||||
|
||||
2
go.sum
2
go.sum
@ -32,6 +32,8 @@ github.com/android-sms-gateway/client-go v0.0.0-20240530135354-8d1ce85b9734 h1:d
|
||||
github.com/android-sms-gateway/client-go v0.0.0-20240530135354-8d1ce85b9734/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
|
||||
github.com/android-sms-gateway/client-go v1.0.0 h1:TPRNHlgcEW6jThsx0y4AG1J7wH5Iry+c6h+ailrSQW4=
|
||||
github.com/android-sms-gateway/client-go v1.0.0/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
|
||||
github.com/android-sms-gateway/client-go v1.0.1-0.20240610220902-94dc5641aa00 h1:GUtH5Pw57cxhpQ3y8EYQFTqbpjQ2dZR6G7BjcHK6lAM=
|
||||
github.com/android-sms-gateway/client-go v1.0.1-0.20240610220902-94dc5641aa00/go.mod h1:DQsReciU1xcaVW3T5Z2bqslNdsAwCFCtghawmA6g6L4=
|
||||
github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI=
|
||||
github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
|
||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"github.com/android-sms-gateway/client-go/smsgateway"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/base"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/push"
|
||||
"github.com/capcom6/sms-gateway/pkg/types"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/limiter"
|
||||
@ -67,7 +68,12 @@ func (h *upstreamHandler) postPush(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := h.pushSvc.Enqueue(c.Context(), v.Token, map[string]string{}); err != nil {
|
||||
event := push.Event{
|
||||
Event: types.ZeroDefault(v.Event, smsgateway.PushMessageEnqueued),
|
||||
Data: v.Data,
|
||||
}
|
||||
|
||||
if err := h.pushSvc.Enqueue(c.Context(), v.Token, &event); err != nil {
|
||||
h.Logger.Error("Can't push message", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,11 +3,11 @@ package webhooks
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/android-sms-gateway/client-go/smsgateway"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/handlers/base"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/webhooks"
|
||||
"github.com/capcom6/sms-gateway/pkg/smsgateway"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/fx"
|
||||
@ -64,7 +64,7 @@ func (h *ThirdPartyController) get(user models.User, c *fiber.Ctx) error {
|
||||
//
|
||||
// Register webhook
|
||||
func (h *ThirdPartyController) post(user models.User, c *fiber.Ctx) error {
|
||||
dto := &smsgateway.WebhookDTO{}
|
||||
dto := &smsgateway.Webhook{}
|
||||
|
||||
if err := h.BodyParserValidator(c, dto); err != nil {
|
||||
return err
|
||||
|
||||
@ -7,7 +7,6 @@ import (
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/auth"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/webhooks"
|
||||
_ "github.com/capcom6/sms-gateway/pkg/smsgateway"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
|
||||
@ -217,7 +217,7 @@ func (s *Service) Enqeue(device models.Device, message smsgateway.Message, opts
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := s.PushSvc.Enqueue(ctx, token, map[string]string{}); err != nil {
|
||||
if err := s.PushSvc.Enqueue(ctx, token, push.NewMessageEnqueuedEvent()); err != nil {
|
||||
s.Logger.Error("Can't enqueue message", zap.String("token", token), zap.Error(err))
|
||||
}
|
||||
}(*device.PushToken)
|
||||
|
||||
36
internal/sms-gateway/modules/push/events.go
Normal file
36
internal/sms-gateway/modules/push/events.go
Normal file
@ -0,0 +1,36 @@
|
||||
package push
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/android-sms-gateway/client-go/smsgateway"
|
||||
)
|
||||
|
||||
type Event struct {
|
||||
Event smsgateway.PushEventType
|
||||
Data any
|
||||
}
|
||||
|
||||
func (e *Event) Map() map[string]string {
|
||||
json, _ := json.Marshal(e.Data)
|
||||
|
||||
return map[string]string{
|
||||
"event": string(e.Event),
|
||||
"data": string(json),
|
||||
}
|
||||
}
|
||||
|
||||
func NewEvent(event smsgateway.PushEventType, data any) *Event {
|
||||
return &Event{
|
||||
Event: event,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func NewMessageEnqueuedEvent() *Event {
|
||||
return NewEvent(smsgateway.PushMessageEnqueued, nil)
|
||||
}
|
||||
|
||||
func NewWebhooksUpdatedEvent() *Event {
|
||||
return NewEvent(smsgateway.PushWebhooksUpdated, nil)
|
||||
}
|
||||
@ -70,8 +70,8 @@ func (s *Service) Run(ctx context.Context) {
|
||||
}
|
||||
|
||||
// Enqueue adds the data to the cache and immediately sends all messages if the debounce is 0.
|
||||
func (s *Service) Enqueue(ctx context.Context, token string, data map[string]string) error {
|
||||
s.cache.Set(token, data)
|
||||
func (s *Service) Enqueue(ctx context.Context, token string, event *Event) error {
|
||||
s.cache.Set(token, event.Map())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
package webhooks
|
||||
|
||||
import "github.com/capcom6/sms-gateway/pkg/smsgateway"
|
||||
import "github.com/android-sms-gateway/client-go/smsgateway"
|
||||
|
||||
func webhookToDTO(model *Webhook) smsgateway.WebhookDTO {
|
||||
return smsgateway.WebhookDTO{
|
||||
func webhookToDTO(model *Webhook) smsgateway.Webhook {
|
||||
return smsgateway.Webhook{
|
||||
ID: model.ExtID,
|
||||
URL: model.URL,
|
||||
Event: model.Event,
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
package webhooks
|
||||
|
||||
import (
|
||||
"github.com/android-sms-gateway/client-go/smsgateway"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/models"
|
||||
"github.com/capcom6/sms-gateway/pkg/smsgateway"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
||||
@ -3,9 +3,9 @@ package webhooks
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/android-sms-gateway/client-go/smsgateway"
|
||||
"github.com/capcom6/go-helpers/slices"
|
||||
"github.com/capcom6/sms-gateway/internal/sms-gateway/modules/db"
|
||||
"github.com/capcom6/sms-gateway/pkg/smsgateway"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@ -21,7 +21,7 @@ func NewService(idgen db.IDGen, webhooks *Repository) *Service {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Select(userID string, filters ...SelectFilter) ([]smsgateway.WebhookDTO, error) {
|
||||
func (s *Service) Select(userID string, filters ...SelectFilter) ([]smsgateway.Webhook, error) {
|
||||
filters = append(filters, WithUserID(userID))
|
||||
|
||||
items, err := s.webhooks.Select(filters...)
|
||||
@ -32,7 +32,7 @@ func (s *Service) Select(userID string, filters ...SelectFilter) ([]smsgateway.W
|
||||
return slices.Map(items, webhookToDTO), nil
|
||||
}
|
||||
|
||||
func (s *Service) Replace(userID string, webhook *smsgateway.WebhookDTO) error {
|
||||
func (s *Service) Replace(userID string, webhook *smsgateway.Webhook) error {
|
||||
if webhook.ID == "" {
|
||||
webhook.ID = s.idgen()
|
||||
}
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
package smsgateway
|
||||
|
||||
type WebhookDTO struct {
|
||||
ID string `json:"id" validate:"max=36" example:"123e4567-e89b-12d3-a456-426614174000"`
|
||||
URL string `json:"url" validate:"required,http_url" example:"https://example.com/webhook"`
|
||||
Event WebhookEvent `json:"event" validate:"required" example:"sms:received"`
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
package smsgateway
|
||||
|
||||
type WebhookEvent string
|
||||
|
||||
const (
|
||||
WebhookEventSmsReceived WebhookEvent = "sms:received"
|
||||
)
|
||||
@ -10,3 +10,19 @@ func OrDefault[T any](v *T, def T) T {
|
||||
}
|
||||
return *v
|
||||
}
|
||||
|
||||
// ZeroDefault returns the default value if the given value is zero, otherwise it returns the value.
|
||||
//
|
||||
// Parameters:
|
||||
// - v: The value to check.
|
||||
// - def: The default value to return if v is zero.
|
||||
//
|
||||
// Returns:
|
||||
// - The default value if v is zero, otherwise the value.
|
||||
func ZeroDefault[T comparable](v T, def T) T {
|
||||
zero := new(T)
|
||||
if v == *zero {
|
||||
return def
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
36
pkg/types/types_test.go
Normal file
36
pkg/types/types_test.go
Normal file
@ -0,0 +1,36 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestZeroDefault(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value string
|
||||
def string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "String zero value",
|
||||
value: "",
|
||||
def: "default",
|
||||
want: "default",
|
||||
},
|
||||
{
|
||||
name: "String non-zero value",
|
||||
value: "value",
|
||||
def: "default",
|
||||
want: "value",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := ZeroDefault(tt.value, tt.def)
|
||||
if got != tt.want {
|
||||
t.Errorf("ZeroDefault() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user