Merge branch 'master' of git://github.com/diaspora/diaspora

This commit is contained in:
Jordi Mallach 2011-03-30 17:29:33 +02:00
commit 8b4d17a65a
52 changed files with 440 additions and 326 deletions

View file

@ -30,10 +30,7 @@ class ApisController < ApplicationController #We should start with this versione
def home_timeline
set_defaults
aspect_ids = current_user.aspects.map{|a| a.id}
timeline = StatusMessage.joins(:aspects).where(:pending => false,
:aspects => {:id => aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate(
timeline = current_user.raw_visible_posts.includes(:comments, :photos, :likes, :dislikes).paginate(
:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
respond_with timeline do |format|

View file

@ -26,9 +26,8 @@ class AspectsController < ApplicationController
@selected_contacts = @aspects.map { |aspect| aspect.contacts }.flatten.uniq
@aspect_ids = @aspects.map { |a| a.id }
@posts = StatusMessage.joins(:aspects).where(:pending => false,
:aspects => {:id => @aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate(
:page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC')
@posts = current_user.raw_visible_posts(:by_members_of => @aspect_ids, :type => 'StatusMessage', :order => session[:sort_order] + ' DESC', :page => params[:page]).includes(
:comments, :mentions, :likes, :dislikes).paginate(:page => params[:page], :per_page => 15, :order => session[:sort_order] + ' DESC')
@fakes = PostsFake.new(@posts)
@contact_count = current_user.contacts.count
@ -79,7 +78,7 @@ class AspectsController < ApplicationController
@aspect = current_user.aspects.where(:id => params[:id]).first
begin
current_user.drop_aspect @aspect
@aspect.destroy
flash[:notice] = I18n.t 'aspects.destroy.success', :name => @aspect.name
redirect_to aspects_path
rescue ActiveRecord::StatementInvalid => e

View file

@ -28,9 +28,7 @@ class PhotosController < ApplicationController
@contacts_of_contact = []
end
@posts = current_user.visible_photos.where(
:author_id => @person.id
).paginate(:page => params[:page])
@posts = current_user.posts_from(@person).where(:type => 'Photo').paginate(:page => params[:page])
render 'people/show'
@ -141,10 +139,10 @@ class PhotosController < ApplicationController
end
def show
@photo = current_user.visible_photos.where(:id => params[:id]).includes(:author, :status_message => :photos).first
@photo ||= Photo.where(:public => true, :id => params[:id]).includes(:author, :status_message => :photos).first
@photo = current_user.visible_photos.where(:id => params[:id]).first
@photo ||= Photo.where(:public => true, :id => params[:id]).first
if @photo
@parent = @photo.status_message
@parent = StatusMessage.where(:id => @photo.status_message_id).includes(:photos).first if @photo.status_message_id
#if photo is not an attachment, fetch comments for self
if @parent

View file

@ -8,11 +8,19 @@ class ProfilesController < ApplicationController
@person = current_user.person
@aspect = :person_edit
@profile = @person.profile
@tags = @profile.tags
@tags_array = []
@tags.each do |obj|
@tags_array << { :name => ("#"+obj.name),
:value => ("#"+obj.name)}
end
end
def update
# upload and set new profile photo
# upload and set new profile photo
params[:profile] ||= {}
params[:profile][:tag_string] = (params[:tags]) ? params[:tags].gsub(',',' ') : ""
params[:profile][:searchable] ||= false
params[:profile][:photo] = Photo.where(:author_id => current_user.person.id,
:id => params[:photo_id]).first if params[:photo_id]

View file

@ -66,7 +66,7 @@ class StatusMessagesController < ApplicationController
end
if request.env['HTTP_REFERER'].include?("people")
flash[:notice] = t('.success', :names => @status_message.mentions.includes(:person => :profile).map{ |mention| mention.person.name }.join(', '))
flash[:notice] = t('status_messages.create.success', :names => @status_message.mentions.includes(:person => :profile).map{ |mention| mention.person.name }.join(', '))
end
respond_to do |format|

View file

@ -8,6 +8,33 @@ class TagsController < ApplicationController
skip_before_filter :which_action_and_user
skip_before_filter :set_grammatical_gender
respond_to :html, :only => [:show]
respond_to :json, :only => [:index]
def index
params[:q].gsub!("#", "")
params[:limit] = !params[:limit].blank? ? params[:limit].to_i : 10
if params[:q].length > 1
@tags = ActsAsTaggableOn::Tag.named_like(params[:q]).limit(params[:limit] - 1)
@array = []
@tags.each do |obj|
@array << { :name => ("#"+obj.name),
:value => ("#"+obj.name)}
end
@array << { :name => ('#' + params[:q]), :value => ("#" + params[:q])}
@array.uniq!
respond_to do |format|
format.json{
render(:json => @array.to_json, :status => 201)
}
end
else
render :nothing => true
end
end
def show
if current_user
@posts = StatusMessage.joins(:aspects).where(:pending => false

View file

@ -21,13 +21,13 @@ module ApplicationHelper
def aspects_with_post aspects, post
aspects.select do |aspect|
PostVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
end
end
def aspects_without_post aspects, post
aspects.reject do |aspect|
PostVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
end
end

View file

@ -48,7 +48,7 @@ module SocketsHelper
v = render_to_string(:partial => 'people/person', :locals => person_hash)
elsif object.is_a? Comment
v = render_to_string(:partial => 'comments/comment', :locals => {:comment => object, :person => object.author, :current_user => user})
v = render_to_string(:partial => 'comments/comment', :locals => {:post => object.post, :comment => object, :person => object.author, :current_user => user})
elsif object.is_a? Like
v = render_to_string(:partial => 'likes/likes', :locals => {:likes => object.post.likes, :dislikes => object.post.dislikes})

View file

@ -8,8 +8,8 @@ class Aspect < ActiveRecord::Base
has_many :aspect_memberships
has_many :contacts, :through => :aspect_memberships
has_many :post_visibilities
has_many :posts, :through => :post_visibilities
has_many :aspect_visibilities
has_many :posts, :through => :aspect_visibilities
validates_presence_of :name
validates_length_of :name, :maximum => 20

View file

@ -11,7 +11,6 @@ class AspectMembership < ActiveRecord::Base
before_destroy :ensure_membership
def ensure_membership
if self.contact.aspect_memberships.count == 1
errors[:base] << I18n.t('shared.contact_list.cannot_remove')

View file

@ -0,0 +1,13 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class AspectVisibility < ActiveRecord::Base
belongs_to :aspect
validates_presence_of :aspect
belongs_to :post
validates_presence_of :post
end

View file

@ -13,6 +13,10 @@ class Contact < ActiveRecord::Base
has_many :aspect_memberships
has_many :aspects, :through => :aspect_memberships
has_many :post_visibilities
has_many :posts, :through => :post_visibilities
validate :not_contact_for_self
validates_uniqueness_of :person_id, :scope => :user_id
@ -28,6 +32,11 @@ class Contact < ActiveRecord::Base
:aspect => aspects.first)
end
def receive_post(post)
PostVisibility.create!(:post_id => post.id, :contact_id => self.id)
post.socket_to_user(self.user, :aspect_ids => self.aspect_ids) if post.respond_to? :socket_to_user
end
def contacts
people = Person.arel_table
incoming_aspects = Aspect.joins(:contacts).where(

View file

@ -15,14 +15,13 @@ module Job
notify_mentioned_users(post)
end
def self.create_visibilities(post, recipient_user_ids)
aspects = Aspect.where(:user_id => recipient_user_ids).joins(:contacts).where(:contacts => {:person_id => post.author_id}).select('aspects.id, aspects.user_id')
Rails.logger.info(:event => :rlb_aspects, :aspect_ids => aspects.map{|a| a.id}.join(','))
aspects.each do |aspect|
contacts = Contact.where(:user_id => recipient_user_ids, :person_id => post.author_id)
contacts.each do |contact|
begin
PostVisibility.create(:aspect_id => aspect.id, :post_id => post.id)
PostVisibility.create(:contact_id => contact.id, :post_id => post.id)
rescue ActiveRecord::RecordNotUnique => e
Rails.logger.info(:event => :unexpected_pv, :aspect_id => aspect.id, :post_id => post.id)
#The post was already visible to that aspect
Rails.logger.info(:event => :unexpected_pv, :contact_id => contact.id, :post_id => post.id)
#The post was already visible to that user
end
end
end

View file

@ -16,16 +16,25 @@ class Post < ActiveRecord::Base
has_many :comments, :order => 'created_at ASC'
has_many :likes, :conditions => '`likes`.`positive` = 1', :dependent => :delete_all
has_many :dislikes, :conditions => '`likes`.`positive` = 0', :class_name => 'Like', :dependent => :delete_all
has_many :aspect_visibilities
has_many :aspects, :through => :aspect_visibilities
has_many :post_visibilities
has_many :aspects, :through => :post_visibilities
has_many :contacts, :through => :post_visibilities
has_many :mentions, :dependent => :destroy
belongs_to :author, :class_name => 'Person'
cattr_reader :per_page
@@per_page = 10
def user_refs
self.post_visibilities.count
if AspectVisibility.exists?(:post_id => self.id)
self.post_visibilities.count + 1
else
self.post_visibilities.count
end
end
def diaspora_handle= nd
@ -68,7 +77,7 @@ class Post < ActiveRecord::Base
local_post = Post.where(:guid => self.guid).first
if local_post && local_post.author_id == self.author_id
known_post = user.visible_posts(:guid => self.guid).first
known_post = user.raw_visible_posts.where(:guid => self.guid).first
if known_post
if known_post.mutable?
known_post.update_attributes(self.attributes)
@ -76,14 +85,14 @@ class Post < ActiveRecord::Base
Rails.logger.info("event=receive payload_type=#{self.class} update=true status=abort sender=#{self.diaspora_handle} reason=immutable existing_post=#{known_post.id}")
end
else
user.add_post_to_aspects(local_post)
user.contact_for(person).receive_post(local_post)
user.notify_if_mentioned(local_post)
Rails.logger.info("event=receive payload_type=#{self.class} update=true status=complete sender=#{self.diaspora_handle} existing_post=#{local_post.id}")
return local_post
end
elsif !local_post
self.save
user.add_post_to_aspects(self)
user.contact_for(person).receive_post(self)
user.notify_if_mentioned(self)
Rails.logger.info("event=receive payload_type=#{self.class} update=false status=complete sender=#{self.diaspora_handle}")
return self

View file

@ -3,13 +3,6 @@
# the COPYRIGHT file.
class PostVisibility < ActiveRecord::Base
belongs_to :aspect
validates_presence_of :aspect
belongs_to :contact
belongs_to :post
validates_presence_of :post
has_one :user, :through => :aspect
has_one :person, :through => :post, :foreign_key => :author_id
end

View file

@ -91,23 +91,18 @@ class User < ActiveRecord::Base
end
######### Aspects ######################
def drop_aspect(aspect)
aspect.destroy
end
def move_contact(person, to_aspect, from_aspect)
return true if to_aspect == from_aspect
contact = contact_for(person)
if add_contact_to_aspect(contact, to_aspect)
membership = contact ? contact.aspect_memberships.where(:aspect_id => from_aspect.id).first : nil
return ( membership && membership.destroy )
else
false
end
add_contact_to_aspect(contact, to_aspect)
membership = contact ? AspectMembership.where(:contact_id => contact.id, :aspect_id => from_aspect.id).first : nil
return(membership && membership.destroy)
end
def add_contact_to_aspect(contact, aspect)
return true if contact.aspect_memberships.where(:aspect_id => aspect.id).count > 0
return true if AspectMembership.exists?(:contact_id => contact.id, :aspect_id => aspect.id)
contact.aspect_memberships.create!(:aspect => aspect)
end
@ -138,14 +133,6 @@ class User < ActiveRecord::Base
post.notify_person(self.person) if post.mentions? self.person
end
def add_post_to_aspects(post)
return unless self.contact_for(post.author)
Rails.logger.debug("event=add_post_to_aspects user_id=#{self.id} post_id=#{post.id}")
add_to_streams(post, self.aspects_with_person(post.author))
post
end
def add_to_streams(post, aspects_to_insert)
post.socket_to_user(self, :aspect_ids => aspects_to_insert.map{|x| x.id}) if post.respond_to? :socket_to_user
aspects_to_insert.each do |aspect|

View file

@ -27,7 +27,7 @@
- if @request_count > 0
%h4
= new_request_link(@request_count)
%h4
%h4.section.invite_friends
!= t('bookmarklet.explanation', :link => link_to(t('bookmarklet.explanation_link_text'), bookmarklet))
- if @invites > 0
.section.invite_friends

View file

@ -3,7 +3,7 @@
-# the COPYRIGHT file.
%li.comment.posted{:data=>{:guid => comment.id}, :class => ("hidden" if(defined? hidden))}
- if current_user && (current_user.owns?(comment) || current_user.owns?(comment.post))
- if current_user && (current_user.owns?(comment) || current_user.owns?(post))
.right.controls
= link_to image_tag('deletelabel.png'), comment_path(comment), :confirm => t('are_you_sure'), :method => :delete, :remote => true, :class => "delete comment_delete", :title => t('delete')
= person_image_link(comment.author)

View file

@ -10,14 +10,14 @@
= image_tag 'icons/spechbubble_2.png', :class => 'more_comments_icon'
%b= comment_toggle(comments.size)
%ul.comments{:id => post_id, :class => ("hidden" if comments.size == 0 && !defined?(force_open) && !is_expanded)}
%ul.comments{:id => post.id, :class => ("hidden" if comments.size == 0 && !defined?(force_open) && !is_expanded)}
-if comments.size > 3
.older_comments{:class => ("hidden inactive" if defined?(condensed) && condensed)}
= render :partial => 'comments/comment', :collection => comments[0..-4]
= render :partial => 'comments/comment', :collection => comments[-3, 3]
= render :partial => 'comments/comment', :collection => comments[0..-4], :locals => {:post => post}
= render :partial => 'comments/comment', :collection => comments[-3, 3], :locals => {:post => post}
-else
= render :partial => 'comments/comment', :collection => comments
= render :partial => 'comments/comment', :collection => comments, :locals => {:post => post}
- unless @commenting_disabled
%li.comment.show
= new_comment_form(post_id, current_user)
= new_comment_form(post.id, current_user)

View file

@ -11,9 +11,11 @@
selectedItemProp: "name",
searchObjProps: "name",
asHtmlID: "contact_ids",
retrieveLimit: 10,
minChars: 3,
keyDelay: 0,
startText: '',
emptyText: '#{t('.no_results')}',
emptyText: '#{t('no_results')}',
preFill: [{ 'name' : "#{params[:name]}",
'value' : "#{params[:contact_id]}"}]
});

View file

@ -76,4 +76,4 @@
%div{:data=>{:guid=>@parent.id}}
.likes_container
= render "likes/likes", :post_id => @parent.id, :likes => @parent.likes, :dislikes => @parent.dislikes
= render "comments/comments", :post_id => @parent.id, :comments => @parent.comments, :always_expanded => true
= render "comments/comments", :post => @parent, :comments => @parent.comments, :always_expanded => true

View file

@ -2,6 +2,41 @@
-# licensed under the Affero General Public License version 3 or later. See
-# the COPYRIGHT file.
- content_for :head do
= include_javascripts :profile
:javascript
$(document).ready(function () {
var data = $.parseJSON( $('#tags_json').val() ),
autocompleteInput = $("#profile_tag_string");
autocompleteInput.autoSuggest("#{tags_path}", {
selectedItemProp: "name",
searchObjProps: "name",
asHtmlID: "tags",
neverSubmit: true,
retriveLimit: 10,
selectionLimit: 5,
minChars: 2,
keyDelay: 0,
startText: "#{t('profiles.edit.your_tags_placeholder')}",
emptyText: '#{t('no_results')}',
preFill: data
});
autocompleteInput.bind('keydown', function(evt){
if(evt.keyCode == 13 || evt.keyCode == 9 || evt.keyCode == 32){
if( $('li.as-result-item.active').length == 0 ){
$('li.as-result-item').first().click();
}
}
});
});
= hidden_field_tag :tags_json, @tags_array.to_json
%h3
= t('profiles.edit.your_public_profile')
@ -14,8 +49,7 @@
%h4
= t('profiles.edit.your_tags')
= text_field_tag 'profile[tag_string]', profile.tag_string, :placeholder => t('profiles.edit.your_tags_placeholder')
= text_field_tag 'profile[tag_string]', ""
%h4
= t('profiles.edit.your_photo')
= render 'photos/new_profile_photo', :aspect => aspect, :person => person

View file

@ -44,4 +44,4 @@
/.likes_container
/ = render "likes/likes", :post_id => post.id, :likes => post.likes, :dislikes => post.dislikes, :current_user => current_user
= render "comments/comments", :post_id => post.id, :comments => post.comments, :current_user => current_user, :condensed => true, :commenting_disabled => (defined?(@commenting_disabled) && @commenting_disabled)
= render "comments/comments", :post => post, :comments => post.comments, :current_user => current_user, :condensed => true, :commenting_disabled => (defined?(@commenting_disabled) && @commenting_disabled)

View file

@ -29,4 +29,4 @@
= t('_comments')
#status_message_stream.stream.show
%div{:data=>{:guid=>@status_message.id}}
= render "comments/comments", :post_id => @status_message.id, :comments => @status_message.comments, :always_expanded => true
= render "comments/comments", :post => @status_message, :comments => @status_message.comments, :always_expanded => true

View file

@ -56,12 +56,14 @@ javascripts:
- public/javascripts/contact-list.js
- public/javascripts/aspect-edit-pane.js
people:
- public/javascripts/vendor/jquery.autoSuggest.js
- public/javascripts/vendor/jquery.autoSuggest.custom.js
photos:
- public/javascripts/photo-show.js
inbox:
- public/javascripts/vendor/jquery.autoSuggest.js
- public/javascripts/vendor/jquery.autoSuggest.custom.js
- public/javascripts/inbox.js
profile:
- public/javascripts/vendor/jquery.autoSuggest.custom.js
stylesheets:
default:

View file

@ -31,6 +31,7 @@ en:
previous: "previous"
_comments: "Comments"
all_aspects: "All Aspects"
no_results: "No Results Found"
#for reference translation, the real activerecord english transations are actually
@ -192,7 +193,6 @@ en:
to: "to"
subject: "subject"
send: "Send"
no_results: "No Results Found"
helper:
new_messages:
zero: "no new messages"

View file

@ -21,6 +21,7 @@ Diaspora::Application.routes.draw do
resources :comments, :only => [:create, :destroy]
get 'tags/:name' => 'tags#show', :as => 'tag'
resources :tags, :only => [:index]
resource :like, :only => [:create]

View file

@ -0,0 +1,92 @@
class PostVisibilitiesOnContacts < ActiveRecord::Migration
def self.move_author_pvs_to_aspect_pvs
where_clause = <<SQL
FROM post_visibilities as pv
INNER JOIN aspects ON aspects.id = pv.aspect_id
INNER JOIN posts ON posts.id = pv.post_id
INNER JOIN people ON posts.author_id = people.id
WHERE people.owner_id = aspects.user_id
SQL
execute("INSERT into aspect_visibilities SELECT pv.id, pv.post_id, pv.aspect_id, pv.created_at, pv.updated_at #{where_clause}")
execute("DELETE pv #{where_clause}")
end
def self.set_pv_contact_ids
execute <<SQL
UPDATE post_visibilities as pv
INNER JOIN posts ON posts.id = pv.post_id
INNER JOIN people ON posts.author_id = people.id
INNER JOIN aspects ON aspects.id = pv.aspect_id
INNER JOIN users ON users.id = aspects.user_id
INNER JOIN contacts ON contacts.user_id = users.id
SET pv.contact_id = contacts.id
WHERE people.id = contacts.person_id
SQL
end
def self.delete_duplicate_pvs
execute('DELETE FROM post_visibilities WHERE post_visibilities.contact_id = 0')
duplicate_rows = execute <<SQL
SELECT COUNT(pv.contact_id), pv.contact_id, pv.post_id from post_visibilities AS pv
GROUP BY pv.contact_id, pv.post_id
HAVING COUNT(*)>1;
SQL
duplicate_rows.each do |row|
count = row.first
contact_id = row[1]
post_id = row.last
execute <<SQL
DELETE FROM post_visibilities
WHERE post_visibilities.contact_id = #{contact_id} AND post_visibilities.post_id = #{post_id}
LIMIT #{count-1}
SQL
end
end
def self.delete_disconnected_pvs
execute <<SQL
DELETE post_visibilities FROM post_visibilities
LEFT OUTER JOIN posts ON post_visibilities.post_id = posts.id
LEFT OUTER JOIN aspects ON post_visibilities.aspect_id = aspects.id
WHERE aspects.id IS NULL OR posts.id IS NULL
SQL
end
def self.up
create_table :aspect_visibilities do |t|
t.integer :post_id, :null => false
t.integer :aspect_id, :null => false
t.timestamps
end
add_index :aspect_visibilities, [:post_id, :aspect_id], :unique => true
add_index :aspect_visibilities, [:aspect_id]
add_index :aspect_visibilities, [:post_id]
add_foreign_key :aspect_visibilities, :aspects, :dependent => :delete
add_foreign_key :aspect_visibilities, :posts, :dependent => :delete
delete_disconnected_pvs
add_column :post_visibilities, :contact_id, :integer, :null => false
move_author_pvs_to_aspect_pvs
set_pv_contact_ids
delete_duplicate_pvs
remove_index :post_visibilities, [:aspect_id, :post_id]
remove_column :post_visibilities, :aspect_id
add_index :post_visibilities, [:contact_id, :post_id], :unique => true
add_index :post_visibilities, [:contact_id]
add_foreign_key :post_visibilities, :contacts, :dependent => :delete
add_foreign_key :post_visibilities, :posts, :dependent => :delete
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110323213655) do
ActiveRecord::Schema.define(:version => 20110328202414) do
create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id", :null => false
@ -23,6 +23,17 @@ ActiveRecord::Schema.define(:version => 20110323213655) do
add_index "aspect_memberships", ["aspect_id"], :name => "index_aspect_memberships_on_aspect_id"
add_index "aspect_memberships", ["contact_id"], :name => "index_aspect_memberships_on_contact_id"
create_table "aspect_visibilities", :force => true do |t|
t.integer "post_id", :null => false
t.integer "aspect_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "aspect_visibilities", ["aspect_id"], :name => "index_aspect_visibilities_on_aspect_id"
add_index "aspect_visibilities", ["post_id", "aspect_id"], :name => "index_aspect_visibilities_on_post_id_and_aspect_id", :unique => true
add_index "aspect_visibilities", ["post_id"], :name => "index_aspect_visibilities_on_post_id"
create_table "aspects", :force => true do |t|
t.string "name", :null => false
t.integer "user_id", :null => false
@ -198,14 +209,14 @@ ActiveRecord::Schema.define(:version => 20110323213655) do
add_index "people", ["owner_id"], :name => "index_people_on_owner_id", :unique => true
create_table "post_visibilities", :force => true do |t|
t.integer "aspect_id", :null => false
t.integer "post_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "contact_id", :null => false
end
add_index "post_visibilities", ["aspect_id", "post_id"], :name => "index_post_visibilities_on_aspect_id_and_post_id", :unique => true
add_index "post_visibilities", ["aspect_id"], :name => "index_post_visibilities_on_aspect_id"
add_index "post_visibilities", ["contact_id", "post_id"], :name => "index_post_visibilities_on_contact_id_and_post_id", :unique => true
add_index "post_visibilities", ["contact_id"], :name => "index_post_visibilities_on_contact_id"
add_index "post_visibilities", ["post_id"], :name => "index_post_visibilities_on_post_id"
create_table "posts", :force => true do |t|
@ -366,6 +377,9 @@ ActiveRecord::Schema.define(:version => 20110323213655) do
add_foreign_key "aspect_memberships", "aspects", :name => "aspect_memberships_aspect_id_fk"
add_foreign_key "aspect_memberships", "contacts", :name => "aspect_memberships_contact_id_fk", :dependent => :delete
add_foreign_key "aspect_visibilities", "aspects", :name => "aspect_visibilities_aspect_id_fk", :dependent => :delete
add_foreign_key "aspect_visibilities", "posts", :name => "aspect_visibilities_post_id_fk", :dependent => :delete
add_foreign_key "comments", "people", :name => "comments_author_id_fk", :column => "author_id", :dependent => :delete
add_foreign_key "comments", "posts", :name => "comments_post_id_fk", :dependent => :delete
@ -379,6 +393,9 @@ ActiveRecord::Schema.define(:version => 20110323213655) do
add_foreign_key "notification_actors", "notifications", :name => "notification_actors_notification_id_fk", :dependent => :delete
add_foreign_key "post_visibilities", "contacts", :name => "post_visibilities_contact_id_fk", :dependent => :delete
add_foreign_key "post_visibilities", "posts", :name => "post_visibilities_post_id_fk", :dependent => :delete
add_foreign_key "posts", "people", :name => "posts_author_id_fk", :column => "author_id", :dependent => :delete
add_foreign_key "profiles", "people", :name => "profiles_person_id_fk", :dependent => :delete

View file

@ -12,7 +12,7 @@ Feature: invitation acceptance
And I should see "getting_started_logo"
When I fill in "profile_first_name" with "O"
And I fill in "profile_last_name" with "Hai"
And I fill in "profile_tag_string" with "#beingawesome"
And I fill in "tags" with "#beingawesome"
And I press "Save and continue"
Then I should see "Profile updated"
And I should see "Would you like to find your Facebook friends on Diaspora?"
@ -30,7 +30,7 @@ Feature: invitation acceptance
And I should see "getting_started_logo"
When I fill in "profile_first_name" with "O"
And I fill in "profile_last_name" with "Hai"
And I fill in "profile_tag_string" with "#tags"
And I fill in "tags" with "#tags"
And I press "Save and continue"
Then I should see "Profile updated"

View file

@ -14,7 +14,7 @@ Feature: new user registration
Scenario: new user goes through the setup wizard
When I fill in "profile_first_name" with "O"
And I fill in "profile_last_name" with "Hai"
And I fill in "profile_tag_string" with "#gender"
And I fill in "tags" with "#tags"
And I press "Save and continue"
And I wait for "step 2" to load
Then I should see "Profile updated"

View file

@ -131,20 +131,6 @@ Given /^a user with username "([^"]*)" is connected with "([^"]*)"$/ do |arg1, a
connect_users(user1, user1.aspects.first, user2, user2.aspects.first)
end
Given /^a user with email "([^\"]*)" has posted a status message "([^\"]*)" in all aspects$/ do |arg1, arg2|
user = User.where(:email => arg1).first
status_message = user.build_post(:status_message, :text => arg2)
def status_message.socket_to_user(a1, a2)
;
end
user.add_to_streams(status_message, user.aspects)
status_message.save!
bob = User.where(:email => "bob@bob.bob").first
raise bob.visible_posts.inspect
end
Given /^there is a user "([^\"]*)" who's tagged "([^\"]*)"$/ do |full_name, tag|
username = full_name.gsub(/\W/, "").underscore
Given "a user named \"#{full_name}\" with email \"#{username}@example.com\""
@ -152,4 +138,4 @@ Given /^there is a user "([^\"]*)" who's tagged "([^\"]*)"$/ do |full_name, tag|
user.profile.tag_string = tag
user.profile.build_tags
user.profile.save!
end
end

View file

@ -5,7 +5,7 @@ module PhotoMover
FileUtils::mkdir_p temp_dir
Dir.chdir 'tmp/exports'
photos = user.visible_posts(:author_id => user.person.id, :type => 'Photo')
photos = user.raw_visible_posts.where(:author_id => user.person.id, :type => 'Photo')
photos_dir = "#{user.id}/photos"
FileUtils::mkdir_p photos_dir

View file

@ -4,7 +4,7 @@
module Diaspora
module Taggable
VALID_TAG_BODY = /[^_\s#*\[\]()\:\@\/"']+/
VALID_TAG_BODY = /[^_\s#*\[\]()\:\@\/"'\.]+/
def self.included(model)
model.class_eval do

View file

@ -84,19 +84,13 @@ module Diaspora
def remove_contact(contact)
bad_person_id = contact.person_id
posts = raw_visible_posts.where(:author_id => bad_person_id).all
visibilities = PostVisibility.joins(:post, :aspect).where(
:posts => {:author_id => bad_person_id},
:aspects => {:user_id => self.id}
)
visibility_ids = visibilities.map{|v| v.id}
PostVisibility.where(:id => visibility_ids).delete_all
posts = contact.posts.all
contact.destroy
posts.each do |post|
if post.post_visibilities(true).count < 1
if post.user_refs < 1
post.destroy
end
end
contact.destroy
end
def disconnected_by(person)

View file

@ -7,30 +7,34 @@ module Diaspora
module Querying
def find_visible_post_by_id( id )
self.raw_visible_posts.where(:id => id).includes({:author => :profile}, {:comments => {:author => :profile}}, :photos).first
post = Post.where(:id => id).joins(:contacts).where(:contacts => {:user_id => self.id}).first
post ||= Post.where(:id => id, :author_id => self.person.id).first
end
def raw_visible_posts
Post.joins(:aspects).where(:pending => false,
:aspects => {:user_id => self.id}).select('DISTINCT `posts`.*')
def raw_visible_posts(opts = {})
opts[:type] ||= ['StatusMessage', 'Photo']
opts[:limit] ||= 20
opts[:order] ||= 'updated_at DESC'
opts[:order] = '`posts`.' + opts[:order]
opts[:limit] = opts[:limit].to_i * opts[:page].to_i if opts[:page]
posts_from_others = Post.joins(:contacts).where(:contacts => {:user_id => self.id})
posts_from_self = self.person.posts.joins(:aspect_visibilities => :aspect).where(:aspects => {:user_id => self.id})
if opts[:by_members_of]
posts_from_others = posts_from_others.joins(:contacts => :aspect_memberships).where(
:aspect_memberships => {:aspect_id => opts[:by_members_of]})
posts_from_self = posts_from_self.where(:aspects => {:id => opts[:by_members_of]})
end
post_ids = Post.connection.execute(posts_from_others.select('posts.id').limit(opts[:limit]).order(opts[:order]).to_sql).map{|r| r.first}
post_ids += Post.connection.execute(posts_from_self.select('posts.id').limit(opts[:limit]).order(opts[:order]).to_sql).map{|r| r.first}
Post.where(:id => post_ids, :pending => false, :type => opts[:type]).select('DISTINCT `posts`.*').limit(opts[:limit])
end
def visible_photos
p = Photo.arel_table
Photo.joins(:aspects).where(p[:status_message_id].not_eq(nil).or(p[:pending].eq(false))
).where(:aspects => {:user_id => self.id}).select('DISTINCT `posts`.*').order("posts.updated_at DESC")
end
def visible_posts( opts = {} )
order = opts.delete(:order)
order ||= 'created_at DESC'
opts[:type] ||= ["StatusMessage", "Photo"]
if (aspect = opts[:by_members_of]) && opts[:by_members_of] != :all
raw_visible_posts.where(:aspects => {:id => aspect.id}).order(order)
else
self.raw_visible_posts.where(opts).order(order)
end
raw_visible_posts(:type => 'Photo')
end
def contact_for(person)
@ -38,7 +42,7 @@ module Diaspora
contact_for_person_id(person.id)
end
def aspects_with_post(post_id)
self.aspects.joins(:post_visibilities).where(:post_visibilities => {:post_id => post_id})
self.aspects.joins(:aspect_visibilities).where(:aspect_visibilities => {:post_id => post_id})
end
def contact_for_person_id(person_id)
@ -78,11 +82,16 @@ module Diaspora
end
def posts_from(person)
asp = Aspect.arel_table
return self.person.posts.where(:pending => false).order("created_at DESC") if person == self.person
con = Contact.arel_table
p = Post.arel_table
person.posts.joins(:aspects).includes(:comments).where(
p[:public].eq(true).or(asp[:user_id].eq(self.id))
).select('DISTINCT `posts`.*').order("posts.created_at DESC")
post_ids = []
if contact = self.contact_for(person)
post_ids = Post.connection.execute(contact.post_visibilities.select('post_visibilities.post_id').to_sql).map{|r| r.first}
end
post_ids += Post.connection.execute(person.posts.where(:public => true).select('posts.id').to_sql).map{|r| r.first}
Post.where(:id => post_ids, :pending => false).select('DISTINCT `posts`.*').order("posts.created_at DESC")
end
end
end

View file

@ -44,7 +44,7 @@ class Postzord::Dispatch
end
def deliver_to_local(people)
return if people.blank?
return if people.blank? || @object.is_a?(Profile)
if @object.is_a?(Post)
batch_deliver_to_local(people)
else

View file

@ -46,8 +46,7 @@ describe ProfilesController do
it 'sets tags' do
params = { :id => @user.person.id,
:profile =>
{ :tag_string => '#apples #oranges'}}
:tags => '#apples #oranges'}
put :update, params
@user.person(true).profile.tag_list.to_set.should == ['apples', 'oranges'].to_set

View file

@ -7,11 +7,34 @@ require 'spec_helper'
describe TagsController do
render_views
describe '#show' do
before do
@user = alice
end
describe '#index (search)' do
before do
@user = alice
sign_in :user, @user
bob.profile.tag_string = "#cats #diaspora #rad"
bob.profile.build_tags
bob.profile.save!
end
it 'responds with json' do
get :index, :q => "ra", :format => 'json'
#parse json
response.body.should include("#diaspora")
response.body.should include("#rad")
end
it 'requires at least two characters' do
get :index, :q => "c", :format => 'json'
response.body.should_not include("#cats")
end
end
describe '#show' do
context 'signed in' do
before do
sign_in :user, @user

View file

@ -66,7 +66,7 @@ describe 'a user receives a post' do
bob.dispatch_post(sm, :to => bob.aspects.first)
end
alice.visible_posts.count.should == 1
alice.raw_visible_posts.count.should == 1
end
context 'mentions' do
@ -154,11 +154,12 @@ describe 'a user receives a post' do
@status_message = @user2.post :status_message, :text => "hi", :to => @aspect2.id
@user1.reload
@aspect.reload
@contact = @user1.contact_for(@user2.person)
end
it "adds a received post to the aspect and visible_posts array" do
it "adds a received post to the the contact" do
@user1.raw_visible_posts.include?(@status_message).should be_true
@aspect.posts.include?(@status_message).should be_true
@contact.posts.include?(@status_message).should be_true
end
it 'removes posts upon disconnecting' do
@ -174,8 +175,9 @@ describe 'a user receives a post' do
@post = Factory.create(:status_message, :author => @person)
@post.post_visibilities.should be_empty
receive_with_zord(@user1, @person, @post.to_diaspora_xml)
@aspect.post_visibilities.reset
@aspect.posts(true).should include(@post)
@contact = @user1.contact_for(@person)
@contact.post_visibilities.reset
@contact.posts(true).should include(@post)
@post.post_visibilities.reset
end
@ -194,9 +196,12 @@ describe 'a user receives a post' do
it 'should keep track of user references for one person ' do
@status_message.reload
@status_message.user_refs.should == 3
@status_message.contacts(true).should include(@contact)
@user1.disconnect(@contact)
@status_message.reload
@status_message.contacts(true).should_not include(@contact)
@status_message.post_visibilities.reset
@status_message.user_refs.should == 2
end

View file

@ -234,12 +234,26 @@ describe Postzord::Dispatch do
end
describe '#deliver_to_local' do
before do
@mailman = Postzord::Dispatch.new(@user, @sm)
end
it 'queues a batch receive' do
local_people = []
local_people << @user.person
mailman = Postzord::Dispatch.new(@user, @sm)
Resque.should_receive(:enqueue).with(Job::ReceiveLocalBatch, @sm.id, [@user.id]).once
mailman.send(:deliver_to_local, local_people)
@mailman.send(:deliver_to_local, local_people)
end
it 'returns if people are empty' do
Resque.should_not_receive(:enqueue)
@mailman.send(:deliver_to_local, [])
end
it 'returns if the object is a profile' do
@mailman.instance_variable_set(:@object, Profile.new)
Resque.should_not_receive(:enqueue)
@mailman.send(:deliver_to_local, [1])
end
end

View file

@ -46,7 +46,7 @@ describe 'making sure the spec runner works' do
it 'allows posting after running' do
message = @user1.post(:status_message, :text => "Connection!", :to => @aspect1.id)
@user2.reload.visible_posts.should include message
@user2.reload.raw_visible_posts.should include message
end
end

View file

@ -120,32 +120,6 @@ describe Aspect do
aspect.posts.include?(status_message).should be true
end
it 'should add post to aspect via receive method' do
aspect = user.aspects.create(:name => 'losers')
aspect2 = user2.aspects.create(:name => 'winners')
connect_users(user, aspect, user2, aspect2)
message = user2.post(:status_message, :text => "Hey Dude", :to => aspect2.id)
aspect.reload
aspect.posts.include?(message).should be true
user.visible_posts(:by_members_of => aspect).include?(message).should be true
end
it 'should retract the post from the aspects as well' do
aspect = user.aspects.create(:name => 'losers')
aspect2 = user2.aspects.create(:name => 'winners')
connect_users(user, aspect, user2, aspect2)
message = user2.post(:status_message, :text => "Hey Dude", :to => aspect2.id)
aspect.reload.post_ids.include?(message.id).should be true
fantasy_resque do
retraction = user2.retract(message)
end
aspect.posts(true).include?(message).should be false
end
end
context "aspect management" do
@ -176,23 +150,7 @@ describe Aspect do
user.reload
end
it 'should keep the contact\'s posts in previous aspect' do
aspect.post_ids.count.should == 1
user.move_contact(user2.person, user.aspects.create(:name => "Another aspect"), aspect)
aspect.reload
aspect.post_ids.count.should == 1
end
it 'should not delete other peoples posts' do
connect_users(user, aspect, user3, aspect3)
user.move_contact(user3.person, user.aspects.create(:name => "Another aspect"), aspect)
aspect.reload
aspect.posts.should == [@message]
end
describe '#move_contact' do
describe 'User#move_contact' do
it 'should be able to move a contact from one of users existing aspects to another' do
user.move_contact(user2.person, aspect1, aspect)

View file

@ -1,19 +0,0 @@
require 'spec_helper'
describe ConversationVisibility do
before do
@user = alice
@aspect = @user.aspects.create(:name => 'Boozers')
@person = Factory(:person)
@post = Factory(:status_message, :author => @person)
end
it 'has an aspect' do
pv = PostVisibility.new(:aspect => @aspect)
pv.aspect.should == @aspect
end
it 'has a post' do
pv = PostVisibility.new(:post => @post)
pv.post.should == @post
end
end

View file

@ -25,12 +25,12 @@ describe Job::ReceiveLocalBatch do
end
describe '.create_visibilities' do
it 'creates a visibility for each user' do
PostVisibility.exists?(:aspect_id => bob.aspects.first.id, :post_id => @post.id).should be_false
PostVisibility.exists?(:contact_id => bob.contact_for(alice.person).id, :post_id => @post.id).should be_false
Job::ReceiveLocalBatch.create_visibilities(@post, [bob.id])
PostVisibility.exists?(:aspect_id => bob.aspects.first.id, :post_id => @post.id).should be_true
PostVisibility.exists?(:contact_id => bob.contact_for(alice.person).id, :post_id => @post.id).should be_true
end
it 'does not raise if a visibility already exists' do
PostVisibility.create!(:aspect_id => bob.aspects.first.id, :post_id => @post.id)
PostVisibility.create!(:contact_id => bob.contact_for(alice.person).id, :post_id => @post.id)
lambda {
Job::ReceiveLocalBatch.create_visibilities(@post, [bob.id])
}.should_not raise_error

View file

@ -9,23 +9,19 @@ describe Mention do
before do
@user = alice
@aspect1 = @user.aspects.create(:name => 'second_aspect')
@mentioned_user = bob
@non_friend = eve
@sm = @user.build_post(:status_message, :text => "hi @{#{@mentioned_user.name}; #{@mentioned_user.diaspora_handle}}", :to => @user.aspects.first)
end
it 'notifies the person being mentioned' do
Notification.should_receive(:notify).with(@mentioned_user, anything(), @sm.author)
@sm.receive(@mentioned_user, @mentioned_user.person)
sm = @user.build_post(:status_message, :text => "hi @{#{bob.name}; #{bob.diaspora_handle}}", :to => @user.aspects.first)
Notification.should_receive(:notify).with(bob, anything(), sm.author)
sm.receive(bob, alice.person)
end
it 'should not notify a user if they do not see the message' do
connect_users(@user, @aspect1, @non_friend, @non_friend.aspects.first)
Notification.should_not_receive(:notify).with(@mentioned_user, anything(), @user.person)
sm2 = @user.post(:status_message, :text => "stuff @{#{@non_friend.name}; #{@non_friend.diaspora_handle}}", :to => @user.aspects.first)
sm2.receive(@non_friend, @non_friend.person)
Notification.should_not_receive(:notify).with(alice, anything(), bob.person)
sm2 = bob.build_post(:status_message, :text => "stuff @{#{alice.name}; #{alice.diaspora_handle}}", :to => bob.aspects.first)
sm2.receive(eve, bob.person)
end
end

View file

@ -1,19 +0,0 @@
require 'spec_helper'
describe PostVisibility do
before do
@user = alice
@aspect = @user.aspects.create(:name => 'Boozers')
@person = Factory(:person)
@post = Factory(:status_message, :author => @person)
end
it 'has an aspect' do
pv = PostVisibility.new(:aspect => @aspect)
pv.aspect.should == @aspect
end
it 'has a post' do
pv = PostVisibility.new(:post => @post)
pv.post.should == @post
end
end

View file

@ -49,7 +49,7 @@ describe "attack vectors" do
zord = Postzord::Receiver.new(user, :salmon_xml => salmon_xml)
zord.perform
user3.reload.visible_posts.should_not include(StatusMessage.find(original_message.id))
user3.reload.raw_visible_posts.should_not include(StatusMessage.find(original_message.id))
end
context 'malicious contact attack vector' do

View file

@ -291,13 +291,6 @@ describe Diaspora::UserModules::Connecting do
bob.reload.raw_visible_posts.include?(@message).should be_false
end
it "deletes the disconnected user's posts from the aspect's posts" do
Post.count.should == 1
bob.aspects.first.reload.posts.include?(@message).should be_true
bob.disconnect bob.contact_for(alice.person)
bob.aspects.first.reload.posts.include?(@message).should be_false
Post.count.should == 1
end
end
end
end

View file

@ -7,21 +7,19 @@ require 'spec_helper'
describe User do
before do
@alice = alice
@alices_aspect = @alice.aspects.first
@eve = eve
@eves_aspect = @eve.aspects.first
@alices_aspect = alice.aspects.first
@eves_aspect = eve.aspects.first
end
describe "#raw_visible_posts" do
it "returns all the posts the user can see" do
connect_users(@eve, @eves_aspect, @alice, @alices_aspect)
self_post = @alice.post(:status_message, :text => "hi", :to => @alices_aspect.id)
visible_post = @eve.post(:status_message, :text => "hello", :to => @eves_aspect.id)
dogs = @eve.aspects.create(:name => "dogs")
invisible_post = @eve.post(:status_message, :text => "foobar", :to => dogs.id)
connect_users(eve, @eves_aspect, alice, @alices_aspect)
self_post = alice.post(:status_message, :text => "hi", :to => @alices_aspect.id)
visible_post = eve.post(:status_message, :text => "hello", :to => @eves_aspect.id)
dogs = eve.aspects.create(:name => "dogs")
invisible_post = eve.post(:status_message, :text => "foobar", :to => dogs.id)
stream = @alice.raw_visible_posts
stream = alice.raw_visible_posts
stream.should include(self_post)
stream.should include(visible_post)
stream.should_not include(invisible_post)
@ -30,20 +28,20 @@ describe User do
context 'with two posts' do
before do
connect_users(@eve, @eves_aspect, @alice, @alices_aspect)
aspect3 = @alice.aspects.create(:name => "Snoozers")
@status_message1 = @eve.post :status_message, :text => "hi", :to => @eves_aspect.id
@status_message2 = @eve.post :status_message, :text => "hey", :public => true , :to => @eves_aspect.id
@status_message3 = @alice.post :status_message, :text => "hey", :public => true , :to => @alices_aspect.id
@status_message4 = @eve.post :status_message, :text => "blah", :public => true , :to => @eves_aspect.id
@status_message5 = @alice.post :status_message, :text => "secrets", :to => aspect3.id
connect_users(eve, @eves_aspect, alice, @alices_aspect)
aspect3 = alice.aspects.create(:name => "Snoozers")
@status_message1 = eve.post :status_message, :text => "hi", :to => @eves_aspect.id
@status_message2 = eve.post :status_message, :text => "hey", :public => true , :to => @eves_aspect.id
@status_message3 = alice.post :status_message, :text => "hey", :public => true , :to => @alices_aspect.id
@status_message4 = eve.post :status_message, :text => "blah", :public => true , :to => @eves_aspect.id
@status_message5 = alice.post :status_message, :text => "secrets", :to => aspect3.id
@pending_status_message = @eve.post :status_message, :text => "hey", :public => true , :to => @eves_aspect.id, :pending => true
@pending_status_message = eve.post :status_message, :text => "hey", :public => true , :to => @eves_aspect.id, :pending => true
end
describe "#visible_posts" do
it "queries by person id" do
query = @eve.visible_posts(:author_id => @eve.person.id)
query = eve.raw_visible_posts.where(:author_id => eve.person.id)
query.include?(@status_message1).should == true
query.include?(@status_message2).should == true
query.include?(@status_message3).should == false
@ -52,7 +50,7 @@ describe User do
end
it "selects public posts" do
query = @eve.visible_posts(:public => true)
query = eve.raw_visible_posts.where(:public => true)
query.include?(@status_message1).should == false
query.include?(@status_message2).should == true
query.include?(@status_message3).should == true
@ -61,7 +59,7 @@ describe User do
end
it "selects non public posts" do
query = @eve.visible_posts(:public => false)
query = eve.raw_visible_posts.where(:public => false)
query.include?(@status_message1).should == true
query.include?(@status_message2).should == false
query.include?(@status_message3).should == false
@ -70,27 +68,18 @@ describe User do
end
it "selects by message contents" do
query = @eve.visible_posts(:text=> "hi")
query = eve.raw_visible_posts.where(:text=> "hi")
query.should == [@status_message1]
end
it "does not return pending posts" do
@pending_status_message.pending.should be_true
@eve.visible_posts.should_not include @pending_status_message
eve.raw_visible_posts.should_not include @pending_status_message
end
it "queries by aspect" do
query = @alice.visible_posts(:by_members_of => @alices_aspect)
query.include?(@status_message1).should == true
query.include?(@status_message2).should == true
query.include?(@status_message3).should == true
query.include?(@status_message4).should == true
query.include?(@status_message5).should == false
@alice.visible_posts(:by_members_of => @alice.aspects.create(:name => "aaaaah")).should be_empty
end
it '#find_visible_post_by_id' do
@eve.find_visible_post_by_id(@status_message1.id).should == @status_message1
@eve.find_visible_post_by_id(@status_message5.id).should == nil
eve.find_visible_post_by_id(@status_message1.id).should == @status_message1
eve.find_visible_post_by_id(@status_message5.id).should == nil
end
end
end
@ -98,7 +87,7 @@ describe User do
context 'with two users' do
describe '#people_in_aspects' do
it 'returns people objects for a users contact in each aspect' do
@alice.people_in_aspects([@alices_aspect]).should == [bob.person]
alice.people_in_aspects([@alices_aspect]).should == [bob.person]
end
it 'returns local/remote people objects for a users contact in each aspect' do
@ -110,37 +99,37 @@ describe User do
asp2 = local_user2.aspects.create(:name => "brb")
asp3 = remote_user.aspects.create(:name => "ttyl")
connect_users(@alice, @alices_aspect, local_user1, asp1)
connect_users(@alice, @alices_aspect, local_user2, asp2)
connect_users(@alice, @alices_aspect, remote_user, asp3)
connect_users(alice, @alices_aspect, local_user1, asp1)
connect_users(alice, @alices_aspect, local_user2, asp2)
connect_users(alice, @alices_aspect, remote_user, asp3)
local_person = remote_user.person
local_person.owner_id = nil
local_person.save
local_person.reload
@alice.people_in_aspects([@alices_aspect]).count.should == 4
@alice.people_in_aspects([@alices_aspect], :type => 'remote').count.should == 1
@alice.people_in_aspects([@alices_aspect], :type => 'local').count.should == 3
alice.people_in_aspects([@alices_aspect]).count.should == 4
alice.people_in_aspects([@alices_aspect], :type => 'remote').count.should == 1
alice.people_in_aspects([@alices_aspect], :type => 'local').count.should == 3
end
it 'does not return people not connected to user on same pod' do
3.times { Factory(:user) }
@alice.people_in_aspects([@alices_aspect]).count.should == 1
alice.people_in_aspects([@alices_aspect]).count.should == 1
end
it "only returns non-pending contacts" do
@alice.send_contact_request_to(Factory(:user).person, @alices_aspect)
alice.send_contact_request_to(Factory(:user).person, @alices_aspect)
@alices_aspect.reload
@alice.reload
alice.reload
@alice.people_in_aspects([@alices_aspect]).should == [bob.person]
alice.people_in_aspects([@alices_aspect]).should == [bob.person]
end
it "returns an empty array when passed an aspect the user doesn't own" do
other_user = Factory(:user_with_aspect)
connect_users(@eve, @eve.aspects.first, other_user, other_user.aspects.first)
@alice.people_in_aspects([other_user.aspects.first]).should == []
connect_users(eve, eve.aspects.first, other_user, other_user.aspects.first)
alice.people_in_aspects([other_user.aspects.first]).should == []
end
end
end
@ -149,46 +138,46 @@ describe User do
let(:person_one) { Factory.create :person }
let(:person_two) { Factory.create :person }
let(:person_three) { Factory.create :person }
let(:aspect) { @alice.aspects.create(:name => 'heroes') }
let(:aspect) { alice.aspects.create(:name => 'heroes') }
describe '#contact_for_person_id' do
it 'returns a contact' do
contact = Contact.create(:user => @alice, :person => person_one, :aspects => [aspect])
@alice.contacts << contact
@alice.contact_for_person_id(person_one.id).should be_true
contact = Contact.create(:user => alice, :person => person_one, :aspects => [aspect])
alice.contacts << contact
alice.contact_for_person_id(person_one.id).should be_true
end
it 'returns the correct contact' do
contact = Contact.create(:user => @alice, :person => person_one, :aspects => [aspect])
@alice.contacts << contact
contact = Contact.create(:user => alice, :person => person_one, :aspects => [aspect])
alice.contacts << contact
contact2 = Contact.create(:user => @alice, :person => person_two, :aspects => [aspect])
@alice.contacts << contact2
contact2 = Contact.create(:user => alice, :person => person_two, :aspects => [aspect])
alice.contacts << contact2
contact3 = Contact.create(:user => @alice, :person => person_three, :aspects => [aspect])
@alice.contacts << contact3
contact3 = Contact.create(:user => alice, :person => person_three, :aspects => [aspect])
alice.contacts << contact3
@alice.contact_for_person_id(person_two.id).person.should == person_two
alice.contact_for_person_id(person_two.id).person.should == person_two
end
it 'returns nil for a non-contact' do
@alice.contact_for_person_id(person_one.id).should be_nil
alice.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 => @alice, :person => person_one, :aspects => [aspect])
@alice.contacts << contact
@eve.contact_for_person_id(person_one.id).should be_nil
contact = Contact.create(:user => alice, :person => person_one, :aspects => [aspect])
alice.contacts << contact
eve.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
@alice.should_receive(:contact_for_person_id).with(person_one.id)
@alice.contact_for(person_one)
alice.should_receive(:contact_for_person_id).with(person_one.id)
alice.contact_for(person_one)
end
it 'returns nil if the input is nil' do
@alice.contact_for(nil).should be_nil
alice.contact_for(nil).should be_nil
end
end
end
@ -197,13 +186,13 @@ describe User do
let!(:user5) {Factory(:user)}
it 'should not have a pending request before connecting' do
request = @alice.request_from(user5.person)
request = alice.request_from(user5.person)
request.should be_nil
end
it 'should have a pending request after sending a request' do
@alice.send_contact_request_to(user5.person, @alice.aspects.first)
request = user5.request_from(@alice.person)
alice.send_contact_request_to(user5.person, alice.aspects.first)
request = user5.request_from(alice.person)
request.should_not be_nil
end
end
@ -218,21 +207,21 @@ describe User do
end
it 'displays public posts for a non-contact' do
@alice.posts_from(@user3.person).should include @public_message
alice.posts_from(@user3.person).should include @public_message
end
it 'does not display private posts for a non-contact' do
@alice.posts_from(@user3.person).should_not include @private_message
alice.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(@alice, @alices_aspect, @user3, @aspect3)
connect_users(alice, @alices_aspect, @user3, @aspect3)
new_message = @user3.post(:status_message, :text=> "hey there", :to => @aspect3.id)
@alice.reload
alice.reload
@alice.posts_from(@user3.person).should include @public_message
@alice.posts_from(@user3.person).should include new_message
alice.posts_from(@user3.person).should include @public_message
alice.posts_from(@user3.person).should include new_message
end
it 'displays recent posts first' do
@ -243,7 +232,7 @@ describe User do
msg4.created_at = Time.now+14
msg4.save!
@alice.posts_from(@user3.person).map{|p| p.id}.should == [msg4, msg3, @public_message].map{|p| p.id}
alice.posts_from(@user3.person).map{|p| p.id}.should == [msg4, msg3, @public_message].map{|p| p.id}
end
end
end

View file

@ -324,18 +324,18 @@ describe User do
end
end
context 'aspects' do
describe 'foreign key between aspects and contacts' do
it 'should delete an empty aspect' do
empty_aspect = alice.aspects.create(:name => 'decoy')
alice.aspects(true).include?(empty_aspect).should == true
alice.drop_aspect(empty_aspect)
empty_aspect.destroy
alice.aspects(true).include?(empty_aspect).should == false
end
it 'should not delete an aspect with contacts' do
aspect = alice.aspects.first
aspect.contacts.count.should > 0
proc { alice.drop_aspect(aspect) }.should raise_error ActiveRecord::StatementInvalid
proc { aspect.destroy }.should raise_error ActiveRecord::StatementInvalid
alice.aspects.include?(aspect).should == true
end
end