Skip to content

Commit

Permalink
removed the redundant ios prefixes from the ios notifications directory
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 22, 2023
1 parent 283f9d8 commit c7a6956
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 110 deletions.
10 changes: 2 additions & 8 deletions .ls-lint.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
ls:
.dir: snake_case
.dir: lowercase
.pb.gw.go: snake_case
.pb.go: snake_case
.go: snake_case

ignore:
- venv
- server/swagger/node_modules
- .idea
- .git
- .github
- .vscode
- apns_auth_key.p8
- swagger
6 changes: 3 additions & 3 deletions server/backend/cron/cronjobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cron
import (
"time"

"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/apns"
"github.com/TUM-Dev/Campus-Backend/server/env"

"github.com/TUM-Dev/Campus-Backend/server/model"
Expand All @@ -16,7 +16,7 @@ import (
type CronService struct {
db *gorm.DB
gf *gofeed.Parser
APNs *ios_apns.Service
APNs *apns.Service
}

const StorageDir = "/Storage/" // target location of files
Expand All @@ -41,7 +41,7 @@ func New(db *gorm.DB) *CronService {
return &CronService{
db: db,
gf: gofeed.NewParser(),
APNs: ios_apns.NewCronService(db),
APNs: apns.NewCronService(db),
}
}

Expand Down
20 changes: 10 additions & 10 deletions server/backend/cron/ios_notifications.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package cron

import (
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_device"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_devices_activity_reset"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_scheduled_update_log"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_scheduling"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/device"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/devices_activity_reset"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/scheduled_update_log"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/scheduling"
)

// Starts the cron job for sending iOS notifications reuses
// the APNs client (ios_apns.Service) stored in CronService
// the APNs client (apns.Service) stored in CronService
func (c *CronService) iosNotificationsCron() error {
if !c.APNs.IsActive {
return nil
}

repo := ios_scheduling.NewRepository(c.db)
devicesRepo := ios_device.NewRepository(c.db)
schedulerRepo := ios_scheduled_update_log.NewRepository(c.db)
repo := scheduling.NewRepository(c.db)
devicesRepo := device.NewRepository(c.db)
schedulerRepo := scheduled_update_log.NewRepository(c.db)

service := ios_scheduling.NewService(repo, devicesRepo, schedulerRepo, c.APNs)
service := scheduling.NewService(repo, devicesRepo, schedulerRepo, c.APNs)

return service.HandleScheduledCron()
}

// Resets the activity of all devices to 0 every day, week, month or year
func (c *CronService) iosActivityReset() error {
service := ios_devices_activity_reset.NewService(c.db)
service := devices_activity_reset.NewService(c.db)

return service.HandleScheduledActivityReset()
}
4 changes: 2 additions & 2 deletions server/backend/cron/new_exam_results_hook.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package cron

import (
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_device"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/device"
"github.com/TUM-Dev/Campus-Backend/server/backend/new_exam_results_hook/new_exam_results_scheduling"
)

func (c *CronService) newExamResultsHookCron() error {
repo := new_exam_results_scheduling.NewRepository(c.db)
devicesRepo := ios_device.NewRepository(c.db)
devicesRepo := device.NewRepository(c.db)

service := new_exam_results_scheduling.NewService(repo, devicesRepo, c.APNs)

Expand Down
27 changes: 13 additions & 14 deletions server/backend/ios_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,34 @@ import (
"context"

pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns/ios_apns_jwt"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_device"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_request_response"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/apns"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/device"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/request_response"
"gorm.io/gorm"
)

type IOSNotificationsService struct {
DB *gorm.DB
APNSToken *ios_apns_jwt.Token
APNSToken *apns.JWTToken
IsActive bool
}

func (s *CampusServer) GetIOSDeviceService() *ios_device.Service {
repository := ios_device.NewRepository(s.db)
func (s *CampusServer) GetIOSDeviceService() *device.Service {
repository := device.NewRepository(s.db)

return ios_device.NewService(repository)
return device.NewService(repository)
}

func (s *CampusServer) GetIOSAPNsService() *ios_apns.Service {
repository := ios_apns.NewRepository(s.db, s.GetIOSNotificationsService().APNSToken)
func (s *CampusServer) GetIOSAPNsService() *apns.Service {
repository := apns.NewRepository(s.db, s.GetIOSNotificationsService().APNSToken)

return ios_apns.NewService(repository)
return apns.NewService(repository)
}

func (s *CampusServer) GetIOSRequestResponseService() *ios_request_response.Service {
repository := ios_request_response.NewRepository(s.db, s.GetIOSNotificationsService().APNSToken)
func (s *CampusServer) GetIOSRequestResponseService() *request_response.Service {
repository := request_response.NewRepository(s.db, s.GetIOSNotificationsService().APNSToken)

return ios_request_response.NewService(repository)
return request_response.NewService(repository)
}

func (s *CampusServer) IOSDeviceRequestResponse(_ context.Context, req *pb.IOSDeviceRequestResponseRequest) (*pb.IOSDeviceRequestResponseReply, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ios_apns
package apns

import (
"bytes"
Expand All @@ -9,7 +9,6 @@ import (
"strconv"
"time"

"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns/ios_apns_jwt"
"github.com/TUM-Dev/Campus-Backend/server/env"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -38,7 +37,7 @@ var (

type Repository struct {
DB gorm.DB
Token *ios_apns_jwt.Token
Token *JWTToken
httpClient *http.Client
}

Expand Down Expand Up @@ -117,7 +116,7 @@ func (r *Repository) SendNotification(notification *model.IOSNotificationPayload
return &response, nil
}

func NewRepository(db *gorm.DB, token *ios_apns_jwt.Token) *Repository {
func NewRepository(db *gorm.DB, token *JWTToken) *Repository {
transport := &http2.Transport{
ReadIdleTimeout: ReadIdleTimeout,
}
Expand All @@ -138,7 +137,7 @@ func NewCronRepository(db *gorm.DB) (*Repository, error) {
return nil, err
}

token, err := ios_apns_jwt.NewToken()
token, err := NewToken()
if err != nil {
log.WithError(err).Error("Could not create APNs token")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// Package ios_apns contains the logic for sending push notifications to iOS devices.
// Package apns contains the logic for sending push notifications to iOS devices.
// and communicating with the Apple Push Notification Service (APNs).
package ios_apns
package apns

import (
"errors"

"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns/ios_apns_jwt"
"github.com/TUM-Dev/Campus-Backend/server/model"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
Expand Down Expand Up @@ -41,19 +40,19 @@ func (s *Service) RequestGradeUpdateForDevice(deviceID string) error {
}

func ValidateRequirementsForIOSNotificationsService() error {
if ios_apns_jwt.ApnsKeyId == "" {
if ApnsKeyId == "" {
return errors.New("APNS_KEY_ID env variable is not set")
}

if ios_apns_jwt.ApnsTeamId == "" {
if ApnsTeamId == "" {
return errors.New("APNS_TEAM_ID env variable is not set")
}

if ios_apns_jwt.ApnsP8FilePath == "" {
if ApnsP8FilePath == "" {
return errors.New("APNS_P8_FILE_PATH env variable is not set")
}

if _, err := ios_apns_jwt.APNsEncryptionKeyFromFile(); err != nil {
if _, err := APNsEncryptionKeyFromFile(); err != nil {
return errors.New("APNS P8 token is not valid or not set")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package ios_apns_jwt handles the generation and validation of the JWT token for the APNs service
package ios_apns_jwt
// Package apns handles the generation and validation of the JWT token for the APNs service
package apns

import (
"crypto/ecdsa"
Expand Down Expand Up @@ -29,7 +29,7 @@ var (
ApnsP8FilePath = os.Getenv("APNS_P8_FILE_PATH")
)

type Token struct {
type JWTToken struct {
sync.Mutex
EncryptionKey *ecdsa.PrivateKey
KeyId string
Expand All @@ -38,13 +38,13 @@ type Token struct {
Bearer string
}

func NewToken() (*Token, error) {
func NewToken() (*JWTToken, error) {
encryptionKey, err := APNsEncryptionKeyFromFile()
if err != nil {
return nil, err
}

token := Token{
token := JWTToken{
EncryptionKey: encryptionKey,
KeyId: ApnsKeyId,
TeamId: ApnsTeamId,
Expand Down Expand Up @@ -99,7 +99,7 @@ func APNsEncryptionKeyFromFile() (*ecdsa.PrivateKey, error) {
return nil, ErrorAuthKeyNotEcdsa
}

func (t *Token) GenerateNewTokenIfExpired() (bearer string) {
func (t *JWTToken) GenerateNewTokenIfExpired() (bearer string) {
t.Lock()
defer t.Unlock()

Expand All @@ -113,11 +113,11 @@ func (t *Token) GenerateNewTokenIfExpired() (bearer string) {
return t.Bearer
}

func (t *Token) IsExpired() bool {
func (t *JWTToken) IsExpired() bool {
return currentTimestamp() >= (t.IssuedAt + TokenTimeout)
}

func (t *Token) Generate() error {
func (t *JWTToken) Generate() error {
if t.EncryptionKey == nil {
return ErrorAuthKeyNil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package ios_crypto provides functions for encrypting and decrypting strings using AES-256-GCM.
package ios_crypto
// Package crypto provides functions for encrypting and decrypting strings using AES-256-GCM.
package crypto

import (
"crypto/aes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ios_device
package device

import (
"errors"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Package ios_device provides functions to register and remove ios devices
package ios_device
// Package device provides functions to register and remove ios devices
package device

import (
pb "github.com/TUM-Dev/Campus-Backend/server/api/tumdev"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ios_devices_activity_reset
package devices_activity_reset

import (
"time"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ios_devices_activity_reset
package devices_activity_reset

import (
"time"

"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_device"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/device"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
Expand Down Expand Up @@ -41,7 +41,7 @@ func (service *Service) HandleScheduledActivityReset() error {

now := time.Now()

devicesRepo := ios_device.NewRepository(service.Repository.DB)
devicesRepo := device.NewRepository(service.Repository.DB)

if now.Sub(daily.LastReset).Hours() > 24 {
if err := service.Repository.ResettedDevicesDaily(); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package ios_request_response
package request_response

import (
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/ios_apns/ios_apns_jwt"
"github.com/TUM-Dev/Campus-Backend/server/backend/ios_notifications/apns"
"github.com/TUM-Dev/Campus-Backend/server/model"
"gorm.io/gorm"
)

type Repository struct {
DB *gorm.DB
Token *ios_apns_jwt.Token
Token *apns.JWTToken
}

func (r *Repository) SaveEncryptedGrade(grade *model.IOSEncryptedGrade) error {
Expand Down Expand Up @@ -70,7 +70,7 @@ func (r *Repository) DeleteAllRequestLogsForThisDeviceWithType(requestLog *model
return nil
}

func NewRepository(db *gorm.DB, token *ios_apns_jwt.Token) *Repository {
func NewRepository(db *gorm.DB, token *apns.JWTToken) *Repository {
return &Repository{
DB: db,
Token: token,
Expand Down
Loading

0 comments on commit c7a6956

Please sign in to comment.