+ self.friends.where(:friend_user_id => new_friend.id).exists?
+ end
+
+ ##
+ # returns true if a user is visible
+ def visible?
+ ["pending","active","confirmed"].include? self.status
+ end
+
+ ##
+ # returns true if a user is active
+ def active?
+ ["active","confirmed"].include? self.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 { |b| b.needs_view? }
+ end
+
+ ##
+ # delete a user - leave the account but purge most personal data
+ def delete
+ self.display_name = "user_#{self.id}"
+ self.description = ""
+ self.home_lat = nil
+ self.home_lon = nil
+ self.image = nil
+ self.email_valid = false
+ self.new_email = nil
+ self.openid_url = nil
+ self.status = "deleted"
+ self.save
+ end
+
+ ##
+ # return a spam score for a user
+ def spam_score
+ changeset_score = self.changesets.size * 50
+ trace_score = self.traces.size * 50
+ diary_entry_score = self.diary_entries.inject(0) { |s,e| s += e.body.spam_score }
+ diary_comment_score = self.diary_comments.inject(0) { |s,c| s += c.body.spam_score }
+
+ score = self.description.spam_score / 4.0
+ score += diary_entry_score / self.diary_entries.length if self.diary_entries.length > 0
+ score += diary_comment_score / self.diary_comments.length if self.diary_comments.length > 0
+ score -= changeset_score
+ score -= trace_score
+
+ return score.to_i
+ end
+
+ ##
+ # perform a spam check on a user
+ def spam_check
+ if status == "active" and spam_score > SPAM_THRESHOLD
+ update_column(:status, "suspended")