Skip to content

Commit

Permalink
chore: add is_ios column and naming changes
Browse files Browse the repository at this point in the history
  • Loading branch information
im-adithya committed Nov 13, 2024
1 parent b99700a commit 572b9d8
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {
e.POST("/nip47", svc.NIP47Handler)
e.POST("/nip47/webhook", svc.NIP47WebhookHandler)
e.POST("/nip47/notifications", svc.NIP47NotificationHandler)
e.POST("/nip47/notifications/go", svc.NIP47ExpoNotificationHandler)
e.POST("/nip47/notifications/push", svc.NIP47PushNotificationHandler)
e.POST("/publish", svc.PublishHandler)
e.POST("/subscriptions", svc.SubscriptionHandler)
e.DELETE("/subscriptions/:id", svc.StopSubscriptionHandler)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module http-nostr
go 1.21.3

require (
github.com/getAlby/exponent-server-sdk-golang/sdk v0.0.0-20241113053439-fb024e3a89b1
github.com/getsentry/sentry-go v0.28.1
github.com/jackc/pgx/v5 v5.6.0
github.com/joho/godotenv v1.5.1
Expand All @@ -28,7 +29,6 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oliveroneill/exponent-server-sdk-golang v0.0.0-20210823140141-d050598be512 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/puzpuzpuz/xsync/v3 v3.1.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/eapache/queue/v2 v2.0.0-20230407133247-75960ed334e4/go.mod h1:I5sHm0Y
github.com/ebitengine/purego v0.7.1 h1:6/55d26lG3o9VCZX8lping+bZcmShseiqlh2bnUDiPA=
github.com/ebitengine/purego v0.7.1/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/getAlby/exponent-server-sdk-golang/sdk v0.0.0-20241113053439-fb024e3a89b1 h1:u1rDPykjuG3DkUAeHlGby4frrynzjJAfZbuK/jLlu6k=
github.com/getAlby/exponent-server-sdk-golang/sdk v0.0.0-20241113053439-fb024e3a89b1/go.mod h1:EK6N2J42WZk795IUD9GGbKL8XAK5UjfUEvxh4d9hobY=
github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k=
github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg=
github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ=
Expand Down Expand Up @@ -148,8 +150,6 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/nbd-wtf/go-nostr v0.31.4 h1:Qq+PHyKixRZR6Tn+omF7LykK/IR6qcdmThNY132pKsA=
github.com/nbd-wtf/go-nostr v0.31.4/go.mod h1:vHKtHyLXDXzYBN0fi/9Y/Q5AD0p+hk8TQVKlldAi0gI=
github.com/oliveroneill/exponent-server-sdk-golang v0.0.0-20210823140141-d050598be512 h1:/ZSmjwl1inqsiHMhn+sPlEtSHdVTf+TH3LNGGdMQ/vA=
github.com/oliveroneill/exponent-server-sdk-golang v0.0.0-20210823140141-d050598be512/go.mod h1:Isv/48UnAjtxS8FD80Bito3ZJqZRyIMxKARIEITfW4k=
github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs=
github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc=
github.com/outcaste-io/ristretto v0.2.3 h1:AK4zt/fJ76kjlYObOeNwh4T3asEuaCmp26pOvUOL9w0=
Expand Down
8 changes: 5 additions & 3 deletions internal/nostr/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Subscription struct {
RelayUrl string
WebhookUrl string
PushToken string
IsIOS bool
Open bool
Ids *[]string `gorm:"-"`
Kinds *[]int `gorm:"-"`
Expand Down Expand Up @@ -186,11 +187,12 @@ type NIP47NotificationRequest struct {
ConnPubkey string `json:"connectionPubkey"`
}

type NIP47ExpoNotificationRequest struct {
type NIP47PushNotificationRequest struct {
RelayUrl string `json:"relayUrl"`
PushToken string `json:"pushToken"`
WalletPubkey string `json:"walletPubkey"`
ConnPubkey string `json:"connectionPubkey"`
IsIOS bool `json:"isIOS"`
}

type NIP47Response struct {
Expand All @@ -216,11 +218,11 @@ type SubscriptionRequest struct {
}

type SubscriptionResponse struct {
SubscriptionId string `json:"subscriptionId"`
SubscriptionId string `json:"subscription_id"`
WebhookUrl string `json:"webhookUrl"`
}

type ExpoSubscriptionResponse struct {
type PushSubscriptionResponse struct {
SubscriptionId string `json:"subscriptionId"`
PushToken string `json:"pushToken"`
WalletPubkey string `json:"walletPubkey"`
Expand Down
4 changes: 2 additions & 2 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"

expo "github.com/getAlby/exponent-server-sdk-golang/sdk"
"github.com/jackc/pgx/v5/stdlib"
expo "github.com/oliveroneill/exponent-server-sdk-golang/sdk"
sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
gormtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorm.io/gorm.v1"
)
Expand Down Expand Up @@ -150,7 +150,7 @@ func NewService(ctx context.Context) (*Service, error) {
subscription := sub
handleEvent := svc.handleSubscribedEvent
if sub.PushToken != "" {
handleEvent = svc.handleSubscribedExpoNotification
handleEvent = svc.handlePushNotification
}
go svc.startSubscription(svc.Ctx, &subscription, nil, handleEvent)
}
Expand Down
53 changes: 28 additions & 25 deletions internal/nostr/expo.go → internal/nostr/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"net/http"
"time"

expo "github.com/getAlby/exponent-server-sdk-golang/sdk"
"github.com/labstack/echo/v4"
"github.com/nbd-wtf/go-nostr"
expo "github.com/oliveroneill/exponent-server-sdk-golang/sdk"
"github.com/sirupsen/logrus"
)

func (svc *Service) NIP47ExpoNotificationHandler(c echo.Context) error {
var requestData NIP47ExpoNotificationRequest
func (svc *Service) NIP47PushNotificationHandler(c echo.Context) error {
var requestData NIP47PushNotificationRequest
if err := c.Bind(&requestData); err != nil {
return c.JSON(http.StatusBadRequest, ErrorResponse{
Message: "Error decoding notification request",
Expand Down Expand Up @@ -69,7 +69,7 @@ func (svc *Service) NIP47ExpoNotificationHandler(c echo.Context) error {
"relay_url": requestData.RelayUrl,
"push_token": requestData.PushToken,
}).Debug("Subscription already started")
return c.JSON(http.StatusOK, ExpoSubscriptionResponse{
return c.JSON(http.StatusOK, PushSubscriptionResponse{
SubscriptionId: existingSubscription.Uuid,
PushToken: requestData.PushToken,
WalletPubkey: requestData.WalletPubkey,
Expand All @@ -87,6 +87,7 @@ func (svc *Service) NIP47ExpoNotificationHandler(c echo.Context) error {
subscription := Subscription{
RelayUrl: requestData.RelayUrl,
PushToken: requestData.PushToken,
IsIOS: requestData.IsIOS,
Open: true,
Since: time.Now(),
Authors: &[]string{requestData.WalletPubkey},
Expand All @@ -110,55 +111,57 @@ func (svc *Service) NIP47ExpoNotificationHandler(c echo.Context) error {
})
}

go svc.startSubscription(svc.Ctx, &subscription, nil, svc.handleSubscribedExpoNotification)
go svc.startSubscription(svc.Ctx, &subscription, nil, svc.handlePushNotification)

return c.JSON(http.StatusOK, ExpoSubscriptionResponse{
return c.JSON(http.StatusOK, PushSubscriptionResponse{
SubscriptionId: subscription.Uuid,
PushToken: requestData.PushToken,
WalletPubkey: requestData.WalletPubkey,
AppPubkey: requestData.ConnPubkey,
})
}

func (svc *Service) handleSubscribedExpoNotification(event *nostr.Event, subscription *Subscription) {
func (svc *Service) handlePushNotification(event *nostr.Event, subscription *Subscription) {
svc.Logger.WithFields(logrus.Fields{
"event_id": event.ID,
"event_kind": event.Kind,
"subscription_id": subscription.ID,
"relay_url": subscription.RelayUrl,
}).Debug("Received subscribed notification")
}).Debug("Received subscribed push notification")

pushToken, _ := expo.NewExponentPushToken(subscription.PushToken)

response, err := svc.client.Publish(
&expo.PushMessage{
To: []expo.ExponentPushToken{pushToken},
Title: "New event",
Body: "",
Data: map[string]string{
"content": event.Content,
"appPubkey": event.Tags.GetFirst([]string{"p", ""}).Value(),
},
Priority: expo.DefaultPriority,
pushMessage := &expo.PushMessage{
To: []expo.ExponentPushToken{pushToken},
Data: map[string]string{
"content": event.Content,
"appPubkey": event.Tags.GetFirst([]string{"p", ""}).Value(),
},
)
}

if subscription.IsIOS {
pushMessage.Title = "Received notification"
pushMessage.MutableContent = true
}

response, err := svc.client.Publish(pushMessage)
if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"push_token": subscription.PushToken,
}).Error("Failed to send expo notification")
"push_token": subscription.PushToken,
}).Error("Failed to send push notification")
return
}

err = response.ValidateResponse()
if err != nil {
svc.Logger.WithError(err).WithFields(logrus.Fields{
"push_token": subscription.PushToken,
}).Error("Failed to valid expo publish response")
"push_token": subscription.PushToken,
}).Error("Failed to validate expo publish response")
return
}

svc.Logger.WithFields(logrus.Fields{
"event_id": event.ID,
"push_token": subscription.PushToken,
"event_id": event.ID,
"push_token": subscription.PushToken,
}).Debug("Push notification sent successfully")
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import (
"gorm.io/gorm"
)

// Add push_token column to subscriptions table
var _202411071013_add_push_token_to_subscriptions = &gormigrate.Migration{
ID: "202411071013_add_push_token_to_subscriptions",
// Add push_token and is_ios column to subscriptions table
var _202411071013_add_push_token_and_is_ios_to_subscriptions = &gormigrate.Migration{
ID: "202411071013_add_push_token_and_is_ios_to_subscriptions",
Migrate: func(tx *gorm.DB) error {
if err := tx.Exec("ALTER TABLE subscriptions ADD COLUMN push_token TEXT").Error; err != nil {
return err
}
if err := tx.Exec("ALTER TABLE subscriptions ADD COLUMN is_ios BOOLEAN DEFAULT NULL").Error; err != nil {
return err
}
if err := tx.Exec("CREATE INDEX IF NOT EXISTS subscriptions_push_token ON subscriptions (push_token)").Error; err != nil {
return err
}
Expand All @@ -24,6 +27,9 @@ var _202411071013_add_push_token_to_subscriptions = &gormigrate.Migration{
if err := tx.Exec("ALTER TABLE subscriptions DROP COLUMN push_token").Error; err != nil {
return err
}
if err := tx.Exec("ALTER TABLE subscriptions DROP COLUMN is_ios").Error; err != nil {
return err
}
return nil
},
}
2 changes: 1 addition & 1 deletion migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Migrate(db *gorm.DB) error {
_202404021628_add_uuid_to_subscriptions,
_202404031539_add_indexes,
_202407171220_add_response_received_at_to_request_events,
_202411071013_add_push_token_to_subscriptions,
_202411071013_add_push_token_and_is_ios_to_subscriptions,
})

return m.Migrate()
Expand Down

0 comments on commit 572b9d8

Please sign in to comment.