WIP moving to mysql

This commit is contained in:
Mike Sofaer, Raphael Sofaer & Sarah Mei 2010-12-18 21:16:39 -08:00
parent 5c21e220ff
commit 3fe2924a60
16 changed files with 146 additions and 127 deletions

View file

@ -10,11 +10,11 @@ class Person < ActiveRecord::Base
require File.join(Rails.root, 'lib/diaspora/web_socket')
include Diaspora::Socketable
xml_accessor :guid
# xml_accessor :guid
# xml_accessor :diaspora_handle
xml_accessor :url
xml_accessor :profile, :as => Profile
xml_reader :exported_key
# xml_accessor :url
# xml_accessor :profile, :as => Profile
# xml_reader :exported_key
has_one :profile
delegate :last_name, :to => :profile
@ -24,6 +24,8 @@ class Person < ActiveRecord::Base
diaspora_handle.downcase!
end
has_many :aspect_memberships
belongs_to :owner, :class_name => 'User'
before_destroy :remove_all_traces

View file

@ -27,7 +27,7 @@ class Photo < Post
end
end
def self.instantiate(params = {})
def self.diaspora_initialize(params = {})
photo = super(params)
image_file = params.delete(:user_file)
photo.random_string = gen_random_string(10)

View file

@ -9,10 +9,10 @@ class Post < ActiveRecord::Base
include ROXML
include Diaspora::Webhooks
xml_reader :guid
xml_reader :diaspora_handle
xml_reader :public
xml_reader :created_at
#xml_accessor :guid
#xml_accessor :diaspora_handle
#xml_accessor :public
#xml_accessor :created_at
has_many :comments, :order => 'created_at ASC'
has_and_belongs_to_many :aspects
@ -26,7 +26,7 @@ class Post < ActiveRecord::Base
attr_accessible :user_refs
def self.instantiate params
def self.diaspora_initialize params
new_post = self.new params.to_hash
new_post.person = params[:person]
params[:aspect_ids].each do |aspect_id|

View file

@ -32,7 +32,7 @@ class Request
}
def self.instantiate(opts = {})
def self.diaspora_initialize(opts = {})
self.new(:from => opts[:from],
:to => opts[:to],
:into => opts[:into])

View file

@ -45,7 +45,7 @@ class StatusMessage < Post
protected
def message_or_photos_present?
if self.message.blank? && self.photos.count == 0
if self.message.blank? && self.photos == []
errors[:base] << 'Status message requires a message or at least one photo'
end
end

View file

@ -30,6 +30,7 @@ class User < ActiveRecord::Base
validates_associated :person
has_one :person, :foreign_key => :owner_id
delegate :diaspora_handle, :to => :person
has_many :invitations_from_me, :class_name => 'Invitation', :foreign_key => :sender_id
has_many :invitations_to_me, :class_name => 'Invitation', :foreign_key => :recipient_id
@ -110,7 +111,7 @@ class User < ActiveRecord::Base
opts[:diaspora_handle] = opts[:person].diaspora_handle
model_class = class_name.to_s.camelize.constantize
model_class.instantiate(opts)
model_class.diaspora_initialize(opts)
end
def dispatch_post(post, opts = {})
@ -143,9 +144,6 @@ class User < ActiveRecord::Base
end
def add_to_streams(post, aspect_ids)
self.raw_visible_posts << post
self.save
post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to? :socket_to_uid
target_aspects = aspects_from_ids(aspect_ids)
target_aspects.each do |aspect|
@ -158,11 +156,8 @@ class User < ActiveRecord::Base
if aspect_ids == "all" || aspect_ids == :all
self.aspects
else
if aspect_ids.respond_to? :to_id
aspect_ids = [aspect_ids]
end
aspect_ids.map!{ |x| x.to_id }
aspects.all(:id.in => aspect_ids)
aspect_ids = aspect_ids.to_a
aspects.where(:id => aspect_ids)
end
end
@ -170,10 +165,11 @@ class User < ActiveRecord::Base
#send to the aspects
target_aspect_ids = aspects.map {|a| a.id}
target_contacts = Contact.all(:aspect_ids.in => target_aspect_ids, :pending => false)
target_people = Person.includes(:aspect_memberships).where(
"aspect_memberships.aspect_id" => target_aspect_ids, "aspect_memberships.pending" => false)
post_to_hub(post) if post.respond_to?(:public) && post.public
push_to_people(post, self.person_objects(target_contacts))
push_to_people(post, target_people)
end
def push_to_people(post, people)

