From 830747a11ec10054fc0202952c8ff673029f8fa6 Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Sun, 6 Oct 2013 23:37:30 +0200 Subject: [PATCH] refactor conversations_controller#create --- Changelog.md | 1 + app/controllers/conversations_controller.rb | 12 +++++------- app/models/conversation.rb | 8 +++++--- app/models/user/social_actions.rb | 13 ++++++++++++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Changelog.md b/Changelog.md index 383ca66bc..687217375 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,7 @@ * 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) * Refactor the Twitter service model [#4387](https://github.com/diaspora/diaspora/pull/4387) +* Refactor ConversationsController#create, move more stuff to User model [#4551](https://github.com/diaspora/diaspora/pull/4551) ## 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 8aef166a9..8b85e43f1 100644 --- a/app/controllers/conversations_controller.rb +++ b/app/controllers/conversations_controller.rb @@ -30,15 +30,13 @@ class ConversationsController < ApplicationController def create # Can't split nil if params[:contact_ids] - person_ids = Contact.where(:id => params[:contact_ids].split(',')).map(&:person_id) + person_ids = current_user.contacts.where(id: params[:contact_ids].split(',')).pluck(:person_id) end - @conversation = Conversation.new - @conversation.subject = params[:conversation][:subject] - @conversation.participant_ids = [*person_ids] | [current_user.person_id] - @conversation.author = current_user.person - message_text = params[:conversation][:text] - @conversation.messages_attributes = [ {:author => current_user.person, :text => message_text }] + opts = params.require(:conversation).permit(:subject) + opts[:participant_ids] = person_ids + opts[:message] = { text: params[:conversation][:text] } + @conversation = current_user.build_conversation(opts) @response = {} if person_ids.present? && @conversation.save diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 8d2f25c25..fee57d81f 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -33,10 +33,10 @@ class Conversation < ActiveRecord::Base def diaspora_handle= nh self.author = Webfinger.new(nh).fetch end - + def first_unread_message(user) if visibility = self.conversation_visibilities.where(:person_id => user.person.id).where('unread > 0').first - self.messages.all[-visibility.unread] + self.messages.all[-visibility.unread] end end @@ -54,7 +54,9 @@ class Conversation < ActiveRecord::Base end def last_author - self.messages.last.author if self.messages.size > 0 + return unless @last_author.present? || self.messages.size > 0 + @last_author_id ||= self.messages.pluck(:author_id).last + @last_author ||= Person.includes(:profile).where(id: @last_author_id).first end def subject diff --git a/app/models/user/social_actions.rb b/app/models/user/social_actions.rb index ca8c8874e..a20085095 100644 --- a/app/models/user/social_actions.rb +++ b/app/models/user/social_actions.rb @@ -25,7 +25,18 @@ module User::SocialActions Comment::Generator.new(self, options.delete(:post), options.delete(:text)).build(options) end + def build_conversation(opts={}) + Conversation.new do |c| + c.author = self.person + c.subject = opts[:subject] + c.participant_ids = [*opts[:participant_ids]] | [self.person_id] + c.messages_attributes = [ + { author: self.person, text: opts[:message][:text] } + ] + end + end + def find_or_create_participation!(target) participations.where(:target_id => target).first || participate!(target) end -end \ No newline at end of file +end