+ el1 = XML::Node.new "user"
+ el1["display_name"] = display_name.to_s
+ el1["account_created"] = creation_time.xmlschema
+ if home_lat && home_lon
+ home = XML::Node.new "home"
+ home["lat"] = home_lat.to_s
+ home["lon"] = home_lon.to_s
+ home["zoom"] = home_zoom.to_s
+ el1 << home
+ end
+ el1
+ end
+
+ def description
+ RichText.new(read_attribute(:description_format), read_attribute(:description))
+ end
+
+ def languages
+ attribute_present?(:languages) ? read_attribute(:languages).split(/ *, */) : []
+ end
+
+ def languages=(languages)
+ write_attribute(:languages, languages.join(","))
+ end
+
+ def preferred_language
+ languages.find { |l| Language.exists?(:code => l) }
+ end
+
+ def preferred_language_from(array)
+ (languages & array.collect(&:to_s)).first
+ end
+
+ def nearby(radius = NEARBY_RADIUS, num = NEARBY_USERS)
+ if home_lon && home_lat
+ gc = OSM::GreatCircle.new(home_lat, home_lon)
+ sql_for_distance = gc.sql_for_distance("home_lat", "home_lon")
+ nearby = User.where("id != ? AND status IN (\'active\', \'confirmed\') AND data_public = ? AND #{sql_for_distance} <= ?", id, true, radius).order(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 is_friends_with?(new_friend)
+ friends.where(:friend_user_id => new_friend.id).exists?
+ 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?
+ has_role? "moderator"
+ end
+
+ ##
+ # returns true if the user has the administrator role, false otherwise
+ def administrator?
+ has_role? "administrator"
+ end
+
+ ##
+ # returns true if the user has the requested role
+ def has_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?)