apollo-backend/internal/api/devices.go

123 lines
2.9 KiB
Go
Raw Normal View History

package api
2021-05-09 20:51:15 -04:00
import (
2022-10-31 22:33:11 -04:00
"context"
2021-05-09 20:51:15 -04:00
"encoding/json"
2021-08-08 14:19:47 -04:00
"fmt"
2021-05-09 20:51:15 -04:00
"net/http"
2021-08-08 14:19:47 -04:00
"strings"
2021-08-14 11:51:27 -04:00
"time"
2021-05-09 20:51:15 -04:00
2021-08-08 14:19:47 -04:00
"github.com/dustin/go-humanize/english"
"github.com/gorilla/mux"
"github.com/sideshow/apns2"
"github.com/sideshow/apns2/payload"
2022-05-23 14:17:25 -04:00
"go.uber.org/zap"
2021-08-14 13:56:03 -04:00
"github.com/christianselig/apollo-backend/internal/domain"
2021-05-09 20:51:15 -04:00
)
2021-08-08 14:19:47 -04:00
const notificationTitle = "📣 Hello, is this thing on?"
func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) {
2022-10-31 22:33:11 -04:00
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
2021-08-08 14:19:47 -04:00
d := &domain.Device{}
2021-05-09 20:51:15 -04:00
if err := json.NewDecoder(r.Body).Decode(d); err != nil {
2022-05-21 10:00:21 -04:00
a.errorResponse(w, r, 500, err)
2021-05-09 20:51:15 -04:00
return
}
2022-03-28 17:05:01 -04:00
d.ExpiresAt = time.Now().Add(domain.DeviceReceiptCheckPeriodDuration)
d.GracePeriodExpiresAt = d.ExpiresAt.Add(domain.DeviceGracePeriodAfterReceiptExpiry)
2021-08-14 11:51:27 -04:00
2021-08-08 14:19:47 -04:00
if err := a.deviceRepo.CreateOrUpdate(ctx, d); err != nil {
2022-05-21 10:00:21 -04:00
a.errorResponse(w, r, 500, err)
2021-08-08 14:19:47 -04:00
return
}
w.WriteHeader(http.StatusOK)
}
func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) {
2022-10-31 22:33:11 -04:00
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
2021-08-08 14:19:47 -04:00
2022-05-07 15:04:35 -04:00
vars := mux.Vars(r)
2021-08-08 14:19:47 -04:00
tok := vars["apns"]
d, err := a.deviceRepo.GetByAPNSToken(ctx, tok)
if err != nil {
2022-05-23 15:15:46 -04:00
a.logger.Error("failed to fetch device from database", zap.Error(err))
2022-05-21 10:00:21 -04:00
a.errorResponse(w, r, 500, err)
2021-08-08 14:19:47 -04:00
return
}
accs, err := a.accountRepo.GetByAPNSToken(ctx, tok)
if err != nil {
2022-05-21 10:00:21 -04:00
a.errorResponse(w, r, 500, err)
2021-08-08 14:19:47 -04:00
return
}
users := make([]string, len(accs))
for i := range accs {
users[i] = accs[i].Username
}
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).
2022-05-21 09:46:12 -04:00
AlertBody(body).
MutableContent().
Sound("traloop.wav")
2021-08-08 14:19:47 -04:00
client := apns2.NewTokenClient(a.apns)
if !d.Sandbox {
client = client.Production()
}
2022-11-11 12:03:39 -05:00
res, err := client.Push(notification)
if err != nil {
2022-05-23 14:17:25 -04:00
a.logger.Info("failed to send test notification", zap.Error(err))
2022-05-21 10:00:21 -04:00
a.errorResponse(w, r, 500, err)
2022-11-11 12:03:39 -05:00
} else if !res.Sent() {
a.errorResponse(w, r, 422, fmt.Errorf("errror sending notification: %d: %s", res.StatusCode, res.Reason))
} else {
w.WriteHeader(http.StatusOK)
2021-05-09 20:51:15 -04:00
}
2021-08-08 14:19:47 -04:00
}
func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) {
2022-10-31 22:33:11 -04:00
ctx, cancel := context.WithCancel(r.Context())
defer cancel()
2022-05-07 15:04:35 -04:00
2021-08-08 14:19:47 -04:00
vars := mux.Vars(r)
dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"])
if err != nil {
2022-05-21 10:00:21 -04:00
a.errorResponse(w, r, 500, err)
2021-08-08 14:19:47 -04:00
return
}
accs, err := a.accountRepo.GetByAPNSToken(ctx, vars["apns"])
if err != nil {
2022-05-21 10:00:21 -04:00
a.errorResponse(w, r, 500, err)
2021-08-08 14:19:47 -04:00
return
}
for _, acc := range accs {
2021-09-25 09:19:42 -04:00
_ = a.accountRepo.Disassociate(ctx, &acc, &dev)
2021-08-08 14:19:47 -04:00
}
2021-05-09 20:51:15 -04:00
2021-09-25 09:19:42 -04:00
_ = a.deviceRepo.Delete(ctx, vars["apns"])
2021-09-11 10:53:19 -04:00
2021-05-09 20:51:15 -04:00
w.WriteHeader(http.StatusOK)
}