| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package sender
- import (
- "fmt"
- "strings"
- "sync/atomic"
- )
- // SubscribePushRow is the GM SubscribePush table entry needed by the sender.
- type SubscribePushRow struct {
- ID int `json:"id"`
- Title1 string `json:"title1"`
- Desc string `json:"desc"`
- IsOpen int `json:"is_open"`
- IsSet int `json:"is_set"`
- SetInitialize int `json:"set_initialize"`
- }
- type subscribeMessage struct {
- Title string
- Description string
- }
- var subscribePushRows atomic.Value // map[int]SubscribePushRow
- // ConfigureSubscribePush installs the one-time startup snapshot fetched from GM.
- func ConfigureSubscribePush(rows []SubscribePushRow) error {
- if len(rows) == 0 {
- return fmt.Errorf("SubscribePush configuration is empty")
- }
- byID := make(map[int]SubscribePushRow, len(rows))
- for _, row := range rows {
- if row.ID <= 0 {
- return fmt.Errorf("SubscribePush row has invalid ID %d", row.ID)
- }
- if row.IsOpen != 0 && (strings.TrimSpace(row.Title1) == "" || strings.TrimSpace(row.Desc) == "") {
- return fmt.Errorf("SubscribePush row %d is enabled but has no message content", row.ID)
- }
- byID[row.ID] = row
- }
- subscribePushRows.Store(byID)
- return nil
- }
- func subscribePushMessage(id int) (subscribeMessage, bool) {
- row, ok := SubscribePushConfig(id)
- if !ok || !SubscribePushActive(id) {
- return subscribeMessage{}, false
- }
- return subscribeMessage{Title: row.Title1, Description: row.Desc}, true
- }
- func SubscribePushActive(id int) bool {
- row, ok := SubscribePushConfig(id)
- return ok && row.IsOpen != 0 && strings.TrimSpace(row.Title1) != "" && strings.TrimSpace(row.Desc) != ""
- }
- // SubscribePushRequiresSetting reports whether the player must have this ID in settings.
- func SubscribePushRequiresSetting(row SubscribePushRow) bool {
- return !(row.IsOpen == 1 && row.IsSet == 0 && row.SetInitialize == 1)
- }
- func SubscribePushConfig(id int) (SubscribePushRow, bool) {
- value := subscribePushRows.Load()
- if value == nil {
- return SubscribePushRow{}, false
- }
- row, ok := value.(map[int]SubscribePushRow)[id]
- return row, ok
- }
- // SubscribePushConfigByID reports whether an ID exists in the GM startup snapshot.
- func SubscribePushConfigByID(id int) bool {
- value := subscribePushRows.Load()
- if value == nil {
- return false
- }
- _, ok := SubscribePushConfig(id)
- return ok
- }
|