2016-07-10 22:47:21 +00:00
|
|
|
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
class Karma::ActivityLog < ApplicationModel
|
|
|
|
belongs_to :object_lookup, class_name: 'ObjectLookup'
|
|
|
|
|
|
|
|
self.table_name = 'karma_activity_logs'
|
|
|
|
|
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
|
|
|
|
|
|
|
|
Karma::ActivityLog.transaction do
|
2016-07-12 08:19:28 +00:00
|
|
|
last_activity = Karma::ActivityLog.where(user_id: user.id).order(created_at: :desc, id: :desc).lock(true).first
|
2016-07-10 22:47:21 +00:00
|
|
|
latest_activity = Karma::ActivityLog.where(
|
|
|
|
user_id: user.id,
|
|
|
|
object_lookup_id: object_id,
|
|
|
|
o_id: o_id,
|
|
|
|
activity_id: activity.id,
|
|
|
|
).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
|
|
|
|
if local_score_total < 0
|
|
|
|
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,
|
2016-07-11 15:12:12 +00:00
|
|
|
score_total: local_score_total,
|
2016-07-10 22:47:21 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
# set new karma level
|
|
|
|
Karma::User.sync(user)
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.latest(user, limit = 20)
|
|
|
|
result = []
|
|
|
|
logs = Karma::ActivityLog.where(user_id: user.id).order(id: :desc).limit(limit)
|
|
|
|
logs.each { |log|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
last[:comments].push comment
|
|
|
|
last[:score_total] = score_total
|
|
|
|
next
|
|
|
|
end
|
|
|
|
comment = {
|
|
|
|
object_id: log.object_id,
|
|
|
|
o_id: log.o_id,
|
|
|
|
description: Karma::Activity.lookup(id: log.activity_id).description,
|
|
|
|
score: log.score,
|
|
|
|
}
|
|
|
|
data = {
|
|
|
|
comments: [comment],
|
|
|
|
score_total: log.score_total,
|
|
|
|
created_at: log.created_at,
|
|
|
|
}
|
|
|
|
result.push data
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|