Skip to content

Commit

Permalink
removed another instance of boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 22, 2023
1 parent 8da5334 commit 852d066
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
14 changes: 3 additions & 11 deletions server/backend/ios_notifications/apns/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ func (r *Repository) CreateRequest(deviceId string, requestType model.IOSBackgro
return &request, nil
}

func (r *Repository) SendAlertNotification(payload *model.IOSNotificationPayload) (*model.IOSRemoteNotificationResponse, error) {
return r.SendNotification(payload, model.IOSAPNSPushTypeAlert, 10)
}

func (r *Repository) SendBackgroundNotification(payload *model.IOSNotificationPayload) (*model.IOSRemoteNotificationResponse, error) {
return r.SendNotification(payload, model.IOSAPNSPushTypeBackground, 10)
}

func (r *Repository) SendNotification(notification *model.IOSNotificationPayload, apnsPushType model.IOSAPNSPushType, priority int) (*model.IOSRemoteNotificationResponse, error) {
func (r *Repository) SendNotification(notification *model.IOSNotificationPayload, apnsPushType model.IOSAPNSPushType) (*model.IOSRemoteNotificationResponse, error) {
body, _ := notification.MarshalJSON()

req, _ := http.NewRequest(http.MethodPost, r.ApnsUrl(notification.DeviceId), bytes.NewBuffer(body))
Expand All @@ -70,7 +62,7 @@ func (r *Repository) SendNotification(notification *model.IOSNotificationPayload
req.Header.Set("apns-push-type", apnsPushType.String())
req.Header.Set("apns-topic", "de.tum.tca")
// can be a value between 1 and 10
req.Header.Set("apns-priority", strconv.Itoa(priority))
req.Header.Set("apns-priority", strconv.Itoa(10))

bearer := r.Token.GenerateNewTokenIfExpired()
req.Header.Set("authorization", "bearer "+bearer)
Expand All @@ -87,7 +79,7 @@ func (r *Repository) SendNotification(notification *model.IOSNotificationPayload
}(resp.Body)

var response model.IOSRemoteNotificationResponse
if err = json.NewDecoder(resp.Body).Decode(&response); err != nil && err != io.EOF {
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil && err != io.EOF {
log.WithError(err).Error("Could not decode APNs response")
return nil, errors.New("could not decode apns response")
}
Expand Down
2 changes: 1 addition & 1 deletion server/backend/ios_notifications/apns/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *Service) RequestGradeUpdateForDevice(deviceID string) error {

notification := model.NewIOSNotificationPayload(deviceID).Background(campusRequestToken.RequestID, model.IOSBackgroundCampusTokenRequest)

if _, err := s.Repository.SendBackgroundNotification(notification); err != nil {
if _, err := s.Repository.SendNotification(notification, model.IOSAPNSPushTypeBackground); err != nil {
log.WithError(err).Error("Could not send background notification")
return errors.New("could not send notification")
}
Expand Down
3 changes: 1 addition & 2 deletions server/backend/ios_notifications/request_response/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ func sendGradesToDevice(device *model.IOSDevice, grades []model.IOSGrade, apns *

log.WithField("DeviceID", device.DeviceID).Info("Sending push notification")

_, err := apns.SendAlertNotification(notificationPayload)
if err != nil {
if _, err := apns.SendNotification(notificationPayload, model.IOSAPNSPushTypeAlert); err != nil {
log.WithField("DeviceID", device.DeviceID).WithError(err).Error("Could not send notification")
}
}
Expand Down

0 comments on commit 852d066

Please sign in to comment.