Gab Social. All are welcome.
This commit is contained in:
80
app/presenters/account_relationships_presenter.rb
Normal file
80
app/presenters/account_relationships_presenter.rb
Normal file
@@ -0,0 +1,80 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AccountRelationshipsPresenter
|
||||
attr_reader :following, :followed_by, :blocking, :blocked_by,
|
||||
:muting, :requested, :domain_blocking,
|
||||
:endorsed
|
||||
|
||||
def initialize(account_ids, current_account_id, **options)
|
||||
@account_ids = account_ids.map { |a| a.is_a?(Account) ? a.id : a }
|
||||
@current_account_id = current_account_id
|
||||
|
||||
@following = cached[:following].merge(Account.following_map(@uncached_account_ids, @current_account_id))
|
||||
@followed_by = cached[:followed_by].merge(Account.followed_by_map(@uncached_account_ids, @current_account_id))
|
||||
@blocking = cached[:blocking].merge(Account.blocking_map(@uncached_account_ids, @current_account_id))
|
||||
@blocked_by = cached[:blocked_by].merge(Account.blocked_by_map(@uncached_account_ids, @current_account_id))
|
||||
@muting = cached[:muting].merge(Account.muting_map(@uncached_account_ids, @current_account_id))
|
||||
@requested = cached[:requested].merge(Account.requested_map(@uncached_account_ids, @current_account_id))
|
||||
@domain_blocking = cached[:domain_blocking].merge(Account.domain_blocking_map(@uncached_account_ids, @current_account_id))
|
||||
@endorsed = cached[:endorsed].merge(Account.endorsed_map(@uncached_account_ids, @current_account_id))
|
||||
|
||||
cache_uncached!
|
||||
|
||||
@following.merge!(options[:following_map] || {})
|
||||
@followed_by.merge!(options[:followed_by_map] || {})
|
||||
@blocking.merge!(options[:blocking_map] || {})
|
||||
@blocked_by.merge!(options[:blocked_by_map] || {})
|
||||
@muting.merge!(options[:muting_map] || {})
|
||||
@requested.merge!(options[:requested_map] || {})
|
||||
@domain_blocking.merge!(options[:domain_blocking_map] || {})
|
||||
@endorsed.merge!(options[:endorsed_map] || {})
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cached
|
||||
return @cached if defined?(@cached)
|
||||
|
||||
@cached = {
|
||||
following: {},
|
||||
followed_by: {},
|
||||
blocking: {},
|
||||
blocked_by: {},
|
||||
muting: {},
|
||||
requested: {},
|
||||
domain_blocking: {},
|
||||
endorsed: {},
|
||||
}
|
||||
|
||||
@uncached_account_ids = []
|
||||
|
||||
@account_ids.each do |account_id|
|
||||
maps_for_account = Rails.cache.read("relationship:#{@current_account_id}:#{account_id}")
|
||||
|
||||
if maps_for_account.is_a?(Hash)
|
||||
@cached.deep_merge!(maps_for_account)
|
||||
else
|
||||
@uncached_account_ids << account_id
|
||||
end
|
||||
end
|
||||
|
||||
@cached
|
||||
end
|
||||
|
||||
def cache_uncached!
|
||||
@uncached_account_ids.each do |account_id|
|
||||
maps_for_account = {
|
||||
following: { account_id => following[account_id] },
|
||||
followed_by: { account_id => followed_by[account_id] },
|
||||
blocking: { account_id => blocking[account_id] },
|
||||
blocked_by: { account_id => blocked_by[account_id] },
|
||||
muting: { account_id => muting[account_id] },
|
||||
requested: { account_id => requested[account_id] },
|
||||
domain_blocking: { account_id => domain_blocking[account_id] },
|
||||
endorsed: { account_id => endorsed[account_id] },
|
||||
}
|
||||
|
||||
Rails.cache.write("relationship:#{@current_account_id}:#{account_id}", maps_for_account, expires_in: 1.day)
|
||||
end
|
||||
end
|
||||
end
|
||||
5
app/presenters/activitypub/collection_presenter.rb
Normal file
5
app/presenters/activitypub/collection_presenter.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ActivityPub::CollectionPresenter < ActiveModelSerializers::Model
|
||||
attributes :id, :type, :size, :items, :page, :part_of, :first, :last, :next, :prev
|
||||
end
|
||||
55
app/presenters/group_relationships_presenter.rb
Normal file
55
app/presenters/group_relationships_presenter.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class GroupRelationshipsPresenter
|
||||
attr_reader :member, :admin
|
||||
|
||||
def initialize(group_ids, current_account_id, **options)
|
||||
@group_ids = group_ids.map { |a| a.is_a?(Group) ? a.id : a }
|
||||
@current_account_id = current_account_id
|
||||
|
||||
@member = cached[:member].merge(Group.member_map(@uncached_group_ids, @current_account_id))
|
||||
@admin = cached[:admin].merge(Group.admin_map(@uncached_group_ids, @current_account_id))
|
||||
|
||||
cache_uncached!
|
||||
|
||||
@member.merge!(options[:member_map] || {})
|
||||
@admin.merge!(options[:admin_map] || {})
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cached
|
||||
return @cached if defined?(@cached)
|
||||
|
||||
@cached = {
|
||||
member: {},
|
||||
admin: {},
|
||||
}
|
||||
|
||||
@uncached_group_ids = []
|
||||
|
||||
@group_ids.each do |group_id|
|
||||
maps_for_group = Rails.cache.read("relationship:#{@current_account_id}:#{group_id}")
|
||||
|
||||
if maps_for_group.is_a?(Hash)
|
||||
@cached.deep_merge!(maps_for_group)
|
||||
else
|
||||
@uncached_group_ids << group_id
|
||||
end
|
||||
end
|
||||
|
||||
@cached
|
||||
end
|
||||
|
||||
def cache_uncached!
|
||||
@uncached_group_ids.each do |group_id|
|
||||
maps_for_account = {
|
||||
member: { group_id => member[group_id] },
|
||||
admin: { group_id => admin[group_id] },
|
||||
}
|
||||
|
||||
Rails.cache.write("relationship:#{@current_account_id}:#{group_id}", maps_for_account, expires_in: 1.day)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
6
app/presenters/initial_state_presenter.rb
Normal file
6
app/presenters/initial_state_presenter.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class InitialStatePresenter < ActiveModelSerializers::Model
|
||||
attributes :settings, :push_subscription, :token,
|
||||
:current_account, :admin, :text
|
||||
end
|
||||
58
app/presenters/instance_presenter.rb
Normal file
58
app/presenters/instance_presenter.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class InstancePresenter
|
||||
delegate(
|
||||
:site_contact_email,
|
||||
:site_title,
|
||||
:site_short_description,
|
||||
:site_description,
|
||||
:site_extended_description,
|
||||
:site_terms,
|
||||
:closed_registrations_message,
|
||||
to: Setting
|
||||
)
|
||||
|
||||
def contact_account
|
||||
Account.find_local(Setting.site_contact_username.strip.gsub(/\A@/, ''))
|
||||
end
|
||||
|
||||
def user_count
|
||||
Rails.cache.fetch('user_count') { User.confirmed.joins(:account).merge(Account.without_suspended).count }
|
||||
end
|
||||
|
||||
def active_user_count
|
||||
Rails.cache.fetch('active_user_count') { Redis.current.pfcount(*(0..3).map { |i| "activity:logins:#{i.weeks.ago.utc.to_date.cweek}" }) }
|
||||
end
|
||||
|
||||
def status_count
|
||||
Rails.cache.fetch('local_status_count') { Account.local.joins(:account_stat).sum('account_stats.statuses_count') }.to_i
|
||||
end
|
||||
|
||||
def domain_count
|
||||
Rails.cache.fetch('distinct_domain_count') { Account.distinct.count(:domain) }
|
||||
end
|
||||
|
||||
def sample_accounts
|
||||
Rails.cache.fetch('sample_accounts', expires_in: 12.hours) { Account.discoverable.popular.limit(3) }
|
||||
end
|
||||
|
||||
def version_number
|
||||
GabSocial::Version
|
||||
end
|
||||
|
||||
def source_url
|
||||
GabSocial::Version.source_url
|
||||
end
|
||||
|
||||
def thumbnail
|
||||
@thumbnail ||= Rails.cache.fetch('site_uploads/thumbnail') { SiteUpload.find_by(var: 'thumbnail') }
|
||||
end
|
||||
|
||||
def hero
|
||||
@hero ||= Rails.cache.fetch('site_uploads/hero') { SiteUpload.find_by(var: 'hero') }
|
||||
end
|
||||
|
||||
def mascot
|
||||
@mascot ||= Rails.cache.fetch('site_uploads/mascot') { SiteUpload.find_by(var: 'mascot') }
|
||||
end
|
||||
end
|
||||
24
app/presenters/status_relationships_presenter.rb
Normal file
24
app/presenters/status_relationships_presenter.rb
Normal file
@@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class StatusRelationshipsPresenter
|
||||
attr_reader :reblogs_map, :favourites_map, :mutes_map, :pins_map
|
||||
|
||||
def initialize(statuses, current_account_id = nil, **options)
|
||||
if current_account_id.nil?
|
||||
@reblogs_map = {}
|
||||
@favourites_map = {}
|
||||
@mutes_map = {}
|
||||
@pins_map = {}
|
||||
else
|
||||
statuses = statuses.compact
|
||||
status_ids = statuses.flat_map { |s| [s.id, s.reblog_of_id] }.uniq.compact
|
||||
conversation_ids = statuses.map(&:conversation_id).compact.uniq
|
||||
pinnable_status_ids = statuses.map(&:proper).select { |s| s.account_id == current_account_id && %w(public unlisted).include?(s.visibility) }.map(&:id)
|
||||
|
||||
@reblogs_map = Status.reblogs_map(status_ids, current_account_id).merge(options[:reblogs_map] || {})
|
||||
@favourites_map = Status.favourites_map(status_ids, current_account_id).merge(options[:favourites_map] || {})
|
||||
@mutes_map = Status.mutes_map(conversation_ids, current_account_id).merge(options[:mutes_map] || {})
|
||||
@pins_map = Status.pins_map(pinnable_status_ids, current_account_id).merge(options[:pins_map] || {})
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user