Skip to content

Commit

Permalink
add support for using either public_name or public_identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
jlbyrne committed Aug 1, 2024
1 parent 6ea675e commit 0604330
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 16 deletions.
7 changes: 4 additions & 3 deletions app/controllers/public_channel_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ class PublicChannelController < ApplicationController
layout "public"

def show
channel = Channel.includes(:site_banner).find_by(public_identifier: params[:public_identifier])
# channel_title is used in the meta tags
@channel_title = channel&.publication_title
channel = Channel.includes(:site_banner).find_by(public_identifier: params[:public_identifier]) || Channel.includes(:site_banner).find_by(public_name: params[:public_identifier])
@crypto_addresses = channel&.crypto_addresses&.pluck(:address, :chain)

# Handle the case when the resource is not found
if channel.nil? || @crypto_addresses.empty?
redirect_to root_path, alert: "Channel not found"
return
end

# channel_title is used in the meta tags
@channel_title = channel&.publication_title
@url = channel.details&.url
@site_banner = channel.site_banner&.read_only_react_property || SiteBanner.new_helper(current_publisher.id, channel.id)

Expand Down
21 changes: 18 additions & 3 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ class Channel < ApplicationRecord
}, allow_nil: true

validates :public_name, uniqueness: true, allow_nil: true
# validates :public_identifier, uniqueness: true, presence: true
validates :public_identifier, uniqueness: true, presence: true
validate :validate_public_url_unique

validate :site_channel_details_brave_publisher_id_unique_for_publisher, if: -> { details_type == "SiteChannelDetails" }

validate :verified_duplicate_channels_must_be_contested, if: -> { verified? }

before_create :set_public_identifier
after_initialize :set_public_identifier, if: -> { public_identifier.nil? }
before_validation :strip_public_name_whitespace

after_save :notify_slack, if: -> { saved_change_to_verified? && verified? }

Expand Down Expand Up @@ -361,7 +363,7 @@ def set_public_identifier
return if public_identifier.present?
identifier = loop do
id = SecureRandom.alphanumeric(10)
break id unless Channel.where(public_identifier: id).exists?
break id unless Channel.where(public_identifier: id).exists? || Channel.where(public_name: id).exists?
end

self.public_identifier = identifier
Expand Down Expand Up @@ -508,4 +510,17 @@ def verified_duplicate_channels_must_be_contested
end
end
end

def validate_public_url_unique
public_name_exists = Channel.where(public_name: public_identifier).exists?
public_identifier_exists = Channel.where(public_identifier: public_name).exists?

if public_name_exists || public_identifier_exists
errors.add(:base, "must be unique across both public_name and public_identifier")
end
end

def strip_public_name_whitespace
self.public_name = public_name.gsub(/\s+/, "") unless public_name.nil?
end
end
Loading

0 comments on commit 0604330

Please sign in to comment.