View file

@ -10,6 +10,10 @@ module Diaspora
self.raw_visible_posts.find id.to_id
end
def raw_visible_posts
Post.joins(:aspects).where(:aspects => {:user_id => self.id})
end
def visible_posts( opts = {} )
opts[:order] ||= 'created_at DESC'
opts[:pending] ||= false

View file

@ -31,10 +31,10 @@ Factory.define :user do |u|
u.password_confirmation { |u| u.password }
u.serialized_private_key OpenSSL::PKey::RSA.generate(1024).export
u.after_build do |user|
user.person = Factory.build(:person, :profile => Factory.create(:profile),
:owner_id => user.id,
:serialized_public_key => user.encryption_key.public_key.export,
:diaspora_handle => "#{user.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}")
user.person = Factory.build(:person, :profile => Factory.create(:profile),
:owner_id => user.id,
:serialized_public_key => user.encryption_key.public_key.export,
:diaspora_handle => "#{user.username}@#{APP_CONFIG[:pod_url].gsub(/(https?:|www\.)\/\//, '').chop!}")
end
end

View file

@ -50,7 +50,7 @@ describe Notification do
end
it ' does not call Notification.create if the object does not notification_type' do
request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)
request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect)
Notification.should_receive(:create).once
Notification.notify(@user, request, @person)
end

View file

@ -43,13 +43,13 @@ describe Photo do
@photo2.random_string.should_not be nil
end
describe '#instantiate' do
describe '#diaspora_initialize' do
it 'sets the persons diaspora handle' do
@photo2.diaspora_handle.should == @user.person.diaspora_handle
end
it 'has a constructor' do
image = File.open(@fixture_name)
photo = Photo.instantiate(
photo = Photo.diaspora_initialize(
:person => @user.person, :user_file => image)
photo.created_at.nil?.should be_true
photo.image.read.nil?.should be_false

View file

@ -15,7 +15,7 @@ describe Request do
describe 'validations' do
before do
@request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)
@request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect)
end
it 'is valid' do
@request.should be_valid
@ -41,18 +41,18 @@ describe Request do
end
it 'is not a duplicate of an existing pending request' do
@request.save
duplicate_request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)
duplicate_request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect)
duplicate_request.should_not be_valid
end
it 'is not to yourself' do
@request = Request.instantiate(:from => @user.person, :to => @user.person, :into => @aspect)
@request = Request.diaspora_initialize(:from => @user.person, :to => @user.person, :into => @aspect)
@request.should_not be_valid
end
end
describe 'scopes' do
before do
@request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)
@request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect)
@request.save
end
describe '.from' do
@ -82,7 +82,7 @@ describe Request do
end
describe '#notification_type' do
before do
@request = Request.instantiate(:from => @user.person, :to => @user2.person, :into => @aspect)
@request = Request.diaspora_initialize(:from => @user.person, :to => @user2.person, :into => @aspect)
end
it "returns 'request_accepted' if there is a pending contact" do
Contact.create(:user_id => @user.id, :person_id => @person.id)

View file

@ -26,7 +26,8 @@ describe StatusMessage do
photo.save!
n.photos << photo
n.valid?.should be_true
n.valid?
n.errors.full_messages.should == []
end
it 'should be postable through the user' do

View file

