diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/gun/connection_pool.ex | 6 | ||||
| -rw-r--r-- | lib/pleroma/gun/connection_pool/worker.ex | 48 | ||||
| -rw-r--r-- | lib/pleroma/plugs/frontend_static.ex | 54 | ||||
| -rw-r--r-- | lib/pleroma/plugs/instance_static.ex | 15 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/object_validator.ex | 7 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/pipeline.ex | 7 | ||||
| -rw-r--r-- | lib/pleroma/web/endpoint.ex | 11 | ||||
| -rw-r--r-- | lib/pleroma/web/views/masto_fe_view.ex | 32 | 
8 files changed, 115 insertions, 65 deletions
| diff --git a/lib/pleroma/gun/connection_pool.ex b/lib/pleroma/gun/connection_pool.ex index 8b41a668c..49e9885bb 100644 --- a/lib/pleroma/gun/connection_pool.ex +++ b/lib/pleroma/gun/connection_pool.ex @@ -19,7 +19,7 @@ defmodule Pleroma.Gun.ConnectionPool do          get_gun_pid_from_worker(worker_pid, true)        [{worker_pid, {gun_pid, _used_by, _crf, _last_reference}}] -> -        GenServer.cast(worker_pid, {:add_client, self(), false}) +        GenServer.call(worker_pid, :add_client)          {:ok, gun_pid}        [] -> @@ -45,7 +45,7 @@ defmodule Pleroma.Gun.ConnectionPool do      # so instead we use cast + monitor      ref = Process.monitor(worker_pid) -    if register, do: GenServer.cast(worker_pid, {:add_client, self(), true}) +    if register, do: GenServer.cast(worker_pid, {:add_client, self()})      receive do        {:conn_pid, pid} -> @@ -70,7 +70,7 @@ defmodule Pleroma.Gun.ConnectionPool do      case query_result do        [worker_pid] -> -        GenServer.cast(worker_pid, {:remove_client, self()}) +        GenServer.call(worker_pid, :remove_client)        [] ->          :ok diff --git a/lib/pleroma/gun/connection_pool/worker.ex b/lib/pleroma/gun/connection_pool/worker.ex index f33447cb6..fec9d0efa 100644 --- a/lib/pleroma/gun/connection_pool/worker.ex +++ b/lib/pleroma/gun/connection_pool/worker.ex @@ -36,7 +36,24 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do    end    @impl true -  def handle_cast({:add_client, client_pid, send_pid_back}, %{key: key} = state) do +  def handle_cast({:add_client, client_pid}, state) do +    case handle_call(:add_client, {client_pid, nil}, state) do +      {:reply, conn_pid, state, :hibernate} -> +        send(client_pid, {:conn_pid, conn_pid}) +        {:noreply, state, :hibernate} +    end +  end + +  @impl true +  def handle_cast({:remove_client, client_pid}, state) do +    case handle_call(:remove_client, {client_pid, nil}, state) do +      {:reply, _, state, :hibernate} -> +        {:noreply, state, :hibernate} +    end +  end + +  @impl true +  def handle_call(:add_client, {client_pid, _}, %{key: key} = state) do      time = :erlang.monotonic_time(:millisecond)      {{conn_pid, _, _, _}, _} = @@ -44,8 +61,6 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do          {conn_pid, [client_pid | used_by], crf(time - last_reference, crf), time}        end) -    if send_pid_back, do: send(client_pid, {:conn_pid, conn_pid}) -      state =        if state.timer != nil do          Process.cancel_timer(state[:timer]) @@ -57,11 +72,11 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do      ref = Process.monitor(client_pid)      state = put_in(state.client_monitors[client_pid], ref) -    {:noreply, state, :hibernate} +    {:reply, conn_pid, state, :hibernate}    end    @impl true -  def handle_cast({:remove_client, client_pid}, %{key: key} = state) do +  def handle_call(:remove_client, {client_pid, _}, %{key: key} = state) do      {{_conn_pid, used_by, _crf, _last_reference}, _} =        Registry.update_value(@registry, key, fn {conn_pid, used_by, crf, last_reference} ->          {conn_pid, List.delete(used_by, client_pid), crf, last_reference} @@ -78,7 +93,7 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do          nil        end -    {:noreply, %{state | timer: timer}, :hibernate} +    {:reply, :ok, %{state | timer: timer}, :hibernate}    end    @impl true @@ -102,22 +117,13 @@ defmodule Pleroma.Gun.ConnectionPool.Worker do    @impl true    def handle_info({:DOWN, _ref, :process, pid, reason}, state) do -    # Sometimes the client is dead before we demonitor it in :remove_client, so the message -    # arrives anyway +    :telemetry.execute( +      [:pleroma, :connection_pool, :client_death], +      %{client_pid: pid, reason: reason}, +      %{key: state.key} +    ) -    case state.client_monitors[pid] do -      nil -> -        {:noreply, state, :hibernate} - -      _ref -> -        :telemetry.execute( -          [:pleroma, :connection_pool, :client_death], -          %{client_pid: pid, reason: reason}, -          %{key: state.key} -        ) - -        handle_cast({:remove_client, pid}, state) -    end +    handle_cast({:remove_client, pid}, state)    end    # LRFU policy: https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.55.1478 diff --git a/lib/pleroma/plugs/frontend_static.ex b/lib/pleroma/plugs/frontend_static.ex new file mode 100644 index 000000000..f549ca75f --- /dev/null +++ b/lib/pleroma/plugs/frontend_static.ex @@ -0,0 +1,54 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Plugs.FrontendStatic do +  require Pleroma.Constants + +  @moduledoc """ +  This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends. +  """ +  @behaviour Plug + +  def file_path(path, frontend_type \\ :primary) do +    if configuration = Pleroma.Config.get([:frontends, frontend_type]) do +      instance_static_path = Pleroma.Config.get([:instance, :static_dir], "instance/static") + +      Path.join([ +        instance_static_path, +        "frontends", +        configuration["name"], +        configuration["ref"], +        path +      ]) +    else +      nil +    end +  end + +  def init(opts) do +    opts +    |> Keyword.put(:from, "__unconfigured_frontend_static_plug") +    |> Plug.Static.init() +  end + +  def call(conn, opts) do +    frontend_type = Map.get(opts, :frontend_type, :primary) +    path = file_path("", frontend_type) + +    if path do +      conn +      |> call_static(opts, path) +    else +      conn +    end +  end + +  defp call_static(conn, opts, from) do +    opts = +      opts +      |> Map.put(:from, from) + +    Plug.Static.call(conn, opts) +  end +end diff --git a/lib/pleroma/plugs/instance_static.ex b/lib/pleroma/plugs/instance_static.ex index 7516f75c3..0fb57e422 100644 --- a/lib/pleroma/plugs/instance_static.ex +++ b/lib/pleroma/plugs/instance_static.ex @@ -16,28 +16,24 @@ defmodule Pleroma.Plugs.InstanceStatic do      instance_path =        Path.join(Pleroma.Config.get([:instance, :static_dir], "instance/static/"), path) -    if File.exists?(instance_path) do -      instance_path -    else +    frontend_path = Pleroma.Plugs.FrontendStatic.file_path(path, :primary) + +    (File.exists?(instance_path) && instance_path) || +      (frontend_path && File.exists?(frontend_path) && frontend_path) ||        Path.join(Application.app_dir(:pleroma, "priv/static/"), path) -    end    end    def init(opts) do      opts      |> Keyword.put(:from, "__unconfigured_instance_static_plug") -    |> Keyword.put(:at, "/__unconfigured_instance_static_plug")      |> Plug.Static.init()    end    for only <- Pleroma.Constants.static_only_files() do -    at = Plug.Router.Utils.split("/") -      def call(%{request_path: "/" <> unquote(only) <> _} = conn, opts) do        call_static(          conn,          opts, -        unquote(at),          Pleroma.Config.get([:instance, :static_dir], "instance/static")        )      end @@ -47,11 +43,10 @@ defmodule Pleroma.Plugs.InstanceStatic do      conn    end -  defp call_static(conn, opts, at, from) do +  defp call_static(conn, opts, from) do      opts =        opts        |> Map.put(:from, from) -      |> Map.put(:at, at)      Plug.Static.call(conn, opts)    end diff --git a/lib/pleroma/web/activity_pub/object_validator.ex b/lib/pleroma/web/activity_pub/object_validator.ex index df926829c..0dcc7be4d 100644 --- a/lib/pleroma/web/activity_pub/object_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validator.ex @@ -9,6 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do    the system.    """ +  alias Pleroma.Activity    alias Pleroma.EctoType.ActivityPub.ObjectValidators    alias Pleroma.Object    alias Pleroma.User @@ -71,6 +72,12 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do             |> UndoValidator.cast_and_validate()             |> Ecto.Changeset.apply_action(:insert) do        object = stringify_keys(object) +      undone_object = Activity.get_by_ap_id(object["object"]) + +      meta = +        meta +        |> Keyword.put(:object_data, undone_object.data) +        {:ok, object, meta}      end    end diff --git a/lib/pleroma/web/activity_pub/pipeline.ex b/lib/pleroma/web/activity_pub/pipeline.ex index 6875c47f6..36e325c37 100644 --- a/lib/pleroma/web/activity_pub/pipeline.ex +++ b/lib/pleroma/web/activity_pub/pipeline.ex @@ -52,6 +52,13 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do        do_not_federate = meta[:do_not_federate] || !Config.get([:instance, :federating])        if !do_not_federate && local do +        activity = +          if object = Keyword.get(meta, :object_data) do +            %{activity | data: Map.put(activity.data, "object", object)} +          else +            activity +          end +          Federator.publish(activity)          {:ok, :federated}        else diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index 226d42c2c..527fb288d 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -28,6 +28,17 @@ defmodule Pleroma.Web.Endpoint do      }    ) +  # Careful! No `only` restriction here, as we don't know what frontends contain. +  plug(Pleroma.Plugs.FrontendStatic, +    at: "/", +    frontend_type: :primary, +    gzip: true, +    cache_control_for_etags: @static_cache_control, +    headers: %{ +      "cache-control" => @static_cache_control +    } +  ) +    # Serve at "/" the static files from "priv/static" directory.    #    # You should set gzip to true if you are running phoenix.digest diff --git a/lib/pleroma/web/views/masto_fe_view.ex b/lib/pleroma/web/views/masto_fe_view.ex index f739dacb6..b1669d198 100644 --- a/lib/pleroma/web/views/masto_fe_view.ex +++ b/lib/pleroma/web/views/masto_fe_view.ex @@ -9,36 +9,6 @@ defmodule Pleroma.Web.MastoFEView do    alias Pleroma.Web.MastodonAPI.AccountView    alias Pleroma.Web.MastodonAPI.CustomEmojiView -  @default_settings %{ -    onboarded: true, -    home: %{ -      shows: %{ -        reblog: true, -        reply: true -      } -    }, -    notifications: %{ -      alerts: %{ -        follow: true, -        favourite: true, -        reblog: true, -        mention: true -      }, -      shows: %{ -        follow: true, -        favourite: true, -        reblog: true, -        mention: true -      }, -      sounds: %{ -        follow: true, -        favourite: true, -        reblog: true, -        mention: true -      } -    } -  } -    def initial_state(token, user, custom_emojis) do      limit = Config.get([:instance, :limit]) @@ -86,7 +56,7 @@ defmodule Pleroma.Web.MastoFEView do            "video\/mp4"          ]        }, -      settings: user.mastofe_settings || @default_settings, +      settings: user.mastofe_settings || %{},        push_subscription: nil,        accounts: %{user.id => render(AccountView, "show.json", user: user, for: user)},        custom_emojis: render(CustomEmojiView, "index.json", custom_emojis: custom_emojis), | 
