Add type PushRequest.

This commit is contained in:
Владислав Весельский 2025-02-06 10:51:07 +03:00
parent 137f53a84d
commit e4f142324f
1 changed files with 103 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package push_common package push_common
import ( import (
"context"
"strconv" "strconv"
"strings" "strings"
@ -93,3 +94,105 @@ type TextVariant struct {
Title string `json:"title"` Title string `json:"title"`
Message string `json:"message"` Message string `json:"message"`
} }
//-----------------------------------------------------------------------------
type PushRequest struct {
Title string `json:"title"`
Message string `json:"message"`
Info string `json:"info"`
Label string `json:"label"` //Note: label for firebase
Ttl uint32 `json:"ttl"`
Jobs []PushJobRequest `json:"jobs"`
ImageUrl string `json:"imageUrl"`
}
func (r *PushRequest) DeviceTokenAmount() uint32 {
return jobRequests_deviceTokenAmount(r.Jobs)
}
func (r *PushRequest) Validate() error {
return validateJobsRequests(r.Jobs)
}
func (r *PushRequest) FindJobByLang(lang int) *PushJobRequest {
for _, j := range r.Jobs {
if j.Language == lang {
return &j
}
}
return nil
}
func (r *PushRequest) GetPlatforms() []Platform {
var platforms []Platform
for _, jobRequest := range r.Jobs {
p := jobRequest.Platform
if !PlatformsContain(platforms, p) {
platforms = append(platforms, p)
}
}
return platforms
}
type PushLaunchRequest struct {
Id int `json:"id"`
JobRequests []PushJobRequest `json:"jobs"` // Длина среза jobRequests может быть равна 0.
}
//func (r *PushLaunchRequest) jobAmount() uint32 {
// return uint32(len(r.JobRequests))
//}
func (r *PushLaunchRequest) DeviceTokenAmount() uint32 {
return jobRequests_deviceTokenAmount(r.JobRequests)
}
func (r *PushLaunchRequest) Validate(ctx context.Context) error {
return validateJobsRequests(r.JobRequests)
}
func validateJobsRequests(jobRequests []PushJobRequest) error {
for _, jobRequest := range jobRequests {
if err := jobRequest.Validate(); err != nil {
return err
}
}
return nil
}
type PushJobRequest struct {
Message string `json:"message"`
Title string `json:"title"`
Platform Platform `json:"platform"`
Language int `json:"language"`
Utc_delta int `json:"utc_delta"`
Device_tokens []string `json:"device_tokens"`
}
func (j *PushJobRequest) Validate() error {
if len(j.Message) == 0 {
return errors.New("No message")
}
if len(j.Device_tokens) == 0 {
return errors.New("No device tokens")
}
if j.Platform == PlatformUnknown {
return errors.New("No platform")
}
return nil
}
func (req *PushJobRequest) deviceTokenAmount() uint32 {
return uint32(len(req.Device_tokens))
}
func jobRequests_deviceTokenAmount(jobRequests []PushJobRequest) uint32 {
var total uint32 = 0
for _, jobRequest := range jobRequests {
total += jobRequest.deviceTokenAmount()
}
return total
}