0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-30 04:30:29 +01:00
discourse-custom-wizard/lib/custom_wizard/log.rb

53 Zeilen
1,3 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-04-15 02:46:44 +02:00
class CustomWizard::Log
include ActiveModel::Serialization
2021-03-11 07:30:15 +01:00
attr_reader :date, :wizard_id, :action, :username, :message
attr_accessor :user
2021-03-11 07:30:15 +01:00
2020-04-15 02:46:44 +02:00
PAGE_LIMIT = 100
2021-03-11 07:30:15 +01:00
2020-04-15 02:46:44 +02:00
def initialize(attrs)
@date = attrs['date']
2021-08-10 15:18:02 +02:00
@action = attrs['action']
@message = attrs['message']
@wizard_id = attrs['wizard_id']
@username = attrs['username']
2020-04-15 02:46:44 +02:00
end
2021-03-11 07:30:15 +01:00
2023-05-01 22:49:44 +02:00
def self.create(wizard_id, action, username, message, date = Time.now)
2020-04-15 02:46:44 +02:00
log_id = SecureRandom.hex(12)
2021-03-11 07:30:15 +01:00
2020-04-15 04:34:39 +02:00
PluginStore.set('custom_wizard_log',
log_id.to_s,
2020-04-15 02:46:44 +02:00
{
2023-05-01 22:49:44 +02:00
date: date,
wizard_id: wizard_id,
2021-08-10 15:18:02 +02:00
action: action,
username: username,
2020-04-15 02:46:44 +02:00
message: message
}
)
end
2021-03-11 07:30:15 +01:00
def self.list_query(wizard_id = nil)
query = PluginStoreRow.where("plugin_name = 'custom_wizard_log' AND (value::json->'date') IS NOT NULL")
query = query.where("(value::json->>'wizard_id') = ?", wizard_id) if wizard_id
query.order("value::json->>'date' DESC")
2020-04-15 02:46:44 +02:00
end
2021-03-11 07:30:15 +01:00
def self.list(page = 0, limit = nil, wizard_id = nil)
2020-11-03 01:24:20 +01:00
limit = limit.to_i > 0 ? limit.to_i : PAGE_LIMIT
page = page.to_i
logs = self.list_query(wizard_id)
2021-03-11 07:30:15 +01:00
result = OpenStruct.new(logs: [], total: nil)
result.total = logs.size
result.logs = logs.limit(limit)
2020-11-03 01:24:20 +01:00
.offset(page * limit)
2020-04-15 02:46:44 +02:00
.map { |r| self.new(JSON.parse(r.value)) }
result
2020-04-15 02:46:44 +02:00
end
2021-03-11 07:30:15 +01:00
end