basic views / controller actions for private messages

This commit is contained in:
danielvincent 2011-02-25 15:02:54 -08:00
parent 9fe0320881
commit c5801ffb37
8 changed files with 147 additions and 4 deletions

View file

@ -1,2 +1,38 @@
class PrivateMessagesController < ApplicationController
before_filter :authenticate_user!
respond_to :html
def index
@messages = PrivateMessage.joins(:private_message_visibilities).where(
:private_message_visibilities => {:person_id => current_user.person.id}).all
end
def create
person_ids = Contact.where(:id => params[:private_message][:contact_ids]).map! do |contact|
contact.person_id
end
person_ids = person_ids | [current_user.person.id]
@message = PrivateMessage.new( :author => current_user.person, :participant_ids => person_ids, :message => params[:private_message][:message] )
if @message.save
Rails.logger.info("event=create type=private_message chars=#{params[:private_message][:message].length}")
end
respond_with @message
end
def show
@message = PrivateMessage.joins(:private_message_visibilities).where(:id => params[:id],
:private_message_visibilities => {:person_id => current_user.person.id}).first
if @message
respond_with @message
else
redirect_to private_messages_path
end
end
end

View file

@ -80,7 +80,7 @@ class StatusMessagesController < ApplicationController
def show
@status_message = current_user.find_visible_post_by_id params[:id]
if @status_message
if @status_messag
@object_aspect_ids = @status_message.aspects.map{|a| a.id}
# mark corresponding notification as read

View file

@ -1,5 +1,12 @@
class PrivateMessage < ActiveRecord::Base
include ROXML
include Diaspora::Guid
belongs_to :author, :class_name => 'Person'
has_many :private_message_visibilities
has_many :recipients, :class_name => 'Person', :through => :private_message_visibilities, :source => :person
has_many :participants, :class_name => 'Person', :through => :private_message_visibilities, :source => :person
def recipients
self.participants - [self.author]
end
end

View file

@ -3,3 +3,15 @@
-# the COPYRIGHT file.
= link_to 'new message', new_private_message_path
%br
%br
%br
- for message in @messages
%b
= message.author.name
%br
= link_to message.message, message
%hr

View file

@ -9,7 +9,7 @@
= form_for PrivateMessage.new do |private_message|
%h4
to
= private_message.text_field :recipients
= text_field_tag "private_message[contact_ids]"
%h4
message

View file

@ -2,4 +2,28 @@
-# licensed under the Affero General Public License version 3 or later. See
-# the COPYRIGHT file.
= link_to 'back', private_messages_path
%br
%br
%br
%br
%h4
from
= @message.author.name
%br
%br
%h4
to
- for recipient in @message.recipients
= recipient.name
%br
%br
%h4
message
= @message.message

View file

@ -1,5 +1,70 @@
require 'spec_helper'
describe PrivateMessagesController do
render_views
before do
@user1 = alice
sign_in :user, @user1
end
describe '#new' do
it 'succeeds' do
get :new
response.should be_success
end
end
describe '#index' do
it 'succeeds' do
get :index
response.should be_success
end
it 'retrieves all messages for a user' do
@create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:author => @user1.person, :message => "cool stuff" }
3.times do
PrivateMessage.create(@create_hash)
end
get :index
assigns[:messages].count.should == 3
end
end
describe '#create' do
it 'creates a private message' do
message_hash = {:private_message => {
:contact_ids => [@user1.contacts.first.id],
:message => "secret stuff"}}
lambda {
post :create, message_hash
}.should change(PrivateMessage, :count).by(1)
end
end
describe '#show' do
before do
@create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:author => @user1.person, :message => "cool stuff" }
@message = PrivateMessage.create(@create_hash)
end
it 'succeeds' do
get :show, :id => @message.id
response.should be_success
assigns[:message].should == @message
end
it 'does not let you access messages where you are not a recipient' do
user2 = eve
sign_in :user, user2
get :show, :id => @message.id
response.code.should == '302'
end
end
end

View file

@ -1,5 +1,4 @@
require 'spec_helper'
describe PrivateMessage do
pending "add some examples to (or delete) #{__FILE__}"
end