Sha256: e845c0d3d3709eed9e780389f14da06faecebb137502c23daa2c9a784f6d2d3c

Contents?: true

Size: 1.99 KB

Versions: 18

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Thredded
  class AutofollowUsers
    def initialize(post)
      @post = post
    end

    def run
      new_followers.each do |user, reason|
        Thredded::UserTopicFollow.create_unless_exists(user.id, post.postable_id, reason)
      end
    end

    # @return [Array<Thredded.user_class>]
    def mentioned_users
      @mentioned_users ||= Thredded::AtNotificationExtractor.new(post).run
    end

    # @return [Hash<Thredded.user_class, Symbol]>] a map of users that should get subscribed to their the follow reason.
    def new_followers
      result = {}
      auto_followers.each { |user| result[user] = :auto }
      exclude_follow_on_mention_opt_outs(mentioned_users).each { |user| result[user] = :mentioned }
      result.delete(post.user)
      result
    end

    private

    attr_reader :post

    # Returns the users that have:
    #   UserMessageboardPreference#auto_follow_topics? && UserPreference#auto_follow_topics?
    # If the `user_preference` for a given does not exist, its default value is used.
    # @return [Enumerable<Thredded.user_class>]
    def auto_followers
      user_board_prefs = post.messageboard.user_messageboard_preferences.each_with_object({}) do |ump, h|
        h[ump.user_id] = ump
      end
      Thredded.user_class.includes(:thredded_user_preference)
        .select(Thredded.user_class.primary_key)
        .find_each(batch_size: 50_000).select do |user|

        result = user_board_prefs[user.id]
        result ||= Thredded::UserMessageboardPreference.new(
          messageboard: post.messageboard,
          user_preference: user.thredded_user_preference
        )
        result.auto_follow_topics?
      end
    end

    # @return [Enumerable<Thredded.user_class>]
    def exclude_follow_on_mention_opt_outs(users)
      users.select do |user|
        user.thredded_user_preference.follow_topics_on_mention? &&
          user.thredded_user_messageboard_preferences.in(post.messageboard).follow_topics_on_mention?
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 2 rubygems

Version Path
thredded-0.16.10 app/commands/thredded/autofollow_users.rb
thredded-0.16.9 app/commands/thredded/autofollow_users.rb
thredded-0.16.8 app/commands/thredded/autofollow_users.rb
thredded-0.16.7 app/commands/thredded/autofollow_users.rb
thredded-0.16.6 app/commands/thredded/autofollow_users.rb
thredded-0.16.5 app/commands/thredded/autofollow_users.rb
thredded-0.16.4 app/commands/thredded/autofollow_users.rb
thredded-0.16.3 app/commands/thredded/autofollow_users.rb
thredded-0.16.1 app/commands/thredded/autofollow_users.rb
thredded-0.16.0 app/commands/thredded/autofollow_users.rb
thredded-0.15.5 app/commands/thredded/autofollow_users.rb
thredded-0.15.4 app/commands/thredded/autofollow_users.rb
thredded-0.15.3 app/commands/thredded/autofollow_users.rb
thredded-0.15.2 app/commands/thredded/autofollow_users.rb
thredded-0.15.1 app/commands/thredded/autofollow_users.rb
threddedDANIEL-0.14.5 app/commands/thredded/autofollow_users.rb
thredded-0.14.4 app/commands/thredded/autofollow_users.rb
thredded-0.14.3 app/commands/thredded/autofollow_users.rb