about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2018-10-07 23:44:58 +0200
committerGitHub <noreply@github.com>2018-10-07 23:44:58 +0200
commit774ac473736cbf348827cf6d861e7fbbb72d7623 (patch)
treec8ce87b43cd8fa58f7124a8a6baf93a71cb274e8 /app/services
parent25744d43b0c9ae58227e1e46ac9e2b33a7944925 (diff)
Add conversations API (#8832)
* Add conversations API

* Add web UI for conversations

* Add test for conversations API

* Add tests for ConversationAccount

* Improve web UI

* Rename ConversationAccount to AccountConversation

* Remove conversations on block and mute

* Change last_status_id to be a denormalization of status_ids

* Add optimistic locking
Diffstat (limited to 'app/services')
-rw-r--r--app/services/after_block_service.rb37
-rw-r--r--app/services/fan_out_on_write_service.rb6
-rw-r--r--app/services/mute_service.rb4
-rw-r--r--app/services/notify_service.rb22
4 files changed, 55 insertions, 14 deletions
diff --git a/app/services/after_block_service.rb b/app/services/after_block_service.rb
index a7dce08c7..706db0d63 100644
--- a/app/services/after_block_service.rb
+++ b/app/services/after_block_service.rb
@@ -2,16 +2,43 @@
 
 class AfterBlockService < BaseService
   def call(account, target_account)
-    FeedManager.instance.clear_from_timeline(account, target_account)
+    clear_home_feed(account, target_account)
     clear_notifications(account, target_account)
+    clear_conversations(account, target_account)
   end
 
   private
 
+  def clear_home_feed(account, target_account)
+    FeedManager.instance.clear_from_timeline(account, target_account)
+  end
+
+  def clear_conversations(account, target_account)
+    AccountConversation.where(account: account)
+                       .where('? = ANY(participant_account_ids)', target_account.id)
+                       .in_batches
+                       .destroy_all
+  end
+
   def clear_notifications(account, target_account)
-    Notification.where(account: account).joins(:follow).where(activity_type: 'Follow', follows: { account_id: target_account.id }).delete_all
-    Notification.where(account: account).joins(mention: :status).where(activity_type: 'Mention', statuses: { account_id: target_account.id }).delete_all
-    Notification.where(account: account).joins(:favourite).where(activity_type: 'Favourite', favourites: { account_id: target_account.id }).delete_all
-    Notification.where(account: account).joins(:status).where(activity_type: 'Status', statuses: { account_id: target_account.id }).delete_all
+    Notification.where(account: account)
+                .joins(:follow)
+                .where(activity_type: 'Follow', follows: { account_id: target_account.id })
+                .delete_all
+
+    Notification.where(account: account)
+                .joins(mention: :status)
+                .where(activity_type: 'Mention', statuses: { account_id: target_account.id })
+                .delete_all
+
+    Notification.where(account: account)
+                .joins(:favourite)
+                .where(activity_type: 'Favourite', favourites: { account_id: target_account.id })
+                .delete_all
+
+    Notification.where(account: account)
+                .joins(:status)
+                .where(activity_type: 'Status', statuses: { account_id: target_account.id })
+                .delete_all
   end
 end
diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb
index 5efd3edb2..ab520276b 100644
--- a/app/services/fan_out_on_write_service.rb
+++ b/app/services/fan_out_on_write_service.rb
@@ -13,6 +13,7 @@ class FanOutOnWriteService < BaseService
     if status.direct_visibility?
       deliver_to_mentioned_followers(status)
       deliver_to_direct_timelines(status)
+      deliver_to_own_conversation(status)
     else
       deliver_to_followers(status)
       deliver_to_lists(status)
@@ -99,6 +100,11 @@ class FanOutOnWriteService < BaseService
     status.mentions.includes(:account).each do |mention|
       Redis.current.publish("timeline:direct:#{mention.account.id}", @payload) if mention.account.local?
     end
+
     Redis.current.publish("timeline:direct:#{status.account.id}", @payload) if status.account.local?
   end
+
+  def deliver_to_own_conversation(status)
+    AccountConversation.add_status(status.account, status)
+  end
 end
diff --git a/app/services/mute_service.rb b/app/services/mute_service.rb
index c6122a152..676804cb9 100644
--- a/app/services/mute_service.rb
+++ b/app/services/mute_service.rb
@@ -5,11 +5,13 @@ class MuteService < BaseService
     return if account.id == target_account.id
 
     mute = account.mute!(target_account, notifications: notifications)
+
     if mute.hide_notifications?
       BlockWorker.perform_async(account.id, target_account.id)
     else
-      FeedManager.instance.clear_from_timeline(account, target_account)
+      MuteWorker.perform_async(account.id, target_account.id)
     end
+
     mute
   end
 end
diff --git a/app/services/notify_service.rb b/app/services/notify_service.rb
index 7d0dcc7ad..63bf8f17a 100644
--- a/app/services/notify_service.rb
+++ b/app/services/notify_service.rb
@@ -8,9 +8,10 @@ class NotifyService < BaseService
 
     return if recipient.user.nil? || blocked?
 
-    create_notification
-    push_notification if @notification.browserable?
-    send_email if email_enabled?
+    create_notification!
+    push_notification! if @notification.browserable?
+    push_to_conversation! if direct_message?
+    send_email! if email_enabled?
   rescue ActiveRecord::RecordInvalid
     return
   end
@@ -100,18 +101,23 @@ class NotifyService < BaseService
     end
   end
 
-  def create_notification
+  def create_notification!
     @notification.save!
   end
 
-  def push_notification
+  def push_notification!
     return if @notification.activity.nil?
 
     Redis.current.publish("timeline:#{@recipient.id}", Oj.dump(event: :notification, payload: InlineRenderer.render(@notification, @recipient, :notification)))
-    send_push_notifications
+    send_push_notifications!
   end
 
-  def send_push_notifications
+  def push_to_conversation!
+    return if @notification.activity.nil?
+    AccountConversation.add_status(@recipient, @notification.target_status)
+  end
+
+  def send_push_notifications!
     subscriptions_ids = ::Web::PushSubscription.where(user_id: @recipient.user.id)
                                                .select { |subscription| subscription.pushable?(@notification) }
                                                .map(&:id)
@@ -121,7 +127,7 @@ class NotifyService < BaseService
     end
   end
 
-  def send_email
+  def send_email!
     return if @notification.activity.nil?
     NotificationMailer.public_send(@notification.type, @recipient, @notification).deliver_later(wait: 2.minutes)
   end