0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-12-01 13:01:10 +01:00
discourse-custom-wizard/lib/custom_wizard/log.rb

49 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)
2024-10-16 13:52:03 +02:00
@date = attrs["date"]
@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
2024-10-16 13:52:03 +02:00
PluginStore.set(
"custom_wizard_log",
2020-04-15 04:34:39 +02:00
log_id.to_s,
2024-10-16 13:52:03 +02:00
{ date: date, wizard_id: wizard_id, action: action, username: username, message: message },
2020-04-15 02:46:44 +02:00
)
end
2021-03-11 07:30:15 +01:00
def self.list_query(wizard_id = nil)
2024-10-16 13:52:03 +02:00
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
2024-10-16 13:52:03 +02:00
result.logs = logs.limit(limit).offset(page * limit).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