2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
class Map < ApplicationRecord
|
2017-02-09 21:53:19 +00:00
|
|
|
ATTRS_TO_WATCH = %w(name desc permission).freeze
|
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
belongs_to :user
|
2016-12-18 21:17:51 +00:00
|
|
|
belongs_to :source, class_name: :Map
|
2017-02-09 21:53:19 +00:00
|
|
|
belongs_to :updated_by, class_name: 'User'
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2017-01-24 00:30:13 +00:00
|
|
|
has_many :topicmappings, -> { Mapping.topicmapping },
|
|
|
|
class_name: :Mapping, dependent: :destroy
|
|
|
|
has_many :synapsemappings, -> { Mapping.synapsemapping },
|
|
|
|
class_name: :Mapping, dependent: :destroy
|
2016-07-26 00:14:23 +00:00
|
|
|
has_many :topics, through: :topicmappings, source: :mappable, source_type: 'Topic'
|
|
|
|
has_many :synapses, through: :synapsemappings, source: :mappable, source_type: 'Synapse'
|
2015-12-11 19:23:41 +00:00
|
|
|
has_many :messages, as: :resource, dependent: :destroy
|
2017-02-09 21:53:19 +00:00
|
|
|
has_many :stars, dependent: :destroy
|
2017-02-11 05:20:42 +00:00
|
|
|
has_many :follows, as: :followed, dependent: :destroy
|
2017-11-25 19:23:47 +00:00
|
|
|
has_many :followers, through: :follows, source: :user
|
2012-10-26 10:04:52 +00:00
|
|
|
|
2016-10-17 00:22:00 +00:00
|
|
|
has_many :access_requests, dependent: :destroy
|
2016-04-24 15:50:35 +00:00
|
|
|
has_many :user_maps, dependent: :destroy
|
|
|
|
has_many :collaborators, through: :user_maps, source: :user
|
|
|
|
|
2016-03-12 23:36:38 +00:00
|
|
|
has_many :webhooks, as: :hookable
|
|
|
|
has_many :events, -> { includes :user }, as: :eventable, dependent: :destroy
|
|
|
|
|
2014-08-15 22:04:22 +00:00
|
|
|
# This method associates the attribute ":image" with a file attachment
|
2016-09-25 15:35:26 +00:00
|
|
|
has_attached_file :screenshot,
|
2016-12-13 03:28:10 +00:00
|
|
|
styles: {
|
|
|
|
thumb: ['220x220#', :png]
|
|
|
|
},
|
|
|
|
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/missing-map-square.png'
|
2015-12-22 18:16:03 +00:00
|
|
|
|
2015-12-17 01:10:52 +00:00
|
|
|
validates :name, presence: true
|
|
|
|
validates :arranged, inclusion: { in: [true, false] }
|
|
|
|
validates :permission, presence: true
|
2015-12-17 01:16:02 +00:00
|
|
|
validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) }
|
2016-01-22 01:59:09 +00:00
|
|
|
|
2014-08-15 22:04:22 +00:00
|
|
|
# Validate the attached image is image/jpg, image/png, etc
|
2017-01-24 00:30:13 +00:00
|
|
|
validates_attachment_content_type :screenshot, content_type: %r{\Aimage/.*\Z}
|
2014-08-15 22:04:22 +00:00
|
|
|
|
2017-03-08 18:50:39 +00:00
|
|
|
after_create :after_created
|
2017-01-03 21:12:58 +00:00
|
|
|
after_update :after_updated
|
2016-12-06 21:46:46 +00:00
|
|
|
after_save :update_deferring_topics_and_synapses, if: :permission_changed?
|
2017-05-14 02:26:43 +00:00
|
|
|
before_destroy :before_destroyed
|
2016-12-06 21:46:46 +00:00
|
|
|
|
|
|
|
delegate :count, to: :topics, prefix: :topic # same as `def topic_count; topics.count; end`
|
|
|
|
delegate :count, to: :synapses, prefix: :synapse
|
|
|
|
delegate :count, to: :contributors, prefix: :contributor
|
|
|
|
delegate :count, to: :stars, prefix: :star
|
|
|
|
|
|
|
|
delegate :name, to: :user, prefix: true
|
|
|
|
|
2015-12-22 18:16:03 +00:00
|
|
|
def mappings
|
2016-09-25 15:35:26 +00:00
|
|
|
topicmappings.or(synapsemappings)
|
2014-08-12 22:14:04 +00:00
|
|
|
end
|
2013-01-18 22:08:06 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
def contributors
|
2016-10-26 00:37:23 +00:00
|
|
|
User.where(id: mappings.map(&:user_id).uniq)
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2014-08-12 15:09:53 +00:00
|
|
|
|
2016-04-24 15:50:35 +00:00
|
|
|
def editors
|
2016-10-26 00:37:23 +00:00
|
|
|
User.where(id: user_id).or(User.where(id: collaborators))
|
2016-04-24 15:50:35 +00:00
|
|
|
end
|
|
|
|
|
2014-08-12 16:01:01 +00:00
|
|
|
def user_image
|
2016-10-17 03:46:55 +00:00
|
|
|
user.image.url(:thirtytwo)
|
2014-08-12 16:01:01 +00:00
|
|
|
end
|
|
|
|
|
2016-04-24 15:50:35 +00:00
|
|
|
def collaborator_ids
|
|
|
|
collaborators.map(&:id)
|
|
|
|
end
|
|
|
|
|
2014-10-08 04:07:54 +00:00
|
|
|
def screenshot_url
|
2016-02-09 03:48:07 +00:00
|
|
|
screenshot.url(:thumb)
|
2014-10-08 04:07:54 +00:00
|
|
|
end
|
|
|
|
|
2014-08-27 02:51:50 +00:00
|
|
|
def created_at_str
|
2016-07-26 00:14:23 +00:00
|
|
|
created_at.strftime('%m/%d/%Y')
|
2014-08-27 02:51:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def updated_at_str
|
2016-07-26 00:14:23 +00:00
|
|
|
updated_at.strftime('%m/%d/%Y')
|
2014-08-27 02:51:50 +00:00
|
|
|
end
|
|
|
|
|
2016-10-05 14:36:03 +00:00
|
|
|
def starred_by_user?(user)
|
2016-10-26 00:37:23 +00:00
|
|
|
user&.stars&.where(map: self)&.exists? || false # return false, not nil
|
2016-10-05 14:36:03 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
def as_json(_options = {})
|
2017-01-24 00:30:13 +00:00
|
|
|
json = super(
|
2017-11-25 19:23:47 +00:00
|
|
|
methods: %i(user_name user_image star_count topic_count synapse_count
|
|
|
|
contributor_count collaborator_ids screenshot_url),
|
|
|
|
except: %i(screenshot_content_type screenshot_file_size screenshot_file_name
|
|
|
|
screenshot_updated_at)
|
2017-01-24 00:30:13 +00:00
|
|
|
)
|
2016-02-09 03:48:07 +00:00
|
|
|
json[:created_at_clean] = created_at_str
|
|
|
|
json[:updated_at_clean] = updated_at_str
|
2014-08-27 02:51:50 +00:00
|
|
|
json
|
2014-08-12 15:09:53 +00:00
|
|
|
end
|
2014-10-07 23:11:55 +00:00
|
|
|
|
2016-09-23 08:41:15 +00:00
|
|
|
# user param helps determine what records are visible
|
|
|
|
def contains(user)
|
2016-09-24 04:54:14 +00:00
|
|
|
{
|
|
|
|
map: self,
|
|
|
|
topics: Pundit.policy_scope(user, topics).to_a,
|
|
|
|
synapses: Pundit.policy_scope(user, synapses).to_a,
|
|
|
|
mappings: Pundit.policy_scope(user, mappings).to_a,
|
|
|
|
mappers: contributors,
|
|
|
|
collaborators: editors,
|
|
|
|
messages: messages.sort_by(&:created_at),
|
2016-10-17 00:22:00 +00:00
|
|
|
stars: stars,
|
|
|
|
requests: access_requests
|
2016-09-24 04:54:14 +00:00
|
|
|
}
|
2016-09-23 08:41:15 +00:00
|
|
|
end
|
2016-09-23 09:02:52 +00:00
|
|
|
|
|
|
|
def add_new_collaborators(user_ids)
|
|
|
|
users = User.where(id: user_ids)
|
2016-09-24 04:54:14 +00:00
|
|
|
added = users.map do |new_user|
|
2016-10-26 00:37:23 +00:00
|
|
|
next nil if editors.include?(new_user)
|
2016-09-24 04:54:14 +00:00
|
|
|
UserMap.create(user_id: new_user.id, map_id: id)
|
|
|
|
new_user.id
|
2016-09-23 09:02:52 +00:00
|
|
|
end
|
2016-09-24 04:54:14 +00:00
|
|
|
added.compact
|
2016-09-23 09:02:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_old_collaborators(user_ids)
|
2016-10-26 00:37:23 +00:00
|
|
|
removed = editors.map(&:id).map do |old_user_id|
|
2016-09-24 04:54:14 +00:00
|
|
|
next nil if user_ids.include?(old_user_id)
|
|
|
|
user_maps.where(user_id: old_user_id).find_each(&:destroy)
|
2016-10-17 00:22:00 +00:00
|
|
|
access_requests.where(user_id: old_user_id).find_each(&:destroy)
|
2016-09-24 04:54:14 +00:00
|
|
|
old_user_id
|
2016-09-23 09:02:52 +00:00
|
|
|
end
|
2016-09-24 04:54:14 +00:00
|
|
|
removed.compact
|
2016-09-23 09:02:52 +00:00
|
|
|
end
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2016-12-06 21:46:46 +00:00
|
|
|
def update_deferring_topics_and_synapses
|
2017-01-24 00:30:13 +00:00
|
|
|
Topic.where(defer_to_map_id: id).update(permission: permission)
|
|
|
|
Synapse.where(defer_to_map_id: id).update(permission: permission)
|
2016-12-06 21:46:46 +00:00
|
|
|
end
|
2016-12-13 07:42:33 +00:00
|
|
|
|
2017-02-09 21:53:19 +00:00
|
|
|
protected
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2017-03-08 18:50:39 +00:00
|
|
|
def after_created
|
2017-11-25 19:23:47 +00:00
|
|
|
FollowService.follow(self, user, 'created')
|
2017-02-11 05:20:42 +00:00
|
|
|
# notify users following the map creator
|
|
|
|
end
|
2017-02-09 21:53:19 +00:00
|
|
|
|
|
|
|
def after_updated
|
|
|
|
return unless ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) }
|
|
|
|
ActionCable.server.broadcast 'map_' + id.to_s, type: 'mapUpdated'
|
|
|
|
end
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2017-02-11 05:20:42 +00:00
|
|
|
def after_updated_async
|
|
|
|
if ATTRS_TO_WATCH.any? { |k| changed_attributes.key?(k) }
|
|
|
|
FollowService.follow(self, updated_by, 'contributed')
|
|
|
|
# NotificationService.notify_followers(self, 'map_updated', changed_attributes)
|
|
|
|
# or better yet publish an event
|
|
|
|
end
|
|
|
|
end
|
|
|
|
handle_asynchronously :after_updated_async
|
2017-05-14 02:26:43 +00:00
|
|
|
|
|
|
|
def before_destroyed
|
|
|
|
Map.where(source_id: id).find_each do |forked_map|
|
|
|
|
forked_map.update(source_id: nil)
|
|
|
|
end
|
|
|
|
end
|
2012-10-24 06:47:08 +00:00
|
|
|
end
|