messages are now relayable, a comment has an author as opposed to a person.

This commit is contained in:
danielvincent 2011-03-01 12:12:08 -08:00
parent c62e9db397
commit 11309574cf
29 changed files with 120 additions and 104 deletions

View file

@ -9,20 +9,14 @@ class ConversationsController < ApplicationController
end
def create
person_ids = Contact.where(:id => params[:conversation][:contact_ids]).map! do |contact|
person_ids = Contact.where(:id => params[:conversation].delete(:contact_ids)).map! do |contact|
contact.person_id
end
person_ids = person_ids | [current_user.person.id]
params[:conversation][:participant_ids] = person_ids | [current_user.person.id]
params[:conversation][:author] = current_user.person
@conversation = Conversation.new(:subject => params[:conversation][:subject], :participant_ids => person_ids)
if @conversation.save
@message = Message.new(:text => params[:message][:text], :author => current_user.person, :conversation_id => @conversation.id )
unless @message.save
@conversation.destroy
end
end
@conversation = Conversation.create(params[:conversation])
respond_with @conversation
end

View file

@ -48,7 +48,7 @@ module SocketsHelper
v = render_to_string(:partial => 'people/person', :locals => person_hash)
elsif object.is_a? Comment
v = render_to_string(:partial => 'comments/comment', :locals => {:comment => object, :person => object.person})
v = render_to_string(:partial => 'comments/comment', :locals => {:comment => object, :person => object.author})
elsif object.is_a? Notification
v = render_to_string(:partial => 'notifications/popup', :locals => {:note => object, :person => opts[:actor]})

View file

@ -29,16 +29,16 @@ class Comment < ActiveRecord::Base
self.text.strip! unless self.text.nil?
end
def diaspora_handle
person.diaspora_handle
self.author.diaspora_handle
end
def diaspora_handle= nh
self.person = Webfinger.new(nh).fetch
self.author = Webfinger.new(nh).fetch
end
def notification_type(user, person)
if self.post.person == user.person
if self.post.author == user.person
return Notifications::CommentOnPost
elsif self.post.comments.where(:person_id => user.person.id) != [] && self.person_id != user.person.id
elsif self.post.comments.where(:author_id => user.person.id) != [] && self.author_id != user.person.id
return Notifications::AlsoCommented
else
return false

View file

@ -4,16 +4,20 @@ class Conversation < ActiveRecord::Base
include Diaspora::Webhooks
xml_attr :subject
xml_attr :messages, :as => [Message]
xml_attr :created_at
xml_attr :messages, :as => [Message]
xml_reader :diaspora_handle
xml_reader :participant_handles
has_many :conversation_visibilities
has_many :participants, :class_name => 'Person', :through => :conversation_visibilities, :source => :person
has_many :messages, :order => 'created_at ASC'
belongs_to :author, :class_name => 'Person'
def self.create(opts={})
msg_opts = opts.delete(:message)
opts = opts.dup
msg_opts = {:author => opts[:author], :text => opts.delete(:text)}
cnv = super(opts)
message = Message.new(msg_opts.merge({:conversation_id => cnv.id}))
@ -21,18 +25,20 @@ class Conversation < ActiveRecord::Base
cnv
end
def author
self.messages.first.author
end
def recipients
self.participants - [self.author]
end
def diaspora_handle
self.author.diaspora_handle
end
def diaspora_handle= nh
self.author = Webfinger.new(nh).fetch
end
def participant_handles
self.participants.map{|p| p.diaspora_handle}.join(";")
end
def participant_handles= handles
handles.split(';').each do |handle|
self.participants << Webfinger.new(handle).fetch

View file

@ -13,14 +13,16 @@ class Message < ActiveRecord::Base
belongs_to :author, :class_name => 'Person'
belongs_to :conversation
after_initialize do
after_create do
#sign comment as commenter
self.author_signature = self.sign_with_key(self.author.owner.encryption_key) if self.author.owner
if !self.parent.blank? && self.parent.author.person.owns?(self.parent)
if !self.parent.blank? && self.author.owns?(self.parent)
#sign comment as post owner
self.parent_author_signature = self.sign_with_key( self.parent.author.owner.encryption_key) if self.parent.author.owner
end
self.save!
self
end
def diaspora_handle

