Skip to content

Commit

Permalink
Merge branch 'master' into people-dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol authored Jan 6, 2025
2 parents c2c8dec + c29b93f commit 9ba333d
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file.
- Dashboard shows comparisons for all reports
- UTM Medium report and API shows (gclid) and (msclkid) for paid searches when no explicit utm medium present.
- Support for `case_sensitive: false` modifiers in Stats API V2 filters for case-insensitive searches.
- Add text version to emails plausible/analytics#4674

### Removed

Expand Down
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ RUN mkdir /app
WORKDIR /app

# install build dependencies
RUN apk add --no-cache git nodejs yarn python3 npm ca-certificates wget gnupg make gcc libc-dev brotli && \
npm install npm@latest -g
RUN apk add --no-cache git nodejs yarn python3 npm ca-certificates wget gnupg make gcc libc-dev brotli

COPY mix.exs ./
COPY mix.lock ./
Expand Down
2 changes: 1 addition & 1 deletion assets/js/dashboard/query-dates.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ test.each([
test.each([
[
{ period: 'custom', from: '2024-08-10', to: '2024-08-20' },
'10 Aug - 20 Aug'
'10 Aug - 20 Aug 24'
],
[{ period: 'realtime' }, 'Realtime']
])(
Expand Down
37 changes: 12 additions & 25 deletions lib/plausible/mailer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,21 @@ defmodule Plausible.Mailer do
use Bamboo.Mailer, otp_app: :plausible
require Logger

@type result() :: :ok | {:error, :hard_bounce} | {:error, :unknown_error}

@spec send(Bamboo.Email.t()) :: result()
@spec send(Bamboo.Email.t()) :: :ok | {:error, :unknown_error}
def send(email) do
case deliver_now(email) do
{:ok, _email} -> :ok
{:ok, _email, _response} -> :ok
{:error, error} -> handle_error(error)
end
end

defp handle_error(%{response: response}) when is_binary(response) do
case Jason.decode(response) do
{:ok, %{"ErrorCode" => 406}} ->
{:error, :hard_bounce}
try do
deliver_now!(email)
rescue
e ->
# this message is ignored by Sentry, only appears in logs
log = "Failed to send e-mail:\n\n " <> Exception.format(:error, e, __STACKTRACE__)
# Sentry report is built entirely from crash_reason
crash_reason = {e, __STACKTRACE__}

{:ok, response} ->
Logger.error("Failed to send e-mail", sentry: %{extra: %{response: inspect(response)}})
{:error, :unknown_error}

{:error, _any} ->
Logger.error("Failed to send e-mail", sentry: %{extra: %{response: inspect(response)}})
Logger.error(log, crash_reason: crash_reason)
{:error, :unknown_error}
else
_sent_email -> :ok
end
end

defp handle_error(error) do
Logger.error("Failed to send e-mail", sentry: %{extra: %{response: inspect(error)}})
{:error, :unknown_error}
end
end
10 changes: 7 additions & 3 deletions lib/plausible/site/admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ defmodule Plausible.SiteAdmin do
from(r in query,
as: :site,
inner_join: o in assoc(r, :owner),
preload: [owner: o, team: [team_memberships: :user]],
inner_join: t in assoc(r, :team),
preload: [owner: o, team: t, guest_memberships: [team_membership: :user]],
or_where: ilike(r.domain, ^search_term),
or_where: ilike(o.email, ^search_term),
or_where: ilike(o.name, ^search_term),
Expand Down Expand Up @@ -193,8 +194,8 @@ defmodule Plausible.SiteAdmin do
end

defp get_other_members(site) do
site.team.team_memberships
|> Enum.map(fn m -> m.user.email <> "(#{to_string(m.role)})" end)
site.guest_memberships
|> Enum.map(fn m -> m.team_membership.user.email <> "(#{member_role(m.role)})" end)
|> Enum.join(", ")
end

Expand All @@ -208,4 +209,7 @@ defmodule Plausible.SiteAdmin do

def create_changeset(schema, attrs), do: Plausible.Site.crm_changeset(schema, attrs)
def update_changeset(schema, attrs), do: Plausible.Site.crm_changeset(schema, attrs)

defp member_role(:editor), do: :admin
defp member_role(other), do: other
end
66 changes: 60 additions & 6 deletions lib/plausible_web/email.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule PlausibleWeb.Email do
use Plausible
use Bamboo.Phoenix, view: PlausibleWeb.EmailView
import Bamboo.Email
import Bamboo.PostmarkHelper

def mailer_email_from do
Expand Down Expand Up @@ -460,17 +460,71 @@ defmodule PlausibleWeb.Email do
def base_email(), do: base_email(%{layout: "base_email.html"})

def base_email(%{layout: layout}) do
mailer_from = Application.get_env(:plausible, :mailer_email)

new_email()
|> put_param("TrackOpens", false)
|> from(mailer_from)
|> from(mailer_email_from())
|> maybe_put_layout(layout)
end

defp maybe_put_layout(email, nil), do: email

defp maybe_put_layout(email, layout) do
put_html_layout(email, {PlausibleWeb.LayoutView, layout})
defp maybe_put_layout(%{assigns: assigns} = email, layout) do
%{email | assigns: Map.put(assigns, :layout, {PlausibleWeb.LayoutView, layout})}
end

@doc false
def render(email, template, assigns \\ []) do
assigns = Map.merge(email.assigns, Map.new(assigns))
html = Phoenix.View.render_to_string(PlausibleWeb.EmailView, template, assigns)
email |> html_body(html) |> text_body(textify(html))
end

defp textify(html) do
Floki.parse_fragment!(html)
|> traverse_and_textify()
|> Floki.text()
|> collapse_whitespace()
end

defp traverse_and_textify([head | tail]) do
[traverse_and_textify(head) | traverse_and_textify(tail)]
end

defp traverse_and_textify(text) when is_binary(text) do
String.replace(text, "\n", "\s")
end

defp traverse_and_textify({"a" = tag, attrs, children}) do
href = with {"href", href} <- List.keyfind(attrs, "href", 0), do: href
children = traverse_and_textify(children)

if href do
text = Floki.text(children)

if text == href do
# avoids rendering "http://localhost:8000 (http://localhost:8000)" in base_email footer
text
else
IO.iodata_to_binary([text, " (", href, ?)])
end
else
{tag, attrs, children}
end
end

defp traverse_and_textify({tag, attrs, children}) do
{tag, attrs, traverse_and_textify(children)}
end

defp traverse_and_textify(other), do: other

defp collapse_whitespace(text) do
text
|> String.split("\n")
|> Enum.map_join("\n", fn line ->
line
|> String.split(" ", trim: true)
|> Enum.join(" ")
end)
end
end
3 changes: 2 additions & 1 deletion lib/workers/check_usage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ defmodule Plausible.Workers.CheckUsage do
inner_join: o in assoc(t, :owner),
inner_lateral_join: s in subquery(Teams.last_subscription_join_query()),
on: true,
left_join: ep in assoc(t, :enterprise_plan),
left_join: ep in Plausible.Billing.EnterprisePlan,
on: ep.team_id == t.id and ep.paddle_plan_id == s.paddle_plan_id,
where:
s.status in [
^Subscription.Status.active(),
Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ defmodule Plausible.MixProject do
defp deps do
[
{:bamboo, "~> 2.3", override: true},
{:bamboo_phoenix, "~> 1.0.0"},
{:bamboo_postmark, git: "https://github.com/plausible/bamboo_postmark.git", branch: "main"},
{:bamboo_smtp, "~> 4.1"},
{:bamboo_mua, "~> 0.2.0"},
Expand Down
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"acceptor_pool": {:hex, :acceptor_pool, "1.0.0", "43c20d2acae35f0c2bcd64f9d2bde267e459f0f3fd23dab26485bf518c281b21", [:rebar3], [], "hexpm", "0cbcd83fdc8b9ad2eee2067ef8b91a14858a5883cb7cd800e6fcd5803e158788"},
"bamboo": {:hex, :bamboo, "2.3.0", "d2392a2cabe91edf488553d3c70638b532e8db7b76b84b0a39e3dfe492ffd6fc", [:mix], [{:hackney, ">= 1.15.2", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.4 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "dd0037e68e108fd04d0e8773921512c940e35d981e097b5793543e3b2f9cd3f6"},
"bamboo_mua": {:hex, :bamboo_mua, "0.2.2", "c50cd41ef684155669e2d99d428fbb87e13797a80829a162b47d3c0a7f7e7ecd", [:mix], [{:bamboo, "~> 2.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:mail, "~> 0.3.0", [hex: :mail, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: false]}], "hexpm", "5fe6e3676640578c6fe8f040b34dda8991ebef8566c0601a984eb4771b85b11f"},
"bamboo_phoenix": {:hex, :bamboo_phoenix, "1.0.0", "f3cc591ffb163ed0bf935d256f1f4645cd870cf436545601215745fb9cc9953f", [:mix], [{:bamboo, ">= 2.0.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.3.0", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "6db88fbb26019c84a47994bb2bd879c0887c29ce6c559bc6385fd54eb8b37dee"},
"bamboo_postmark": {:git, "https://github.com/plausible/bamboo_postmark.git", "5eac6dfdffacd273bd9aacdd3e494a600bb0b170", [branch: "main"]},
"bamboo_smtp": {:hex, :bamboo_smtp, "4.2.2", "e9f57a2300df9cb496c48751bd7668a86a2b89aa2e79ccaa34e0c46a5f64c3ae", [:mix], [{:bamboo, "~> 2.2.0", [hex: :bamboo, repo: "hexpm", optional: false]}, {:gen_smtp, "~> 1.2.0", [hex: :gen_smtp, repo: "hexpm", optional: false]}], "hexpm", "28cac2ec8adaae02aed663bf68163992891a3b44cfd7ada0bebe3e09bed7207f"},
"bcrypt_elixir": {:hex, :bcrypt_elixir, "3.1.0", "0b110a9a6c619b19a7f73fa3004aa11d6e719a67e672d1633dc36b6b2290a0f7", [:make, :mix], [{:comeonin, "~> 5.3", [hex: :comeonin, repo: "hexpm", optional: false]}, {:elixir_make, "~> 0.6", [hex: :elixir_make, repo: "hexpm", optional: false]}], "hexpm", "2ad2acb5a8bc049e8d5aa267802631912bb80d5f4110a178ae7999e69dca1bf7"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ defmodule Plausible.IngestRepo.Migrations.AddScrollDepthToImportedPages do
execute """
ALTER TABLE imported_pages
#{@on_cluster}
DROP COLUMN scroll_depth
DROP COLUMN IF EXISTS scroll_depth
"""
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule Plausible.IngestRepo.Migrations.DropAndAddScrollDepthToImportedPages do
use Ecto.Migration

@on_cluster Plausible.MigrationUtils.on_cluster_statement("imported_pages")

def up do
execute """
ALTER TABLE imported_pages
#{@on_cluster}
DROP COLUMN scroll_depth
"""

execute """
ALTER TABLE imported_pages
#{@on_cluster}
ADD COLUMN scroll_depth Nullable(UInt64)
"""
end

def down do
execute """
ALTER TABLE imported_pages
#{@on_cluster}
DROP COLUMN scroll_depth
"""
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Plausible.IngestRepo.Migrations.AddPageleaveVisitorsToImportedPages do
use Ecto.Migration

@on_cluster Plausible.MigrationUtils.on_cluster_statement("imported_pages")

def up do
execute """
ALTER TABLE imported_pages
#{@on_cluster}
ADD COLUMN pageleave_visitors UInt64
"""
end

def down do
execute """
ALTER TABLE imported_pages
#{@on_cluster}
DROP COLUMN pageleave_visitors
"""
end
end
3 changes: 3 additions & 0 deletions test/plausible/imported/csv_importer_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ defmodule Plausible.Imported.CSVImporterTest do
assert email.html_body =~
~s[Please click <a href="http://localhost:8000/#{URI.encode_www_form(exported_site.domain)}/download/export">here</a>]

assert email.text_body =~
~r[Please click here \(http://localhost:8000/#{URI.encode_www_form(exported_site.domain)}/download/export\) to start the download process.]

# download archive
on_ee do
ExAws.request!(
Expand Down
Loading

0 comments on commit 9ba333d

Please sign in to comment.