diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 51cdf7bae..8f7806a18 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -13,10 +13,14 @@ class CommentsController < ApplicationController target = current_user.find_visible_post_by_id params[:comment][:post_id] text = params[:comment][:text] - @comment = current_user.comment(text, :on => target) if target - if @comment + @comment = current_user.build_comment(text, :on => target) + + if @comment.save(:safe => true) + raise 'MongoMapper failed to catch a failed save' unless @comment.id Rails.logger.info("event=comment_create user=#{current_user.diaspora_handle} status=success comment=#{@comment.id}") + current_user.dispatch_comment(@comment) + respond_to do |format| format.js{ render :json => { :post_id => @comment.post_id, :comment_id => @comment.id, diff --git a/app/models/comment.rb b/app/models/comment.rb index f7390a16a..524fcbf05 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -33,7 +33,7 @@ class Comment belongs_to :post, :class_name => "Post" belongs_to :person, :class_name => "Person" - validates_presence_of :text, :diaspora_handle + validates_presence_of :text, :diaspora_handle, :post validates_with HandleValidator before_save do diff --git a/app/models/user.rb b/app/models/user.rb index 002c01cd1..c19aaf592 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -152,7 +152,7 @@ class User def build_post(class_name, opts = {}) opts[:person] = self.person - opts[:diaspora_handle] = self.person.diaspora_handle + opts[:diaspora_handle] = opts[:person].diaspora_handle model_class = class_name.to_s.camelize.constantize model_class.instantiate(opts) @@ -164,9 +164,12 @@ class User aspect_ids = validate_aspect_permissions(aspect_ids) self.raw_visible_posts << post self.save + + #socket post Rails.logger.info("event=dispatch user=#{diaspora_handle} post=#{post.id.to_s}") push_to_aspects(post, aspect_ids) post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) && !post.pending + if post.public self.services.each do |service| self.send("post_to_#{service.provider}".to_sym, service, post.message) @@ -271,35 +274,41 @@ class User ######## Commenting ######## def comment(text, options = {}) comment = build_comment(text, options) - if comment + + if comment.save + raise 'MongoMapper failed to catch a failed save' unless comment.id dispatch_comment comment - comment.socket_to_uid id end comment end def build_comment(text, options = {}) - raise "must comment on something!" unless options[:on] - comment = Comment.new(:person_id => self.person.id, :diaspora_handle => self.person.diaspora_handle, :text => text, :post => options[:on]) - comment.creator_signature = comment.sign_with_key(encryption_key) - if comment.save - comment - else - Rails.logger.warn "event=build_comment status=save_failure user=#{self.diaspora_handle} comment=#{comment.id}" - false + comment = Comment.new(:person_id => self.person.id, + :diaspora_handle => self.person.diaspora_handle, + :text => text, + :post => options[:on]) + + #sign comment as commenter + comment.creator_signature = comment.sign_with_key(self.encryption_key) + + if !comment.post_id.blank? && owns?(comment.post) + #sign comment as post owner + comment.post_creator_signature = comment.sign_with_key(self.encryption_key) end + + comment end def dispatch_comment(comment) if owns? comment.post + #push DOWNSTREAM (to original audience) Rails.logger.info "event=dispatch_comment direction=downstream user=#{self.diaspora_handle} comment=#{comment.id}" - comment.post_creator_signature = comment.sign_with_key(encryption_key) - comment.save aspects = aspects_with_post(comment.post_id) push_to_people(comment, people_in_aspects(aspects)) + elsif owns? comment + #push UPSTREAM (to poster) Rails.logger.info "event=dispatch_comment direction=upstream user=#{self.diaspora_handle} comment=#{comment.id}" - comment.save push_to_people comment, [comment.post.person] end end diff --git a/lib/diaspora/user/receiving.rb b/lib/diaspora/user/receiving.rb index a69ad11c3..62809085c 100644 --- a/lib/diaspora/user/receiving.rb +++ b/lib/diaspora/user/receiving.rb @@ -100,18 +100,32 @@ module Diaspora end def receive_comment comment + + commenter = comment.person + unless comment.post.person == self.person || comment.verify_post_creator_signature Rails.logger.info("event=receive status=abort reason='comment signature not valid' recipient=#{self.diaspora_handle} sender=#{comment.post.person.diaspora_handle} payload_type=#{comment.class} post_id=#{comment.post_id}") return end - self.visible_people = self.visible_people | [comment.person] + + self.visible_people = self.visible_people | [commenter] self.save - comment.person.save - comment.save! + + commenter.save + + #sign comment as the post creator if you've been hit UPSTREAM + if owns? comment.post + comment.post_creator_signature = comment.sign_with_key(encryption_key) + comment.save + end + + #dispatch comment DOWNSTREAM, received it via UPSTREAM unless owns?(comment) dispatch_comment comment + comment.save end - comment.socket_to_uid(id) if (comment.respond_to?(:socket_to_uid) && !self.owns?(comment)) + + comment.socket_to_uid(self.id, :aspect_ids => comment.post.aspect_ids) comment end diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index 4074792a9..0c2e94c07 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -15,13 +15,14 @@ describe CommentsController do before do sign_in :user, user + EM.stub!(:next_tick).and_yield(:block) end describe '#create' do let(:comment_hash) { {:comment =>{ :text =>"facebook, is that you?", - :post_id =>"#{@post.id}"}} + :post_id =>"#{@post.id}"}} } context "on a post from a contact" do