Skip to content

Commit

Permalink
Fix “Authorized applications” inefficiently and incorrectly getting l…
Browse files Browse the repository at this point in the history
…ast use date (mastodon#25060)
  • Loading branch information
ClearlyClaire authored and skerit committed Jul 7, 2023
1 parent 4cde9b8 commit 051b434
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
12 changes: 12 additions & 0 deletions app/controllers/oauth/authorized_applications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Oauth::AuthorizedApplicationsController < Doorkeeper::AuthorizedApplicatio
before_action :set_body_classes
before_action :set_cache_headers

before_action :set_last_used_at_by_app, only: :index, unless: -> { request.format == :json }

skip_before_action :require_functional!

include Localized
Expand All @@ -35,4 +37,14 @@ def require_not_suspended!
def set_cache_headers
response.cache_control.replace(private: true, no_store: true)
end

def set_last_used_at_by_app
@last_used_at_by_app = Doorkeeper::AccessToken
.select('DISTINCT ON (application_id) application_id, last_used_at')
.where(resource_owner_id: current_resource_owner.id)
.where.not(last_used_at: nil)
.order(application_id: :desc, last_used_at: :desc)
.pluck(:application_id, :last_used_at)
.to_h
end
end
4 changes: 0 additions & 4 deletions app/lib/application_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ module ApplicationExtension
validates :redirect_uri, length: { maximum: 2_000 }
end

def most_recently_used_access_token
@most_recently_used_access_token ||= access_tokens.where.not(last_used_at: nil).order(last_used_at: :desc).first
end

def confirmation_redirect_uri
redirect_uri.lines.first.strip
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/oauth/authorized_applications/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

.announcements-list__item__action-bar
.announcements-list__item__meta
- if application.most_recently_used_access_token
= t('doorkeeper.authorized_applications.index.last_used_at', date: l(application.most_recently_used_access_token.last_used_at.to_date))
- if @last_used_at_by_app[application.id]
= t('doorkeeper.authorized_applications.index.last_used_at', date: l(@last_used_at_by_app[application.id].to_date))
- else
= t('doorkeeper.authorized_applications.index.never_used')

Expand Down

0 comments on commit 051b434

Please sign in to comment.