diff options
author | Fire Demon <firedemon@creature.cafe> | 2020-07-25 23:19:56 -0500 |
---|---|---|
committer | Fire Demon <firedemon@creature.cafe> | 2020-08-30 05:45:15 -0500 |
commit | 70763ef0ea6cb6b997ac144d8b4d855b16fb838c (patch) | |
tree | 22188b8531085b35652f5459fc04e84384d1a231 /app/models/account_domain_permission.rb | |
parent | 3c6057034eea75f1992c9d2f2b2c2e401858bdc7 (diff) |
[Feature, Privacy] Add base support for per-domain post privacy
Diffstat (limited to 'app/models/account_domain_permission.rb')
-rw-r--r-- | app/models/account_domain_permission.rb | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/app/models/account_domain_permission.rb b/app/models/account_domain_permission.rb new file mode 100644 index 000000000..ffa9cbbec --- /dev/null +++ b/app/models/account_domain_permission.rb @@ -0,0 +1,61 @@ +# frozen_string_literal: true +# == Schema Information +# +# Table name: account_domain_permissions +# +# id :bigint(8) not null, primary key +# account_id :bigint(8) not null +# domain :string default(""), not null +# visibility :integer default("public"), not null +# + +class AccountDomainPermission < ApplicationRecord + include Paginable + + belongs_to :account, inverse_of: :domain_permissions + enum visibility: [:public, :unlisted, :private, :direct, :limited], _suffix: :visibility + + class << self + def create_by_domains(permissions_list) + Array(permissions_list).map(&method(:normalize)).map do |permissions| + where(**permissions).first_or_create + end + end + + def create_by_domains!(permissions_list) + Array(permissions_list).map(&method(:normalize)).map do |permissions| + where(**permissions).first_or_create! + end + end + + def create_or_update(domain_permissions) + domain_permissions = normalize(domain_permissions) + permissions = find_by(domain: domain_permissions[:domain]) + if permissions.present? + permissions.update(**domain_permissions) + else + create(**domain_permissions) + end + permissions + end + + def create_or_update!(domain_permissions) + domain_permissions = normalize(domain_permissions) + permissions = find_by(domain: domain_permissions[:domain]) + if permissions.present? + permissions.update!(**domain_permissions) + else + create!(**domain_permissions) + end + permissions + end + + private + + def normalize(hash) + hash.symbolize_keys! + hash[:domain] = hash[:domain].strip.downcase + hash.compact + end + end +end |