Class: ESM::Xm8Notification

Inherits:
Struct
  • Object
show all
Defined in:
lib/esm/model/xm8_notification.rb,
lib/esm/model/xm8_notification/custom.rb,
lib/esm/model/xm8_notification/base_raid.rb,
lib/esm/model/xm8_notification/flag_stolen.rb,
lib/esm/model/xm8_notification/hack_started.rb,
lib/esm/model/xm8_notification/flag_restored.rb,
lib/esm/model/xm8_notification/grind_started.rb,
lib/esm/model/xm8_notification/marxet_item_sold.rb,
lib/esm/model/xm8_notification/flag_steal_started.rb,
lib/esm/model/xm8_notification/charge_plant_started.rb,
lib/esm/model/xm8_notification/protection_money_due.rb,
lib/esm/model/xm8_notification/protection_money_paid.rb

Defined Under Namespace

Classes: BaseRaid, ChargePlantStarted, Custom, FlagRestored, FlagStealStarted, FlagStolen, GrindStarted, HackStarted, InvalidContent, InvalidType, MarxetItemSold, ProtectionMoneyDue, ProtectionMoneyPaid

Constant Summary collapse

TYPES =
{
  "base-raid": BaseRaid,
  "charge-plant-started": ChargePlantStarted,
  custom: Custom,
  "flag-restored": FlagRestored,
  "flag-steal-started": FlagStealStarted,
  "flag-stolen": FlagStolen,
  "grind-started": GrindStarted,
  "hack-started": HackStarted,
  "marxet-item-sold": MarxetItemSold,
  "protection-money-due": ProtectionMoneyDue,
  "protection-money-paid": ProtectionMoneyPaid
}.with_indifferent_access.freeze
STATES =

Must match the ENUM in MySQL for xm8_notifications

[
  STATE_NEW = "new",
  STATE_PENDING = "pending",
  STATE_FAILED = "failed",
  STATE_SENT = "sent"
]
DETAILS_NOT_REGISTERED =
"recipient not registered"
DETAILS_DM =
"direct message"
DETAILS_CUSTOM =
"custom route(s)"
DETAILS_NO_DESTINATION =
"#{DETAILS_DM} disallowed, no #{DETAILS_CUSTOM}"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Xm8Notification

Returns a new instance of Xm8Notification.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/esm/model/xm8_notification.rb', line 60

def initialize(**opts)
  # ID is internal for tracking purposes
  opts[:id] = SecureRandom.uuid

  super

  # Notification generation data
  @context = {
    communityid: server.community.community_id,
    serverid: server.server_id,
    servername: server.server_name,
    territoryid: content.territory_id || "",
    territoryname: content.territory_name || "",
    item: content.item_name || "",
    amount: content.poptabs_received || ""
  }
end

Class Method Details

.failed_state(state_details) ⇒ Object



52
53
54
# File 'lib/esm/model/xm8_notification.rb', line 52

def self.failed_state(state_details)
  {state: Xm8Notification::STATE_FAILED, state_details:}
end

.from(hash) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/esm/model/xm8_notification.rb', line 40

def self.from(hash)
  type = hash[:type]
  klass = TYPES[type]
  raise InvalidType, "\"#{type}\" is not a valid XM8 notification type" if klass.nil?

  hash[:content] = hash[:content].to_ostruct

  notification = klass.new(**hash.without(:type))
  notification.validate!
  notification
end

.sent_state(state_details) ⇒ Object



56
57
58
# File 'lib/esm/model/xm8_notification.rb', line 56

def self.sent_state(state_details)
  {state: Xm8Notification::STATE_SENT, state_details:}
end

Instance Method Details

#send_to_recipientsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/esm/model/xm8_notification.rb', line 82

def send_to_recipients
  default_block = ->(h, k) { h[k] = [] }
  states = {
    success: Hash.new(&default_block),
    failure: Hash.new(&default_block)
  }

  user_ids = recipient_notification_mapping.keys.map(&:id)

  send_to_dm(states, user_ids)
  send_to_custom_routes(states, user_ids)

  process_undeliverable_notifications(states)
  update_notification_states(states)

  nil
end

#to_embedObject



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/esm/model/xm8_notification.rb', line 100

def to_embed
  embed = ESM::Notification.build_random(
    community_id: server.community.id,
    type:,
    category: "xm8",
    **@context
  )

  embed.footer = "[#{server.server_id}] #{server.server_name}"
  embed
end

#typeObject



78
79
80
# File 'lib/esm/model/xm8_notification.rb', line 78

def type
  @type ||= self.class.name.demodulize.underscore.dasherize
end

#validate!Object

Raises:



112
113
114
# File 'lib/esm/model/xm8_notification.rb', line 112

def validate!
  raise InvalidContent unless valid?
end