2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2016-09-21 17:22:40 +00:00
|
|
|
class Event < ApplicationRecord
|
2016-12-14 15:08:59 +00:00
|
|
|
KINDS = %w(user_present_on_map conversation_started_on_map
|
|
|
|
topic_added_to_map topic_moved_on_map topic_removed_from_map
|
2016-12-15 03:08:51 +00:00
|
|
|
synapse_added_to_map synapse_removed_from_map
|
|
|
|
topic_updated synapse_updated).freeze
|
2016-03-12 23:36:38 +00: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 00:14:23 +00:00
|
|
|
validates :kind, inclusion: { in: KINDS }
|
|
|
|
validates :eventable, presence: true
|
2016-03-12 23:36:38 +00:00
|
|
|
|
|
|
|
def belongs_to?(this_user)
|
2016-07-26 00:14:23 +00:00
|
|
|
user_id == this_user.id
|
2016-03-12 23:36:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def notify_webhooks!
|
2016-07-26 00:14:23 +00:00
|
|
|
map.webhooks.each { |webhook| WebhookService.publish! webhook: webhook, event: self }
|
2016-03-12 23:36:38 +00:00
|
|
|
end
|
|
|
|
handle_asynchronously :notify_webhooks!
|
|
|
|
end
|