Module: NewsmastMastodon::Concerns::FeedManagerConcern

Extended by:
ActiveSupport::Concern
Defined in:
app/models/newsmast_mastodon/concerns/feed_manager_concern.rb

Instance Method Summary collapse

Instance Method Details

#filter_from_custom?(status, account, crutches = nil) ⇒ Boolean

Filter for custom timeline.

Parameters:

  • status (Status)
  • account (Account)
  • crutches (Hash) (defaults to: nil)

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
# File 'app/models/newsmast_mastodon/concerns/feed_manager_concern.rb', line 126

def filter_from_custom?(status, , crutches = nil)
  crutches ||= build_crutches(.id, [status])

  base_filter = filter_from_home(status, .id, crutches, :custom)
  return true if base_filter == :filter

  false
end

#merge_into_custom(from_account, into_account) ⇒ void

This method returns an undefined value.

Merge an account’s statuses into custom timeline.

Parameters:

  • from_account (Account)
  • into_account (Account)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/newsmast_mastodon/concerns/feed_manager_concern.rb', line 79

def merge_into_custom(, )
  return unless .user&.signed_in_recently?

  timeline_key = key(:custom, .id)
  aggregate    = .user&.aggregates_reblogs?
  query        = .statuses.list_eligible_visibility
                             .includes(:preloadable_poll, :media_attachments, reblog: :account)
                             .limit(FeedManager::MAX_ITEMS / 4)

  if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
    oldest_score = redis.zrange(timeline_key, 0, 0, with_scores: true).first.last.to_i
    query = query.where('id > ?', oldest_score)
  end

  statuses = query.to_a
  crutches = build_crutches(.id, statuses)

  statuses.each do |status|
    next if filter_from_custom?(status, , crutches)

    add_to_feed(:custom, .id, status, aggregate_reblogs: aggregate)
  end

  trim(:custom, .id)
end

#populate_custom(account) ⇒ void

This method returns an undefined value.

Populate custom timeline from scratch.

Parameters:

  • account (Account)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/models/newsmast_mastodon/concerns/feed_manager_concern.rb', line 52

def populate_custom()
  limit        = FeedManager::MAX_ITEMS / 2
  aggregate    = .user&.aggregates_reblogs?
  timeline_key = key(:custom, .id)

  statuses = Status
             .where(account: custom_timeline_accounts_for())
             .list_eligible_visibility
             .includes(:preloadable_poll, :media_attachments, :account, reblog: :account)
             .limit(limit)
             .order(id: :desc)

  crutches = build_crutches(.id, statuses)

  statuses.each do |status|
    next if filter_from_custom?(status, , crutches)

    add_to_feed(:custom, .id, status, aggregate_reblogs: aggregate)
  end

  trim(:custom, .id)
end

#push_to_custom(account, status, update: false) ⇒ Boolean

Push a status to custom timeline.

Parameters:

  • account (Account)
  • status (Status)
  • update (Boolean) (defaults to: false)

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/newsmast_mastodon/concerns/feed_manager_concern.rb', line 19

def push_to_custom(, status, update: false)
  return false unless .user&.signed_in_recently?
  return false if filter_from_custom?(status, )
  return false unless add_to_feed(:custom, .id, status, aggregate_reblogs: .user&.aggregates_reblogs?)

  trim(:custom, .id)
  if push_update_required?("timeline:custom:#{.id}")
    PushUpdateWorker.perform_async(.id, status.id, "timeline:custom:#{.id}", { 'update' => update })
  end
  true
end

#unmerge_from_custom(from_account, into_account) ⇒ void

This method returns an undefined value.

Remove an account’s statuses from custom timeline.

Parameters:

  • from_account (Account)
  • into_account (Account)


109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/newsmast_mastodon/concerns/feed_manager_concern.rb', line 109

def unmerge_from_custom(, )
  timeline_key        = key(:custom, .id)
  timeline_status_ids = redis.zrange(timeline_key, 0, -1)

  .statuses.select(:id, :reblog_of_id)
              .where(id: timeline_status_ids)
              .reorder(nil)
              .find_each do |status|
    remove_from_feed(:custom, .id, status, aggregate_reblogs: .user&.aggregates_reblogs?)
  end
end

#unpush_from_custom(account, status, update: false) ⇒ Boolean

Remove a status from custom timeline.

Parameters:

  • account (Account)
  • status (Status)
  • update (Boolean) (defaults to: false)

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/newsmast_mastodon/concerns/feed_manager_concern.rb', line 36

def unpush_from_custom(, status, update: false)
  return false unless NewsmastMastodon::CommunityAdmin.table_exists?
  return false unless NewsmastMastodon::CommunityAdmin.exists?(
    is_boost_bot: true,
    account_id: .id,
    account_status: NewsmastMastodon::CommunityAdmin.[:active]
  )
  return false unless remove_from_feed(:custom, .id, status, aggregate_reblogs: .user&.aggregates_reblogs?)

  redis.publish("timeline:custom:#{.id}", Oj.dump(event: :delete, payload: status.id.to_s)) unless update
  true
end