User notes from conversation messages; still needs UI

Change-Id: I6747e590ce54d76349ed44a314c73abaea2b912f
Reviewed-on: https://gerrit.instructure.com/4869
Reviewed-by: Jon Jensen <jon@instructure.com>
Tested-by: Hudson <hudson@instructure.com>
This commit is contained in:
Jacob Fugal 2011-07-30 12:03:18 -06:00
parent d169efb400
commit 7214776a70
5 changed files with 57 additions and 21 deletions

View File

@ -51,6 +51,7 @@ class ConversationsController < ApplicationController
if @recipient_ids.present? && params[:body].present?
@conversation = @current_user.initiate_conversation(@recipient_ids)
message = @conversation.add_message(params[:body])
message.generate_user_note if params[:user_note]
render :json => {:participants => jsonify_users(@conversation.participants),
:conversation => jsonify_conversation(@conversation.reload),
:message => message}
@ -119,6 +120,7 @@ class ConversationsController < ApplicationController
def add_message
if params[:body].present?
message = @conversation.add_message(params[:body])
message.generate_user_note if params[:user_note]
render :json => {:conversation => jsonify_conversation(@conversation.reload), :message => message}
else
render :json => {}, :status => :bad_request

View File

@ -85,4 +85,15 @@ class ConversationMessage < ActiveRecord::Base
:current_user => author.short_name
end
end
def generate_user_note
return unless recipients.size == 1
recipient = recipients.first
return unless recipient.grants_right?(author, :create_user_notes)
self.extend TextHelper
title = t(:subject, "Private message, %{timestamp}", :timestamp => date_string(created_at))
note = format_message(body).first
recipient.user_notes.create(:creator => author, :title => title, :note => note)
end
end

View File

@ -68,25 +68,4 @@ class UserNote < ActiveRecord::Base
self.user.save
end
def self.add_from_message(message)
return unless message && message.recipients.size == 1
to = message.recipient_users.first
from = message.user
if to.grants_right?(from, :create_user_notes)
note = to.user_notes.new
note.created_by_id = from.id
note.title = t :subject, "%{message_subject} (Added from a message)", :message_subject => message.subject
note.note = message.body
if root_note = message.root_context_message
note.note += "\n\n-------------------------\n"
note.note += t :in_reply_to, "In reply to: %{message_subject}\nFrom: %{user}\n\n", :message_subject => root_note.subject, :user => root_note.user.name
note.note += root_note.body
end
# The note content built up above is all plaintext, but note is an html field.
self.extend TextHelper
note.note = format_message(note.note).first
note.save
end
end
end

View File

@ -105,6 +105,23 @@ describe ConversationsController do
response.should be_success
@conversation.messages.size.should == 2
end
it "should generate a user note when requested" do
course_with_teacher_logged_in(:active_all => true)
conversation
post 'add_message', :conversation_id => @conversation.id, :body => "hello world"
response.should be_success
message = @conversation.messages.first # newest message is first
student = message.recipients.first
student.user_notes.size.should == 0
post 'add_message', :conversation_id => @conversation.id, :body => "make a note", :user_note => 1
response.should be_success
message = @conversation.messages.first
student = message.recipients.first
student.user_notes.size.should == 1
end
end
describe "POST 'add_recipients'" do

View File

@ -92,4 +92,31 @@ describe ConversationMessage do
event.messages_sent["Added To Conversation"].map(&:user_id).should_not be_include(@first_student.id)
end
end
context "generate_user_note" do
it "should add a user note under nominal circumstances" do
course_with_teacher
student = student_in_course.user
conversation = @teacher.initiate_conversation([student.id])
message = conversation.add_message("reprimanded!")
message.created_at = Time.at(0) # Jan 1, 1970 00:00:00 UTC
note = message.generate_user_note
student.user_notes.size.should be(1)
student.user_notes.first.should eql(note)
note.creator.should eql(@teacher)
note.title.should eql("Private message, Jan 1, 1970")
note.note.should eql("reprimanded!")
end
it "should fail if there's more than one recipient" do
course_with_teacher
student1 = student_in_course.user
student2 = student_in_course.user
conversation = @teacher.initiate_conversation([student1.id, student2.id])
message = conversation.add_message("message")
message.generate_user_note.should be_nil
student1.user_notes.size.should be(0)
student2.user_notes.size.should be(0)
end
end
end