diff --git a/app/assets/javascripts/atWhoAutoComplete.js b/app/assets/javascripts/atWhoAutoComplete.js
index 16ff63b839..c5b8210d79 100644
--- a/app/assets/javascripts/atWhoAutoComplete.js
+++ b/app/assets/javascripts/atWhoAutoComplete.js
@@ -1,8 +1,17 @@
(function() {
// settings at https://github.com/ichord/At.js/wiki/Base-Document#settings
+
+ // checks if the 'name' key in the JSON data is named 'username' or 'name' and then returns the
+ // correct key-value
+ const displayName = (item) => item.username ? item.username : item.name;
+
var at_config = {
at: "@",
+ displayTpl: (item) => `
${displayName(item)}`,
+ insertTpl: (item) => `@${displayName(item)}`,
+ // loads and saves remote JSON data by URL
+ 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 31388cbbe6..37febfba6b 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 e026ea41c9..50bc8252e6 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -505,6 +505,18 @@ 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.status, 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")
+ .where("rusers.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 716675d785..d9afed1613 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'