From 515ac90bf269abdd5718ea61d6579f53427f40cb Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Mon, 26 Jul 2021 08:24:51 +0000 Subject: [PATCH 01/11] Bump github.com/jackc/pgx/v4 from 4.12.0 to 4.13.0 Bumps [github.com/jackc/pgx/v4](https://github.com/jackc/pgx) from 4.12.0 to 4.13.0. - [Release notes](https://github.com/jackc/pgx/releases) - [Changelog](https://github.com/jackc/pgx/blob/master/CHANGELOG.md) - [Commits](https://github.com/jackc/pgx/compare/v4.12.0...v4.13.0) --- updated-dependencies: - dependency-name: github.com/jackc/pgx/v4 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- go.mod | 3 ++- go.sum | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9a8a815..49e153e 100644 --- a/go.mod +++ b/go.mod @@ -8,9 +8,10 @@ require ( github.com/Microsoft/go-winio v0.5.0 // indirect github.com/adjust/rmq/v4 v4.0.1 github.com/go-co-op/gocron v1.6.2 + github.com/go-kit/kit v0.10.0 // indirect github.com/go-redis/redis/v8 v8.11.0 github.com/heroku/x v0.0.31 - github.com/jackc/pgx/v4 v4.12.0 + github.com/jackc/pgx/v4 v4.13.0 github.com/joho/godotenv v1.3.0 github.com/julienschmidt/httprouter v1.3.0 github.com/sideshow/apns2 v0.20.0 diff --git a/go.sum b/go.sum index 366d097..9eac838 100644 --- a/go.sum +++ b/go.sum @@ -141,6 +141,7 @@ github.com/go-ini/ini v1.33.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3I github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o= +github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= @@ -290,11 +291,15 @@ github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfG github.com/jackc/pgconn v1.8.1/go.mod h1:JV6m6b6jhjdmzchES0drzCcYcAHS1OPD5xu3OZ/lE2g= github.com/jackc/pgconn v1.9.0 h1:gqibKSTJup/ahCsNKyMZAniPuZEfIqfXFc8FOWVYR+Q= github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= +github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= +github.com/jackc/pgconn v1.10.0 h1:4EYhlDVEMsJ30nNj0mmgwIUXoq7e9sMJrVC2ED6QlCU= +github.com/jackc/pgconn v1.10.0/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd h1:eDErF6V/JPJON/B7s68BxwHgfmyOntHJQ8IOaz0x4R8= github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= +github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= @@ -319,6 +324,9 @@ github.com/jackc/pgtype v1.3.1-0.20200606141011-f6355165a91c/go.mod h1:cvk9Bgu/V github.com/jackc/pgtype v1.7.0/go.mod h1:ZnHF+rMePVqDKaOfJVI4Q8IVvAQMryDlDkZnKOI75BE= github.com/jackc/pgtype v1.8.0 h1:iFVCcVhYlw0PulYCVoguRGm0SE9guIcPcccnLzHj8bA= github.com/jackc/pgtype v1.8.0/go.mod h1:PqDKcEBtllAtk/2p6z6SHdXW5UB+MhE75tUol2OKexE= +github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM= +github.com/jackc/pgtype v1.8.1 h1:9k0IXtdJXHJbyAWQgbWr1lU+MEhPXZz6RIXxfR5oxXs= +github.com/jackc/pgtype v1.8.1/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= @@ -328,6 +336,9 @@ github.com/jackc/pgx/v4 v4.6.1-0.20200606145419-4e5062306904/go.mod h1:ZDaNWkt9s github.com/jackc/pgx/v4 v4.11.0/go.mod h1:i62xJgdrtVDsnL3U8ekyrQXEwGNTRoG7/8r+CIdYfcc= github.com/jackc/pgx/v4 v4.12.0 h1:xiP3TdnkwyslWNp77yE5XAPfxAsU9RMFDe0c1SwN8h4= github.com/jackc/pgx/v4 v4.12.0/go.mod h1:fE547h6VulLPA3kySjfnSG/e2D861g/50JlVUa/ub60= +github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= +github.com/jackc/pgx/v4 v4.13.0 h1:JCjhT5vmhMAf/YwBHLvrBn4OGdIQBiFG6ym8Zmdx570= +github.com/jackc/pgx/v4 v4.13.0/go.mod h1:9P4X524sErlaxj0XSGZk7s+LD0eOyu1ZDUrrpznYDF0= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.0/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= @@ -600,6 +611,8 @@ golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWP golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= +golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190125153040-c74c464bbbf2/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= From 0d296a672eb94a359eabecf2effd4b534e9f2f1b Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:05:09 -0400 Subject: [PATCH 02/11] add test notification endpoint --- go.mod | 1 + go.sum | 2 ++ internal/api/api.go | 19 ++++++++++++- internal/api/devices.go | 63 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9a8a815..9ccb6bd 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/DataDog/datadog-go v4.8.1+incompatible github.com/Microsoft/go-winio v0.5.0 // indirect github.com/adjust/rmq/v4 v4.0.1 + github.com/dustin/go-humanize v1.0.0 // indirect github.com/go-co-op/gocron v1.6.2 github.com/go-redis/redis/v8 v8.11.0 github.com/heroku/x v0.0.31 diff --git a/go.sum b/go.sum index 366d097..d9bf387 100644 --- a/go.sum +++ b/go.sum @@ -112,6 +112,8 @@ github.com/dgryski/go-metro v0.0.0-20180109044635-280f6062b5bc/go.mod h1:c9O8+fp github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs= github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU= github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I= diff --git a/internal/api/api.go b/internal/api/api.go index d4f83df..ffcfa11 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -9,6 +9,7 @@ import ( "github.com/DataDog/datadog-go/statsd" "github.com/jackc/pgx/v4/pgxpool" "github.com/julienschmidt/httprouter" + "github.com/sideshow/apns2/token" "github.com/sirupsen/logrus" "github.com/christianselig/apollo-backend/internal/data" @@ -21,6 +22,7 @@ type api struct { db *pgxpool.Pool reddit *reddit.Client models *data.Models + apns *token.Token } func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, db *pgxpool.Pool) *api { @@ -31,9 +33,23 @@ func NewAPI(ctx context.Context, logger *logrus.Logger, statsd *statsd.Client, d 16, ) + var apns *token.Token + { + authKey, err := token.AuthKeyFromFile(os.Getenv("APPLE_KEY_PATH")) + if err != nil { + panic(err) + } + + apns = &token.Token{ + AuthKey: authKey, + KeyID: os.Getenv("APPLE_KEY_ID"), + TeamID: os.Getenv("APPLE_TEAM_ID"), + } + } + models := data.NewModels(ctx, db) - return &api{logger, statsd, db, reddit, models} + return &api{logger, statsd, db, reddit, models, apns} } func (a *api) Server(port int) *http.Server { @@ -49,6 +65,7 @@ func (a *api) Routes() *httprouter.Router { router.GET("/v1/health", a.healthCheckHandler) router.POST("/v1/device", a.upsertDeviceHandler) + router.POST("/v1/device/test", a.testDeviceHandler) router.POST("/v1/device/:apns/account", a.upsertAccountHandler) return router diff --git a/internal/api/devices.go b/internal/api/devices.go index 2f6eafd..b0cd243 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -1,10 +1,16 @@ package api import ( + "context" "encoding/json" + "fmt" "net/http" + "github.com/dustin/go-humanize/english" "github.com/julienschmidt/httprouter" + "github.com/sideshow/apns2" + "github.com/sideshow/apns2/payload" + "github.com/sirupsen/logrus" "github.com/christianselig/apollo-backend/internal/data" ) @@ -23,3 +29,60 @@ func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request, _ http w.WriteHeader(http.StatusOK) } + +func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + ctx := context.Background() + + d, err := a.models.Devices.GetByAPNSToken(ps.ByName("apns")) + if err != nil { + a.logger.WithFields(logrus.Fields{ + "err": err, + }).Info("failed fetching device from database") + a.errorResponse(w, r, 500, err.Error()) + return + } + + stmt := ` + SELECT username + FROM accounts + INNER JOIN devices_accounts ON devices.account_id = accounts.id + WHERE devices_accounts.device = $1` + rows, err := a.db.Query(ctx, stmt, d.ID) + if err != nil { + a.logger.WithFields(logrus.Fields{ + "apns": ps.ByName("apns"), + "err": err, + }).Error("failed to fetch device accounts") + return + } + defer rows.Close() + + var users []string + for rows.Next() { + var user string + rows.Scan(&user) + users = append(users, user) + } + + body := fmt.Sprintf("Active usernames are: %s", english.OxfordWordSeries(users, "and")) + notification := &apns2.Notification{} + notification.Topic = "com.christianselig.Apollo" + notification.Payload = payload. + NewPayload(). + Category("test-notification"). + AlertBody(body) + + client := apns2.NewTokenClient(a.apns) + if !d.Sandbox { + client = client.Production() + } + + if _, err := client.Push(notification); err != nil { + a.logger.WithFields(logrus.Fields{ + "err": err, + }).Info("failed to send test notification") + a.errorResponse(w, r, 500, err.Error()) + return + } + w.WriteHeader(http.StatusOK) +} From 3d679fe87819ddd7b18c94f0f2b3dd303ebc792c Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:06:00 -0400 Subject: [PATCH 03/11] fix route --- internal/api/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/api/api.go b/internal/api/api.go index ffcfa11..03c7c5d 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -65,7 +65,7 @@ func (a *api) Routes() *httprouter.Router { router.GET("/v1/health", a.healthCheckHandler) router.POST("/v1/device", a.upsertDeviceHandler) - router.POST("/v1/device/test", a.testDeviceHandler) + router.POST("/v1/device/:apns/test", a.testDeviceHandler) router.POST("/v1/device/:apns/account", a.upsertAccountHandler) return router From a5b69521c842dd4af30ab1673721ee3fd930b7dc Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:06:50 -0400 Subject: [PATCH 04/11] fix query --- internal/api/devices.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/api/devices.go b/internal/api/devices.go index b0cd243..b3c0a5b 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -46,7 +46,7 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httpr SELECT username FROM accounts INNER JOIN devices_accounts ON devices.account_id = accounts.id - WHERE devices_accounts.device = $1` + WHERE devices_accounts.device_id = $1` rows, err := a.db.Query(ctx, stmt, d.ID) if err != nil { a.logger.WithFields(logrus.Fields{ From c67d7c6882ccb6ea3ce0e162e7c5ad2bda127e59 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:18:02 -0400 Subject: [PATCH 05/11] womp womp --- internal/api/devices.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/api/devices.go b/internal/api/devices.go index b3c0a5b..4c7ef90 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -32,8 +32,9 @@ func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request, _ http func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { ctx := context.Background() + tok := ps.ByName("apns") - d, err := a.models.Devices.GetByAPNSToken(ps.ByName("apns")) + d, err := a.models.Devices.GetByAPNSToken(tok) if err != nil { a.logger.WithFields(logrus.Fields{ "err": err, @@ -45,12 +46,12 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httpr stmt := ` SELECT username FROM accounts - INNER JOIN devices_accounts ON devices.account_id = accounts.id + INNER JOIN devices_accounts ON devices_accounts.account_id = accounts.id WHERE devices_accounts.device_id = $1` rows, err := a.db.Query(ctx, stmt, d.ID) if err != nil { a.logger.WithFields(logrus.Fields{ - "apns": ps.ByName("apns"), + "apns": tok, "err": err, }).Error("failed to fetch device accounts") return From 2cfc5f1bec1e632c974ce1c21b54baf2d17c2817 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:23:07 -0400 Subject: [PATCH 06/11] fuck me sidways --- internal/api/devices.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/api/devices.go b/internal/api/devices.go index 4c7ef90..ddd5ebb 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -68,6 +68,7 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httpr body := fmt.Sprintf("Active usernames are: %s", english.OxfordWordSeries(users, "and")) notification := &apns2.Notification{} notification.Topic = "com.christianselig.Apollo" + notification.DeviceToken = d.APNSToken notification.Payload = payload. NewPayload(). Category("test-notification"). From e39693d47b9e440f4f2c6338b40ff78e3dd1e786 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:38:07 -0400 Subject: [PATCH 07/11] Add notification title --- internal/api/devices.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/api/devices.go b/internal/api/devices.go index ddd5ebb..0e9d36f 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -72,6 +72,7 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httpr notification.Payload = payload. NewPayload(). Category("test-notification"). + AlertTitle("Test notification"). AlertBody(body) client := apns2.NewTokenClient(a.apns) From 8723bf6c5c65c12045826b3a5b4d27168d6a407b Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:42:16 -0400 Subject: [PATCH 08/11] change notification title --- internal/api/devices.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/api/devices.go b/internal/api/devices.go index 0e9d36f..851296e 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -15,6 +15,8 @@ import ( "github.com/christianselig/apollo-backend/internal/data" ) +const notificationTitle = "📣 Hello, is this thing on?" + func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { d := &data.Device{} if err := json.NewDecoder(r.Body).Decode(d); err != nil { @@ -72,7 +74,7 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httpr notification.Payload = payload. NewPayload(). Category("test-notification"). - AlertTitle("Test notification"). + AlertTitle(notificationTitle). AlertBody(body) client := apns2.NewTokenClient(a.apns) From 4261c6771cf3e9e5db07d6191f48d9940bd61acf Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Mon, 26 Jul 2021 13:54:24 -0400 Subject: [PATCH 09/11] add receipt and device deletion --- internal/api/api.go | 4 ++++ internal/api/devices.go | 4 ++++ internal/api/receipt.go | 12 ++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 internal/api/receipt.go diff --git a/internal/api/api.go b/internal/api/api.go index 03c7c5d..234bd5c 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -67,6 +67,10 @@ func (a *api) Routes() *httprouter.Router { router.POST("/v1/device", a.upsertDeviceHandler) router.POST("/v1/device/:apns/test", a.testDeviceHandler) router.POST("/v1/device/:apns/account", a.upsertAccountHandler) + router.DELETE("/v1/device/:apns", a.deleteDeviceHandler) + + router.POST("/v1/receipt", a.checkReceiptHandler) + router.POST("/v1/receipt/:apns", a.checkReceiptHandler) return router } diff --git a/internal/api/devices.go b/internal/api/devices.go index 851296e..f20428b 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -91,3 +91,7 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httpr } w.WriteHeader(http.StatusOK) } + +func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + w.WriteHeader(http.StatusOK) +} diff --git a/internal/api/receipt.go b/internal/api/receipt.go new file mode 100644 index 0000000..b623b57 --- /dev/null +++ b/internal/api/receipt.go @@ -0,0 +1,12 @@ +package api + +import ( + "net/http" + + "github.com/julienschmidt/httprouter" +) + +func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + w.Write([]byte(`{"status_code": 11, "status_message": "Receipt is valid, lifetime subscription", "subscription_type": "lifetime"}`)) + w.WriteHeader(http.StatusOK) +} From ebd5fce6789949020c1c5ba03b6f237ffe3a7159 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Tue, 27 Jul 2021 15:25:22 -0400 Subject: [PATCH 10/11] Add custom field with account list --- internal/api/devices.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/api/devices.go b/internal/api/devices.go index f20428b..15ab5b9 100644 --- a/internal/api/devices.go +++ b/internal/api/devices.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "net/http" + "strings" "github.com/dustin/go-humanize/english" "github.com/julienschmidt/httprouter" @@ -67,13 +68,14 @@ func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httpr users = append(users, user) } - body := fmt.Sprintf("Active usernames are: %s", english.OxfordWordSeries(users, "and")) + body := fmt.Sprintf("Active usernames are: %s. Tap me for more info!", english.OxfordWordSeries(users, "and")) notification := &apns2.Notification{} notification.Topic = "com.christianselig.Apollo" notification.DeviceToken = d.APNSToken notification.Payload = payload. NewPayload(). Category("test-notification"). + Custom("test_accounts", strings.Join(users, ",")). AlertTitle(notificationTitle). AlertBody(body) From 5054462cadee42fe3dbfd695c36006ecff21cdf3 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Tue, 27 Jul 2021 15:28:10 -0400 Subject: [PATCH 11/11] Tweak receipt check response --- internal/api/receipt.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/api/receipt.go b/internal/api/receipt.go index b623b57..c78ac19 100644 --- a/internal/api/receipt.go +++ b/internal/api/receipt.go @@ -6,7 +6,29 @@ import ( "github.com/julienschmidt/httprouter" ) +const receiptResponse = `{ + "products": [ + { + "name": "ultra", + "status": "SANBOX", + "subscription_type": "SANDBOX" + }, + { + "name": "pro", + "status": "SANDBOX" + }, + { + "name": "community_icons", + "status": "SANDBOX" + }, + { + "name": "spca", + "status": "SANDBOX" + } + ] +}` + func (a *api) checkReceiptHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - w.Write([]byte(`{"status_code": 11, "status_message": "Receipt is valid, lifetime subscription", "subscription_type": "lifetime"}`)) + w.Write([]byte(receiptResponse)) w.WriteHeader(http.StatusOK) }