Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorJames Lopez <james@jameslopez.es>2016-10-05 17:41:32 +0300
committerRémy Coutable <remy@rymai.me>2017-04-14 16:20:55 +0300
commit2951a8543ef97ceb1bcaca5f5140d822729c950b (patch)
treeff02540e813cd93e8df8a438c9b22cdacc96982e /app
parentb61199ce0ccdfcd11a338778ce300cd15e5f2a43 (diff)
Add user activity service and spec. Also added relevant - NOT offline - migration
It uses a user activity table instead of a column in users. Tested with mySQL and postgreSQL
Diffstat (limited to 'app')
-rw-r--r--app/controllers/projects/git_http_controller.rb6
-rw-r--r--app/controllers/sessions_controller.rb5
-rw-r--r--app/models/user.rb2
-rw-r--r--app/models/user_activity.rb19
-rw-r--r--app/services/event_create_service.rb2
-rw-r--r--app/services/users/activity_service.rb26
6 files changed, 60 insertions, 0 deletions
diff --git a/app/controllers/projects/git_http_controller.rb b/app/controllers/projects/git_http_controller.rb
index 37f6f637ff0..10adddb4636 100644
--- a/app/controllers/projects/git_http_controller.rb
+++ b/app/controllers/projects/git_http_controller.rb
@@ -5,6 +5,8 @@ class Projects::GitHttpController < Projects::GitHttpClientController
# GET /foo/bar.git/info/refs?service=git-receive-pack (git push)
def info_refs
if upload_pack? && upload_pack_allowed?
+ log_user_activity
+
render_ok
elsif receive_pack? && receive_pack_allowed?
render_ok
@@ -106,4 +108,8 @@ class Projects::GitHttpController < Projects::GitHttpClientController
def access_klass
@access_klass ||= wiki? ? Gitlab::GitAccessWiki : Gitlab::GitAccess
end
+
+ def log_user_activity
+ Users::ActivityService.new(user, 'pull').execute
+ end
end
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
index d3091a4f8e9..8c6ba4915cd 100644
--- a/app/controllers/sessions_controller.rb
+++ b/app/controllers/sessions_controller.rb
@@ -35,6 +35,7 @@ class SessionsController < Devise::SessionsController
# hide the signed-in notification
flash[:notice] = nil
log_audit_event(current_user, with: authentication_method)
+ log_user_activity(current_user)
end
end
@@ -123,6 +124,10 @@ class SessionsController < Devise::SessionsController
for_authentication.security_event
end
+ def log_user_activity(user)
+ Users::ActivityService.new(user, 'login').execute
+ end
+
def load_recaptcha
Gitlab::Recaptcha.load_configurations!
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 457ba05fb04..1dde5c89699 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -101,6 +101,7 @@ class User < ActiveRecord::Base
has_many :assigned_issues, dependent: :nullify, foreign_key: :assignee_id, class_name: "Issue"
has_many :assigned_merge_requests, dependent: :nullify, foreign_key: :assignee_id, class_name: "MergeRequest"
+ has_one :user_activity, dependent: :destroy
# Issues that a user owns are expected to be moved to the "ghost" user before
# the user is destroyed. If the user owns any issues during deletion, this
@@ -158,6 +159,7 @@ class User < ActiveRecord::Base
alias_attribute :private_token, :authentication_token
delegate :path, to: :namespace, allow_nil: true, prefix: true
+ delegate :last_activity_at, to: :user_activity, allow_nil: true
state_machine :state, initial: :active do
event :block do
diff --git a/app/models/user_activity.rb b/app/models/user_activity.rb
new file mode 100644
index 00000000000..c5fdbff0feb
--- /dev/null
+++ b/app/models/user_activity.rb
@@ -0,0 +1,19 @@
+class UserActivity < ActiveRecord::Base
+ belongs_to :user, inverse_of: :user_activity
+
+ validates :user, uniqueness: true, presence: true
+ validates :last_activity_at, presence: true
+
+ # Updated version of http://apidock.com/rails/ActiveRecord/Timestamp/touch
+ # That accepts a new record.
+ def touch
+ current_time = current_time_from_proper_timezone
+
+ if persisted?
+ update_column(:last_activity_at, current_time)
+ else
+ self.last_activity_at = current_time
+ save!(validate: false)
+ end
+ end
+end
diff --git a/app/services/event_create_service.rb b/app/services/event_create_service.rb
index e24cc66e0fe..0f3a485a3fd 100644
--- a/app/services/event_create_service.rb
+++ b/app/services/event_create_service.rb
@@ -72,6 +72,8 @@ class EventCreateService
def push(project, current_user, push_data)
create_event(project, current_user, Event::PUSHED, data: push_data)
+
+ Users::ActivityService.new(current_user, 'push').execute
end
private
diff --git a/app/services/users/activity_service.rb b/app/services/users/activity_service.rb
new file mode 100644
index 00000000000..b81f947cd01
--- /dev/null
+++ b/app/services/users/activity_service.rb
@@ -0,0 +1,26 @@
+module Users
+ class ActivityService
+ def initialize(author, activity)
+ @author = author.respond_to?(:user) ? author.user : author
+ @activity = activity
+ end
+
+ def execute
+ return unless @author && @author.is_a?(User)
+
+ record_activity
+ end
+
+ private
+
+ def record_activity
+ user_activity.touch
+
+ Rails.logger.debug("Recorded activity: #{@activity} for User ID: #{@author.id} (username: #{@author.username}")
+ end
+
+ def user_activity
+ UserActivity.find_or_initialize_by(user: @author)
+ end
+ end
+end