metamaps--metamaps/app/models/user.rb

154 lines
4.2 KiB
Ruby
Raw Normal View History

2016-09-24 03:00:46 +00:00
# frozen_string_literal: true
2012-09-23 02:39:12 +00:00
require 'open-uri'
class User < ApplicationRecord
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
has_many :tokens
has_many :stars
has_many :user_maps, dependent: :destroy
has_many :shared_maps, through: :user_maps, source: :map
has_many :follows, as: :followed
has_many :followers, through: :follows, source: :user
has_many :following, class_name: 'Follow'
2015-12-11 22:29:17 +00:00
after_create :generate_code
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
2015-12-11 19:23:41 +00:00
serialize :settings, UserPreference
2015-12-11 19:23:41 +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
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
validates :joinedwithcode, presence: true, inclusion: { in: $codes, message: '%{value} is not valid' }, on: :create
2015-12-11 19:23:41 +00:00
# This method associates the attribute ":image" with a file attachment
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
},
default_url: 'https://s3.amazonaws.com/metamaps-assets/site/user.png'
2015-12-11 19:23:41 +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
def as_json(_options = {})
Into master: two finger pan/zoom, map and topic follows (for internal testing) on the UI, map activity emails (#1084) * fix topic spec * fix synapse/mapping spec * brakeman csrf warning suppressed :| * follows for maps in the ui for internal testing only still (#1072) * follows for maps in the ui for testers * require user for these actions * match how map follow works * include ability to unfollow from email * fixup templates * add unfollow_from_email to the policies * Update _cheatsheet.html.erb Clean up text, clarify, and bring in line with current functionality * topicsRegex and synapsesRegex should allow commas (#1073) * even better import csv regexes * prevent double prompt on file drop import * topic card in react (#1031) * its coming along * links bar * scssify a bunch * metacode image working a bit better * metacode selector in react topic card * riek editing for name field on topic card * riek submit on enter * factor out Title and Links from Topic Card component, but not the listeners * create working Desc editor * styling is much better now * textarea min height for desc * disallow images in topic card markdown * shift enter is linebreak, enter is save * attachments split out, but it's pretty buggy * move listeners into Links.js * slightly wider metacodeTitle * fix positioning on metacode selector * fix metacode selection * move metacode and permissions into subcomponents * fixes * prevent editing on desc/title if not authorized to edit * fix topic card draggability * fix embedly * fix md test * remove the removed link card manually with jquery * fix test syntax * eslint * more eslin * reuse authorizedToEdit * convert metacode sets to a json object for react * add the html in react whoop * fix metacode styling * sort wasn't working * finishing metacode select * readd the above link input border * fix syntax * multiline title editable textarea * more portable metacode selector component * factor out #metacodeOptions into one react component with a callback :D:D:D * render metacodeOptions in right click menu with react * render metacodeOptions in right click menu with react * fix up right click menu's metacode editing * fix topic card title character counter * ignore metamaps secret bundle in ag * simplify Attachments props * factor out embedly card into its own component; it seems to help * link resetter * fix edit icon on title in topic card * move mapCount and synapseCount hover/click logic to react * fix up the showMore control * metacode selection tweaks * tweak links bar spacing in topic card * rubocop * remove TODOs * more badass permissions selector * close permission selector when you click outside * fix overeager metacode selector * more modular attachments component * fix bug in Desc.js * fix right click styling * permission changes are different than edit rights * bad module ref * ensure maxLength on topic titles * hellz yeah (#1074) * fix drop from two touches to one * don't commit activity service * ability to select/unselect all metacodes in custom set with keyboard shortcut (fix #390) (#1078) * ability to select/unselect all metacodes in custom set with keyboard shortcut * select all button * nicer all/none buttons * set up react testing (#1080) * install mocha-webpack. also switch hark to npm version instead of github version * well, mocha-webpack runs * add jsdom for tests * upgrade to webpack 2 * fix npm run test errors * ImportDialogBox component tests * Fixes bug where pressing delete key while editing text will suggest... (#1083) * Fixes bug where pressing delete key while editing text will suggest the deletion of selected map entities * Changed the DEL key to remove entities instead of delete them * temporarily disable code climate duplication engine * add topic following for internal testing * daily map activity emails (#1081) * data prepared, task setup * add the basics of the email template * cover granular permissions * unfollow this map * break out permissions tests better * rename so test runs
2017-03-07 03:49:46 +00:00
json = { id: id,
name: name,
image: image.url(:sixtyfour),
admin: admin }
Into master: two finger pan/zoom, map and topic follows (for internal testing) on the UI, map activity emails (#1084) * fix topic spec * fix synapse/mapping spec * brakeman csrf warning suppressed :| * follows for maps in the ui for internal testing only still (#1072) * follows for maps in the ui for testers * require user for these actions * match how map follow works * include ability to unfollow from email * fixup templates * add unfollow_from_email to the policies * Update _cheatsheet.html.erb Clean up text, clarify, and bring in line with current functionality * topicsRegex and synapsesRegex should allow commas (#1073) * even better import csv regexes * prevent double prompt on file drop import * topic card in react (#1031) * its coming along * links bar * scssify a bunch * metacode image working a bit better * metacode selector in react topic card * riek editing for name field on topic card * riek submit on enter * factor out Title and Links from Topic Card component, but not the listeners * create working Desc editor * styling is much better now * textarea min height for desc * disallow images in topic card markdown * shift enter is linebreak, enter is save * attachments split out, but it's pretty buggy * move listeners into Links.js * slightly wider metacodeTitle * fix positioning on metacode selector * fix metacode selection * move metacode and permissions into subcomponents * fixes * prevent editing on desc/title if not authorized to edit * fix topic card draggability * fix embedly * fix md test * remove the removed link card manually with jquery * fix test syntax * eslint * more eslin * reuse authorizedToEdit * convert metacode sets to a json object for react * add the html in react whoop * fix metacode styling * sort wasn't working * finishing metacode select * readd the above link input border * fix syntax * multiline title editable textarea * more portable metacode selector component * factor out #metacodeOptions into one react component with a callback :D:D:D * render metacodeOptions in right click menu with react * render metacodeOptions in right click menu with react * fix up right click menu's metacode editing * fix topic card title character counter * ignore metamaps secret bundle in ag * simplify Attachments props * factor out embedly card into its own component; it seems to help * link resetter * fix edit icon on title in topic card * move mapCount and synapseCount hover/click logic to react * fix up the showMore control * metacode selection tweaks * tweak links bar spacing in topic card * rubocop * remove TODOs * more badass permissions selector * close permission selector when you click outside * fix overeager metacode selector * more modular attachments component * fix bug in Desc.js * fix right click styling * permission changes are different than edit rights * bad module ref * ensure maxLength on topic titles * hellz yeah (#1074) * fix drop from two touches to one * don't commit activity service * ability to select/unselect all metacodes in custom set with keyboard shortcut (fix #390) (#1078) * ability to select/unselect all metacodes in custom set with keyboard shortcut * select all button * nicer all/none buttons * set up react testing (#1080) * install mocha-webpack. also switch hark to npm version instead of github version * well, mocha-webpack runs * add jsdom for tests * upgrade to webpack 2 * fix npm run test errors * ImportDialogBox component tests * Fixes bug where pressing delete key while editing text will suggest... (#1083) * Fixes bug where pressing delete key while editing text will suggest the deletion of selected map entities * Changed the DEL key to remove entities instead of delete them * temporarily disable code climate duplication engine * add topic following for internal testing * daily map activity emails (#1081) * data prepared, task setup * add the basics of the email template * cover granular permissions * unfollow this map * break out permissions tests better * rename so test runs
2017-03-07 03:49:46 +00:00
if (_options[:follows])
json['follows'] = {
topics: following.where(followed_type: 'Topic').to_a.map(&:followed_id),
maps: following.where(followed_type: 'Map').to_a.map(&:followed_id)
}
end
if (_options[:email])
json['email'] = email
end
json
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
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
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
def all_accessible_maps
maps + shared_maps
end
def recent_metacodes
topics.order(:created_at).pluck(:metacode_id).uniq.first(5)
end
def most_used_metacodes
topics.to_a.each_with_object(Hash.new(0)) do |topic, memo|
memo[topic.metacode_id] += 1
memo
end.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
# generate a random 8 letter/digit code that they can use to invite people
def generate_code
self.code ||= rand(36**8).to_s(36)
$codes.push(self.code)
self.generation = get_generation!
end
def get_generation!
2015-11-06 09:08:42 +00:00
if code == joinedwithcode
update(generation: 0)
else
update(generation: User.find_by(code: joinedwithcode).generation + 1)
end
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?
end
def settings
# make sure we always return a UserPreference instance
self[:settings] = UserPreference.new if self[:settings].nil?
self[:settings]
end
2015-12-11 19:23:41 +00:00
def settings=(val)
self[:settings] = val
end
# 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