Commit 260d86d5 authored by Florian Staudacher's avatar Florian Staudacher Committed by Florian Staudacher

refactor ConversationsController, move query builing to User model

parent 6e62a997
......@@ -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)
......
......@@ -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 @@ class ConversationsController < ApplicationController
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
......
......@@ -45,6 +45,7 @@ class Person < ActiveRecord::Base
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
......
......@@ -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 @@ class User < ActiveRecord::Base
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
......
......@@ -29,7 +29,7 @@ class AccountDeleter
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 @@ class AccountDeleter
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 @@ class AccountDeleter
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
......@@ -208,6 +208,37 @@ FactoryGirl.define do
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)
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment