From 6ca6da862ad1aa0ef9f25bbfc29887d476ec2904 Mon Sep 17 00:00:00 2001 From: TildaDares Date: Thu, 10 Jun 2021 17:07:00 +0100 Subject: [PATCH] Autocompletion of usernames in comment boxes --- app/assets/javascripts/atWhoAutoComplete.js | 6 ++++++ app/controllers/users_controller.rb | 5 +++++ app/models/user.rb | 11 +++++++++++ config/routes.rb | 1 + 4 files changed, 23 insertions(+) diff --git a/app/assets/javascripts/atWhoAutoComplete.js b/app/assets/javascripts/atWhoAutoComplete.js index 16ff63b8399..374c5b24132 100644 --- a/app/assets/javascripts/atWhoAutoComplete.js +++ b/app/assets/javascripts/atWhoAutoComplete.js @@ -1,8 +1,14 @@ (function() { // settings at https://github.com/ichord/At.js/wiki/Base-Document#settings + + const displayName = (item) => item.username ? item.username : item.name; + var at_config = { at: "@", + displayTpl: (item) => `
  • ${displayName(item)}
  • `, + insertTpl: (item) => `@${displayName(item)}`, + data: '/users/active', delay: 400, callbacks: { remoteFilter: debounce(function(query, callback) { diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 31388cbbe6e..37febfba6bf 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -412,6 +412,11 @@ def verify_email redirect_to "/login", flash: { notice: action_msg } end + def recently_active_users + active_users = User.recently_active_users + render json: active_users, root: false + end + private def subscribe_multiple_tag(tag_list) diff --git a/app/models/user.rb b/app/models/user.rb index e026ea41c93..4cc863fd82d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -505,6 +505,17 @@ def latest_location recent_locations.last end + def self.recently_active_users(limit = 5, order = 'last_updated DESC') + Rails.cache.fetch('users/active', expires_in: 24.hours) do + User.select('rusers.username, rusers.id, MAX(node_revisions.timestamp) AS last_updated') + .joins("INNER JOIN `node_revisions` ON `node_revisions`.`uid` = `rusers`.`id` ") + .where("node_revisions.status = 1") + .group('rusers.id') + .order(order) + .limit(limit) + end + end + private def decrease_likes_banned diff --git a/config/routes.rb b/config/routes.rb index 716675d785e..d9afed1613e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -53,6 +53,7 @@ get 'signup' => 'users#new' get 'home' => 'home#front' get 'verify/:token' => 'users#verify_email' + get 'users/active' => 'users#recently_active_users' resources :relationships, only: [:create, :destroy] get '/wiki/:id/comments', to: 'wiki#comments'