Skip to content

Commit

Permalink
Add contribution banner customizer, migrate public channel page files
Browse files Browse the repository at this point in the history
  • Loading branch information
jlbyrne committed Aug 21, 2024
1 parent acb6822 commit 0610bae
Show file tree
Hide file tree
Showing 40 changed files with 4,522 additions and 301 deletions.
136 changes: 136 additions & 0 deletions app/controllers/api/nextv1/contribution_page_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
class Api::Nextv1::ContributionPageController < Api::Nextv1::BaseController
include PublishersHelper
include ActiveStorage::SetCurrent

MAX_IMAGE_SIZE = 10_000_000

def index
channel_list = current_publisher.channels.as_json(only: [:id, :details_type],
include: {
details: {only: [], methods: [:url, :publication_title]}
})
render(json: channel_list)
end

def show
current_channel = current_publisher.channels.find(params[:id])

SiteBanner.new_helper(current_publisher.id, current_channel.id) if !current_channel.site_banner

channel_data = format_channel_data(current_channel.reload)
render(json: channel_data)
end

def update
begin
current_channel = current_publisher.channels.find(params[:id])
site_banner = current_channel.site_banner
rescue ActiveRecord::RecordNotFound
return render json: {}, status: 404
end

permitted_params = params.permit(
:contribution_page,
:id,
:format,
:description,
:title,
:logo,
:cover,
socialLinks: [:twitter, :reddit, :github, :vimeo, :youtube, :twitch]
)

logo_length = permitted_params[:logo]&.length || 0
cover_length = permitted_params[:cover]&.length || 0

if (cover_length > MAX_IMAGE_SIZE) || (logo_length > MAX_IMAGE_SIZE)
render(json: {errors: t("banner.upload_too_big")}, status: 400)
end

begin
# don't erase old data, particularly other social links stored in the existing json string
new_data = site_banner.read_only_react_property.deep_merge(permitted_params.to_hash.transform_keys(&:to_sym))
# the 'sanatize' method in the update helper doesn't handle ruby hashes well
site_banner.update_helper(new_data[:title], new_data[:description], new_data[:socialLinks].to_json)

if permitted_params[:logo]
site_banner.logo.attach(
image_properties(permitted_params[:logo])
)
site_banner.save!
end

if permitted_params[:cover]
site_banner.background_image.attach(
image_properties(permitted_params[:cover])
)
site_banner.save!
end
site_banner.reload
channel_data = format_channel_data(current_channel)
render(json: channel_data, status: 200)
rescue => e
LogException.perform(e, publisher: current_publisher)
render(json: {errors: "channel banner could not be updated"}, status: 400)
end
end

def destroy_attachment
begin
current_channel = current_publisher.channels.find(params[:id])
site_banner = current_channel.site_banner
rescue ActiveRecord::RecordNotFound
return render json: {}, status: 404
end

permitted_params = params.permit(:logo, :cover)

site_banner.logo.purge if permitted_params[:logo] && site_banner.logo.attached?
site_banner.background_image.purge if permitted_params[:cover] && site_banner.background_image.attached?

channel_data = format_channel_data(current_channel.reload)
render(json: channel_data)
end

private

def format_channel_data(channel)
channel.as_json(only: [:details_type, :id, :public_identifier],
include: {
details: {only: [], methods: [:url, :publication_title]},
site_banner: {only: [], methods: [:read_only_react_property]}
})
end

def image_properties(data)
if data.starts_with?("data:image/jpeg") || data.starts_with?("data:image/jpg")
extension = ".jpg"
elsif data.starts_with?("data:image/png")
extension = ".png"
elsif data.starts_with?("data:image/webp")
extension = ".webp"
else
LogException.perform(StandardError.new("Unknown image format:" + data), params: {})
return nil
end
filename = Time.now.to_s.tr!(" ", "_").tr!(":", "_") + current_publisher.id

temp_file = Tempfile.new([filename, extension])
File.binwrite(temp_file.path, Base64.decode64(data))

original_image_path = temp_file.path
temp_file.rewind
new_filename = generate_filename(source_image_path: original_image_path)
{
io: File.open(original_image_path),
filename: new_filename + extension + ".padded",
content_type: "image/#{extension}"
}
end

def generate_filename(source_image_path:)
File.open(source_image_path, "r") do |f|
Digest::SHA256.hexdigest f.read
end
end
end
43 changes: 43 additions & 0 deletions app/controllers/api/nextv1/public_channel_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Api::Nextv1::PublicChannelController < Api::Nextv1::BaseController
def show
channel = Channel.includes(:site_banner).find_by(public_identifier: params[:public_identifier])
channel_title = channel&.publication_title
crypto_addresses = channel&.crypto_addresses&.pluck(:address, :chain)

# Handle the case when the resource is not found
if channel.nil? || @crypto_addresses&.empty?
return render json: {}, status: 404
end

begin
url = channel.details&.url
site_banner = channel.site_banner&.read_only_react_property || SiteBanner.new_helper(current_publisher.id, channel.id)

crypto_constants = {
solana_main_url: ENV["SOLANA_MAIN_URL"],
solana_bat_address: ENV["SOLANA_BAT_ADDRESS"],
eth_bat_address: ENV["ETH_BAT_ADDRESS"],
eth_usdc_address: ENV["ETH_USDC_ADDRESS"],
solana_usdc_address: ENV["SOLANA_USDC_ADDRESS"]
}

response_data = {
url: url,
site_banner: site_banner,
crypto_addresses: crypto_addresses,
title: channel_title,
crypto_constants: crypto_constants
}

render(json: response_data.to_json, status: 200)
rescue => e
LogException.perform(e)
render(json: {errors: "channel information not found"}, status: 400)
end
end

def get_ratios
ratios = Ratio::Ratio.channel_page_cached
render json: ratios["payload"]
end
end
9 changes: 9 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@
get "publishers/security", to: "publishers#security"
get "home/dashboard", to: "home#dashboard"

resources :contribution_page, only: %i[index show update] do
member do
delete :destroy_attachment
end
end

resources :channels, only: %i[destroy] do
resources :crypto_address_for_channels, only: %i[index create destroy] do
collection do
Expand Down Expand Up @@ -214,6 +220,9 @@
resource :bitflyer_connection
resource :uphold_connection, except: [:new]
end

get "c/:public_identifier", to: "public_channel#show", as: :public_channel
get "/get_ratios", to: "public_channel#get_ratios"
end

namespace :v1, defaults: {format: :json} do
Expand Down
Loading

0 comments on commit 0610bae

Please sign in to comment.