View file

@ -24,6 +24,13 @@ class Post < ActiveRecord::Base
after_destroy :propogate_retraction
def author
self.person
end
def author= author
self.person = author
end
def user_refs
self.post_visibilities.count
end

View file

@ -133,7 +133,7 @@ class User < ActiveRecord::Base
######## Commenting ########
def build_comment(text, options = {})
comment = Comment.new(:person_id => self.person.id,
comment = Comment.new(:author_id => self.person.id,
:text => text,
:post => options[:on])
comment.set_guid

View file

@ -3,10 +3,10 @@
-# the COPYRIGHT file.
%li.comment{:data=>{:guid => comment.id}, :class => ("hidden" if(defined? hidden))}
= person_image_link(comment.person)
= person_image_link(comment.author)
.content
%strong
= person_link(comment.person)
= person_link(comment.author)
= markdownify(comment.text, :youtube_maps => comment.youtube_titles)

View file

@ -20,6 +20,7 @@ class CreateConversationsAndMessagesAndVisibilities < ActiveRecord::Migration
create_table :conversations do |t|
t.string :subject
t.string :guid, :null => false
t.integer :author_id, :null => false
t.timestamps
end

View file

@ -0,0 +1,13 @@
class RenamePersonToAuthor < ActiveRecord::Migration
def self.up
remove_foreign_key(:comments, :people)
rename_column :comments, :person_id, :author_id
add_foreign_key(:comments, :people, :column => :author_id, :dependent => :delete)
end
def self.down
remove_foreign_key(:comments, :people, :column => :author_id)
rename_column :comments, :author_id, :person_id
add_foreign_key(:comments, :people, :dependent => :delete)
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20110228233419) do
ActiveRecord::Schema.define(:version => 20110301014507) do
create_table "aspect_memberships", :force => true do |t|
t.integer "aspect_id", :null => false
@ -41,7 +41,7 @@ ActiveRecord::Schema.define(:version => 20110228233419) do
create_table "comments", :force => true do |t|
t.text "text", :null => false
t.integer "post_id", :null => false
t.integer "person_id", :null => false
t.integer "author_id", :null => false
t.string "guid", :null => false
t.text "author_signature"
t.text "parent_author_signature"
@ -51,9 +51,9 @@ ActiveRecord::Schema.define(:version => 20110228233419) do
t.string "mongo_id"
end
add_index "comments", ["author_id"], :name => "index_comments_on_person_id"
add_index "comments", ["guid"], :name => "index_comments_on_guid", :unique => true
add_index "comments", ["mongo_id"], :name => "index_comments_on_mongo_id"
add_index "comments", ["person_id"], :name => "index_comments_on_person_id"
add_index "comments", ["post_id"], :name => "index_comments_on_post_id"
create_table "contacts", :force => true do |t|
@ -85,6 +85,7 @@ ActiveRecord::Schema.define(:version => 20110228233419) do
create_table "conversations", :force => true do |t|
t.string "subject"
t.string "guid", :null => false
t.integer "author_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
@ -511,7 +512,7 @@ ActiveRecord::Schema.define(:version => 20110228233419) do
add_foreign_key "aspect_memberships", "aspects", :name => "aspect_memberships_aspect_id_fk"
add_foreign_key "aspect_memberships", "contacts", :name => "aspect_memberships_contact_id_fk", :dependent => :delete
add_foreign_key "comments", "people", :name => "comments_person_id_fk", :dependent => :delete
add_foreign_key "comments", "people", :name => "comments_author_id_fk", :column => "author_id", :dependent => :delete
add_foreign_key "comments", "posts", :name => "comments_post_id_fk", :dependent => :delete
add_foreign_key "contacts", "people", :name => "contacts_person_id_fk", :dependent => :delete

View file

@ -89,7 +89,7 @@ module Diaspora
accessors = self.class.roxml_attrs.collect do |definition|
definition.accessor
end
['author', 'author_signature', 'parent_author_signature'].each do |acc|
['author_signature', 'parent_author_signature'].each do |acc|
accessors.delete acc
end
accessors

