Skip to content

Commit

Permalink
refactor ConversationsController, move query builing to User model
Browse files Browse the repository at this point in the history
  • Loading branch information
Raven24 committed Sep 29, 2013
1 parent 6e62a99 commit 260d86d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Ported fileuploader to Backbone and refactored publisher views [#4480](https://github.com/diaspora/diaspora/pull/4480)
* Refactor 404.html, fix [#4078](https://github.com/diaspora/diaspora/issues/4078)
* Remove the (now useless) last post link from the user profile. [#4540](https://github.com/diaspora/diaspora/pull/4540)
* Refactor ConversationsController, move query building to User model. [#4547](https://github.com/diaspora/diaspora/pull/4547)

## Bug fixes
* Highlight down arrow at the user menu on hover [#4441](https://github.com/diaspora/diaspora/pull/4441)
Expand Down
16 changes: 7 additions & 9 deletions app/controllers/conversations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ class ConversationsController < ApplicationController
respond_to :html, :mobile, :json, :js

def index
@conversations = Conversation.joins(:conversation_visibilities).where(
:conversation_visibilities => {:person_id => current_user.person_id}).paginate(
:page => params[:page], :per_page => 15, :order => 'updated_at DESC')
@conversations = current_user.conversations.paginate(
:page => params[:page], :per_page => 15)

@visibilities = current_user.conversation_visibilities.paginate(
:page => params[:page], :per_page => 15)

@visibilities = ConversationVisibility.where(:person_id => current_user.person_id).paginate(
:page => params[:page], :per_page => 15, :order => 'updated_at DESC')

@conversation = Conversation.joins(:conversation_visibilities).where(
:conversation_visibilities => {:person_id => current_user.person_id, :conversation_id => params[:conversation_id]}).first

@unread_counts = {}
@visibilities.each { |v| @unread_counts[v.conversation_id] = v.unread }

@first_unread_message_id = @conversation.try(:first_unread_message, current_user).try(:id)

@authors = {}
Expand Down Expand Up @@ -60,8 +59,7 @@ def create
end

def show
if @conversation = Conversation.joins(:conversation_visibilities).where(:id => params[:id],
:conversation_visibilities => {:person_id => current_user.person_id}).first
if @conversation = current_user.conversations.where(id: params[:id]).first

@first_unread_message_id = @conversation.first_unread_message(current_user).try(:id)
if @visibility = ConversationVisibility.where(:conversation_id => params[:id], :person_id => current_user.person.id).first
Expand Down
1 change: 1 addition & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def downcase_diaspora_handle
has_many :photos, :foreign_key => :author_id, :dependent => :destroy # This person's own photos
has_many :comments, :foreign_key => :author_id, :dependent => :destroy # This person's own comments
has_many :participations, :foreign_key => :author_id, :dependent => :destroy
has_many :conversation_visibilities

has_many :roles

Expand Down
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class User < ActiveRecord::Base
has_many :blocks
has_many :ignored_people, :through => :blocks, :source => :person

has_many :conversation_visibilities, through: :person, order: 'updated_at DESC'
has_many :conversations, through: :conversation_visibilities, order: 'updated_at DESC'

has_many :notifications, :foreign_key => :recipient_id

before_save :guard_unconfirmed_email,
Expand Down Expand Up @@ -338,7 +341,7 @@ def update_profile(params)

def update_profile_with_omniauth( user_info )
update_profile( self.profile.from_omniauth_hash( user_info ) )
end
end

def deliver_profile_update
Postzord::Dispatcher.build(self, profile).post
Expand Down
8 changes: 4 additions & 4 deletions lib/account_deleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def perform!
remove_share_visibilities_on_persons_posts
delete_contacts_of_me
tombstone_person_and_profile

if self.user
#user deletion methods
remove_share_visibilities_on_contacts_posts
Expand All @@ -50,7 +50,7 @@ def special_ar_user_associations
end

def ignored_ar_user_associations
[:followed_tags, :invited_by, :contact_people, :aspect_memberships, :ignored_people]
[:followed_tags, :invited_by, :contact_people, :aspect_memberships, :ignored_people, :conversation_visibilities, :conversations]
end

def delete_standard_user_associations
Expand Down Expand Up @@ -101,12 +101,12 @@ def tombstone_user
def delete_contacts_of_me
Contact.all_contacts_of_person(self.person).destroy_all
end

def normal_ar_person_associates_to_delete
[:posts, :photos, :mentions, :participations, :roles]
end

def ignored_or_special_ar_person_associations
[:comments, :contacts, :notification_actors, :notifications, :owner, :profile ]
[:comments, :contacts, :notification_actors, :notifications, :owner, :profile, :conversation_visibilities]
end
end
31 changes: 31 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ def r_str
association(:post, :factory => :status_message)
end

factory(:conversation) do
association(:author, factory: :person)
sequence(:subject) { |n| "conversation ##{n}" }

after(:build) do |c|
c.participants << c.author
end
end

factory(:conversation_with_message, parent: :conversation) do
after(:build) do |c|
msg = FactoryGirl.build(:message)
msg.conversation_id = c.id
c.participants << msg.author
msg.save
end
end

factory(:message) do
association(:author, factory: :person)
sequence(:text) { |n| "message text ##{n}" }
end

factory(:message_with_conversation, parent: :message) do
after(:build) do |msg|
c = FactoryGirl.build(:conversation)
c.participants << msg.author
msg.conversation_id = c.id
end
end

#templates
factory(:status_with_photo_backdrop, :parent => :status_message_with_photo)

Expand Down
25 changes: 25 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@
require 'spec_helper'

describe User do
context "relations" do
context "#conversations" do
it "doesn't find anything when there is nothing to find" do
u = FactoryGirl.create(:user)
u.conversations.should be_empty
end

it "finds the users conversations" do
c = FactoryGirl.create(:conversation, { author: alice.person })

alice.conversations.should include c
end

it "doesn't find other users conversations" do
c1 = FactoryGirl.create(:conversation)
c2 = FactoryGirl.create(:conversation)
c_own = FactoryGirl.create(:conversation, { author: alice.person })

alice.conversations.should include c_own
alice.conversations.should_not include c1
alice.conversations.should_not include c2
end
end
end

describe "private key" do
it 'has a key' do
alice.encryption_key.should_not be nil
Expand Down

0 comments on commit 260d86d

Please sign in to comment.