diff --git a/Changelog.md b/Changelog.md index 3280470d7a3..24af5f144f7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -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) diff --git a/app/controllers/conversations_controller.rb b/app/controllers/conversations_controller.rb index 9e5102cf79a..8aef166a9de 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -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 = {} @@ -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 diff --git a/app/models/person.rb b/app/models/person.rb index 87de1e53270..765a8539a69 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 521bf057bd3..25afb1c7eef 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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, @@ -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 diff --git a/lib/account_deleter.rb b/lib/account_deleter.rb index ea92d6331eb..da2d7ba9e31 100644 --- a/lib/account_deleter.rb +++ b/lib/account_deleter.rb @@ -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 @@ -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 @@ -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 diff --git a/spec/factories.rb b/spec/factories.rb index 4967416f661..fcede7e1b2d 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 60c1e157cf6..9e3c62d24d2 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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