View file

@ -7,7 +7,7 @@ module Diaspora
module Querying
def find_visible_post_by_id( id )
self.raw_visible_posts.where(:id => id).includes({:person => :profile}, {:comments => {:person => :profile}}, :photos).first
self.raw_visible_posts.where(:id => id).includes({:person => :profile}, {:comments => {:author => :profile}}, :photos).first
end
def raw_visible_posts

View file

@ -6,22 +6,22 @@ class PostsFake
end
def initialize(posts)
person_ids = []
posts.each do |p|
person_ids << p.person_id
author_ids = []
posts.each do |p|
author_ids << p.person_id
p.comments.each do |c|
person_ids << c.person_id
author_ids << c.author_id
end
end
people = Person.where(:id => person_ids).includes(:profile)
people = Person.where(:id => author_ids).includes(:profile)
@people_hash = {}
people.each{|person| @people_hash[person.id] = person}
@post_fakes = posts.map do |post|
f = Fake.new(post, self)
f = Fake.new(post, self)
f.comments = post.comments.map do |comment|
Fake.new(comment, self)
Fake.new(comment, self)
end
f
end

View file

@ -26,7 +26,7 @@ class Postzord::Dispatch
user_ids = [*local_people].map{|x| x.owner_id }
local_users = User.where(:id => user_ids)
self.notify_users(local_users)
local_users << @sender if @object.person.local?
local_users << @sender if @object.author.local?
self.socket_to_users(local_users)
else
self.deliver_to_local(local_people)
@ -73,7 +73,7 @@ class Postzord::Dispatch
def notify_users(users)
users.each do |user|
Resque.enqueue(Job::NotifyLocalUsers, user.id, @object.class.to_s, @object.id, @object.person_id)
Resque.enqueue(Job::NotifyLocalUsers, user.id, @object.class.to_s, @object.id, @object.author.id)
end
end
def socket_to_users(users)

View file

@ -41,11 +41,11 @@ describe CommentsController do
post :create, comment_hash
response.code.should == '201'
end
it "doesn't overwrite person_id" do
it "doesn't overwrite author_id" do
new_user = Factory.create(:user)
comment_hash[:person_id] = new_user.person.id.to_s
comment_hash[:author_id] = new_user.person.id.to_s
post :create, comment_hash
Comment.find_by_text(comment_hash[:text]).person_id.should == @user1.person.id
Comment.find_by_text(comment_hash[:text]).author_id.should == @user1.person.id
end
it "doesn't overwrite id" do
old_comment = @user1.comment("hello", :on => @post)

View file

@ -11,9 +11,9 @@ describe ConversationVisibilitiesController do
@user1 = alice
sign_in :user, @user1
@create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => "cool stuff" }
@conversation = Conversation.create(@create_hash)
hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => 'not spam', :text => 'cool stuff'}
@conversation = Conversation.create(hash)
end
describe '#destroy' do

View file

@ -21,14 +21,12 @@ describe ConversationsController do
response.should be_success
end
it 'retrieves all messages for a user' do
@conversation_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => 'not spam' }
@message_hash = {:author => @user1.person, :text => 'cool stuff'}
it 'retrieves all conversations for a user' do
hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => 'not spam', :text => 'cool stuff'}
3.times do
cnv = Conversation.create(@conversation_hash)
Message.create(@message_hash.merge({:conversation_id => cnv.id}))
cnv = Conversation.create(hash)
end
get :index
@ -38,34 +36,38 @@ describe ConversationsController do
describe '#create' do
before do
@message_hash = {:conversation => {
:contact_ids => [@user1.contacts.first.id],
:subject => "secret stuff"},
:message => {:text => "text"}
}
@hash = {:conversation => {
:contact_ids => [@user1.contacts.first.id],
:subject => "secret stuff",
:text => 'text'}}
end
it 'creates a conversation' do
lambda {
post :create, @message_hash
post :create, @hash
}.should change(Conversation, :count).by(1)
end
it 'creates a message' do
lambda {
post :create, @message_hash
post :create, @hash
}.should change(Message, :count).by(1)
end
it 'sets the author to the current_user' do
pending
@hash[:author] = Factory.create(:user)
post :create, @hash
Message.first.author.should == @user1.person
Conversation.first.author.should == @user1.person
end
end
describe '#show' do
before do
conversation_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => 'not spam' }
message_hash = {:author => @user1.person, :text => 'cool stuff'}
@conversation = Conversation.create(conversation_hash)
@message = Message.create(message_hash.merge({:conversation_id => @conversation.id}))
hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => 'not spam', :text => 'cool stuff'}
@conversation = Conversation.create(hash)
end
it 'succeeds' do

