2021-07-13 17:14:25 +00:00
|
|
|
package api
|
2021-05-10 00:51:15 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-08 18:19:47 +00:00
|
|
|
"context"
|
2021-05-10 00:51:15 +00:00
|
|
|
"encoding/json"
|
2021-08-08 18:19:47 +00:00
|
|
|
"fmt"
|
2021-05-10 00:51:15 +00:00
|
|
|
"net/http"
|
2021-08-08 18:19:47 +00:00
|
|
|
"strings"
|
2021-05-10 00:51:15 +00:00
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
"github.com/christianselig/apollo-backend/internal/domain"
|
|
|
|
"github.com/dustin/go-humanize/english"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/sideshow/apns2"
|
|
|
|
"github.com/sideshow/apns2/payload"
|
|
|
|
"github.com/sirupsen/logrus"
|
2021-05-10 00:51:15 +00:00
|
|
|
)
|
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
const notificationTitle = "📣 Hello, is this thing on?"
|
|
|
|
|
|
|
|
func (a *api) upsertDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
d := &domain.Device{}
|
2021-05-10 00:51:15 +00:00
|
|
|
if err := json.NewDecoder(r.Body).Decode(d); err != nil {
|
2021-07-13 17:14:25 +00:00
|
|
|
a.errorResponse(w, r, 500, err.Error())
|
2021-05-10 00:51:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-08 18:19:47 +00:00
|
|
|
if err := a.deviceRepo.CreateOrUpdate(ctx, d); err != nil {
|
|
|
|
a.errorResponse(w, r, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
tok := vars["apns"]
|
|
|
|
|
|
|
|
d, err := a.deviceRepo.GetByAPNSToken(ctx, tok)
|
|
|
|
if err != nil {
|
|
|
|
a.logger.WithFields(logrus.Fields{
|
|
|
|
"err": err,
|
|
|
|
}).Info("failed fetching device from database")
|
|
|
|
a.errorResponse(w, r, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accs, err := a.accountRepo.GetByAPNSToken(ctx, tok)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, 500, err.Error())
|
|
|
|
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).
|
|
|
|
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")
|
2021-07-13 17:14:25 +00:00
|
|
|
a.errorResponse(w, r, 500, err.Error())
|
2021-05-10 00:51:15 +00:00
|
|
|
return
|
|
|
|
}
|
2021-08-08 18:19:47 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dev, err := a.deviceRepo.GetByAPNSToken(ctx, vars["apns"])
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accs, err := a.accountRepo.GetByAPNSToken(ctx, vars["apns"])
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r, 500, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, acc := range accs {
|
|
|
|
a.accountRepo.Disassociate(ctx, &acc, &dev)
|
|
|
|
}
|
2021-05-10 00:51:15 +00:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
2021-08-14 15:24:36 +00:00
|
|
|
|
2021-07-26 17:05:09 +00:00
|
|
|
func (a *api) testDeviceHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
ctx := context.Background()
|
2021-07-26 17:18:02 +00:00
|
|
|
tok := ps.ByName("apns")
|
2021-07-26 17:05:09 +00:00
|
|
|
|
2021-07-26 17:18:02 +00:00
|
|
|
d, err := a.models.Devices.GetByAPNSToken(tok)
|
2021-07-26 17:05:09 +00:00
|
|
|
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
|
2021-07-26 17:18:02 +00:00
|
|
|
INNER JOIN devices_accounts ON devices_accounts.account_id = accounts.id
|
2021-07-26 17:06:50 +00:00
|
|
|
WHERE devices_accounts.device_id = $1`
|
2021-07-26 17:05:09 +00:00
|
|
|
rows, err := a.db.Query(ctx, stmt, d.ID)
|
|
|
|
if err != nil {
|
|
|
|
a.logger.WithFields(logrus.Fields{
|
2021-07-26 17:18:02 +00:00
|
|
|
"apns": tok,
|
2021-07-26 17:05:09 +00:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2021-07-27 19:25:22 +00:00
|
|
|
body := fmt.Sprintf("Active usernames are: %s. Tap me for more info!", english.OxfordWordSeries(users, "and"))
|
2021-07-26 17:05:09 +00:00
|
|
|
notification := &apns2.Notification{}
|
|
|
|
notification.Topic = "com.christianselig.Apollo"
|
2021-07-26 17:23:07 +00:00
|
|
|
notification.DeviceToken = d.APNSToken
|
2021-07-26 17:05:09 +00:00
|
|
|
notification.Payload = payload.
|
|
|
|
NewPayload().
|
|
|
|
Category("test-notification").
|
2021-07-27 19:25:22 +00:00
|
|
|
Custom("test_accounts", strings.Join(users, ",")).
|
2021-07-26 17:42:16 +00:00
|
|
|
AlertTitle(notificationTitle).
|
2021-07-26 17:05:09 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-07-26 17:54:24 +00:00
|
|
|
|
|
|
|
func (a *api) deleteDeviceHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|