diff --git a/app/models/user.rb b/app/models/user.rb index ca9a0d842..ab6f68b0a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -28,35 +28,35 @@ class User key :username, :unique => true key :serialized_private_key, String - key :invites, Integer, :default => 5 - key :invitation_token, String - key :invitation_sent_at, DateTime - key :inviter_ids, Array - key :friend_ids, Array + key :invites, Integer, :default => 5 + key :invitation_token, String + key :invitation_sent_at, DateTime + key :inviter_ids, Array + key :friend_ids, Array key :pending_request_ids, Array - key :visible_post_ids, Array - key :visible_person_ids, Array + key :visible_post_ids, Array + key :visible_person_ids, Array validates_presence_of :username one :person, :class_name => 'Person', :foreign_key => :owner_id - many :inviters, :in => :inviter_ids, :class_name => 'User' - many :friends, :in => :friend_ids, :class_name => 'Person' - many :visible_people, :in => :visible_person_ids, :class_name => 'Person' # One of these needs to go - many :pending_requests, :in => :pending_request_ids, :class_name => 'Request' - many :raw_visible_posts, :in => :visible_post_ids, :class_name => 'Post' + many :inviters, :in => :inviter_ids, :class_name => 'User' + many :friends, :in => :friend_ids, :class_name => 'Person' + many :visible_people, :in => :visible_person_ids, :class_name => 'Person' # One of these needs to go + many :pending_requests, :in => :pending_request_ids, :class_name => 'Request' + many :raw_visible_posts, :in => :visible_post_ids, :class_name => 'Post' many :aspects, :class_name => 'Aspect' after_create :seed_aspects - before_validation :downcase_username, :on => :create + before_validation :strip_and_downcase_username, :on => :create validates_with InvitedUserValidator before_destroy :unfriend_everyone, :remove_person - def self.find_for_authentication(conditions={}) + def self.find_for_authentication(conditions={}) if conditions[:username] =~ /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i # email regex conditions[:email] = conditions.delete(:username) else @@ -77,12 +77,12 @@ class User end ######### Aspects ###################### - def aspect( opts = {} ) + def aspect(opts = {}) opts[:user] = self Aspect.create(opts) end - def drop_aspect( aspect ) + def drop_aspect(aspect) if aspect.people.size == 0 aspect.destroy else @@ -90,7 +90,7 @@ class User end end - def move_friend( opts = {}) + def move_friend(opts = {}) return true if opts[:to] == opts[:from] friend = Person.first(:_id => opts[:friend_id]) if self.friend_ids.include?(friend.id) @@ -101,7 +101,7 @@ class User to_aspect.people << friend to_aspect.posts << posts_to_move from_aspect.person_ids.delete(friend.id.to_id) - posts_to_move.each{ |x| from_aspect.post_ids.delete(x.id)} + posts_to_move.each { |x| from_aspect.post_ids.delete(x.id) } from_aspect.save to_aspect.save return true @@ -114,8 +114,8 @@ class User def post(class_name, options = {}) if class_name == :photo raise ArgumentError.new("No album_id given") unless options[:album_id] - aspect_ids = aspects_with_post( options[:album_id] ) - aspect_ids.map!{ |aspect| aspect.id } + aspect_ids = aspects_with_post(options[:album_id]) + aspect_ids.map! { |aspect| aspect.id } else aspect_ids = options.delete(:to) end @@ -132,7 +132,7 @@ class User post end - def update_post( post, post_hash = {} ) + def update_post(post, post_hash = {}) if self.owns? post post.update_attributes(post_hash) end @@ -158,7 +158,7 @@ class User aspect_ids end - def build_post( class_name, options = {}) + def build_post(class_name, options = {}) options[:person] = self.person model_class = class_name.to_s.camelize.constantize post = model_class.instantiate(options) @@ -168,18 +168,18 @@ class User post end - def push_to_aspects( post, aspect_ids ) + def push_to_aspects(post, aspect_ids) if aspect_ids == :all || aspect_ids == "all" aspects = self.aspects elsif aspect_ids.is_a?(Array) && aspect_ids.first.class == Aspect aspects = aspect_ids else - aspects = self.aspects.find_all_by_id( aspect_ids ) + aspects = self.aspects.find_all_by_id(aspect_ids) end #send to the aspects target_people = [] - aspects.each{ |aspect| + aspects.each { |aspect| aspect.posts << post aspect.save target_people = target_people | aspect.people @@ -192,15 +192,15 @@ class User def push_to_people(post, people) salmon = salmon(post) - people.each{|person| + people.each { |person| xml = salmon.xml_for person - push_to_person( person, xml) + push_to_person(person, xml) } end - def push_to_person( person, xml ) + def push_to_person(person, xml) Rails.logger.debug("#{self.real_name} is adding xml to message queue to #{person.receive_url}") - QUEUE.add_post_request( person.receive_url, xml ) + QUEUE.add_post_request(person.receive_url, xml) QUEUE.process end @@ -209,7 +209,7 @@ class User QUEUE.add_hub_notification(APP_CONFIG[:pubsub_server], self.public_url) end - def salmon( post ) + def salmon(post) created_salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml) created_salmon end @@ -224,7 +224,7 @@ class User comment end - def build_comment( text, options = {}) + def build_comment(text, options = {}) raise "must comment on something!" unless options[:on] comment = Comment.new(:person_id => self.person.id, :text => text, :post => options[:on]) comment.creator_signature = comment.sign_with_key(encryption_key) @@ -236,7 +236,7 @@ class User end end - def dispatch_comment( comment ) + def dispatch_comment(comment) if owns? comment.post comment.post_creator_signature = comment.sign_with_key(encryption_key) comment.save @@ -248,9 +248,9 @@ class User end ######### Posts and Such ############### - def retract( post ) - aspect_ids = aspects_with_post( post.id ) - aspect_ids.map!{|aspect| aspect.id.to_s} + def retract(post) + aspect_ids = aspects_with_post(post.id) + aspect_ids.map! { |aspect| aspect.id.to_s } post.unsocket_from_uid(self.id, :aspect_ids => aspect_ids) if post.respond_to? :unsocket_from_uid retraction = Retraction.for(post) @@ -269,7 +269,7 @@ class User end ###Invitations############ - def invite_user( opts = {} ) + def invite_user(opts = {}) if self.invites > 0 aspect_id = opts.delete(:aspect_id) @@ -279,9 +279,9 @@ class User raise "Must invite to your aspect" end request = Request.instantiate( - :to => "http://local_request.example.com", - :from => self.person, - :into => aspect_id + :to => "http://local_request.example.com", + :from => self.person, + :into => aspect_id ) invited_user = User.invite!(:email => opts[:email], :request => request, :inviter => self) @@ -319,7 +319,7 @@ class User invitable end - def accept_invitation!( opts = {} ) + def accept_invitation!(opts = {}) if self.invited? self.username = opts[:username] self.password = opts[:password] @@ -341,7 +341,7 @@ class User end ###Helpers############ - def self.instantiate!( opts = {} ) + def self.instantiate!(opts = {}) opts[:person][:diaspora_handle] = "#{opts[:username]}@#{APP_CONFIG[:terse_pod_url]}" opts[:person][:url] = APP_CONFIG[:pod_url] @@ -359,17 +359,20 @@ class User "#{self.username}@#{APP_CONFIG[:terse_pod_url]}" end - def downcase_username - username.downcase! if username + def strip_and_downcase_username + if username.present? + username.strip! + username.downcase! + end end def as_json(opts={}) { :user => { - :posts => self.raw_visible_posts.each{|post| post.as_json}, - :friends => self.friends.each {|friend| friend.as_json}, - :aspects => self.aspects.each {|aspect| aspect.as_json}, - :pending_requests => self.pending_requests.each{|request| request.as_json}, + :posts => self.raw_visible_posts.each { |post| post.as_json }, + :friends => self.friends.each { |friend| friend.as_json }, + :aspects => self.aspects.each { |aspect| aspect.as_json }, + :pending_requests => self.pending_requests.each { |request| request.as_json }, } } end @@ -380,9 +383,9 @@ class User end def encryption_key - OpenSSL::PKey::RSA.new( serialized_private_key ) + OpenSSL::PKey::RSA.new(serialized_private_key) end - + protected def remove_person @@ -390,11 +393,11 @@ class User end def unfriend_everyone - friends.each{ |friend| + friends.each { |friend| if friend.owner? friend.owner.unfriended_by self.person - else - self.unfriend friend + else + self.unfriend friend end } end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 14a387a7e..b239b0f1a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -46,6 +46,12 @@ describe User do user.valid? user.username.should == "someuppercase" end + + it "strips leading and trailing whitespace" do + user = Factory.build(:user, :username => " janie ") + user.should be_valid + user.username.should == "janie" + end end describe "of email" do