2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2012-09-23 02:39:12 +00:00
|
|
|
require 'open-uri'
|
|
|
|
|
2016-09-21 17:22:40 +00:00
|
|
|
class User < ApplicationRecord
|
2016-11-01 03:18:27 +00:00
|
|
|
acts_as_messageable # mailboxer notifications
|
|
|
|
|
2014-08-12 22:14:04 +00:00
|
|
|
has_many :topics
|
|
|
|
has_many :synapses
|
|
|
|
has_many :maps
|
|
|
|
has_many :mappings
|
2016-03-11 06:16:04 +00:00
|
|
|
has_many :tokens
|
2016-08-31 20:58:49 +00:00
|
|
|
has_many :stars
|
2016-04-24 15:50:35 +00:00
|
|
|
has_many :user_maps, dependent: :destroy
|
|
|
|
has_many :shared_maps, through: :user_maps, source: :map
|
2016-07-26 00:14:23 +00:00
|
|
|
|
2015-12-11 22:29:17 +00:00
|
|
|
after_create :generate_code
|
2013-07-11 15:13:27 +00:00
|
|
|
|
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2013-01-26 01:49:40 +00:00
|
|
|
serialize :settings, UserPreference
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
validates :password, presence: true,
|
|
|
|
length: { within: 8..40 },
|
|
|
|
on: :create
|
|
|
|
validates :password, length: { within: 8..40 },
|
|
|
|
allow_blank: true,
|
|
|
|
on: :update
|
|
|
|
validates :password, confirmation: true
|
2014-10-22 00:31:59 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
validates :name, presence: true # done by devise
|
|
|
|
validates :email, presence: true # done by devise
|
|
|
|
validates :name, uniqueness: true # done by devise
|
|
|
|
validates :email, uniqueness: true # done by devise
|
2014-10-24 15:30:26 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
validates :joinedwithcode, presence: true, inclusion: { in: $codes, message: '%{value} is not valid' }, on: :create
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2014-07-08 01:02:43 +00:00
|
|
|
# This method associates the attribute ":image" with a file attachment
|
2016-07-26 00:14:23 +00:00
|
|
|
has_attached_file :image, styles: {
|
|
|
|
thirtytwo: ['32x32#', :png],
|
|
|
|
sixtyfour: ['64x64#', :png],
|
|
|
|
ninetysix: ['96x96#', :png],
|
|
|
|
onetwentyeight: ['128x128#', :png]
|
2014-09-03 23:05:25 +00:00
|
|
|
},
|
2016-07-26 00:14:23 +00:00
|
|
|
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/user.png'
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2014-07-08 01:02:43 +00:00
|
|
|
# Validate the attached image is image/jpg, image/png, etc
|
2016-09-24 03:00:46 +00:00
|
|
|
validates_attachment_content_type :image, content_type: %r{\Aimage/.*\Z}
|
2014-08-12 15:09:53 +00:00
|
|
|
|
2015-11-06 09:08:42 +00:00
|
|
|
# override default as_json
|
2016-07-26 00:14:23 +00:00
|
|
|
def as_json(_options = {})
|
|
|
|
{ id: id,
|
|
|
|
name: name,
|
|
|
|
image: image.url(:sixtyfour),
|
|
|
|
admin: admin }
|
2014-08-12 15:09:53 +00:00
|
|
|
end
|
2015-11-06 09:08:42 +00:00
|
|
|
|
|
|
|
def as_json_for_autocomplete
|
2015-11-28 13:51:28 +00:00
|
|
|
json = {}
|
|
|
|
json['id'] = id
|
|
|
|
json['label'] = name
|
|
|
|
json['value'] = name
|
|
|
|
json['profile'] = image.url(:sixtyfour)
|
|
|
|
json['mapCount'] = maps.count
|
|
|
|
json['generation'] = generation
|
2016-07-26 00:14:23 +00:00
|
|
|
json['created_at'] = created_at.strftime('%m/%d/%Y')
|
|
|
|
json['rtype'] = 'mapper'
|
2015-11-28 13:51:28 +00:00
|
|
|
json
|
2015-11-06 09:08:42 +00:00
|
|
|
end
|
2016-09-18 15:22:51 +00:00
|
|
|
|
|
|
|
def recentMetacodes
|
|
|
|
array = []
|
|
|
|
self.topics.sort{|a,b| b.created_at <=> a.created_at }.each do |t|
|
|
|
|
if array.length < 5 and array.index(t.metacode_id) == nil
|
|
|
|
array.push(t.metacode_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
array
|
|
|
|
end
|
|
|
|
|
|
|
|
def mostUsedMetacodes
|
|
|
|
self.topics.to_a.reduce({}) { |memo, topic|
|
|
|
|
if memo[topic.metacode_id] == nil
|
|
|
|
memo[topic.metacode_id] = 1
|
|
|
|
else
|
|
|
|
memo[topic.metacode_id] = memo[topic.metacode_id] + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
memo
|
|
|
|
}.to_a.sort{ |a, b| b[1] <=> a[1] }.map{|i| i[0]}.slice(0, 5)
|
|
|
|
end
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2017-01-22 21:42:04 +00:00
|
|
|
def as_rdf(opts = {})
|
|
|
|
base_url = opts[:base_url] || 'https://metamaps.cc'
|
|
|
|
output = ''
|
|
|
|
output += %(d:mapper_#{id} a foaf:OnlineAccount ;\n)
|
|
|
|
output += %( foaf:accountName "#{name}" ;\n)
|
|
|
|
output += %( foaf:accountServiceHomepage "#{base_url}/mapper/#{id}" ;\n)
|
|
|
|
output[-2] = '.' # change last ; to a .
|
|
|
|
output += %(\n)
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2016-10-17 05:20:48 +00:00
|
|
|
def all_accessible_maps
|
|
|
|
maps + shared_maps
|
|
|
|
end
|
|
|
|
|
2016-11-13 22:29:07 +00:00
|
|
|
def recent_metacodes
|
|
|
|
topics.order(:created_at).pluck(:metacode_id).uniq.first(5)
|
2016-10-05 14:45:39 +00:00
|
|
|
end
|
|
|
|
|
2016-11-13 22:29:07 +00:00
|
|
|
def most_used_metacodes
|
|
|
|
topics.to_a.each_with_object(Hash.new(0)) do |topic, memo|
|
|
|
|
memo[topic.metacode_id] += 1
|
2016-10-05 14:45:39 +00:00
|
|
|
memo
|
2016-11-13 22:29:07 +00:00
|
|
|
end.to_a.sort { |a, b| b[1] <=> a[1] }.map { |i| i[0] }.slice(0, 5)
|
2016-10-05 14:45:39 +00:00
|
|
|
end
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2016-07-26 00:14:23 +00:00
|
|
|
# generate a random 8 letter/digit code that they can use to invite people
|
2013-07-11 15:13:27 +00:00
|
|
|
def generate_code
|
2016-07-26 00:14:23 +00:00
|
|
|
self.code ||= rand(36**8).to_s(36)
|
2014-11-21 22:24:08 +00:00
|
|
|
$codes.push(self.code)
|
2015-12-15 04:33:22 +00:00
|
|
|
self.generation = get_generation!
|
2014-11-21 21:50:42 +00:00
|
|
|
end
|
|
|
|
|
2015-12-15 04:33:22 +00:00
|
|
|
def get_generation!
|
2015-11-06 09:08:42 +00:00
|
|
|
if code == joinedwithcode
|
|
|
|
update(generation: 0)
|
2014-11-21 21:50:42 +00:00
|
|
|
else
|
2017-01-24 00:30:13 +00:00
|
|
|
update(generation: User.find_by(code: joinedwithcode).generation + 1)
|
2014-11-21 21:50:42 +00:00
|
|
|
end
|
2013-07-11 15:13:27 +00:00
|
|
|
end
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2016-09-24 03:00:46 +00:00
|
|
|
def starred_map?(map)
|
|
|
|
stars.where(map_id: map.id).exists?
|
2016-08-31 20:58:49 +00:00
|
|
|
end
|
|
|
|
|
2013-01-26 01:49:40 +00:00
|
|
|
def settings
|
|
|
|
# make sure we always return a UserPreference instance
|
2016-07-26 00:14:23 +00:00
|
|
|
self[:settings] = UserPreference.new if self[:settings].nil?
|
|
|
|
self[:settings]
|
2013-01-26 01:49:40 +00:00
|
|
|
end
|
2015-12-11 19:23:41 +00:00
|
|
|
|
2013-01-26 01:49:40 +00:00
|
|
|
def settings=(val)
|
2016-07-26 00:14:23 +00:00
|
|
|
self[:settings] = val
|
2013-01-26 01:49:40 +00:00
|
|
|
end
|
2016-11-01 03:18:27 +00:00
|
|
|
|
|
|
|
# Mailboxer hooks and helper functions
|
|
|
|
|
|
|
|
def mailboxer_email(_message)
|
|
|
|
return email if emails_allowed
|
|
|
|
# else return nil, which sends no email
|
|
|
|
end
|
|
|
|
|
|
|
|
def mailboxer_notifications
|
|
|
|
mailbox.notifications
|
|
|
|
end
|
|
|
|
|
|
|
|
def mailboxer_notification_receipts
|
|
|
|
mailbox.receipts.includes(:notification).where(mailbox_type: nil)
|
|
|
|
end
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|