Merge pull request #4551 from Raven24/conversations

refactor conversations_controller#create
This commit is contained in:
Jonne Haß 2013-10-11 01:14:01 -07:00
commit ada6594755
4 changed files with 23 additions and 11 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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
end