Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for using either public_name or public_identifier #4490

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading