Add type PushRequest.
This commit is contained in:
parent
137f53a84d
commit
e4f142324f
103
push_common.go
103
push_common.go
|
@ -1,6 +1,7 @@
|
|||
package push_common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -93,3 +94,105 @@ type TextVariant struct {
|
|||
Title string `json:"title"`
|
||||
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue