Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
CS: Enable searching for teams with subscriptions (#5415)
hq16f8156a48242
diff --git a/extra/lib/plausible/customer_support/resource.ex b/extra/lib/plausible/customer_support/resource.exindex 04b368cb08..3c2d0c6800 100644--- a/extra/lib/plausible/customer_support/resource.ex+++ b/extra/lib/plausible/customer_support/resource.ex@@ -13,7 +13,7 @@ defmodule Plausible.CustomerSupport.Resource dotype: String.t()}- @callback search(String.t(), pos_integer()) :: list(schema())+ @callback search(String.t(), Keyword.t()) :: list(schema())@callback get(pos_integer()) :: schema()@callback component() :: module()@callback type() :: String.t()diff --git a/extra/lib/plausible/customer_support/resource/site.ex b/extra/lib/plausible/customer_support/resource/site.exindex 397f88ae56..df09883d08 100644--- a/extra/lib/plausible/customer_support/resource/site.ex+++ b/extra/lib/plausible/customer_support/resource/site.ex@@ -3,7 +3,11 @@ defmodule Plausible.CustomerSupport.Resource.Site douse Plausible.CustomerSupport.Resource, component: PlausibleWeb.CustomerSupport.Live.Site@impl true- def search("", limit) do+ def search(input, opts \\ [])++ def search("", opts) do+ limit = Keyword.fetch!(opts, :limit)+q =from s in Plausible.Site,inner_join: t in assoc(s, :team),@@ -17,18 +21,21 @@ defmodule Plausible.CustomerSupport.Resource.Site doPlausible.Repo.all(q)end- def search(input, limit) do+ def search(input, opts) do+ limit = Keyword.fetch!(opts, :limit)+q =from s in Plausible.Site,inner_join: t in assoc(s, :team),inner_join: o in assoc(t, :owners),where:ilike(s.domain, ^"%#{input}%") or ilike(t.name, ^"%#{input}%") or- ilike(o.name, ^"%#{input}%"),+ ilike(o.name, ^"%#{input}%") or ilike(o.email, ^"%#{input}%"),order_by: [desc: fragment("?.domain = ?", s, ^input),desc: fragment("?.name = ?", t, ^input),desc: fragment("?.name = ?", o, ^input),+ desc: fragment("?.email = ?", o, ^input),asc: s.domain],limit: ^limit,diff --git a/extra/lib/plausible/customer_support/resource/team.ex b/extra/lib/plausible/customer_support/resource/team.exindex c984ff59a6..c40c1c3c75 100644--- a/extra/lib/plausible/customer_support/resource/team.ex+++ b/extra/lib/plausible/customer_support/resource/team.ex@@ -1,33 +1,60 @@defmodule Plausible.CustomerSupport.Resource.Team do@moduledoc falseuse Plausible.CustomerSupport.Resource, component: PlausibleWeb.CustomerSupport.Live.Team+ alias Plausible.Teams@impl true- def search("", limit) do+ def search(input, opts \\ [])++ def search("", opts) do+ limit = Keyword.fetch!(opts, :limit)+q =from t in Plausible.Teams.Team,+ as: :team,inner_join: o in assoc(t, :owners),limit: ^limit,where: not is_nil(t.trial_expiry_date),+ left_lateral_join: s in subquery(Teams.last_subscription_join_query()),+ on: true,order_by: [desc: :id],- preload: [owners: o]+ preload: [owners: o, subscription: s]Plausible.Repo.all(q)end- def search(input, limit) do+ def search(input, opts) do+ limit = Keyword.fetch!(opts, :limit)+q =from t in Plausible.Teams.Team,+ as: :team,inner_join: o in assoc(t, :owners),- where: ilike(t.name, ^"%#{input}%") or ilike(o.name, ^"%#{input}%"),+ where:+ ilike(t.name, ^"%#{input}%") or ilike(o.name, ^"%#{input}%") or+ ilike(o.email, ^"%#{input}%"),limit: ^limit,order_by: [desc: fragment("?.name = ?", t, ^input),desc: fragment("?.name = ?", o, ^input),+ desc: fragment("?.email = ?", o, ^input),asc: t.name],preload: [owners: o]+ q =+ if opts[:with_subscription_only?] do+ from t in q,+ inner_lateral_join: s in subquery(Teams.last_subscription_join_query()),+ on: true,+ preload: [subscription: s]+ else+ from t in q,+ left_lateral_join: s in subquery(Teams.last_subscription_join_query()),+ on: true,+ preload: [subscription: s]+ end+Plausible.Repo.all(q)enddiff --git a/extra/lib/plausible/customer_support/resource/user.ex b/extra/lib/plausible/customer_support/resource/user.exindex 618c03c594..6d52cbd2e1 100644--- a/extra/lib/plausible/customer_support/resource/user.ex+++ b/extra/lib/plausible/customer_support/resource/user.ex@@ -9,7 +9,11 @@ defmodule Plausible.CustomerSupport.Resource.User doend@impl true- def search("", limit) do+ def search(input, opts \\ [])++ def search("", opts) do+ limit = Keyword.fetch!(opts, :limit)+q =from u in Plausible.Auth.User,order_by: [@@ -21,7 +25,9 @@ defmodule Plausible.CustomerSupport.Resource.User doPlausible.Repo.all(q)end- def search(input, limit) do+ def search(input, opts) do+ limit = Keyword.fetch!(opts, :limit)+q =from u in Plausible.Auth.User,where: ilike(u.email, ^"%#{input}%") or ilike(u.name, ^"%#{input}%"),diff --git a/extra/lib/plausible_web/live/customer_support.ex b/extra/lib/plausible_web/live/customer_support.exindex 99467d0fe2..e844f1ca62 100644--- a/extra/lib/plausible_web/live/customer_support.ex+++ b/extra/lib/plausible_web/live/customer_support.ex@@ -29,54 +29,88 @@ defmodule PlausibleWeb.Live.CustomerSupport do~H"""<.flash_messages flash={@flash} />- <div class="container pt-6">- <div class="group mt-6 pb-5 border-b border-gray-200 dark:border-gray-500 flex items-center justify-between">- <h2 class="text-2xl font-bold leading-7 text-gray-900 dark:text-gray-100 sm:text-3xl sm:leading-9 sm:truncate flex-shrink-0">- 💬 Customer Support- </h2>+ <div x-data="{ openHelp: false }">+ <div+ id="help"+ x-show="openHelp"+ x-cloak+ class="p-16 fixed top-0 left-0 w-full h-full bg-gray-800 text-gray-300 bg-opacity-95 z-50 flex items-center justify-center"+ >+ <div @click.away="openHelp = false" @click="openHelp = false">+ Prefix your searches with: <br /><br />+ <div class="font-mono">+ <strong>site:</strong>input<br />+ <p class="font-sans pl-2 mb-1">+ Search for sites exclusively. Input will be checked against site's domain, team's name, owners' names and e-mails.+ </p>+ <strong>user:</strong>input<br />+ <p class="font-sans pl-2 mb-1">+ Search for users exclusively. Input will be checked against user's name and e-mail.+ </p>+ <strong>team:</strong>input<br />+ <p class="font-sans pl-2 mb-1">+ Search for teams exclusively. Input will be checked against user's name and e-mail.+ </p>++ <strong>team:</strong>input <strong>+sub</strong>+ <br />+ <p class="font-sans pl-2 mb-1">+ Like above, but only finds team(s) with subscription (in any status).+ </p>+ </div>+ </div></div>- <div class="mb-4 mt-4">- <.filter_bar filter_text={@filter_text} placeholder="Search everything"></.filter_bar>- </div>+ <div class="container pt-6">+ <div class="group mt-6 pb-5 border-b border-gray-200 dark:border-gray-500 flex items-center justify-between">+ <h2 class="text-2xl font-bold leading-7 text-gray-900 dark:text-gray-100 sm:text-3xl sm:leading-9 sm:truncate flex-shrink-0">+ 💬 Customer Support+ </h2>+ </div>++ <div class="mb-4 mt-4">+ <.filter_bar filter_text={@filter_text} placeholder="Search everything">+ <a class="cursor-pointer" @click="openHelp = !openHelp">+ <Heroicons.question_mark_circle class="text-indigo-700 dark:text-gray-500 w-5 h-5 hover:stroke-2" />+ </a>+ </.filter_bar>+ </div>- <ul :if={!@current} class="my-6 grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3">- <li :for={r <- @results} class="group relative">- <.link- patch={"/cs/#{r.type}s/#{r.type}/#{r.id}"}- data-test-type={r.type}- data-test-id={r.id}- >- <div class="col-span-1 bg-white dark:bg-gray-800 rounded-lg shadow p-4 group-hover:shadow-lg cursor-pointer">- <div class="text-gray-800 dark:text-gray-500 w-full flex items-center justify-between space-x-4">- <.render_result resource={r} />+ <ul :if={!@current} class="my-6 grid grid-cols-1 gap-6 sm:grid-cols-2 lg:grid-cols-3">+ <li :for={r <- @results} class="group relative">+ <.link+ patch={"/cs/#{r.type}s/#{r.type}/#{r.id}"}+ data-test-type={r.type}+ data-test-id={r.id}+ >+ <div class="col-span-1 bg-white dark:bg-gray-800 rounded-lg shadow p-4 group-hover:shadow-lg cursor-pointer">+ <div class="text-gray-800 dark:text-gray-500 w-full flex items-center justify-between space-x-4">+ <.render_result resource={r} />+ </div></div>- </div>- </.link>- </li>- </ul>+ </.link>+ </li>+ </ul>- <div- id="modal"- class={[+ <div class={[if(is_nil(@current), do: "hidden")- ]}- >- <div class="overflow-auto bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-300 w-full h-3/4 max-w-7xl max-h-full p-4 rounded-lg shadow-lg">- <div class="flex justify-between text-xs">- <.styled_link onclick="window.history.go(-1); return false;">- ← Previous- </.styled_link>- <.styled_link :if={@current} class="text-xs" href={kaffy_url(@current, @id)}>- open in Kaffy- </.styled_link>+ ]}>+ <div class="overflow-auto bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-300 w-full h-3/4 max-w-7xl max-h-full p-4 rounded-lg shadow-lg">+ <div class="flex justify-between text-xs">+ <.styled_link onclick="window.history.go(-1); return false;">+ ← Previous+ </.styled_link>+ <.styled_link :if={@current} class="text-xs" href={kaffy_url(@current, @id)}>+ open in Kaffy+ </.styled_link>+ </div>+ <.live_component+ :if={@current}+ module={@current.component()}+ resource_id={@id}+ id={"#{@current.type()}-#{@id}"}+ /></div>- <.live_component- :if={@current}- module={@current.component()}- resource_id={@id}- id={"#{@current.type()}-#{@id}"}- /></div></div></div>@@ -143,13 +177,13 @@ defmodule PlausibleWeb.Live.CustomerSupport dodefp spawn_searches(input) doinput = String.trim(input)- {resources, input, limit} =+ {resources, input, opts} =maybe_focus_search(input)resources|> Task.async_stream(fn resource ->input- |> resource.search(limit)+ |> resource.search(opts)|> Enum.map(&resource.dump/1)end)|> Enum.reduce([], fn {:ok, results}, acc ->@@ -158,23 +192,33 @@ defmodule PlausibleWeb.Live.CustomerSupport doenddefp maybe_focus_search(lone_modifier) when lone_modifier in ["site:", "team:", "user:"] do- {[], "", 0}+ {[], "", limit: 0}enddefp maybe_focus_search("site:" <> rest) do- {[Resource.Site], rest, 90}+ {[Resource.Site], rest, limit: 90}enddefp maybe_focus_search("team:" <> rest) do- {[Resource.Team], rest, 90}+ [input | mods] = String.split(rest, "+", trim: true)+ input = String.trim(input)++ opts =+ if "sub" in mods do+ [limit: 90, with_subscription_only?: true]+ else+ [limit: 90]+ end++ {[Resource.Team], input, opts}enddefp maybe_focus_search("user:" <> rest) do- {[Resource.User], rest, 90}+ {[Resource.User], rest, limit: 90}enddefp maybe_focus_search(input) do- {@resources, input, 30}+ {@resources, input, limit: 30}enddefp set_filter_text(socket, filter_text) dodiff --git a/extra/lib/plausible_web/live/customer_support/live/team.ex b/extra/lib/plausible_web/live/customer_support/live/team.exindex 17a72ff93c..06987caa5b 100644--- a/extra/lib/plausible_web/live/customer_support/live/team.ex+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex@@ -480,6 +480,13 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-800">Team</span>++ <span+ :if={@resource.object.subscription}+ class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-green-100 text-green-800"+ >+ $+ </span></div><hr class="mt-4 mb-4 flex-grow border-t border-gray-200 dark:border-gray-600" />diff --git a/test/plausible_web/live/customer_support_test.exs b/test/plausible_web/live/customer_support_test.exsindex 81c0f1fc89..342b8f3bc7 100644--- a/test/plausible_web/live/customer_support_test.exs+++ b/test/plausible_web/live/customer_support_test.exs@@ -60,6 +60,40 @@ defmodule PlausibleWeb.Live.CustomerSupportTest doassert_search_result(html, "site", site2.id)end++ test "search teams", %{conn: conn} do+ team1 = new_user(team: [name: "Team One"]) |> team_of()++ team2 =+ new_user(team: [name: "Team Two"])+ |> subscribe_to_growth_plan()+ |> team_of()++ team3 = new_user(team: [name: "Team Three"]) |> team_of()++ {:ok, lv, _html} = live(conn, @cs_index)++ type_into_input(lv, "filter-text", "team:Team")+ html = render(lv)++ assert_search_result(html, "team", team1.id)+ assert_search_result(html, "team", team2.id)+ assert_search_result(html, "team", team3.id)++ type_into_input(lv, "filter-text", "team:Team T")+ html = render(lv)++ refute_search_result(html, "team", team1.id)+ assert_search_result(html, "team", team2.id)+ assert_search_result(html, "team", team3.id)++ type_into_input(lv, "filter-text", "team:Team T +sub")+ html = render(lv)++ refute_search_result(html, "team", team1.id)+ assert_search_result(html, "team", team2.id)+ refute_search_result(html, "team", team3.id)+ endenddefp assert_search_result(doc, type, id) do