Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def website_params
attribute_names = [
:url, :repository, :about_type, :about_id, :in_production, :in_production_at,
:in_showcase,
:git_provider, :git_endpoint, :git_branch, :plausible_url,
:git_provider, :git_endpoint, :git_branch, :plausible_url, :should_setup_plausible,
:feature_posts, :feature_agenda, :feature_portfolio, :feature_jobboard, :feature_alumni, :feature_syndication, :feature_alerts, :feature_hourly_publication,
:default_time_zone, :hosting, :apache_config_custom_content,
:deployment_status_badge, :autoupdate_theme, :archive_content, :years_before_archive_content,
Expand Down
7 changes: 7 additions & 0 deletions app/jobs/communication/website/plausible/setup_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Communication::Website::Plausible::SetupJob < Communication::Website::BaseJob
queue_as :mice

def execute
website.plausible_setup_safely
end
end
23 changes: 23 additions & 0 deletions app/models/communication/website/with_plausible.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
module Communication::Website::WithPlausible
extend ActiveSupport::Concern

included do
attr_accessor :should_setup_plausible

after_save :plausible_setup, if: -> { should_setup_plausible == "1" && !has_plausible? }
end

def has_plausible?
plausible_url.present?
end
Expand All @@ -13,4 +19,21 @@ def plausible_site_domain
rescue URI::InvalidURIError
nil
end

def plausible_setup
Communication::Website::Plausible::SetupJob.perform_later(id)
@should_setup_plausible = nil
end

def plausible_setup_safely
plausible_site_id = URI.parse(url).host
plausible_site_id = plausible_site_id.delete_prefix("www.") if plausible_site_id.start_with?("www.")
plausible_site = plausible.create_site(plausible_site_id)
plausible_shared_link = plausible.create_shared_link(plausible_site["id"], "Osuny")
update(plausible_url: plausible_shared_link["url"])
end

def plausible
@plausible ||= Plausible.new
end
end
28 changes: 28 additions & 0 deletions app/services/plausible.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Plausible

def create_site(site_id)
response = client.post("sites", {
domain: site_id,
timezone: Time.zone.name
})
JSON.parse(response.body)
end

def create_shared_link(site_id, name)
response = client.put("sites/shared-links", { site_id: site_id, name: name })
JSON.parse(response.body)
end

protected

def client
unless @client
@client = Faraday.new url: 'https://plausible.io/api/v1/'
@client.request :authorization,
"Bearer",
ENV['PLAUSIBLE_API_KEY']
end
@client
end

end
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
<div class="row">
<div class="col-xl-6">
<%= f.input :url %>
<%= f.input :should_setup_plausible, as: :check_box %>
</div>
</div>

<% content_for :action_bar_right do %>
<%= cancel [:admin, @website] %>
<%= submit f %>
<% end %>
<% end %>
<% end %>
16 changes: 9 additions & 7 deletions config/application.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,32 @@ GOOD_JOB_EXECUTION_MODE: external

KEYCDN_HOST:

LANGUAGE_TOOL_ADD_ON_TOKEN:
LANGUAGE_TOOL_USERNAME:
LANGUAGE_TOOL_ADD_ON_TOKEN:
LANGUAGE_TOOL_USERNAME:

LIBRETRANSLATE_API_KEY:
LIBRETRANSLATE_API_KEY:

MAIL_FROM_DEFAULT_ADDRESS:
MAIL_FROM_DEFAULT_NAME:

MAINTENANCE: "false"

MICROLINK_API_KEY:
MICROLINK_API_KEY:

OSUNY_API_AUTOUPDATE_THEME_KEY:
OSUNY_DEVELOPMENT_DBNAME: # Can be used when working on two incompatible branches (e.g. main & i18n)
OSUNY_DESIGN:
OSUNY_DESIGN:
OSUNY_STAGING_APP_NAME:
OSUNY_STAGING_PG_ADDON_ID:
OSUNY_SHOWCASE:
OSUNY_TRANSPARENCY:
OSUNY_SHOWCASE:
OSUNY_TRANSPARENCY:

OTP_SECRET_ENCRYPTION_KEY:

PEXELS_API_KEY:

PLAUSIBLE_API_KEY:

RACK_TIMEOUT_SERVICE_TIMEOUT: "0"

SCALEWAY_OS_ACCESS_KEY_ID:
Expand Down
Loading