diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/html.ex | 17 | ||||
| -rw-r--r-- | lib/pleroma/object.ex | 5 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 35 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/utils.ex | 55 | ||||
| -rw-r--r-- | lib/pleroma/web/common_api/common_api.ex | 17 | ||||
| -rw-r--r-- | lib/pleroma/web/common_api/utils.ex | 12 | 
6 files changed, 106 insertions, 35 deletions
diff --git a/lib/pleroma/html.ex b/lib/pleroma/html.ex index 5b152d926..1e48749a8 100644 --- a/lib/pleroma/html.ex +++ b/lib/pleroma/html.ex @@ -28,9 +28,13 @@ defmodule Pleroma.HTML do    def filter_tags(html), do: filter_tags(html, nil)    def strip_tags(html), do: Scrubber.scrub(html, Scrubber.StripTags) +  # TODO: rename object to activity because that's what it is really working with    def get_cached_scrubbed_html_for_object(content, scrubbers, object, module) do      key = "#{module}#{generate_scrubber_signature(scrubbers)}|#{object.id}" -    Cachex.fetch!(:scrubber_cache, key, fn _key -> ensure_scrubbed_html(content, scrubbers) end) + +    Cachex.fetch!(:scrubber_cache, key, fn _key -> +      ensure_scrubbed_html(content, scrubbers, object.data["object"]["fake"] || false) +    end)    end    def get_cached_stripped_html_for_object(content, object, module) do @@ -44,11 +48,20 @@ defmodule Pleroma.HTML do    def ensure_scrubbed_html(          content, -        scrubbers +        scrubbers, +        false = _fake        ) do      {:commit, filter_tags(content, scrubbers)}    end +  def ensure_scrubbed_html( +        content, +        scrubbers, +        true = _fake +      ) do +    {:ignore, filter_tags(content, scrubbers)} +  end +    defp generate_scrubber_signature(scrubber) when is_atom(scrubber) do      generate_scrubber_signature([scrubber])    end diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index 8a670645d..013d62157 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -44,6 +44,11 @@ defmodule Pleroma.Object do    # Use this whenever possible, especially when walking graphs in an O(N) loop!    def normalize(%Activity{object: %Object{} = object}), do: object +  # A hack for fake activities +  def normalize(%Activity{data: %{"object" => %{"fake" => true} = data}}) do +    %Object{id: "pleroma:fake_object_id", data: data} +  end +    # Catch and log Object.normalize() calls where the Activity's child object is not    # preloaded.    def normalize(%Activity{data: %{"object" => %{"id" => ap_id}}}) do diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 6e1ed7ec9..f217e7bac 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -113,15 +113,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do    def decrease_replies_count_if_reply(_object), do: :noop -  def insert(map, local \\ true) when is_map(map) do +  def insert(map, local \\ true, fake \\ false) when is_map(map) do      with nil <- Activity.normalize(map), -         map <- lazy_put_activity_defaults(map), +         map <- lazy_put_activity_defaults(map, fake),           :ok <- check_actor_is_active(map["actor"]),           {_, true} <- {:remote_limit_error, check_remote_limit(map)},           {:ok, map} <- MRF.filter(map), +         {recipients, _, _} = get_recipients(map), +         {:fake, false, map, recipients} <- {:fake, fake, map, recipients},           {:ok, object} <- insert_full_object(map) do -      {recipients, _, _} = get_recipients(map) -        {:ok, activity} =          Repo.insert(%Activity{            data: map, @@ -146,8 +146,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do        stream_out(activity)        {:ok, activity}      else -      %Activity{} = activity -> {:ok, activity} -      error -> {:error, error} +      %Activity{} = activity -> +        {:ok, activity} + +      {:fake, true, map, recipients} -> +        activity = %Activity{ +          data: map, +          local: local, +          actor: map["actor"], +          recipients: recipients, +          id: "pleroma:fakeid" +        } + +        Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity) +        {:ok, activity} + +      error -> +        {:error, error}      end    end @@ -190,7 +205,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      end    end -  def create(%{to: to, actor: actor, context: context, object: object} = params) do +  def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do      additional = params[:additional] || %{}      # only accept false as false value      local = !(params[:local] == false) @@ -201,13 +216,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do               %{to: to, actor: actor, published: published, context: context, object: object},               additional             ), -         {:ok, activity} <- insert(create_data, local), +         {:ok, activity} <- insert(create_data, local, fake), +         {:fake, false, activity} <- {:fake, fake, activity},           _ <- increase_replies_count_if_reply(create_data),           # Changing note count prior to enqueuing federation task in order to avoid           # race conditions on updating user.info           {:ok, _actor} <- increase_note_count_if_public(actor, activity),           :ok <- maybe_federate(activity) do        {:ok, activity} +    else +      {:fake, true, activity} -> +        {:ok, activity}      end    end diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 77841278a..32545937e 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -175,18 +175,26 @@ defmodule Pleroma.Web.ActivityPub.Utils do    Adds an id and a published data if they aren't there,    also adds it to an included object    """ -  def lazy_put_activity_defaults(map) do -    %{data: %{"id" => context}, id: context_id} = create_context(map["context"]) - +  def lazy_put_activity_defaults(map, fake \\ false) do      map = -      map -      |> Map.put_new_lazy("id", &generate_activity_id/0) -      |> Map.put_new_lazy("published", &make_date/0) -      |> Map.put_new("context", context) -      |> Map.put_new("context_id", context_id) +      unless fake do +        %{data: %{"id" => context}, id: context_id} = create_context(map["context"]) + +        map +        |> Map.put_new_lazy("id", &generate_activity_id/0) +        |> Map.put_new_lazy("published", &make_date/0) +        |> Map.put_new("context", context) +        |> Map.put_new("context_id", context_id) +      else +        map +        |> Map.put_new("id", "pleroma:fakeid") +        |> Map.put_new_lazy("published", &make_date/0) +        |> Map.put_new("context", "pleroma:fakecontext") +        |> Map.put_new("context_id", -1) +      end      if is_map(map["object"]) do -      object = lazy_put_object_defaults(map["object"], map) +      object = lazy_put_object_defaults(map["object"], map, fake)        %{map | "object" => object}      else        map @@ -196,7 +204,18 @@ defmodule Pleroma.Web.ActivityPub.Utils do    @doc """    Adds an id and published date if they aren't there.    """ -  def lazy_put_object_defaults(map, activity \\ %{}) do +  def lazy_put_object_defaults(map, activity \\ %{}, fake) + +  def lazy_put_object_defaults(map, activity, true = _fake) do +    map +    |> Map.put_new_lazy("published", &make_date/0) +    |> Map.put_new("id", "pleroma:fake_object_id") +    |> Map.put_new("context", activity["context"]) +    |> Map.put_new("fake", true) +    |> Map.put_new("context_id", activity["context_id"]) +  end + +  def lazy_put_object_defaults(map, activity, _fake) do      map      |> Map.put_new_lazy("id", &generate_object_id/0)      |> Map.put_new_lazy("published", &make_date/0) @@ -404,13 +423,15 @@ defmodule Pleroma.Web.ActivityPub.Utils do              activity.data            ),          where: activity.actor == ^follower_id, +        # this is to use the index          where:            fragment( -            "? @> ?", +            "coalesce((?)->'object'->>'id', (?)->>'object') = ?",              activity.data, -            ^%{object: followed_id} +            activity.data, +            ^followed_id            ), -        order_by: [desc: :id], +        order_by: [fragment("? desc nulls last", activity.id)],          limit: 1        ) @@ -567,13 +588,15 @@ defmodule Pleroma.Web.ActivityPub.Utils do              activity.data            ),          where: activity.actor == ^blocker_id, +        # this is to use the index          where:            fragment( -            "? @> ?", +            "coalesce((?)->'object'->>'id', (?)->>'object') = ?", +            activity.data,              activity.data, -            ^%{object: blocked_id} +            ^blocked_id            ), -        order_by: [desc: :id], +        order_by: [fragment("? desc nulls last", activity.id)],          limit: 1        ) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 25b990677..745d1839b 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -172,13 +172,16 @@ defmodule Pleroma.Web.CommonAPI do               end)             ) do        res = -        ActivityPub.create(%{ -          to: to, -          actor: user, -          context: context, -          object: object, -          additional: %{"cc" => cc, "directMessage" => visibility == "direct"} -        }) +        ActivityPub.create( +          %{ +            to: to, +            actor: user, +            context: context, +            object: object, +            additional: %{"cc" => cc, "directMessage" => visibility == "direct"} +          }, +          Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false +        )        res      end diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 40cea3090..9cd8b3758 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -15,6 +15,8 @@ defmodule Pleroma.Web.CommonAPI.Utils do    alias Pleroma.Web.Endpoint    alias Pleroma.Web.MediaProxy +  require Logger +    # This is a hack for twidere.    def get_by_id_or_ap_id(id) do      activity = @@ -240,15 +242,21 @@ defmodule Pleroma.Web.CommonAPI.Utils do      Strftime.strftime!(date, "%a %b %d %H:%M:%S %z %Y")    end -  def date_to_asctime(date) do -    with {:ok, date, _offset} <- date |> DateTime.from_iso8601() do +  def date_to_asctime(date) when is_binary(date) do +    with {:ok, date, _offset} <- DateTime.from_iso8601(date) do        format_asctime(date)      else        _e -> +        Logger.warn("Date #{date} in wrong format, must be ISO 8601")          ""      end    end +  def date_to_asctime(date) do +    Logger.warn("Date #{date} in wrong format, must be ISO 8601") +    "" +  end +    def to_masto_date(%NaiveDateTime{} = date) do      date      |> NaiveDateTime.to_iso8601()  | 
