remove after_dispatch hook
handle photos in StatusMessageCreationService
This commit is contained in:
parent
20dabbd15f
commit
7184d4334e
7 changed files with 56 additions and 74 deletions
|
|
@ -66,11 +66,6 @@ class StatusMessage < Post
|
||||||
write_attribute(:text, text)
|
write_attribute(:text, text)
|
||||||
end
|
end
|
||||||
|
|
||||||
def attach_photos_by_ids(photo_ids)
|
|
||||||
return [] unless photo_ids.present?
|
|
||||||
self.photos << Photo.where(:id => photo_ids, :author_id => self.author_id)
|
|
||||||
end
|
|
||||||
|
|
||||||
def nsfw
|
def nsfw
|
||||||
self.raw_message.match(/#nsfw/i) || super
|
self.raw_message.match(/#nsfw/i) || super
|
||||||
end
|
end
|
||||||
|
|
@ -110,24 +105,6 @@ class StatusMessage < Post
|
||||||
self.mentions.where(:person_id => person.id).first.try(:notify_recipient)
|
self.mentions.where(:person_id => person.id).first.try(:notify_recipient)
|
||||||
end
|
end
|
||||||
|
|
||||||
def after_dispatch(sender)
|
|
||||||
self.update_and_dispatch_attached_photos(sender)
|
|
||||||
end
|
|
||||||
|
|
||||||
def update_and_dispatch_attached_photos(sender)
|
|
||||||
if self.photos.any?
|
|
||||||
logger.info "dispatch photos for StatusMessage:#{guid}"
|
|
||||||
Photo.where(status_message_guid: guid).update_all(:public => self.public)
|
|
||||||
self.photos.each do |photo|
|
|
||||||
if photo.pending
|
|
||||||
sender.add_to_streams(photo, self.aspects)
|
|
||||||
sender.dispatch_post(photo)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
Photo.where(status_message_guid: guid).update_all(:pending => false)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def comment_email_subject
|
def comment_email_subject
|
||||||
message.title
|
message.title
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -42,8 +42,13 @@ class StatusMessageCreationService
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_photos(status_message, photos)
|
def add_photos(status_message, photos)
|
||||||
status_message.attach_photos_by_ids(photos)
|
if photos.present?
|
||||||
status_message.photos.each {|photo| photo.public = status_message.public }
|
status_message.photos << Photo.where(id: photos, author_id: status_message.author_id)
|
||||||
|
status_message.photos.each do |photo|
|
||||||
|
photo.public = status_message.public
|
||||||
|
photo.pending = false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def process(status_message, aspect_ids, services)
|
def process(status_message, aspect_ids, services)
|
||||||
|
|
@ -55,6 +60,7 @@ class StatusMessageCreationService
|
||||||
def add_to_streams(status_message, aspect_ids)
|
def add_to_streams(status_message, aspect_ids)
|
||||||
aspects = user.aspects_from_ids(aspect_ids)
|
aspects = user.aspects_from_ids(aspect_ids)
|
||||||
user.add_to_streams(status_message, aspects)
|
user.add_to_streams(status_message, aspects)
|
||||||
|
status_message.photos.each {|photo| user.add_to_streams(photo, aspects) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def dispatch(status_message, services)
|
def dispatch(status_message, services)
|
||||||
|
|
|
||||||
|
|
@ -42,11 +42,6 @@ module Diaspora
|
||||||
def subscribers
|
def subscribers
|
||||||
raise 'You must override subscribers in order to enable federation on this model'
|
raise 'You must override subscribers in order to enable federation on this model'
|
||||||
end
|
end
|
||||||
|
|
||||||
# @param [User] sender
|
|
||||||
# @note this is a hook(optional)
|
|
||||||
def after_dispatch(sender)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -64,18 +64,11 @@ class Postzord::Dispatcher
|
||||||
def post
|
def post
|
||||||
self.deliver_to_services(@opts[:url], @opts[:services] || [])
|
self.deliver_to_services(@opts[:url], @opts[:services] || [])
|
||||||
self.post_to_subscribers if @subscribers.present?
|
self.post_to_subscribers if @subscribers.present?
|
||||||
self.process_after_dispatch_hooks
|
|
||||||
@object
|
@object
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
# @return [Object]
|
|
||||||
def process_after_dispatch_hooks
|
|
||||||
@object.after_dispatch(@sender)
|
|
||||||
@object
|
|
||||||
end
|
|
||||||
|
|
||||||
def post_to_subscribers
|
def post_to_subscribers
|
||||||
remote_people, local_people = @subscribers.partition{ |person| person.owner_id.nil? }
|
remote_people, local_people = @subscribers.partition{ |person| person.owner_id.nil? }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -222,7 +222,6 @@ describe StatusMessagesController, :type => :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
it "sets the pending bit of referenced photos" do
|
it "sets the pending bit of referenced photos" do
|
||||||
skip # TODO
|
|
||||||
inlined_jobs do
|
inlined_jobs do
|
||||||
post :create, @hash
|
post :create, @hash
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -361,28 +361,6 @@ describe StatusMessage, type: :model do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#after_dispatch" do
|
|
||||||
before do
|
|
||||||
@photos = [alice.build_post(:photo, pending: true, user_file: File.open(photo_fixture_name)),
|
|
||||||
alice.build_post(:photo, pending: true, user_file: File.open(photo_fixture_name))]
|
|
||||||
@photos.each(&:save!)
|
|
||||||
@status_message = alice.build_post(:status_message, text: "the best pebble.")
|
|
||||||
@status_message.photos << @photos
|
|
||||||
@status_message.save!
|
|
||||||
alice.add_to_streams(@status_message, alice.aspects)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "sets pending to false on any attached photos" do
|
|
||||||
@status_message.after_dispatch(alice)
|
|
||||||
expect(@photos.all? {|p| p.reload.pending }).to be false
|
|
||||||
end
|
|
||||||
|
|
||||||
it "dispatches any attached photos" do
|
|
||||||
expect(alice).to receive(:dispatch_post).twice
|
|
||||||
@status_message.after_dispatch(alice)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "oembed" do
|
describe "oembed" do
|
||||||
let(:youtube_url) { "https://www.youtube.com/watch?v=3PtFwlKfvHI" }
|
let(:youtube_url) { "https://www.youtube.com/watch?v=3PtFwlKfvHI" }
|
||||||
let(:message_text) { "#{youtube_url} is so cool. so is this link -> https://joindiaspora.com" }
|
let(:message_text) { "#{youtube_url} is so cool. so is this link -> https://joindiaspora.com" }
|
||||||
|
|
|
||||||
|
|
@ -99,24 +99,58 @@ describe StatusMessageCreationService do
|
||||||
expect(photos.map(&:id).map(&:to_s)).to match_array(photo_ids)
|
expect(photos.map(&:id).map(&:to_s)).to match_array(photo_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "it marks the photos as non-public if the post is non-public" do
|
|
||||||
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids, public: false))
|
|
||||||
status_message.photos.each do |photo|
|
|
||||||
expect(photo.public).to be_falsey
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "it marks the photos as public if the post is public" do
|
|
||||||
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids, public: true))
|
|
||||||
status_message.photos.each do |photo|
|
|
||||||
expect(photo.public).to be_truthy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "does not attach photos without photos param" do
|
it "does not attach photos without photos param" do
|
||||||
status_message = StatusMessageCreationService.new(alice).create(params)
|
status_message = StatusMessageCreationService.new(alice).create(params)
|
||||||
expect(status_message.photos).to be_empty
|
expect(status_message.photos).to be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with aspect_ids" do
|
||||||
|
it "it marks the photos as non-public if the post is non-public" do
|
||||||
|
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids))
|
||||||
|
status_message.photos.each do |photo|
|
||||||
|
expect(photo.public).to be_falsey
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "creates aspect_visibilities for the Photo" do
|
||||||
|
alice.aspects.create(name: "another aspect")
|
||||||
|
|
||||||
|
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids))
|
||||||
|
status_message.photos.each do |photo|
|
||||||
|
expect(photo.aspect_visibilities.map(&:aspect)).to eq([aspect])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not create aspect_visibilities if the post is public" do
|
||||||
|
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids, public: true))
|
||||||
|
status_message.photos.each do |photo|
|
||||||
|
expect(photo.aspect_visibilities).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sets pending to false on any attached photos" do
|
||||||
|
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids))
|
||||||
|
status_message.photos.each do |photo|
|
||||||
|
expect(photo.reload.pending).to be_falsey
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with public" do
|
||||||
|
it "it marks the photos as public if the post is public" do
|
||||||
|
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids, public: true))
|
||||||
|
status_message.photos.each do |photo|
|
||||||
|
expect(photo.public).to be_truthy
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "sets pending to false on any attached photos" do
|
||||||
|
status_message = StatusMessageCreationService.new(alice).create(params.merge(photos: photo_ids, public: true))
|
||||||
|
status_message.photos.each do |photo|
|
||||||
|
expect(photo.reload.pending).to be_falsey
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "dispatch" do
|
context "dispatch" do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue