build_like and build_comment now take only an options hash, and call build_relayable

This commit is contained in:
Raphael Sofaer 2011-05-30 15:40:16 -07:00
parent 71c3a29aa6
commit 537de1ce4c
10 changed files with 36 additions and 44 deletions

View file

@ -14,7 +14,7 @@ class CommentsController < ApplicationController
text = params[:text]
if target
@comment = current_user.build_comment(text, :on => target)
@comment = current_user.build_comment(:text => text, :on => target)
if @comment.save
Rails.logger.info("event=create type=comment user=#{current_user.diaspora_handle} status=success comment=#{@comment.id} chars=#{params[:text].length}")

View file

@ -12,7 +12,7 @@ class LikesController < ApplicationController
target = current_user.find_visible_post_by_id params[:post_id]
positive = (params[:positive] == 'true') ? true : false
if target
@like = current_user.build_like(positive, :on => target)
@like = current_user.build_like(:positive => positive, :on => target)
if @like.save
Rails.logger.info("event=create type=like user=#{current_user.diaspora_handle} status=success like=#{@like.id} positive=#{positive}")

View file

@ -63,7 +63,7 @@ class Message < ActiveRecord::Base
vis.save
self
else
raise NotVisibileException("Attempting to access a ConversationVisibility that does not exist!")
raise NotVisibleException("Attempting to access a ConversationVisibility that does not exist!")
end
end

View file

@ -153,38 +153,30 @@ class User < ActiveRecord::Base
Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
end
######## Commenting ########
def build_comment(text, options = {})
comment = Comment.new(:author_id => self.person.id,
:text => text,
:post => options[:on])
comment.set_guid
#sign comment as commenter
comment.author_signature = comment.sign_with_key(self.encryption_key)
def build_relayable(model, options = {})
options[:post] = options.delete(:on)
m = model.new(options.merge(:author_id => self.person.id))
m.set_guid
if !comment.post_id.blank? && person.owns?(comment.parent)
#sign comment as post owner
comment.parent_author_signature = comment.sign_with_key(self.encryption_key)
#sign relayable as model creator
m.author_signature = m.sign_with_key(self.encryption_key)
if !m.post_id.blank? && person.owns?(m.parent)
#sign relayable as parent object owner
m.parent_author_signature = m.sign_with_key(self.encryption_key)
end
comment
m
end
######## Commenting ########
def build_comment(options = {})
build_relayable(Comment, options)
end
######## Liking ########
def build_like(positive, options = {})
like = Like.new(:author_id => self.person.id,
:positive => positive,
:post => options[:on])
like.set_guid
#sign like as liker
like.author_signature = like.sign_with_key(self.encryption_key)
if !like.post_id.blank? && person.owns?(like.parent)
#sign like as post owner
like.parent_author_signature = like.sign_with_key(self.encryption_key)
end
like
def build_like(options = {})
build_relayable(Like, options)
end
def liked?(post)
@ -194,7 +186,7 @@ class User < ActiveRecord::Base
return false
end
end
def like_for(post)
[post.likes, post.dislikes].each do |likes|
likes.each do |like|

View file

@ -104,7 +104,7 @@ describe AspectsController do
post.save!
@posts << post
end
alice.build_comment('lalala', :on => @posts.first ).save
alice.build_comment(:text => 'lalala', :on => @posts.first ).save
end
describe "post visibilities" do

View file

@ -11,7 +11,7 @@ describe LikesController do
@aspect1 = @user1.aspects.first
@aspect2 = @user2.aspects.first
@controller.stub(:current_user).and_return(alice)
sign_in :user, @user1
end
@ -76,7 +76,7 @@ describe LikesController do
context 'your like' do
before do
@message = bob.post(:status_message, :text => "hey", :to => @aspect1.id)
@like = alice.build_like(true, :on => @message)
@like = alice.build_like(:positive => true, :on => @message)
@like.save
end
@ -87,11 +87,11 @@ describe LikesController do
end
it 'does not let a user destroy other likes' do
like2 = eve.build_like(true, :on => @message)
like2 = eve.build_like(:positive => true, :on => @message)
like2.save
expect {
delete :destroy, :format => "js", :post_id => like2.post_id, :id => like2.id
expect {
delete :destroy, :format => "js", :post_id => like2.post_id, :id => like2.id
}.should_not change(Like, :count)
end
end

View file

@ -84,7 +84,7 @@ describe Postzord::Dispatch do
end
context "local leia" do
before do
@comment = @local_leia.build_comment "yo", :on => @post
@comment = @local_leia.build_comment :text => "yo", :on => @post
@comment.save
end
context "local leia's mailman" do
@ -156,7 +156,7 @@ describe Postzord::Dispatch do
end
context "local luke" do
before do
@comment = @local_luke.build_comment "yo", :on => @post
@comment = @local_luke.build_comment :text => "yo", :on => @post
@comment.save
@mailman = Postzord::Dispatch.new(@local_luke, @comment)
end
@ -182,7 +182,7 @@ describe Postzord::Dispatch do
context "remote raphael's post is commented on by local luke" do
before do
@post = Factory(:status_message, :author => @remote_raphael)
@comment = @local_luke.build_comment "yo", :on => @post
@comment = @local_luke.build_comment :text => "yo", :on => @post
@comment.save
@mailman = Postzord::Dispatch.new(@local_luke, @comment)
end

View file

@ -104,7 +104,7 @@ describe Comment do
mock_http.should_receive(:get).with(/\/feeds\/api\/videos/, nil).twice.and_return(
[nil, 'Foobar <title>'+expected_title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
comment = alice.build_comment url, :on => @message
comment = alice.build_comment :text => url, :on => @message
comment.save!
Comment.find(comment.id).youtube_titles.should == { first_video_id => CGI::escape(expected_title), second_video_id => CGI::escape(expected_title) }
@ -118,7 +118,7 @@ describe Comment do
@local_parent = @local_luke.post :status_message, :text => "hi", :to => @local_luke.aspects.first
@object_by_parent_author = @local_luke.comment("yo", :on => @local_parent)
@object_by_recipient = @local_leia.build_comment("yo", :on => @local_parent)
@object_by_recipient = @local_leia.build_comment(:text => "yo", :on => @local_parent)
@dup_object_by_parent_author = @object_by_parent_author.dup
@object_on_remote_parent = @local_luke.comment("Yeah, it was great", :on => @remote_parent)

View file

@ -91,7 +91,7 @@ describe Like do
@local_parent = @local_luke.post :status_message, :text => "foobar", :to => @local_luke.aspects.first
@object_by_parent_author = @local_luke.like(1, :on => @local_parent)
@object_by_recipient = @local_leia.build_like(1, :on => @local_parent)
@object_by_recipient = @local_leia.build_like(:positive => 1, :on => @local_parent)
@dup_object_by_parent_author = @object_by_parent_author.dup
@object_on_remote_parent = @local_luke.like(0, :on => @remote_parent)

View file

@ -27,7 +27,7 @@ class User
def comment(text, options = {})
fantasy_resque do
c = build_comment(text, options)
c = build_comment(options.merge(:text => text))
if c.save!
Postzord::Dispatch.new(self, c).post
end
@ -37,7 +37,7 @@ class User
def like(positive, options ={})
fantasy_resque do
l = build_like(positive, options)
l = build_like(options.merge(:positive => positive))
if l.save!
Postzord::Dispatch.new(self, l).post
end