activity_controller.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. class Api::V1::Instances::ActivityController < Api::BaseController
  3. before_action :require_enabled_api!
  4. skip_before_action :set_cache_headers
  5. skip_before_action :require_authenticated_user!, unless: :whitelist_mode?
  6. def show
  7. expires_in 1.day, public: true
  8. render_with_cache json: :activity, expires_in: 1.day
  9. end
  10. private
  11. def activity
  12. statuses_tracker = ActivityTracker.new('activity:statuses:local', :basic)
  13. logins_tracker = ActivityTracker.new('activity:logins', :unique)
  14. registrations_tracker = ActivityTracker.new('activity:accounts:local', :basic)
  15. (0...12).map do |i|
  16. start_of_week = i.weeks.ago
  17. end_of_week = start_of_week + 6.days
  18. {
  19. week: start_of_week.to_i.to_s,
  20. statuses: statuses_tracker.sum(start_of_week, end_of_week).to_s,
  21. logins: logins_tracker.sum(start_of_week, end_of_week).to_s,
  22. registrations: registrations_tracker.sum(start_of_week, end_of_week).to_s,
  23. }
  24. end
  25. end
  26. def require_enabled_api!
  27. head 404 unless Setting.activity_api_enabled && !whitelist_mode?
  28. end
  29. end