Class: NewsmastMastodon::BanStatusService

Inherits:
Object
  • Object
show all
Includes:
DatabaseHelper, Redisable
Defined in:
app/services/newsmast_mastodon/ban_status_service.rb

Instance Method Summary collapse

Instance Method Details

#check_and_ban_status(status) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/services/newsmast_mastodon/ban_status_service.rb', line 10

def check_and_ban_status(status)
  setting_filter_types = ['Content filters', 'Spam filters']

  @status = status

  with_read_replica do
    setting_filter_types.each do |setting_filter_type|
      # The keys wil generated as below
      # 1.) content_filters_banned_status_ids
      # 2.) span_filters_banned_status_ids
      redis_key = "#{setting_filter_type.downcase.gsub(/\s+/, '_')}_banned_status_ids"
      server_setting = NewsmastMastodon::ServerSetting.find_by(name: setting_filter_type)
      next unless server_setting&.value

      filters = fetch_filters_from_all_keys(server_setting.name)
      active_filters = filters.map { |f| JSON.parse(f) }.select { |f| f['is_active'] }
      active_filters.each do |f|
        keyword = f['keyword']
        filter_type = f['filter_type'].downcase

        # Check if the keyword matches in the account object
        # This will ban the account if the keyword is found in username, display_name, or note
        # This is done before checking hashtags or content to ensure account banning is prioritized
        (keyword)

        if filter_type == 'hashtag' || filter_type == 'both'
          tag_ids = @status.tags.where(name: keyword.downcase.gsub('#', '')).ids
          if tag_ids.present?
            # If the tag is banned, update its is_banned attribute to true
            # to trigger update_tags callback for elastic search
            with_primary do
              Tag.where(id: tag_ids).find_each do |tag|
                tag.update(listable: false, trendable: false)
              end
            end
            redis.zadd(redis_key, @status.id, @status.id)
          end
        end

        if filter_type == 'both' || filter_type == 'content'
          include_keyword = @status.search_word_in_status(keyword)
          if include_keyword
            redis.zadd(redis_key, @status.id, @status.id)
          end
        end
      end
    end
  end

  # Combine banned status_ids both content & spam filters
  # And also remove if the records exceeded 400 limit
  return combine_banned_status_ids(@status.id)
end

#global_keyword_matches_in_status?(status_id, community_id, filter_type) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/services/newsmast_mastodon/ban_status_service.rb', line 82

def global_keyword_matches_in_status?(status_id, community_id, filter_type)
  @status = Status.find(status_id)

  cache_key = "global_filter_keywords_#{filter_type}"
  global_filter_keywords = Rails.cache.fetch(cache_key, expires_in: 24.hours) do
    NewsmastMastodon::CommunityFilterKeyword.where(patchwork_community_id: nil, filter_type: filter_type).to_a
  end

  return false if filter_type == 'filter_out' && global_filter_keywords.empty?

  return true if filter_type == 'filter_in' && global_filter_keywords.empty?

  global_filter_keywords.any? do |keyword|
    if keyword.is_filter_hashtag
      search_term = keyword.keyword.downcase.strip
      @status.tags.where("LOWER(name) = ?", search_term).present?
    else
      @status.search_word_in_status(keyword.keyword)
    end
  end
end

#keyword_matches_in_status?(status_id, community_id, filter_type) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/services/newsmast_mastodon/ban_status_service.rb', line 64

def keyword_matches_in_status?(status_id, community_id, filter_type)
  @status = Status.find(status_id)
  filter_keywords = NewsmastMastodon::CommunityFilterKeyword.where(patchwork_community_id: community_id, filter_type: filter_type)

  return false if filter_type == 'filter_out' && filter_keywords.empty?

  return true if filter_type == 'filter_in' && filter_keywords.empty?

  filter_keywords.any? do |keyword|
    if keyword.is_filter_hashtag
      search_term = keyword.keyword.downcase.strip
      @status.tags.where("LOWER(name) = ?", search_term).present?
    else
      @status.search_word_in_status(keyword.keyword)
    end
  end
end