@ -48,7 +48,7 @@ describe Diaspora::UserModules::Connecting do
context 'contact requesting' do
describe '#receive_contact_request' do
before do
@r = Request.instantiate(:to => user.person, :from => person)
@r = Request.diaspora_initialize(:to => user.person, :from => person)
end
it 'adds a request to pending if it was not sent by user' do
@ -89,9 +89,9 @@ describe Diaspora::UserModules::Connecting do
context 'received a contact request' do
let(:request_for_user) {Request.instantiate(:to => user.person, :from => person)}
let(:request2_for_user) {Request.instantiate(:to => user.person, :from => person_one)}
let(:request_from_myself) {Request.instantiate(:to => user.person, :from => user.person)}
let(:request_for_user) {Request.diaspora_initialize(:to => user.person, :from => person)}
let(:request2_for_user) {Request.diaspora_initialize(:to => user.person, :from => person_one)}
let(:request_from_myself) {Request.diaspora_initialize(:to => user.person, :from => user.person)}
before do
user.receive(request_for_user.to_diaspora_xml, person)
@received_request = Request.from(person).to(user.person).first
@ -126,9 +126,9 @@ describe Diaspora::UserModules::Connecting do
Request.to(user2).count.should == 0
user2.contacts.empty?.should be true
@request = Request.instantiate(:to => user.person, :from => person_one)
@request_two = Request.instantiate(:to => user2.person, :from => person_one)
@request_three = Request.instantiate(:to => user2.person, :from => user.person)
@request = Request.diaspora_initialize(:to => user.person, :from => person_one)
@request_two = Request.diaspora_initialize(:to => user2.person, :from => person_one)
@request_three = Request.diaspora_initialize(:to => user2.person, :from => user.person)
@req_xml = @request.to_diaspora_xml
@req_two_xml = @request_two.to_diaspora_xml
@ -197,8 +197,8 @@ describe Diaspora::UserModules::Connecting do
describe 'a user accepting rejecting multiple people' do
before do
@request = Request.instantiate(:to => user.person, :from => person_one)
@request_two = Request.instantiate(:to => user.person, :from => person_two)
@request = Request.diaspora_initialize(:to => user.person, :from => person_one)
@request_two = Request.diaspora_initialize(:to => user.person, :from => person_two)
end
it "keeps the right counts of contacts" do

View file

@ -129,7 +129,7 @@ describe User do
let!(:aspect4) { user4.aspects.create(:name => 'heroes') }
let!(:post) { user.build_post :status_message, :message => "hey" }
let!(:request) { Request.instantiate(:from => user3.person, :to => user4.person) }
let!(:request) { Request.diaspora_initialize(:from => user3.person, :to => user4.person) }
before do
connect_users(user, aspect, user2, aspect2)

View file

