diff options
-rw-r--r-- | app/controllers/admin/settings_controller.rb | 4 | ||||
-rw-r--r-- | app/controllers/api/v1/instances/activity_controller.rb | 36 | ||||
-rw-r--r-- | app/controllers/api/v1/instances/peers_controller.rb | 17 | ||||
-rw-r--r-- | app/controllers/application_controller.rb | 9 | ||||
-rw-r--r-- | app/controllers/auth/confirmations_controller.rb | 6 | ||||
-rw-r--r-- | app/controllers/concerns/user_tracking_concern.rb | 1 | ||||
-rw-r--r-- | app/lib/activity_tracker.rb | 31 | ||||
-rw-r--r-- | app/models/form/admin_settings.rb | 4 | ||||
-rw-r--r-- | app/models/status.rb | 6 | ||||
-rw-r--r-- | app/models/user.rb | 15 | ||||
-rw-r--r-- | app/views/admin/settings/edit.html.haml | 8 | ||||
-rw-r--r-- | config/locales/en.yml | 6 | ||||
-rw-r--r-- | config/routes.rb | 6 | ||||
-rw-r--r-- | config/settings.yml | 3 |
14 files changed, 144 insertions, 8 deletions
diff --git a/app/controllers/admin/settings_controller.rb b/app/controllers/admin/settings_controller.rb index eed5fb6b5..487282dc3 100644 --- a/app/controllers/admin/settings_controller.rb +++ b/app/controllers/admin/settings_controller.rb @@ -17,6 +17,8 @@ module Admin bootstrap_timeline_accounts thumbnail min_invite_role + activity_api_enabled + peers_api_enabled ).freeze BOOLEAN_SETTINGS = %w( @@ -24,6 +26,8 @@ module Admin open_deletion timeline_preview show_staff_badge + activity_api_enabled + peers_api_enabled ).freeze UPLOAD_SETTINGS = %w( diff --git a/app/controllers/api/v1/instances/activity_controller.rb b/app/controllers/api/v1/instances/activity_controller.rb new file mode 100644 index 000000000..36f52c38d --- /dev/null +++ b/app/controllers/api/v1/instances/activity_controller.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +class Api::V1::Instances::ActivityController < Api::BaseController + before_action :require_enabled_api! + + respond_to :json + + def show + render_cached_json('api:v1:instances:activity:show', expires_in: 1.day) { activity } + end + + private + + def activity + weeks = [] + + 12.times do |i| + day = i.weeks.ago.to_date + week_id = day.cweek + week = Date.commercial(day.cwyear, week_id) + + weeks << { + week: week.to_time.to_i.to_s, + statuses: Redis.current.get("activity:statuses:local:#{week_id}") || 0, + logins: Redis.current.pfcount("activity:logins:#{week_id}"), + registrations: Redis.current.get("activity:accounts:local:#{week_id}") || 0, + } + end + + weeks + end + + def require_enabled_api! + head 404 unless Setting.activity_api_enabled + end +end diff --git a/app/controllers/api/v1/instances/peers_controller.rb b/app/controllers/api/v1/instances/peers_controller.rb new file mode 100644 index 000000000..2070c487d --- /dev/null +++ b/app/controllers/api/v1/instances/peers_controller.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class Api::V1::Instances::PeersController < Api::BaseController + before_action :require_enabled_api! + + respond_to :json + + def index + render_cached_json('api:v1:instances:peers:index', expires_in: 1.day) { Account.remote.domains } + end + + private + + def require_enabled_api! + head 404 unless Setting.peers_api_enabled + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a213302cb..51a978f44 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -121,4 +121,13 @@ class ApplicationController < ActionController::Base end end end + + def render_cached_json(cache_key, **options) + data = Rails.cache.fetch(cache_key, { raw: true }.merge(options)) do + yield.to_json + end + + expires_in options[:expires_in], public: true + render json: data + end end diff --git a/app/controllers/auth/confirmations_controller.rb b/app/controllers/auth/confirmations_controller.rb index d5e8e58ed..2fdb281f4 100644 --- a/app/controllers/auth/confirmations_controller.rb +++ b/app/controllers/auth/confirmations_controller.rb @@ -2,10 +2,4 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController layout 'auth' - - def show - super do |user| - BootstrapTimelineWorker.perform_async(user.account_id) if user.errors.empty? - end - end end diff --git a/app/controllers/concerns/user_tracking_concern.rb b/app/controllers/concerns/user_tracking_concern.rb index 8663c3086..1e3132941 100644 --- a/app/controllers/concerns/user_tracking_concern.rb +++ b/app/controllers/concerns/user_tracking_concern.rb @@ -17,6 +17,7 @@ module UserTrackingConcern # Mark as signed-in today current_user.update_tracked_fields!(request) + ActivityTracker.record('activity:logins', current_user.id) # Regenerate feed if needed regenerate_feed! if user_needs_feed_update? diff --git a/app/lib/activity_tracker.rb b/app/lib/activity_tracker.rb new file mode 100644 index 000000000..50e927b0c --- /dev/null +++ b/app/lib/activity_tracker.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class ActivityTracker + EXPIRE_AFTER = 90.days.seconds + + class << self + def increment(prefix) + key = [prefix, current_week].join(':') + + redis.incrby(key, 1) + redis.expire(key, EXPIRE_AFTER) + end + + def record(prefix, value) + key = [prefix, current_week].join(':') + + redis.pfadd(key, value) + redis.expire(key, value) + end + + private + + def redis + Redis.current + end + + def current_week + Time.zone.today.cweek + end + end +end diff --git a/app/models/form/admin_settings.rb b/app/models/form/admin_settings.rb index c1d2cf420..dd629279c 100644 --- a/app/models/form/admin_settings.rb +++ b/app/models/form/admin_settings.rb @@ -30,6 +30,10 @@ class Form::AdminSettings :bootstrap_timeline_accounts=, :min_invite_role, :min_invite_role=, + :activity_api_enabled, + :activity_api_enabled=, + :peers_api_enabled, + :peers_api_enabled=, to: Setting ) end diff --git a/app/models/status.rb b/app/models/status.rb index 8579ff9e4..00dcec624 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -135,6 +135,7 @@ class Status < ApplicationRecord end after_create_commit :store_uri, if: :local? + after_create_commit :update_statistics, if: :local? around_create Mastodon::Snowflake::Callbacks @@ -308,4 +309,9 @@ class Status < ApplicationRecord def set_local self.local = account.local? end + + def update_statistics + return unless public_visibility? || unlisted_visibility? + ActivityTracker.increment('activity:statuses:local') + end end diff --git a/app/models/user.rb b/app/models/user.rb index 578622fdf..3ce6517a6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -122,9 +122,19 @@ class User < ApplicationRecord update!(disabled: false) end + def confirm + return if confirmed? + + super + update_statistics! + end + def confirm! + return if confirmed? + skip_confirmation! save! + update_statistics! end def promote! @@ -202,4 +212,9 @@ class User < ApplicationRecord def sanitize_languages filtered_languages.reject!(&:blank?) end + + def update_statistics! + BootstrapTimelineWorker.perform_async(account_id) + ActivityTracker.increment('activity:accounts:local') + end end diff --git a/app/views/admin/settings/edit.html.haml b/app/views/admin/settings/edit.html.haml index c7c25f528..4f9115ed2 100644 --- a/app/views/admin/settings/edit.html.haml +++ b/app/views/admin/settings/edit.html.haml @@ -46,5 +46,13 @@ .fields-group = f.input :bootstrap_timeline_accounts, wrapper: :with_block_label, label: t('admin.settings.bootstrap_timeline_accounts.title'), hint: t('admin.settings.bootstrap_timeline_accounts.desc_html') + %hr/ + + .fields-group + = f.input :activity_api_enabled, as: :boolean, wrapper: :with_label, label: t('admin.settings.activity_api_enabled.title'), hint: t('admin.settings.activity_api_enabled.desc_html') + + .fields-group + = f.input :peers_api_enabled, as: :boolean, wrapper: :with_label, label: t('admin.settings.peers_api_enabled.title'), hint: t('admin.settings.peers_api_enabled.desc_html') + .actions = f.button :button, t('generic.save_changes'), type: :submit diff --git a/config/locales/en.yml b/config/locales/en.yml index 325391cfd..e4425b424 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -265,12 +265,18 @@ en: unresolved: Unresolved view: View settings: + activity_api_enabled: + desc_html: Counts of locally posted statuses, active users, and new registrations in weekly buckets + title: Publish aggregate statistics about user activity bootstrap_timeline_accounts: desc_html: Separate multiple usernames by comma. Only local and unlocked accounts will work. Default when empty is all local admins. title: Default follows for new users contact_information: email: Business e-mail username: Contact username + peers_api_enabled: + desc_html: Domain names this instance has encountered in the fediverse + title: Publish list of discovered instances registrations: closed_message: desc_html: Displayed on frontpage when registrations are closed. You can use HTML tags diff --git a/config/routes.rb b/config/routes.rb index 467849c03..80a2c6d13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -241,7 +241,11 @@ Rails.application.routes.draw do resources :apps, only: [:create] - resource :instance, only: [:show] + resource :instance, only: [:show] do + resources :peers, only: [:index], controller: 'instances/peers' + resource :activity, only: [:show], controller: 'instances/activity' + end + resource :domain_blocks, only: [:show, :create, :destroy] resources :follow_requests, only: [:index] do diff --git a/config/settings.yml b/config/settings.yml index f03a32e50..4a2519464 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -47,7 +47,8 @@ defaults: &defaults - webmaster - administrator bootstrap_timeline_accounts: '' - + activity_api_enabled: true + peers_api_enabled: true development: <<: *defaults |