+ def description
+ RichText.new(self[:description_format], self[:description])
+ end
+
+ def languages
+ attribute_present?(:languages) ? self[:languages].split(/ *[, ] */) : []
+ end
+
+ def languages=(languages)
+ self[:languages] = languages.join(",")
+ end
+
+ def preferred_language
+ languages.find { |l| Language.exists?(:code => l) }
+ end
+
+ def preferred_languages
+ @preferred_languages ||= Locale.list(languages)
+ end
+
+ def home_location?
+ home_lat && home_lon
+ end
+
+ def nearby(radius = Settings.nearby_radius, num = Settings.nearby_users)
+ if home_location?
+ gc = OSM::GreatCircle.new(home_lat, home_lon)
+ sql_for_area = QuadTile.sql_for_area(gc.bounds(radius), "home_")
+ sql_for_distance = gc.sql_for_distance("home_lat", "home_lon")
+ nearby = User.active.identifiable
+ .where.not(:id => id)
+ .where(sql_for_area)
+ .where("#{sql_for_distance} <= ?", radius)
+ .order(Arel.sql(sql_for_distance))
+ .limit(num)
+ else
+ nearby = []
+ end
+ nearby
+ end
+
+ def distance(nearby_user)
+ OSM::GreatCircle.new(home_lat, home_lon).distance(nearby_user.home_lat, nearby_user.home_lon)
+ end
+
+ def friends_with?(new_friend)
+ friendships.exists?(:befriendee => new_friend)
+ end
+
+ ##
+ # returns true if a user is visible
+ def visible?
+ %w[pending active confirmed].include? status
+ end
+
+ ##
+ # returns true if a user is active
+ def active?
+ %w[active confirmed].include? status
+ end
+
+ ##
+ # returns true if the user has the moderator role, false otherwise
+ def moderator?
+ role? "moderator"
+ end
+
+ ##
+ # returns true if the user has the administrator role, false otherwise
+ def administrator?
+ role? "administrator"
+ end
+
+ ##
+ # returns true if the user has the importer role, false otherwise
+ def importer?
+ role? "importer"
+ end
+
+ ##
+ # returns true if the user has the requested role
+ def role?(role)
+ roles.any? { |r| r.role == role }
+ end
+
+ ##
+ # returns the first active block which would require users to view
+ # a message, or nil if there are none.
+ def blocked_on_view
+ blocks.active.detect(&:needs_view?)
+ end
+
+ ##
+ # revoke any authentication tokens
+ def revoke_authentication_tokens
+ oauth_tokens.authorized.each(&:invalidate!)
+ access_tokens.not_expired.each(&:revoke)
+ end
+
+ ##
+ # remove personal data - leave the account but purge most personal data
+ def remove_personal_data
+ avatar.purge_later
+
+ self.display_name = "user_#{id}"
+ self.description = ""
+ self.home_lat = nil
+ self.home_lon = nil
+ self.email_valid = false
+ self.new_email = nil
+ self.auth_provider = nil
+ self.auth_uid = nil
+
+ save
+ end
+
+ ##
+ # return a spam score for a user
+ def spam_score
+ changeset_score = changesets.size * 50
+ trace_score = traces.size * 50
+ diary_entry_score = diary_entries.visible.inject(0) { |acc, elem| acc + elem.body.spam_score }
+ diary_comment_score = diary_comments.visible.inject(0) { |acc, elem| acc + elem.body.spam_score }
+
+ score = description.spam_score / 4.0
+ score += diary_entries.visible.where("created_at > ?", 1.day.ago).count * 10
+ score += diary_entry_score / diary_entries.visible.length unless diary_entries.visible.empty?
+ score += diary_comment_score / diary_comments.visible.length unless diary_comments.visible.empty?
+ score -= changeset_score
+ score -= trace_score
+
+ score.to_i
+ end
+
+ ##
+ # perform a spam check on a user
+ def spam_check
+ suspend! if may_suspend? && spam_score > Settings.spam_threshold
+ end
+
+ ##
+ # return an oauth 1 access token for a specified application
+ def access_token(application_key)
+ ClientApplication.find_by(:key => application_key).access_token_for_user(self)
+ end
+
+ ##
+ # return an oauth 2 access token for a specified application
+ def oauth_token(application_id)
+ application = Doorkeeper.config.application_model.find_by(:uid => application_id)
+
+ Doorkeeper.config.access_token_model.find_or_create_for(
+ :application => application,
+ :resource_owner => self,
+ :scopes => application.scopes
+ )
+ end
+
+ def fingerprint
+ digest = Digest::SHA256.new
+ digest.update(email)
+ digest.update(pass_crypt)
+ digest.hexdigest
+ end
+
+ def active_reports
+ issues
+ .with_status(:open)
+ .joins(:reports)
+ .where("reports.updated_at >= COALESCE(issues.resolved_at, '1970-01-01')")
+ .count
+ end
+
+ def max_messages_per_hour
+ account_age_in_seconds = Time.now.utc - created_at
+ account_age_in_hours = account_age_in_seconds / 3600
+ recent_messages = messages.where("sent_on >= ?", Time.now.utc - 3600).count
+ max_messages = account_age_in_hours.ceil + recent_messages - (active_reports * 10)
+ max_messages.clamp(0, Settings.max_messages_per_hour)
+ end
+
+ def max_friends_per_hour
+ account_age_in_seconds = Time.now.utc - created_at
+ account_age_in_hours = account_age_in_seconds / 3600
+ recent_friends = Friendship.where(:befriendee => self).where("created_at >= ?", Time.now.utc - 3600).count
+ max_friends = account_age_in_hours.ceil + recent_friends - (active_reports * 10)
+ max_friends.clamp(0, Settings.max_friends_per_hour)
+ end
+
+ def max_changeset_comments_per_hour
+ if moderator?
+ Settings.moderator_changeset_comments_per_hour
+ else
+ previous_comments = changeset_comments.limit(200).count
+ max_comments = previous_comments / 200.0 * Settings.max_changeset_comments_per_hour
+ max_comments = max_comments.floor.clamp(Settings.initial_changeset_comments_per_hour, Settings.max_changeset_comments_per_hour)
+ max_comments /= 2**active_reports
+ max_comments.floor.clamp(Settings.min_changeset_comments_per_hour, Settings.max_changeset_comments_per_hour)
+ end
+ end
+
+ def deletion_allowed_at
+ unless Settings.user_account_deletion_delay.nil?
+ last_changeset = changesets.reorder(:closed_at => :desc).first
+ return last_changeset.closed_at.utc + Settings.user_account_deletion_delay.hours if last_changeset
+ end
+ creation_time.utc
+ end
+
+ def deletion_allowed?
+ deletion_allowed_at <= Time.now.utc
+ end
+
+ private
+
+ def encrypt_password
+ if pass_crypt_confirmation
+ self.pass_crypt, self.pass_salt = PasswordHash.create(pass_crypt)
+ self.pass_crypt_confirmation = nil
+ end
+ end
+
+ def update_tile
+ self.home_tile = QuadTile.tile_for_point(home_lat, home_lon) if home_location?
+ end