@ -6,141 +6,157 @@ require 'spec_helper'
describe User do
let(:user) {make_user}
let!(:aspect) { user.aspects.create(:name => "cats")}
let!(:user2) { Factory(:user_with_aspect) }
let!(:aspect2) { user2.aspects.first }
before do
@user = Factory(:user)
@aspect = @user.aspects.create(:name => "cats")
@user2 = Factory(:user_with_aspect)
@aspect2 = @user2.aspects.first
let(:person_one) { Factory.create :person }
let(:person_two) { Factory.create :person }
let(:person_three) { Factory.create :person }
@person_one = Factory.create :person
@person_two = Factory.create :person
@person_three = Factory.create :person
end
describe "#raw_visible_posts" do
it "returns all the posts the user can see" do
@user2.add_contact_to_aspect(@user.person, @aspect2)
self_post = @user.post(:status_message, :message => "hi", :to => @aspect.id)
visible_post = @user2.post(:status_message, :message => "hello", :to => @aspect2.id)
dogs = @user2.aspects.create(:name => "dogs")
invisible_post = @user2.post(:status_message, :message => "foobar", :to => dogs.id)
stream = @user.raw_visible_posts
stream.should include(self_post)
stream.should include(visible_post)
stream.should_not include(invisible_post)
end
end
context 'with two posts' do
let!(:status_message1) { user2.post :status_message, :message => "hi", :to => aspect2.id }
let!(:status_message2) { user2.post :status_message, :message => "hey", :public => true , :to => aspect2.id }
let!(:status_message4) { user2.post :status_message, :message => "blah", :public => true , :to => aspect2.id }
let!(:status_message3) { user.post :status_message, :message => "hey", :public => true , :to => aspect.id }
let!(:status_message1) { @user2.post :status_message, :message => "hi", :to => @aspect2.id }
let!(:status_message2) { @user2.post :status_message, :message => "hey", :public => true , :to => @aspect2.id }
let!(:status_message4) { @user2.post :status_message, :message => "blah", :public => true , :to => @aspect2.id }
let!(:status_message3) { @user.post :status_message, :message => "hey", :public => true , :to => @aspect.id }
let!(:pending_status_message) { user2.post :status_message, :message => "hey", :public => true , :to => aspect2.id, :pending => true }
let!(:pending_status_message) { @user2.post :status_message, :message => "hey", :public => true , :to => @aspect2.id, :pending => true }
describe "#visible_posts" do
it "queries by person id" do
query = user2.visible_posts(:person_id => user2.person.id)
query = @user2.visible_posts(:person_id => @user2.person.id)
query.include?(status_message1).should == true
query.include?(status_message2).should == true
end
it "selects public posts" do
query = user2.visible_posts(:public => true)
query = @user2.visible_posts(:public => true)
query.include?(status_message2).should == true
query.include?(status_message1).should == false
end
it "selects non public posts" do
query = user2.visible_posts(:public => false)
query = @user2.visible_posts(:public => false)
query.include?(status_message1).should == true
query.include?(status_message2).should == false
end
it "selects by message contents" do
user2.visible_posts(:message => "hi").include?(status_message1).should == true
@user2.visible_posts(:message => "hi").include?(status_message1).should == true
end
it "does not return pending posts" do
pending_status_message.pending.should be_true
user2.visible_posts.should_not include pending_status_message
@user2.visible_posts.should_not include pending_status_message
user2.visible_posts(:by_members_of => aspect2).should_not include pending_status_message
@user2.visible_posts(:by_members_of => @aspect2).should_not include pending_status_message
end
context 'with two users' do
let!(:first_aspect) {user.aspects.create(:name => 'bruisers')}
let!(:second_aspect) {user.aspects.create(:name => 'losers')}
let!(:first_aspect) {@user.aspects.create(:name => 'bruisers')}
let!(:second_aspect) {@user.aspects.create(:name => 'losers')}
it "queries by aspect" do
connect_users(user, first_aspect, user2, user2.aspects.first)
user.receive status_message1.to_diaspora_xml, user2.person
connect_users(@user, first_aspect, @user2, @user2.aspects.first)
@user.receive status_message1.to_diaspora_xml, @user2.person
user.visible_posts(:by_members_of => first_aspect).should =~ [status_message1]
user.visible_posts(:by_members_of => second_aspect).should =~ []
@user.visible_posts(:by_members_of => first_aspect).should =~ [status_message1]
@user.visible_posts(:by_members_of => second_aspect).should =~ []
end
it '#find_visible_post_by_id' do
user2.find_visible_post_by_id(status_message1.id).should == status_message1
user.find_visible_post_by_id(status_message1.id).should == nil
@user2.find_visible_post_by_id(status_message1.id).should == status_message1
@user.find_visible_post_by_id(status_message1.id).should == nil
end
end
end
describe '#my_posts' do
it 'should return only my posts' do
posts2 = user2.my_posts
posts2 = @user2.my_posts
posts2.should include status_message1
posts2.should include status_message2
posts2.should_not include status_message3
user.my_posts.should include status_message3
@user.my_posts.should include status_message3
end
it 'returns query objexts so chainable' do
user2.my_posts.where(:_id => status_message1.id.to_s).all.should == [status_message1]
@user2.my_posts.where(:_id => status_message1.id.to_s).all.should == [status_message1]
pub_posts = user2.my_posts.where(:public => true).all
pub_posts = @user2.my_posts.where(:public => true).all
pub_posts.should_not include status_message1
pub_posts.should include status_message2
pub_posts.should include status_message4
pub_posts.should_not include status_message3
user.my_posts.where(:public => false).all.should == []
@user.my_posts.where(:public => false).all.should == []
end
end
end
context 'with two users' do
let!(:user) {make_user}
let!(:user) {Factory(:user)}
let!(:first_aspect) {user.aspects.create(:name => 'bruisers')}
let!(:second_aspect) {user.aspects.create(:name => 'losers')}
let!(:user4) { Factory.create(:user_with_aspect)}
before do
connect_users(user, first_aspect, user4, user4.aspects.first)
connect_users(user, second_aspect, user2, user2.aspects.first)
connect_users(user, second_aspect, @user2, @user2.aspects.first)
end
describe '#contacts_not_in_aspect' do
it 'finds the people who are not in the given aspect' do
people = user.contacts_not_in_aspect(first_aspect)
people.should == [user2.person]
people = @user.contacts_not_in_aspect(first_aspect)
people.should == [@user2.person]
end
end
describe '#person_objects' do
it 'returns "person" objects for all of my contacts' do
people = user.person_objects
people = @user.person_objects
people.size.should == 2
[user4.person, user2.person].each{ |p| people.should include p }
[user4.person, @user2.person].each{ |p| people.should include p }
end
it 'should return people objects given a collection of contacts' do
target_contacts = [user.contact_for(user2.person)]
people = user.person_objects(target_contacts)
people.should == [user2.person]
target_contacts = [@user.contact_for(user2.person)]
people = @user.person_objects(target_contacts)
people.should == [@user2.person]
end
end
describe '#people_in_aspects' do
it 'returns people objects for a users contact in each aspect' do
people = user.people_in_aspects([first_aspect])
people = @user.people_in_aspects([first_aspect])
people.should == [user4.person]
people = user.people_in_aspects([second_aspect])
people.should == [user2.person]
people = @user.people_in_aspects([second_aspect])
people.should == [@user2.person]
end
it 'returns local/remote people objects for a users contact in each aspect' do
local_user1 = make_user
local_user2 = make_user
remote_user = make_user
local_user1 = Factory(:user)
local_user2 = Factory(:user)
remote_user = Factory(:user)
asp1 = local_user1.aspects.create(:name => "lol")
asp2 = local_user2.aspects.create(:name => "brb")
@ -155,17 +171,17 @@ describe User do
local_person.save
local_person.reload
user.people_in_aspects([first_aspect]).count.should == 4
user.people_in_aspects([first_aspect], :type => 'remote').count.should == 1
user.people_in_aspects([first_aspect], :type => 'local').count.should == 3
@user.people_in_aspects([first_aspect]).count.should == 4
@user.people_in_aspects([first_aspect], :type => 'remote').count.should == 1
@user.people_in_aspects([first_aspect], :type => 'local').count.should == 3
end
it 'does not return people not connected to user on same pod' do
local_user1 = make_user
local_user2 = make_user
local_user3 = make_user
local_user1 = Factory(:user)
local_user2 = Factory(:user)
local_user3 = Factory(:user)
user.people_in_aspects([first_aspect]).count.should == 1
@user.people_in_aspects([first_aspect]).count.should == 1
end
end
end
@ -174,84 +190,84 @@ describe User do
let(:person_one) { Factory.create :person }
let(:person_two) { Factory.create :person }
let(:person_three) { Factory.create :person }
let(:aspect) { user.aspects.create(:name => 'heroes') }
let(:aspect) { @user.aspects.create(:name => 'heroes') }
describe '#contact_for_person_id' do
it 'returns a contact' do
contact = Contact.create(:user => user, :person => person_one, :aspects => [aspect])
user.contacts << contact
user.contact_for_person_id(person_one.id).should be_true
contact = Contact.create(:user => @user, :person => person_one, :aspects => [aspect])
@user.contacts << contact
@user.contact_for_person_id(person_one.id).should be_true
end
it 'returns the correct contact' do
contact = Contact.create(:user => user, :person => person_one, :aspects => [aspect])
user.contacts << contact
contact = Contact.create(:user => @user, :person => person_one, :aspects => [aspect])
@user.contacts << contact
contact2 = Contact.create(:user => user, :person => person_two, :aspects => [aspect])
user.contacts << contact2
contact2 = Contact.create(:user => @user, :person => person_two, :aspects => [aspect])
@user.contacts << contact2
contact3 = Contact.create(:user => user, :person => person_three, :aspects => [aspect])
user.contacts << contact3
contact3 = Contact.create(:user => @user, :person => person_three, :aspects => [aspect])
@user.contacts << contact3
user.contact_for_person_id(person_two.id).person.should == person_two
@user.contact_for_person_id(person_two.id).person.should == person_two
end
it 'returns nil for a non-contact' do
user.contact_for_person_id(person_one.id).should be_nil
@user.contact_for_person_id(person_one.id).should be_nil
end
it 'returns nil when someone else has contact with the target' do
contact = Contact.create(:user => user, :person => person_one, :aspects => [aspect])
user.contacts << contact
user2.contact_for_person_id(person_one.id).should be_nil
contact = Contact.create(:user => @user, :person => person_one, :aspects => [aspect])
@user.contacts << contact
@user2.contact_for_person_id(person_one.id).should be_nil
end
end
describe '#contact_for' do
it 'takes a person_id and returns a contact' do
user.should_receive(:contact_for_person_id).with(person_one.id)
user.contact_for(person_one)
@user.should_receive(:contact_for_person_id).with(person_one.id)
@user.contact_for(person_one)
end
end
end
describe "#request_for" do
let!(:user5) {make_user}
let!(:user5) {Factory(:user)}
it 'should not have a pending request before connecting' do
request = user.request_for(user5.person)
request = @user.request_for(user5.person)
request.should be_nil
end
it 'should have a pending request after sending a request' do
user.send_contact_request_to(user5.person, user.aspects.first)
request = user.reload.request_for(user5.person)
@user.send_contact_request_to(user5.person, @user.aspects.first)
request = @user.reload.request_for(user5.person)
request.should_not be_nil
end
end
describe '#posts_from' do
let!(:user3) {make_user}
let!(:user3) {Factory(:user)}
let!(:aspect3) {user3.aspects.create(:name => "bros")}
let!(:public_message) {user3.post(:status_message, :message => "hey there", :to => 'all', :public => true)}
let!(:private_message) {user3.post(:status_message, :message => "hey there", :to => aspect3.id)}
it 'displays public posts for a non-contact' do
user.posts_from(user3.person).should include public_message
@user.posts_from(user3.person).should include public_message
end
it 'does not display private posts for a non-contact' do
user.posts_from(user3.person).should_not include private_message
@user.posts_from(user3.person).should_not include private_message
end
it 'displays private and public posts for a non-contact after connecting' do
connect_users(user, aspect, user3, aspect3)
new_message = user3.post(:status_message, :message => "hey there", :to => aspect3.id)
user.reload
@user.reload
user.posts_from(user3.person).should include public_message
user.posts_from(user3.person).should include new_message
@user.posts_from(user3.person).should include public_message
@user.posts_from(user3.person).should include new_message
end
end
end

View file

@ -267,7 +267,7 @@ describe User do
fixture_filename = 'button.png'
fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', fixture_filename)
image = File.open(fixture_name)
@photo = Photo.instantiate(
@photo = Photo.diaspora_initialize(
:person => user.person, :user_file => image)
@photo.save!
@params = {:photo => @photo}