email_digest_agent.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module Agents
  2. class EmailDigestAgent < Agent
  3. include EmailConcern
  4. default_schedule "5am"
  5. cannot_create_events!
  6. description <<-MD
  7. The EmailDigestAgent collects any Events sent to it and sends them all via email when run.
  8. The email will be sent to your account's address and will have a `subject` and an optional `headline` before
  9. listing the Events. If the Events' payloads contain a `message`, that will be highlighted, otherwise everything in
  10. their payloads will be shown.
  11. Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
  12. MD
  13. def default_options
  14. {
  15. 'subject' => "You have some notifications!",
  16. 'headline' => "Your notifications:",
  17. 'expected_receive_period_in_days' => "2"
  18. }
  19. end
  20. def receive(incoming_events)
  21. incoming_events.each do |event|
  22. self.memory['queue'] ||= []
  23. self.memory['queue'] << event.payload
  24. self.memory['events'] ||= []
  25. self.memory['events'] << event.id
  26. end
  27. end
  28. def check
  29. if self.memory['queue'] && self.memory['queue'].length > 0
  30. ids = self.memory['events'].join(",")
  31. groups = self.memory['queue'].map { |payload| present(payload) }
  32. log "Sending digest mail to #{user.email} with events [#{ids}]"
  33. SystemMailer.delay.send_message(:to => user.email, :subject => interpolated_options['subject'], :headline => interpolated_options['headline'], :groups => groups)
  34. self.memory['queue'] = []
  35. self.memory['events'] = []
  36. end
  37. end
  38. end
  39. end