subscribe_push_config.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package sender
  2. import (
  3. "fmt"
  4. "strings"
  5. "sync/atomic"
  6. )
  7. // SubscribePushRow is the GM SubscribePush table entry needed by the sender.
  8. type SubscribePushRow struct {
  9. ID int `json:"id"`
  10. Title1 string `json:"title1"`
  11. Desc string `json:"desc"`
  12. IsOpen int `json:"is_open"`
  13. IsSet int `json:"is_set"`
  14. SetInitialize int `json:"set_initialize"`
  15. }
  16. type subscribeMessage struct {
  17. Title string
  18. Description string
  19. }
  20. var subscribePushRows atomic.Value // map[int]SubscribePushRow
  21. // ConfigureSubscribePush installs the one-time startup snapshot fetched from GM.
  22. func ConfigureSubscribePush(rows []SubscribePushRow) error {
  23. if len(rows) == 0 {
  24. return fmt.Errorf("SubscribePush configuration is empty")
  25. }
  26. byID := make(map[int]SubscribePushRow, len(rows))
  27. for _, row := range rows {
  28. if row.ID <= 0 {
  29. return fmt.Errorf("SubscribePush row has invalid ID %d", row.ID)
  30. }
  31. if row.IsOpen != 0 && (strings.TrimSpace(row.Title1) == "" || strings.TrimSpace(row.Desc) == "") {
  32. return fmt.Errorf("SubscribePush row %d is enabled but has no message content", row.ID)
  33. }
  34. byID[row.ID] = row
  35. }
  36. subscribePushRows.Store(byID)
  37. return nil
  38. }
  39. func subscribePushMessage(id int) (subscribeMessage, bool) {
  40. row, ok := SubscribePushConfig(id)
  41. if !ok || !SubscribePushActive(id) {
  42. return subscribeMessage{}, false
  43. }
  44. return subscribeMessage{Title: row.Title1, Description: row.Desc}, true
  45. }
  46. func SubscribePushActive(id int) bool {
  47. row, ok := SubscribePushConfig(id)
  48. return ok && row.IsOpen != 0 && strings.TrimSpace(row.Title1) != "" && strings.TrimSpace(row.Desc) != ""
  49. }
  50. // SubscribePushRequiresSetting reports whether the player must have this ID in settings.
  51. func SubscribePushRequiresSetting(row SubscribePushRow) bool {
  52. return !(row.IsOpen == 1 && row.IsSet == 0 && row.SetInitialize == 1)
  53. }
  54. func SubscribePushConfig(id int) (SubscribePushRow, bool) {
  55. value := subscribePushRows.Load()
  56. if value == nil {
  57. return SubscribePushRow{}, false
  58. }
  59. row, ok := value.(map[int]SubscribePushRow)[id]
  60. return row, ok
  61. }
  62. // SubscribePushConfigByID reports whether an ID exists in the GM startup snapshot.
  63. func SubscribePushConfigByID(id int) bool {
  64. value := subscribePushRows.Load()
  65. if value == nil {
  66. return false
  67. }
  68. _, ok := SubscribePushConfig(id)
  69. return ok
  70. }