added the forgotten files
This commit is contained in:
parent
3812612c86
commit
f58c477673
2 changed files with 108 additions and 0 deletions
32
app/controllers/messages_controller.rb
Normal file
32
app/controllers/messages_controller.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||||
|
# licensed under the Affero General Public License version 3 or later. See
|
||||||
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
|
class MessagesController < ApplicationController
|
||||||
|
include ApplicationHelper
|
||||||
|
before_filter :authenticate_user!
|
||||||
|
|
||||||
|
respond_to :html, :mobile
|
||||||
|
respond_to :json, :only => :show
|
||||||
|
|
||||||
|
def create
|
||||||
|
cnv = Conversation.joins(:conversation_visibilities).where(:id => params[:conversation_id],
|
||||||
|
:conversation_visibilities => {:person_id => current_user.person.id}).first
|
||||||
|
|
||||||
|
if cnv
|
||||||
|
message = Message.new(:conversation_id => cnv.id, :text => params[:text], :author => current_user.person)
|
||||||
|
|
||||||
|
if message.save
|
||||||
|
Rails.logger.info("event=create type=comment user=#{current_user.diaspora_handle} status=success message=#{message.id} chars=#{params[:text].length}")
|
||||||
|
Postzord::Dispatch.new(current_user, message).post
|
||||||
|
|
||||||
|
respond_with cnv
|
||||||
|
else
|
||||||
|
render :nothing => true, :status => 406
|
||||||
|
end
|
||||||
|
else
|
||||||
|
render :nothing => true, :status => 406
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
76
spec/controllers/messages_controller_spec.rb
Normal file
76
spec/controllers/messages_controller_spec.rb
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||||
|
# licensed under the Affero General Public License version 3 or later. See
|
||||||
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe MessagesController do
|
||||||
|
render_views
|
||||||
|
|
||||||
|
before do
|
||||||
|
@user1 = alice
|
||||||
|
@user2 = bob
|
||||||
|
|
||||||
|
@aspect1 = @user1.aspects.first
|
||||||
|
@aspect2 = @user2.aspects.first
|
||||||
|
|
||||||
|
sign_in :user, @user1
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#create' do
|
||||||
|
before do
|
||||||
|
@create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
|
||||||
|
:subject => "cool stuff", :text => "stuff"}
|
||||||
|
|
||||||
|
end
|
||||||
|
context "on my own post" do
|
||||||
|
before do
|
||||||
|
@cnv = Conversation.create(@create_hash)
|
||||||
|
@message_hash = {:conversation_id => @cnv.id, :text => "here is something else"}
|
||||||
|
end
|
||||||
|
it 'responds to format js' do
|
||||||
|
lambda{
|
||||||
|
post :create, @message_hash
|
||||||
|
}.should change(Message, :count).by(1)
|
||||||
|
response.code.should == '201'
|
||||||
|
response.should redirect_to(@cnv)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "on a post from a contact" do
|
||||||
|
before do
|
||||||
|
@create_hash[:author] = @user2.person
|
||||||
|
@cnv = Conversation.create(@create_hash)
|
||||||
|
@message_hash = {:conversation_id => @cnv.id, :text => "here is something else"}
|
||||||
|
end
|
||||||
|
it 'comments' do
|
||||||
|
post :create, @message_hash
|
||||||
|
response.code.should == '201'
|
||||||
|
end
|
||||||
|
it "doesn't overwrite author_id" do
|
||||||
|
new_user = Factory.create(:user)
|
||||||
|
@message_hash[:author_id] = new_user.person.id.to_s
|
||||||
|
post :create, @message_hash
|
||||||
|
Message.find_by_text(@message_hash[:text]).author_id.should == @user1.person.id
|
||||||
|
end
|
||||||
|
it "doesn't overwrite id" do
|
||||||
|
old_message = Message.create(:text => "hello", :author_id => @user1.person.id, :conversation_id => @cnv.id)
|
||||||
|
@message_hash[:id] = old_message.id
|
||||||
|
post :create, @message_hash
|
||||||
|
old_message.reload.text.should == 'hello'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
context 'on a post from a stranger' do
|
||||||
|
before do
|
||||||
|
@create_hash[:author] = eve.person
|
||||||
|
@cnv = Conversation.create(@create_hash)
|
||||||
|
@message_hash = {:conversation_id => @cnv.id, :text => "here is something else"}
|
||||||
|
end
|
||||||
|
it 'posts no comment' do
|
||||||
|
Message.should_not_receive(:new)
|
||||||
|
post :create, @message_hash
|
||||||
|
response.code.should == '406'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue