2016-09-24 11:00:46 +08:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 11:23:47 -08:00
|
|
|
|
2016-09-22 01:22:40 +08:00
|
|
|
class Event < ApplicationRecord
|
2018-01-20 14:10:26 -08:00
|
|
|
KINDS = %w[user_present_on_map user_not_present_on_map
|
2017-01-25 15:32:13 -08:00
|
|
|
conversation_started_on_map
|
2017-01-23 19:30:13 -05:00
|
|
|
topic_added_to_map topic_moved_on_map topic_removed_from_map
|
|
|
|
synapse_added_to_map synapse_removed_from_map
|
2018-01-20 14:10:26 -08:00
|
|
|
topic_updated synapse_updated].freeze
|
2016-03-13 10:36:38 +11:00
|
|
|
|
|
|
|
belongs_to :eventable, polymorphic: true
|
|
|
|
belongs_to :map
|
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
scope :chronologically, -> { order('created_at asc') }
|
|
|
|
|
|
|
|
after_create :notify_webhooks!, if: :map
|
|
|
|
|
2016-07-26 08:14:23 +08:00
|
|
|
validates :kind, inclusion: { in: KINDS }
|
|
|
|
validates :eventable, presence: true
|
2016-03-13 10:36:38 +11:00
|
|
|
|
|
|
|
def belongs_to?(this_user)
|
2016-07-26 08:14:23 +08:00
|
|
|
user_id == this_user.id
|
2016-03-13 10:36:38 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_webhooks!
|
2016-07-26 08:14:23 +08:00
|
|
|
map.webhooks.each { |webhook| WebhookService.publish! webhook: webhook, event: self }
|
2016-03-13 10:36:38 +11:00
|
|
|
end
|
|
|
|
handle_asynchronously :notify_webhooks!
|
|
|
|
end
|