trabajo-afectivo/app/models/karma/activity_log.rb

120 lines
3.3 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2016-07-10 22:47:21 +00:00
class Karma::ActivityLog < ApplicationModel
belongs_to :object_lookup, optional: true
belongs_to :user, class_name: '::User'
2016-07-10 22:47:21 +00:00
self.table_name = 'karma_activity_logs'
2017-05-05 09:16:47 +00:00
=begin
add karma activity log of an object
Karma::ActivityLog.add('ticket create', User.find(1), 'Ticket', 123)
=end
2016-07-11 15:12:12 +00:00
def self.add(action, user, object, o_id, force = false)
2016-07-10 22:47:21 +00:00
activity = Karma::Activity.lookup(name: action)
if object
object_id = ObjectLookup.by_name(object)
end
# scheduler transactions causes a lot of calls
# so we try to cache the add process
# to skip the time loss of the transaction
# to increase performance
if !force
cache = Cache.read("Karma::ActivityLog.add::#{activity.once_ttl.seconds}::#{action}::#{user.id}::#{object}::#{o_id}")
return cache if cache
end
2016-07-10 22:47:21 +00:00
Karma::ActivityLog.transaction do
last_activity = Karma::ActivityLog.where(user_id: user.id).order(id: :desc).lock(true).first
2016-07-10 22:47:21 +00:00
latest_activity = Karma::ActivityLog.where(
user_id: user.id,
2016-07-10 22:47:21 +00:00
object_lookup_id: object_id,
o_id: o_id,
activity_id: activity.id,
2016-07-10 22:47:21 +00:00
).find_by('created_at >= ?', Time.zone.now - activity.once_ttl.seconds)
2016-07-11 15:12:12 +00:00
return false if !force && latest_activity
2016-07-10 22:47:21 +00:00
score_total = 0
if last_activity
score_total = last_activity.score_total
end
2016-07-11 15:12:12 +00:00
local_score_total = score_total + activity.score
2016-10-24 21:59:18 +00:00
if local_score_total.negative?
2016-07-11 15:12:12 +00:00
local_score_total = 0
end
2016-07-10 22:47:21 +00:00
Karma::ActivityLog.create(
object_lookup_id: object_id,
o_id: o_id,
user_id: user.id,
activity_id: activity.id,
score: activity.score,
score_total: local_score_total,
2016-07-10 22:47:21 +00:00
)
if !force
Cache.write("Karma::ActivityLog.add::#{activity.once_ttl.seconds}::#{action}::#{user.id}::#{object}::#{o_id}", true, expires_in: activity.once_ttl.seconds)
end
2016-07-10 22:47:21 +00:00
end
# set new karma level
Karma::User.sync(user)
true
end
2017-05-05 09:16:47 +00:00
=begin
remove whole karma activity log of an object
Karma::ActivityLog.remove('Ticket', 123)
=end
def self.remove(object_name, o_id)
object_id = ObjectLookup.by_name(object_name)
Karma::ActivityLog.where(
object_lookup_id: object_id,
o_id: o_id,
2017-05-05 09:16:47 +00:00
).destroy_all
end
2016-07-14 17:24:56 +00:00
def self.latest(user, limit = 12)
2016-07-10 22:47:21 +00:00
result = []
logs = Karma::ActivityLog.where(user_id: user.id).order(id: :desc).limit(limit)
logs.each do |log|
2016-07-10 22:47:21 +00:00
last = result.last
if last && last[:object_id] == log.object_id && last[:o_id] == log.o_id && last[:created_at] == log.created_at
comment = {
description: Karma::Activity.lookup(id: log.activity_id).description,
score: log.score,
2016-07-10 22:47:21 +00:00
}
last[:comments].push comment
last[:score_total] = score_total
next
end
comment = {
object_id: log.object_id,
o_id: log.o_id,
2016-07-10 22:47:21 +00:00
description: Karma::Activity.lookup(id: log.activity_id).description,
score: log.score,
2016-07-10 22:47:21 +00:00
}
data = {
comments: [comment],
2016-07-10 22:47:21 +00:00
score_total: log.score_total,
created_at: log.created_at,
2016-07-10 22:47:21 +00:00
}
result.push data
end
2016-07-10 22:47:21 +00:00
result
end
end