refactored comments to work like status_messages in the controller. weird stuff was happening when using EM::next_tick; holding off on that for now.
This commit is contained in:
parent
8f67f90307
commit
f59c6e2427
5 changed files with 50 additions and 22 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ describe CommentsController do
|
|||
|
||||
before do
|
||||
sign_in :user, user
|
||||
EM.stub!(:next_tick).and_yield(:block)
|
||||
end
|
||||
|
||||
describe '#create' do
|
||||
|
|
|
|||
Loading…
Reference in a new issue