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]
|
target = current_user.find_visible_post_by_id params[:comment][:post_id]
|
||||||
text = params[:comment][:text]
|
text = params[:comment][:text]
|
||||||
|
|
||||||
@comment = current_user.comment(text, :on => target) if target
|
@comment = current_user.build_comment(text, :on => target)
|
||||||
if @comment
|
|
||||||
|
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}")
|
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|
|
respond_to do |format|
|
||||||
format.js{ render :json => { :post_id => @comment.post_id,
|
format.js{ render :json => { :post_id => @comment.post_id,
|
||||||
:comment_id => @comment.id,
|
:comment_id => @comment.id,
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ class Comment
|
||||||
belongs_to :post, :class_name => "Post"
|
belongs_to :post, :class_name => "Post"
|
||||||
belongs_to :person, :class_name => "Person"
|
belongs_to :person, :class_name => "Person"
|
||||||
|
|
||||||
validates_presence_of :text, :diaspora_handle
|
validates_presence_of :text, :diaspora_handle, :post
|
||||||
validates_with HandleValidator
|
validates_with HandleValidator
|
||||||
|
|
||||||
before_save do
|
before_save do
|
||||||
|
|
|
||||||
|
|
@ -152,7 +152,7 @@ class User
|
||||||
|
|
||||||
def build_post(class_name, opts = {})
|
def build_post(class_name, opts = {})
|
||||||
opts[:person] = self.person
|
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 = class_name.to_s.camelize.constantize
|
||||||
model_class.instantiate(opts)
|
model_class.instantiate(opts)
|
||||||
|
|
@ -164,9 +164,12 @@ class User
|
||||||
aspect_ids = validate_aspect_permissions(aspect_ids)
|
aspect_ids = validate_aspect_permissions(aspect_ids)
|
||||||
self.raw_visible_posts << post
|
self.raw_visible_posts << post
|
||||||
self.save
|
self.save
|
||||||
|
|
||||||
|
#socket post
|
||||||
Rails.logger.info("event=dispatch user=#{diaspora_handle} post=#{post.id.to_s}")
|
Rails.logger.info("event=dispatch user=#{diaspora_handle} post=#{post.id.to_s}")
|
||||||
push_to_aspects(post, aspect_ids)
|
push_to_aspects(post, aspect_ids)
|
||||||
post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) && !post.pending
|
post.socket_to_uid(id, :aspect_ids => aspect_ids) if post.respond_to?(:socket_to_uid) && !post.pending
|
||||||
|
|
||||||
if post.public
|
if post.public
|
||||||
self.services.each do |service|
|
self.services.each do |service|
|
||||||
self.send("post_to_#{service.provider}".to_sym, service, post.message)
|
self.send("post_to_#{service.provider}".to_sym, service, post.message)
|
||||||
|
|
@ -271,35 +274,41 @@ class User
|
||||||
######## Commenting ########
|
######## Commenting ########
|
||||||
def comment(text, options = {})
|
def comment(text, options = {})
|
||||||
comment = build_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
|
dispatch_comment comment
|
||||||
comment.socket_to_uid id
|
|
||||||
end
|
end
|
||||||
comment
|
comment
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_comment(text, options = {})
|
def build_comment(text, options = {})
|
||||||
raise "must comment on something!" unless options[:on]
|
comment = Comment.new(:person_id => self.person.id,
|
||||||
comment = Comment.new(:person_id => self.person.id, :diaspora_handle => self.person.diaspora_handle, :text => text, :post => options[:on])
|
:diaspora_handle => self.person.diaspora_handle,
|
||||||
comment.creator_signature = comment.sign_with_key(encryption_key)
|
:text => text,
|
||||||
if comment.save
|
:post => options[:on])
|
||||||
comment
|
|
||||||
else
|
#sign comment as commenter
|
||||||
Rails.logger.warn "event=build_comment status=save_failure user=#{self.diaspora_handle} comment=#{comment.id}"
|
comment.creator_signature = comment.sign_with_key(self.encryption_key)
|
||||||
false
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
|
comment
|
||||||
end
|
end
|
||||||
|
|
||||||
def dispatch_comment(comment)
|
def dispatch_comment(comment)
|
||||||
if owns? comment.post
|
if owns? comment.post
|
||||||
|
#push DOWNSTREAM (to original audience)
|
||||||
Rails.logger.info "event=dispatch_comment direction=downstream user=#{self.diaspora_handle} comment=#{comment.id}"
|
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)
|
aspects = aspects_with_post(comment.post_id)
|
||||||
push_to_people(comment, people_in_aspects(aspects))
|
push_to_people(comment, people_in_aspects(aspects))
|
||||||
|
|
||||||
elsif owns? comment
|
elsif owns? comment
|
||||||
|
#push UPSTREAM (to poster)
|
||||||
Rails.logger.info "event=dispatch_comment direction=upstream user=#{self.diaspora_handle} comment=#{comment.id}"
|
Rails.logger.info "event=dispatch_comment direction=upstream user=#{self.diaspora_handle} comment=#{comment.id}"
|
||||||
comment.save
|
|
||||||
push_to_people comment, [comment.post.person]
|
push_to_people comment, [comment.post.person]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -100,18 +100,32 @@ module Diaspora
|
||||||
end
|
end
|
||||||
|
|
||||||
def receive_comment comment
|
def receive_comment comment
|
||||||
|
|
||||||
|
commenter = comment.person
|
||||||
|
|
||||||
unless comment.post.person == self.person || comment.verify_post_creator_signature
|
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}")
|
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
|
return
|
||||||
end
|
end
|
||||||
self.visible_people = self.visible_people | [comment.person]
|
|
||||||
|
self.visible_people = self.visible_people | [commenter]
|
||||||
self.save
|
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)
|
unless owns?(comment)
|
||||||
dispatch_comment comment
|
dispatch_comment comment
|
||||||
|
comment.save
|
||||||
end
|
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
|
comment
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,7 @@ describe CommentsController do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
sign_in :user, user
|
sign_in :user, user
|
||||||
|
EM.stub!(:next_tick).and_yield(:block)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#create' do
|
describe '#create' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue