Skip to content

Commit

Permalink
Track link service (go-delve#15)
Browse files Browse the repository at this point in the history
* Initial commit for track link service

* fix track_link migration

* add rpc and db methods for track link

- creating track links (still need to add generation of track link id)
- finding track link by track link id
- incrementing hits for track link

* name is now track_link

* rpc method to replace urls with track links

- still need to add postgres function to automagically generate the
tracklinkids

* track links cannot be https

* rename mms shorten_urls column to track_links

* rename New client function to NewClient

* add track links column to sms

* rename rpc methods to match spec

* add message_type column to track link

* use track link service to shorten links in sms/mms

* rename GenerateShortUrls to GenerateTrackLinks

- coz they're not short urls they're track links duh

* add track_link_generate function to db

- given an int length generate a track link id
- checks to see if id already exists in track link table and will
generate another if it does
- let's see whether this is sufficient...

* fix up modd after rebase
  • Loading branch information
samwfan authored Nov 27, 2020
1 parent a50b963 commit 0b76820
Show file tree
Hide file tree
Showing 40 changed files with 712 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
ALTER TABLE mms RENAME COLUMN track_links TO shorten_urls;
COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
ALTER TABLE mms RENAME COLUMN shorten_urls TO track_links;
COMMIT;
17 changes: 9 additions & 8 deletions backend/mms/rpc/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (db *db) FindByID(ctx context.Context, id string) (*MMS, error) {
sql := `
SELECT id, account_id, created_at, updated_at, provider_key, message_id, message_ref,
country, subject, messsage, content_urls, recipient, sender, status,
shorten_urls, unsub
track_links, unsub
FROM mms
WHERE id = $1
`
Expand All @@ -51,7 +51,7 @@ func (db *db) FindByID(ctx context.Context, id string) (*MMS, error) {
&mms.Recipient,
&mms.Sender,
&mms.Status,
&mms.ShortenURLs,
&mms.TrackLinks,
&mms.Unsub,
)
if err != nil {
Expand All @@ -67,7 +67,7 @@ func (db *db) FindByIDAndAccountID(ctx context.Context, id, accountID string) (*
sql := `
SELECT id, account_id, created_at, updated_at, provider_key, message_id, message_ref,
country, subject, messsage, content_urls, recipient, sender, status,
shorten_urls, unsub
track_links, unsub
FROM mms
WHERE id = $1 and account_id = $2
`
Expand All @@ -89,7 +89,7 @@ func (db *db) FindByIDAndAccountID(ctx context.Context, id, accountID string) (*
&mms.Recipient,
&mms.Sender,
&mms.Status,
&mms.ShortenURLs,
&mms.TrackLinks,
&mms.Unsub,
)
if err != nil {
Expand All @@ -101,14 +101,15 @@ func (db *db) FindByIDAndAccountID(ctx context.Context, id, accountID string) (*

func (db *db) InsertMMS(ctx context.Context, mms MMS) (*MMS, error) {
sql := `
INSERT INTO mms (account_id, created_at, updated_at, provider_key,
INSERT INTO mms (id, account_id, created_at, updated_at, provider_key,
message_ref, country, subject, message,
content_urls, recipient, sender, status, shorten_urls)
VALUES ($1, NOW(), NOW(), $2, $3, $4, $5, $6, $7, $8, $9, $10)
content_urls, recipient, sender, status, track_links)
VALUES ($1, $2, NOW(), NOW(), $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING id, created_at, updated_at
`

if err := db.postgres.QueryRow(ctx, sql,
mms.ID,
mms.AccountID,
mms.ProviderKey,
mms.MessageRef,
Expand All @@ -119,7 +120,7 @@ func (db *db) InsertMMS(ctx context.Context, mms MMS) (*MMS, error) {
mms.Recipient,
mms.Sender,
mms.Status,
mms.ShortenURLs,
mms.TrackLinks,
).Scan(&mms.ID, &mms.CreatedAt, &mms.UpdatedAt); err != nil {
return &MMS{}, err
}
Expand Down
25 changes: 22 additions & 3 deletions backend/mms/rpc/mms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"time"

"github.com/burstsms/mtmo-tp/backend/mms/worker"
tracklink "github.com/burstsms/mtmo-tp/backend/track_link/rpc/client"
webhook "github.com/burstsms/mtmo-tp/backend/webhook/rpc/client"
"github.com/google/uuid"
)

type MMS struct {
Expand All @@ -23,7 +25,7 @@ type MMS struct {
Recipient string `json:"recipient"`
Sender string `json:"sender"`
Status string `json:"status"`
ShortenURLs bool `json:"shorten_urls"`
TrackLinks bool `json:"track_links"`
Unsub bool `json:"unsub"`
}

Expand All @@ -36,7 +38,7 @@ type SendParams struct {
Country string
MessageRef string
ContentURLs []string
ShortenURLs bool
TrackLinks bool
}

type SendReply struct {
Expand All @@ -46,10 +48,27 @@ type SendReply struct {
func (s *MMSService) Send(p SendParams, r *SendReply) error {
ctx := context.Background()

uid := uuid.New().String()

msg := p.Message
if p.TrackLinks {
rsp, err := s.svc.TrackLink.GenerateTrackLinks(tracklink.GenerateTrackLinksParams{
AccountID: p.AccountID,
MessageID: uid,
MessageType: Name,
Message: p.Message,
})
if err != nil {
return err
}
msg = rsp.Message
}

newMMS := MMS{
ID: uid,
AccountID: p.AccountID,
Subject: p.Subject,
Message: p.Message,
Message: msg,
Recipient: p.Recipient,
Sender: p.Sender,
Country: p.Country,
Expand Down
8 changes: 7 additions & 1 deletion backend/mms/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/burstsms/mtmo-tp/backend/lib/rabbit"
"github.com/burstsms/mtmo-tp/backend/lib/rpc"
tracklink "github.com/burstsms/mtmo-tp/backend/track_link/rpc/client"
webhook "github.com/burstsms/mtmo-tp/backend/webhook/rpc/client"
)

Expand All @@ -14,8 +15,13 @@ type webhookSvc interface {
PublishMMSStatusUpdate(params webhook.PublishMMSStatusUpdateParams) error
}

type tracklinkSvc interface {
GenerateTrackLinks(p tracklink.GenerateTrackLinksParams) (r *tracklink.GenerateTrackLinksReply, err error)
}

type ConfigSvc struct {
Webhook webhookSvc
Webhook webhookSvc
TrackLink tracklinkSvc
}

type NoParams struct{}
Expand Down
6 changes: 5 additions & 1 deletion backend/mms/rpc/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/burstsms/mtmo-tp/backend/lib/rabbit"
"github.com/burstsms/mtmo-tp/backend/lib/rpc"
mmsRPC "github.com/burstsms/mtmo-tp/backend/mms/rpc"
tracklink "github.com/burstsms/mtmo-tp/backend/track_link/rpc/client"
webhook "github.com/burstsms/mtmo-tp/backend/webhook/rpc/client"

"github.com/kelseyhightower/envconfig"
Expand All @@ -19,6 +20,8 @@ type Env struct {
RabbitExchangeType string `envconfig:"RABBIT_EXCHANGE_TYPE"`
WebhookRPCHost string `envconfig:"WEBHOOK_RPC_HOST"`
WebhookRPCPort int `envconfig:"WEBHOOK_RPC_PORT"`
TrackLinkRPCHost string `envconfig:"TRACK_LINK_RPC_HOST"`
TrackLinkRPCPort int `envconfig:"TRACK_LINK_RPC_PORT"`
}

func main() {
Expand All @@ -41,7 +44,8 @@ func main() {
}

svc := mmsRPC.ConfigSvc{
Webhook: webhook.NewClient(env.WebhookRPCHost, env.WebhookRPCPort),
Webhook: webhook.NewClient(env.WebhookRPCHost, env.WebhookRPCPort),
TrackLink: tracklink.NewClient(env.TrackLinkRPCHost, env.TrackLinkRPCPort),
}

mmsrpc, err := mmsRPC.NewService(env.PostgresURL, rabbitmq, rabbitOpts, svc)
Expand Down
7 changes: 6 additions & 1 deletion backend/modd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,17 @@ webhook/**/*.go lib/**/*.go {
daemon: @run ./webhook/worker/post/cmd
}

# oput service and workers
# optout service and workers
optout/**/*.go lib/**/*.go {
daemon: @run ./optout/rpc/server
daemon: @run ./optout/inbound/server
}

# track link service and workers
track_link/**/*.go lib/**/*.go {
daemon: @run ./track_link/rpc/server
}

# Other services

tecloo/**/*.go {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
ALTER_TABLE sms DROP COLUMN IF EXISTS track_links;
COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
BEGIN;
ALTER TABLE sms ADD COLUMN track_links BOOL DEFAULT FALSE;
COMMIT;
16 changes: 9 additions & 7 deletions backend/sms/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
account "github.com/burstsms/mtmo-tp/backend/account/rpc/client"
"github.com/burstsms/mtmo-tp/backend/lib/rabbit"
"github.com/burstsms/mtmo-tp/backend/lib/rpc"
tracklink "github.com/burstsms/mtmo-tp/backend/track_link/rpc/client"
webhook "github.com/burstsms/mtmo-tp/backend/webhook/rpc/client"
)

Expand All @@ -15,11 +16,12 @@ type NoParams struct{}
type NoReply struct{}

type SMSService struct {
db *db
webhookRPC *webhook.Client
accountRPC *account.Client
name string
features SMSFeatures
db *db
webhookRPC *webhook.Client
accountRPC *account.Client
tracklinkRPC *tracklink.Client
name string
features SMSFeatures
}

type SMSFeatures struct {
Expand All @@ -39,14 +41,14 @@ func (s *Service) Receiver() interface{} {
return s.receiver
}

func NewService(features SMSFeatures, postgresURL string, rabbitmq rabbit.Conn, webhook *webhook.Client, account *account.Client, redisURL string) (rpc.Service, error) {
func NewService(features SMSFeatures, postgresURL string, rabbitmq rabbit.Conn, webhook *webhook.Client, account *account.Client, tracklink *tracklink.Client, redisURL string) (rpc.Service, error) {
gob.Register(map[string]interface{}{})
db, err := NewDB(postgresURL, rabbitmq, redisURL)
if err != nil {
return nil, err
}
service := &Service{
receiver: &SMSService{db: db, name: Name, webhookRPC: webhook, accountRPC: account, features: features},
receiver: &SMSService{db: db, name: Name, webhookRPC: webhook, accountRPC: account, tracklinkRPC: tracklink, features: features},
}

return service, nil
Expand Down
6 changes: 5 additions & 1 deletion backend/sms/rpc/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/burstsms/mtmo-tp/backend/lib/rabbit"
"github.com/burstsms/mtmo-tp/backend/lib/rpc"
smsRPC "github.com/burstsms/mtmo-tp/backend/sms/rpc"
tracklinkRPC "github.com/burstsms/mtmo-tp/backend/track_link/rpc/client"
webhookRPC "github.com/burstsms/mtmo-tp/backend/webhook/rpc/client"

"github.com/kelseyhightower/envconfig"
Expand All @@ -25,6 +26,8 @@ type Env struct {
AccountRPCPort int `envconfig:"ACCOUNT_RPC_PORT"`
TrackLinkDomain string `envconfig:"TRACKLINK_DOMAIN"`
OptOutLinkDomain string `envconfig:"OPTOUTLINK_DOMAIN"`
TrackLinkRPCHost string `envconfig:"TRACK_LINK_RPC_HOST"`
TrackLinkRPCPort int `envconfig:"TRACK_LINK_RPC_PORT"`
}

func main() {
Expand All @@ -43,13 +46,14 @@ func main() {

wrpc := webhookRPC.NewClient(env.WebhookRPCHost, env.WebhookRPCPort)
arpc := accountRPC.New(env.AccountRPCHost, env.AccountRPCPort)
tlrpc := tracklinkRPC.NewClient(env.TrackLinkRPCHost, env.TrackLinkRPCPort)

features := smsRPC.SMSFeatures{
TrackLinkDomain: env.TrackLinkDomain,
OptOutLinkDomain: env.OptOutLinkDomain,
}

srpc, err := smsRPC.NewService(features, env.PostgresURL, rabbitmq, wrpc, arpc, env.RedisURL)
srpc, err := smsRPC.NewService(features, env.PostgresURL, rabbitmq, wrpc, arpc, tlrpc, env.RedisURL)
if err != nil {
log.Fatalf("failed to initialise service: %s reason: %s\n", smsRPC.Name, err)
}
Expand Down
21 changes: 20 additions & 1 deletion backend/sms/rpc/sms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/burstsms/mtmo-tp/backend/sms/biz"
"github.com/burstsms/mtmo-tp/backend/sms/worker/msg"
tracklink "github.com/burstsms/mtmo-tp/backend/track_link/rpc/client"
"github.com/google/uuid"
)

type SMS struct {
Expand All @@ -21,6 +23,7 @@ type SMS struct {
GSM bool
CreatedAt time.Time
UpdatedAt time.Time
TrackLinks bool
}

type SendParams struct {
Expand All @@ -41,6 +44,21 @@ type SendReply struct {
}

func (s *SMSService) Send(p SendParams, r *SendReply) error {
uid := uuid.New().String()

message := p.Message
if p.TrackLinks {
rsp, err := s.tracklinkRPC.GenerateTrackLinks(tracklink.GenerateTrackLinksParams{
AccountID: p.AccountID,
MessageID: uid,
MessageType: Name,
Message: p.Message,
})
if err != nil {
return err
}
message = rsp.Message
}

recipientNumber := p.Recipient
var country string
Expand Down Expand Up @@ -74,10 +92,11 @@ func (s *SMSService) Send(p SendParams, r *SendReply) error {
isGSM := biz.IsGSMString(p.Message)

newSMS := SMS{
ID: uid,
AccountID: p.AccountID,
MessageRef: p.MessageRef,
Country: country,
Message: p.Message,
Message: message,
SMSCount: count,
GSM: isGSM,
Recipient: recipientNumber,
Expand Down
18 changes: 10 additions & 8 deletions backend/sms/rpc/sms_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import "github.com/jackc/pgx/v4"
func (db *db) InsertSMS(p SMS) (*SMS, error) {
var sms SMS
err := db.postgres.QueryRow(bg(), `INSERT INTO
sms(account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status)
values($1, '', NOW(), NOW(), $2, $3, $4, $5, $6, $7, $8, 'pending')
RETURNING id, account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status
sms(id, account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status, track_links)
values($1, $2, '', NOW(), NOW(), $3, $4, $5, $6, $7, $8, $9, 'pending', $10)
RETURNING id, account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status, track_links
`,
p.ID,
p.AccountID,
p.MessageRef,
p.Country,
Expand All @@ -17,7 +18,8 @@ func (db *db) InsertSMS(p SMS) (*SMS, error) {
p.GSM,
p.Recipient,
p.Sender,
).Scan(&sms.ID, &sms.AccountID, &sms.MessageID, &sms.CreatedAt, &sms.UpdatedAt, &sms.MessageRef, &sms.Country, &sms.Message, &sms.SMSCount, &sms.GSM, &sms.Recipient, &sms.Sender, &sms.Status)
p.TrackLinks,
).Scan(&sms.ID, &sms.AccountID, &sms.MessageID, &sms.CreatedAt, &sms.UpdatedAt, &sms.MessageRef, &sms.Country, &sms.Message, &sms.SMSCount, &sms.GSM, &sms.Recipient, &sms.Sender, &sms.Status, &sms.TrackLinks)
if err != nil {
return &SMS{}, err
}
Expand Down Expand Up @@ -70,11 +72,11 @@ func (db *db) MarkFailed(smsID string) error {
func (db *db) FindSMSByMessageID(messageID string) (*SMS, error) {
var sms SMS
err := db.postgres.QueryRow(bg(), `
SELECT id, account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status
SELECT id, account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status, track_links
FROM sms
WHERE message_id = $1`,
messageID,
).Scan(&sms.ID, &sms.AccountID, &sms.MessageID, &sms.CreatedAt, &sms.UpdatedAt, &sms.MessageRef, &sms.Country, &sms.Message, &sms.SMSCount, &sms.GSM, &sms.Recipient, &sms.Sender, &sms.Status)
).Scan(&sms.ID, &sms.AccountID, &sms.MessageID, &sms.CreatedAt, &sms.UpdatedAt, &sms.MessageRef, &sms.Country, &sms.Message, &sms.SMSCount, &sms.GSM, &sms.Recipient, &sms.Sender, &sms.Status, &sms.TrackLinks)
if err != nil {
return &SMS{}, err
}
Expand All @@ -85,7 +87,7 @@ func (db *db) FindSMSByMessageID(messageID string) (*SMS, error) {
func (db *db) FindSMSRelatedToMO(accountID string, mosender string, morecipient string) (*SMS, error) {
var sms SMS
err := db.postgres.QueryRow(bg(), `
SELECT id, account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status
SELECT id, account_id, message_id, created_at, updated_at, message_ref, country, message, sms_count, gsm, recipient, sender, status, track_links
FROM sms
WHERE account_id = $1 AND sender = $3 AND recipient = $2
AND created_at BETWEEN NOW() - INTERVAL '72 HOURS' AND NOW()
Expand All @@ -95,7 +97,7 @@ func (db *db) FindSMSRelatedToMO(accountID string, mosender string, morecipient
accountID,
mosender,
morecipient,
).Scan(&sms.ID, &sms.AccountID, &sms.MessageID, &sms.CreatedAt, &sms.UpdatedAt, &sms.MessageRef, &sms.Country, &sms.Message, &sms.SMSCount, &sms.GSM, &sms.Recipient, &sms.Sender, &sms.Status)
).Scan(&sms.ID, &sms.AccountID, &sms.MessageID, &sms.CreatedAt, &sms.UpdatedAt, &sms.MessageRef, &sms.Country, &sms.Message, &sms.SMSCount, &sms.GSM, &sms.Recipient, &sms.Sender, &sms.Status, &sms.TrackLinks)
if err != nil && err != pgx.ErrNoRows {
return &SMS{}, err
}
Expand Down
Loading

0 comments on commit 0b76820

Please sign in to comment.