Newer
Older
require 'lib/diaspora/user/friending.rb'
class User
include MongoMapper::Document
include Diaspora::UserModules::Friending
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
danielvincent
a validé
key :friend_ids, Array
maxwell
a validé
key :_post_ids, Array
danielvincent
a validé
one :person, :class_name => 'Person', :foreign_key => :owner_id
danielvincent
a validé
many :friends, :in => :friend_ids, :class_name => 'Person'
many :pending_requests, :in => :pending_request_ids, :class_name => 'Request'
many :raw_visible_posts, :in => :visible_post_ids, :class_name => 'Post'
danielvincent
a validé
many :groups, :class_name => 'Group'
before_validation_on_create :setup_person
before_validation :do_bad_things
def method_missing(method, *args)
self.person.send(method, *args)
end
maxwell
a validé
def real_name
"#{person.profile.first_name.to_s} #{person.profile.last_name.to_s}"
######### Groups ######################
def group( opts = {} )
opts[:user] = self
Group.create(opts)
end
maxwell
a validé
def post(class_name, options = {})
options[:person] = self.person
raise "You must post to someone." if group_ids.nil? || group_ids.empty?
group_ids = [group_ids] unless group_ids.is_a? Array
group_ids.map!{|gid| ensure_bson gid }
options.delete(:to)
model_class = class_name.to_s.camelize.constantize
post = model_class.instantiate(options)
post.creator_signature = post.sign_with_key(encryption_key)
groups = self.groups.find_all_by_id( group_ids )
group.posts << post
post.socket_to_uid(id, :group_ids => group_ids) if post.respond_to?(:socket_to_uid)
self.raw_visible_posts << post
self.save
def visible_posts( opts = {} )
if opts[:by_members_of]
return raw_visible_posts if opts[:by_members_of] == :all
group = self.groups.find_by_id( opts[:by_members_of].id )
group.posts
######## Commenting ########
def 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)
if comment.save
dispatch_comment comment
comment.socket_to_uid id
comment
Rails.logger.warn "this failed to save: #{comment.inspect}"
def dispatch_comment( comment )
if owns? comment.post
comment.post_creator_signature = comment.sign_with_key(encryption_key)
comment.save
comment.push_downstream
elsif owns? comment
comment.save
comment.push_upstream
######### Posts and Such ###############
Raphael
a validé
def retract( post )
post.unsocket_from_uid(self.id) if post.respond_to? :unsocket_from_uid
Raphael
a validé
retraction = Retraction.for(post)
retraction.creator_signature = retraction.sign_with_key( encryption_key )
retraction.push_to( self.friends.all )
Raphael
a validé
retraction
end
########### Profile ######################
def update_profile(params)
if self.person.update_attributes(params)
self.profile.push_to( self.friends.all )
true
else
false
end
end
###### Receiving #######
def receive xml
object = Diaspora::Parser.from_xml(xml)
Rails.logger.debug("Receiving object:\n#{object.inspect}")
raise "Signature was not valid on: #{object.inspect}" unless object.signature_valid?
if object.type == 'Person' && object.signature_valid?
Rails.logger.info( "the person id is #{object.post_id} the friend found is #{visible_person_by_id(object.post_id).inspect}")
unfriended_by visible_person_by_id(object.post_id)
groups = self.groups_with_person(object.person)
groups.each{ |group| group.post_ids.delete(ensure_bson(object.post_id))
group.save
}
person = Diaspora::Parser.get_or_create_person_object_from_xml( xml )
person.serialized_key ||= object.exported_key
object.person = person
object.person.save
ilya
a validé
old_request = Request.first(:id => object.id)
object.group_id = old_request.group_id if old_request
receive_friend_request(object)
elsif object.is_a? Profile
person = Diaspora::Parser.owner_id_from_xml xml
dispatch_comment object unless owns?(object)
Rails.logger.debug("Saving object: #{object}")
object.user_refs += 1
self.raw_visible_posts << object
self.save
groups = self.groups_with_person(object.person)
groups.each{ |group|
group.posts << object
group.save
object.socket_to_uid(id, :group_id => group.id) if (object.respond_to?(:socket_to_uid) && !self.owns?(object))
###Helpers############
def self.instantiate!( opts = {} )
opts[:person][:email] = opts[:email]
opts[:person][:serialized_key] = generate_key
User.create!( opts)
def terse_url
terse= self.url.gsub(/https?:\/\//, '')
terse.gsub!(/www\./, '')
terse = terse.chop! if terse[-1, 1] == '/'
terse
end
def do_bad_things
self.password_confirmation = self.password
end
id = ensure_bson id
return self.person if id == self.person.id
friends.detect{|x| x.id == id }
end
def group_by_id( id )
id = ensure_bson id
groups.detect{|x| x.id == id }
end
def album_by_id( id )
id = ensure_bson id
albums.detect{|x| x.id == id }
end
def groups_with_person person
maxwell
a validé
id = ensure_bson person.object_id
groups.select {|group| group.person_ids.include? id}
def setup_person
self.person.serialized_key ||= generate_key.export
self.person.email ||= email
self.person.save!
end
def generate_key
OpenSSL::PKey::RSA::generate 1024
end
OpenSSL::PKey::RSA::generate 1024