View file

@ -85,8 +85,8 @@ end
Factory.define(:comment) do |comment|
comment.sequence(:text) {|n| "#{n} cats"}
comment.association(:person)
comment.association :post, :factory => :status_message
comment.association(:author, :factory => :person)
comment.association(:post, :factory => :status_message)
end
Factory.define(:notification) do |n|

View file

@ -197,7 +197,7 @@ describe 'a user receives a post' do
post_in_db.comments.should == []
receive_with_zord(@user2, @user1.person, @xml)
post_in_db.comments(true).first.person.should == @user3.person
post_in_db.comments(true).first.author.should == @user3.person
end
it 'should correctly marshal a stranger for the downstream user' do
@ -225,7 +225,7 @@ describe 'a user receives a post' do
receive_with_zord(@user2, @user1.person, @xml)
post_in_db.comments(true).first.person.should == remote_person
post_in_db.comments(true).first.author.should == remote_person
end
end

View file

@ -412,7 +412,7 @@ describe DataConversion::ImportToMysql do
@migrator.process_raw_comments
comment = Comment.first
comment.post_id.should == Post.where(:mongo_id => "4d2b6ebecc8cb43cc2000029").first.id
comment.person_id.should == Person.where(:mongo_id => "4d2b6eb7cc8cb43cc2000017").first.id
comment.author_id.should == Person.where(:mongo_id => "4d2b6eb7cc8cb43cc2000017").first.id
end
end
describe "notifications" do

View file

@ -20,7 +20,7 @@ describe Diaspora::Parser do
describe "parsing compliant XML object" do
it 'should be able to correctly parse comment fields' do
post = @user1.post :status_message, :message => "hello", :to => @aspect1.id
comment = Factory.create(:comment, :post => post, :person => @person, :diaspora_handle => @person.diaspora_handle, :text => "Freedom!")
comment = Factory.create(:comment, :post => post, :author => @person, :diaspora_handle => @person.diaspora_handle, :text => "Freedom!")
comment.delete
xml = comment.to_diaspora_xml
comment_from_xml = Diaspora::Parser.from_xml(xml)

View file

@ -8,7 +8,7 @@ describe PostsFake do
@people << post.person
4.times do
comment = Factory(:comment, :post => post)
@people << comment.person
@people << comment.author
end
@posts << post
end
@ -30,7 +30,7 @@ describe PostsFake do
end
end
describe PostsFake::Fake do
include Rails.application.routes.url_helpers
include Rails.application.routes.url_helpers
before do
@post = mock()
@fakes = mock()

View file

@ -134,7 +134,7 @@ describe Postzord::Dispatch do
end
context "remote raphael" do
before do
@comment = Factory.build(:comment, :person => @remote_raphael, :post => @post)
@comment = Factory.build(:comment, :author => @remote_raphael, :post => @post)
@comment.save
@mailman = Postzord::Dispatch.new(@local_luke, @comment)
end

View file

@ -3,7 +3,7 @@
# the COPYRIGHT file.
require 'spec_helper'
require File.join(Rails.root, "spec", "lib", "diaspora", "relayable_spec")
require File.join(Rails.root, "spec", "shared_behaviors", "relayable")
describe Comment do
before do
@ -80,7 +80,7 @@ describe Comment do
@marshalled_comment = Comment.from_xml(@xml)
end
it 'marshals the author' do
@marshalled_comment.person.should == @commenter.person
@marshalled_comment.author.should == @commenter.person
end
it 'marshals the post' do
@marshalled_comment.post.should == @post

