about summary refs log tree commit diff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/controllers/admin/settings_controller.rb4
-rw-r--r--app/controllers/api/v1/instances/activity_controller.rb36
-rw-r--r--app/controllers/api/v1/instances/peers_controller.rb17
-rw-r--r--app/controllers/application_controller.rb9
-rw-r--r--app/controllers/auth/confirmations_controller.rb6
-rw-r--r--app/controllers/concerns/user_tracking_concern.rb1
-rw-r--r--app/lib/activity_tracker.rb31
-rw-r--r--app/models/form/admin_settings.rb4
-rw-r--r--app/models/status.rb6
-rw-r--r--app/models/user.rb15
-rw-r--r--app/views/admin/settings/edit.html.haml8
11 files changed, 131 insertions, 6 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