Refactor user preferences, add defaults from file
* Turn prefs into a flat hash structure, since nesting is not worth the trouble * Add a custom serializer class for prefs * Add a config file for defaults and merge set prefs with unset ones * Use booleans for "true" and "false", and integers where appropriate
This commit is contained in:
@@ -45,8 +45,8 @@ class SettingsController < ApplicationController
|
||||
|
||||
def user_params
|
||||
params.require(:user).permit(preferences: [
|
||||
lightning: [:notify_sats_received],
|
||||
xmpp: [:exchange_contacts_with_invitees]
|
||||
:lightning_notify_sats_received,
|
||||
:xmpp_exchange_contacts_with_invitees
|
||||
])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -12,7 +12,7 @@ class WebhooksController < ApplicationController
|
||||
end
|
||||
|
||||
user = User.find_by!(ln_account: payload[:user_login])
|
||||
notify = user.preferences.dig("lightning", "notify_sats_received")
|
||||
notify = user.preferences[:lightning_notify_sats_received]
|
||||
case notify
|
||||
when "xmpp"
|
||||
notify_xmpp(user.address, payload[:amount], payload[:memo])
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
class User < ApplicationRecord
|
||||
include EmailValidatable
|
||||
|
||||
serialize :preferences, Hash, default: {}
|
||||
serialize :preferences, UserPreferences
|
||||
|
||||
# Relations
|
||||
has_many :invitations, dependent: :destroy
|
||||
@@ -64,7 +64,7 @@ class User < ApplicationRecord
|
||||
|
||||
if inviter.present?
|
||||
if Setting.ejabberd_enabled? &&
|
||||
inviter.pref_enabled?("xmpp:exchange_contacts_with_invitees")
|
||||
inviter.preferences[:xmpp_exchange_contacts_with_invitees]
|
||||
exchange_xmpp_contact_with_inviter
|
||||
end
|
||||
end
|
||||
@@ -138,11 +138,6 @@ class User < ApplicationRecord
|
||||
ldap.delete_attribute(dn,:service)
|
||||
end
|
||||
|
||||
def pref_enabled?(key)
|
||||
value = preferences.dig(*key.split(":"))
|
||||
[true, "true", "enabled", 1].include?(value)
|
||||
end
|
||||
|
||||
def exchange_xmpp_contact_with_inviter
|
||||
return unless inviter.services_enabled.include?("xmpp") &&
|
||||
services_enabled.include?("xmpp")
|
||||
|
||||
29
app/models/user_preferences.rb
Normal file
29
app/models/user_preferences.rb
Normal file
@@ -0,0 +1,29 @@
|
||||
DEFAULT_PREFS = YAML.load_file("#{Rails.root}/config/default_preferences.yml")
|
||||
|
||||
class UserPreferences
|
||||
def self.dump(value)
|
||||
process(value).to_yaml
|
||||
end
|
||||
|
||||
def self.load(string)
|
||||
stored_prefs = YAML.load(string || "{}")
|
||||
DEFAULT_PREFS.merge(stored_prefs).with_indifferent_access
|
||||
end
|
||||
|
||||
def self.is_integer?(value)
|
||||
value.to_i.to_s == value
|
||||
end
|
||||
|
||||
def self.process(hash)
|
||||
hash.each do |key, value|
|
||||
if value == "true"
|
||||
hash[key] = true
|
||||
elsif value == "false"
|
||||
hash[key] = false
|
||||
elsif value.is_a?(String) && is_integer?(value)
|
||||
hash[key] = value.to_i
|
||||
end
|
||||
end
|
||||
hash.stringify_keys!.to_h
|
||||
end
|
||||
end
|
||||
@@ -8,13 +8,11 @@
|
||||
description: "Notify me when sats are sent to my Lightning Address"
|
||||
) do %>
|
||||
<% f.fields_for :preferences do |p| %>
|
||||
<% p.fields_for :lightning do |l| %>
|
||||
<%= l.select :notify_sats_received, options_for_select([
|
||||
["off", "off"],
|
||||
["Chat (Jabber)", "xmpp"],
|
||||
["E-Mail", "email"]
|
||||
], selected: @user.preferences.dig('lightning', 'notify_sats_received')) %>
|
||||
<% end %>
|
||||
<%= p.select :lightning_notify_sats_received, options_for_select([
|
||||
["off", "disabled"],
|
||||
["Chat (Jabber)", "xmpp"],
|
||||
["E-Mail", "email"]
|
||||
], selected: @user.preferences[:lightning_notify_sats_received]) %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
<h3>Contacts</h3>
|
||||
<ul role="list">
|
||||
<%= render FormElements::FieldsetToggleComponent.new(
|
||||
field_name: "user[preferences][xmpp][exchange_contacts_with_invitees]",
|
||||
enabled: @user.pref_enabled?("xmpp:exchange_contacts_with_invitees"),
|
||||
field_name: "user[preferences][xmpp_exchange_contacts_with_invitees]",
|
||||
enabled: @user.preferences[:xmpp_exchange_contacts_with_invitees],
|
||||
title: "Exchange contacts when invited user signs up",
|
||||
description: "Add each others contacts, so you can chat with them immediately"
|
||||
) %>
|
||||
|
||||
Reference in New Issue
Block a user