| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ActivityPub::DistributionWorker | 
					
						
							|  |  |  |   include Sidekiq::Worker | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   sidekiq_options queue: 'push' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def perform(status_id) | 
					
						
							|  |  |  |     @status  = Status.find(status_id) | 
					
						
							|  |  |  |     @account = @status.account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return if skip_distribution? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ActivityPub::DeliveryWorker.push_bulk(inboxes) do |inbox_url| | 
					
						
							| 
									
										
										
										
											2018-12-30 08:48:59 +00:00
										 |  |  |       [payload, @account.id, inbox_url] | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-07-13 00:16:06 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     relay! if relayable? | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  |   rescue ActiveRecord::RecordNotFound | 
					
						
							|  |  |  |     true | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def skip_distribution? | 
					
						
							| 
									
										
										
										
											2018-10-17 15:13:04 +00:00
										 |  |  |     @status.direct_visibility? || @status.limited_visibility? | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 00:16:06 +00:00
										 |  |  |   def relayable? | 
					
						
							|  |  |  |     @status.public_visibility? | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  |   def inboxes | 
					
						
							| 
									
										
										
										
											2018-12-30 18:00:04 +00:00
										 |  |  |     # Deliver the status to all followers. | 
					
						
							|  |  |  |     # If the status is a reply to another local status, also forward it to that | 
					
						
							|  |  |  |     # status' authors' followers. | 
					
						
							|  |  |  |     @inboxes ||= if @status.reply? && @status.thread.account.local? && @status.distributable? | 
					
						
							|  |  |  |                    @account.followers.or(@status.thread.account.followers).inboxes | 
					
						
							|  |  |  |                  else | 
					
						
							|  |  |  |                    @account.followers.inboxes | 
					
						
							|  |  |  |                  end | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-26 11:47:38 +00:00
										 |  |  |   def signed_payload | 
					
						
							| 
									
										
										
										
											2018-12-30 08:48:59 +00:00
										 |  |  |     Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@account)) | 
					
						
							| 
									
										
										
										
											2017-08-26 11:47:38 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-30 08:48:59 +00:00
										 |  |  |   def unsigned_payload | 
					
						
							|  |  |  |     ActiveModelSerializers::SerializableResource.new( | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  |       @status, | 
					
						
							|  |  |  |       serializer: ActivityPub::ActivitySerializer, | 
					
						
							|  |  |  |       adapter: ActivityPub::Adapter | 
					
						
							| 
									
										
										
										
											2017-08-26 11:47:38 +00:00
										 |  |  |     ).as_json | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2018-07-13 00:16:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-30 08:48:59 +00:00
										 |  |  |   def payload | 
					
						
							|  |  |  |     @payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-13 00:16:06 +00:00
										 |  |  |   def relay! | 
					
						
							|  |  |  |     ActivityPub::DeliveryWorker.push_bulk(Relay.enabled.pluck(:inbox_url)) do |inbox_url| | 
					
						
							| 
									
										
										
										
											2018-12-30 08:48:59 +00:00
										 |  |  |       [payload, @account.id, inbox_url] | 
					
						
							| 
									
										
										
										
											2018-07-13 00:16:06 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2017-08-12 22:44:41 +00:00
										 |  |  | end |