View file

@ -9,14 +9,8 @@ describe Conversation do
@user1 = alice
@user2 = bob
@create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], :subject => "cool stuff",
:message => {:author => @user1.person, :text => 'hey'}}
=begin
@message = Message.new(:author => @user1.person, :text => "stuff")
@cnv.messages << @message
@message.save
@xml = @cnv.to_diaspora_xml
=end
@create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => "cool stuff", :text => 'hey'}
end
it 'creates a message on create' do

View file

@ -3,34 +3,28 @@
# the COPYRIGHT file.
require 'spec_helper'
require File.join(Rails.root, "spec", "lib", "diaspora", "relayable_spec")
require File.join(Rails.root, "spec", "shared_behaviors", "relayable")
describe Message do
before do
@user1 = alice
@user2 = bob
@create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], :subject => "cool stuff",
:message => {:author => @user1.person, :text => "stuff"} }
@create_hash = { :author => @user1.person, :participant_ids => [@user1.contacts.first.person.id, @user1.person.id],
:subject => "cool stuff", :text => "stuff"}
@cnv = Conversation.create(@create_hash)
@message = @cnv.messages.first
@xml = @message.to_diaspora_xml
end
describe '#after_initialize' do
before do
@create_hash = { :participant_ids => [@user1.contacts.first.person.id, @user1.person.id], :subject => "cool stuff"}
@cnv = Conversation.new(@create_hash)
@cnv.save
@msg = Message.new(:text => "21312", :conversation => @cnv)
end
describe '#before_create' do
it 'signs the message' do
@msg.author_signature.should_not be_blank
@message.author_signature.should_not be_blank
end
it 'signs the message author if author of conversation' do
@msg.parent_author_signature.should_not be_blank
@message.parent_author_signature.should_not be_blank
end
end
@ -46,6 +40,7 @@ describe Message do
it 'serializes the created_at time' do
@xml.should include(@message.created_at.to_s)
end
it 'serializes the conversation_guid time' do
@xml.should include(@message.conversation.guid)
end
@ -55,12 +50,12 @@ describe Message do
before do
@local_luke, @local_leia, @remote_raphael = set_up_friends
cnv_hash = {:subject => 'cool story, bro', :participant_ids => [@local_luke.person, @local_leia.person, @remote_raphael].map(&:id),
:message => {:author => @remote_raphael, :text => 'hey'}}
cnv_hash = {:author => @remote_raphael, :participant_ids => [@local_luke.person, @local_leia.person, @remote_raphael].map(&:id),
:subject => 'cool story, bro', :text => 'hey'}
@remote_parent = Conversation.create(cnv_hash.dup)
cnv_hash[:message][:author] = @local_luke.person
cnv_hash[:author] = @local_luke.person
@local_parent = Conversation.create(cnv_hash)
msg_hash = {:author => @local_luke.person, :text => 'yo', :conversation => @local_parent}

View file

@ -154,8 +154,8 @@ describe Person do
end
it "deletes a person's comments on person deletion" do
Factory.create(:comment, :person_id => @deleter.id, :diaspora_handle => @deleter.diaspora_handle, :text => "i love you", :post => @other_status)
Factory.create(:comment, :person_id => @person.id,:diaspora_handle => @person.diaspora_handle, :text => "you are creepy", :post => @other_status)
Factory.create(:comment, :author_id => @deleter.id, :diaspora_handle => @deleter.diaspora_handle, :text => "i love you", :post => @other_status)
Factory.create(:comment, :author_id => @person.id,:diaspora_handle => @person.diaspora_handle, :text => "you are creepy", :post => @other_status)
lambda {@deleter.destroy}.should change(Comment, :count).by(-1)
end

View file

@ -61,6 +61,7 @@ describe Diaspora::Relayable do
end
it 'sockets to the user' do
pending
@object_by_recipient.should_receive(:socket_to_user).exactly(3).times
@object_by_recipient.receive(@local_luke, @local_leia.person)
end