Merge pull request #4226 from kevivmatrix/ajax_message

private message sending changed to ajax
This commit is contained in:
Jonne Haß 2013-07-06 02:41:24 -07:00
commit d762ca5a43
6 changed files with 49 additions and 9 deletions

View file

@ -44,6 +44,7 @@
* Leaving the `to` field blank when sending a private message causes a server error [#4227](https://github.com/diaspora/diaspora/issues/4227)
* Fix hashtags that start a line when posting to Facebook or Twitter [#3768](https://github.com/diaspora/diaspora/issues/3768) [#4154](https://github.com/diaspora/diaspora/issues/4154)
* Show avatar of recent user in conversation list [#4237](https://github.com/diaspora/diaspora/issues/4237)
* Private message fails if contact not entered correctly [#4210](https://github.com/diaspora/diaspora/issues/4210)
## Features

View file

@ -29,7 +29,7 @@ var View = {
/* Clear forms after successful submit, this is some legacy dan hanson stuff, do we still want it? */
$.fn.clearForm = function() {
return this.each(function() {
if ($(this).is('form')) {
if ($(this).is('form') && !$(this).hasClass('form_do_not_clear')) {
return $(':input', this).clearForm();
}
if ($(this).hasClass('clear_on_submit') || $(this).is(':text') || $(this).is(':password') || $(this).is('textarea')) {

View file

@ -39,20 +39,22 @@ class ConversationsController < ApplicationController
message_text = params[:conversation].delete(:text)
params[:conversation][:messages_attributes] = [ {:author => current_user.person, :text => message_text }]
@response = {}
@conversation = Conversation.new(params[:conversation])
if person_ids.present? && @conversation.save
Postzord::Dispatcher.build(current_user, @conversation).post
flash[:notice] = I18n.t('conversations.create.sent')
@response[:success] = true
@response[:message] = I18n.t('conversations.create.sent')
@response[:conversation_id] = @conversation.id
else
flash[:error] = I18n.t('conversations.create.fail')
@response[:success] = false
@response[:message] = I18n.t('conversations.create.fail')
if person_ids.blank?
flash[:error] = I18n.t('conversations.create.no_contact')
@response[:message] = I18n.t('conversations.create.no_contact')
end
end
if params[:profile]
redirect_to person_path(params[:profile])
else
redirect_to conversations_path(:conversation_id => @conversation.id)
respond_to do |format|
format.js
end
end

View file

@ -0,0 +1,11 @@
var response = <%= raw @response.to_json %>;
<% if session[:mobile_view] %>
window.location.href = "<%= conversations_path(conversation_id: @conversation.id) %>";
<% else %>
Diaspora.page.flashMessages.render({ 'success':response.success, 'notice':response.message });
if(response.success){
$("#new_conversation").removeClass('form_do_not_clear').clearForm();
$.facebox.close();
window.location.href = "<%= conversations_path(conversation_id: @conversation.id) %>";
}
<% end %>

View file

@ -32,7 +32,7 @@
%h3
= t('conversations.index.new_message')
= form_for Conversation.new do |conversation|
= form_for Conversation.new, html: {class: "new_conversation form_do_not_clear"}, remote: true do |conversation|
.span-2
%h4

View file

@ -97,6 +97,13 @@ describe ConversationsController do
}.should change(Message, :count).by(1)
end
it 'should set response with success to true and message to success message' do
post :create, @hash
assigns[:response][:success].should == true
assigns[:response][:message].should == I18n.t('conversations.create.sent')
assigns[:response][:conversation_id].should == Conversation.first.id
end
it 'sets the author to the current_user' do
@hash[:author] = FactoryGirl.create(:user)
post :create, @hash
@ -143,6 +150,13 @@ describe ConversationsController do
post :create, @hash
}.should change(Message, :count).by(1)
end
it 'should set response with success to true and message to success message' do
post :create, @hash
assigns[:response][:success].should == true
assigns[:response][:message].should == I18n.t('conversations.create.sent')
assigns[:response][:conversation_id].should == Conversation.first.id
end
end
context 'with empty text' do
@ -167,6 +181,12 @@ describe ConversationsController do
post :create, @hash
}.should_not change(Message, :count).by(1)
end
it 'should set response with success to false and message to create fail' do
post :create, @hash
assigns[:response][:success].should == false
assigns[:response][:message].should == I18n.t('conversations.create.fail')
end
end
context 'with empty contact' do
@ -191,6 +211,12 @@ describe ConversationsController do
post :create, @hash
}.should_not change(Message, :count).by(1)
end
it 'should set response with success to false and message to fail due to no contact' do
post :create, @hash
assigns[:response][:success].should == false
assigns[:response][:message].should == I18n.t('conversations.create.no_contact')
end
end
context 'with nil contact' do