diff options
| author | Francis Dinh <normandy@firemail.cc> | 2018-04-25 00:46:28 -0400 | 
|---|---|---|
| committer | Francis Dinh <normandy@firemail.cc> | 2018-04-25 00:46:28 -0400 | 
| commit | 9e0dd21ed637ec7b884570b2ad8705b5fac72580 (patch) | |
| tree | 6efae2883556ee499dcefb0083412701b501f3e8 /lib | |
| parent | 0df1a4efc841af4a12f45c1551f372d867ff7e1d (diff) | |
| parent | d4a54a90c9ae39947c71876d4d2988a05a02d7a1 (diff) | |
| download | pleroma-9e0dd21ed637ec7b884570b2ad8705b5fac72580.tar.gz pleroma-9e0dd21ed637ec7b884570b2ad8705b5fac72580.zip  | |
Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into feature/unrepeats
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/pleroma/formatter.ex | 10 | ||||
| -rw-r--r-- | lib/pleroma/user.ex | 10 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 12 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/mrf/drop_policy.ex | 8 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/mrf/noop_policy.ex | 5 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/mrf/simple_policy.ex | 75 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/transmogrifier.ex | 9 | ||||
| -rw-r--r-- | lib/pleroma/web/activity_pub/utils.ex | 3 | ||||
| -rw-r--r-- | lib/pleroma/web/common_api/common_api.ex | 1 | ||||
| -rw-r--r-- | lib/pleroma/web/endpoint.ex | 2 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 2 | ||||
| -rw-r--r-- | lib/pleroma/web/oauth/oauth_controller.ex | 3 | ||||
| -rw-r--r-- | lib/pleroma/web/ostatus/activity_representer.ex | 9 | ||||
| -rw-r--r-- | lib/pleroma/web/ostatus/handlers/note_handler.ex | 22 | ||||
| -rw-r--r-- | lib/pleroma/web/web_finger/web_finger.ex | 5 | 
15 files changed, 146 insertions, 30 deletions
diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 515909af1..456416fbd 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -144,7 +144,7 @@ defmodule Pleroma.Formatter do      @emoji    end -  @link_regex ~r/https?:\/\/[\w\.\/?=\-#\+%&@~\(\):]+[\w\/]/u +  @link_regex ~r/https?:\/\/[\w\.\/?=\-#\+%&@~'\(\):]+[\w\/]/u    def html_escape(text) do      Regex.split(@link_regex, text, include_captures: true) @@ -168,7 +168,13 @@ defmodule Pleroma.Formatter do      subs =        subs ++          Enum.map(links, fn {uuid, url} -> -          {uuid, "<a href='#{url}'>#{url}</a>"} +          {:safe, link} = Phoenix.HTML.Link.link(url, to: url) + +          link = +            link +            |> IO.iodata_to_binary() + +          {uuid, link}          end)      {subs, uuid_text} diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index c77fd6816..e9196ae03 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -322,6 +322,16 @@ defmodule Pleroma.User do      update_and_set_cache(cs)    end +  def decrease_note_count(%User{} = user) do +    note_count = user.info["note_count"] || 0 +    note_count = if note_count <= 0, do: 0, else: note_count - 1 +    new_info = Map.put(user.info, "note_count", note_count) + +    cs = info_changeset(user, %{info: new_info}) + +    update_and_set_cache(cs) +  end +    def update_note_count(%User{} = user) do      note_count_query =        from( diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index ace230804..ec161074d 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -10,6 +10,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do    @httpoison Application.get_env(:pleroma, :httpoison) +  @instance Application.get_env(:pleroma, :instance) +  @rewrite_policy Keyword.get(@instance, :rewrite_policy) +    def get_recipients(data) do      (data["to"] || []) ++ (data["cc"] || [])    end @@ -17,7 +20,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do    def insert(map, local \\ true) when is_map(map) do      with nil <- Activity.get_by_ap_id(map["id"]),           map <- lazy_put_activity_defaults(map), -         :ok <- insert_full_object(map) do +         :ok <- insert_full_object(map), +         {:ok, map} <- @rewrite_policy.filter(map) do        {:ok, activity} =          Repo.insert(%Activity{            data: map, @@ -61,7 +65,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do               additional             ),           {:ok, activity} <- insert(create_data, local), -         :ok <- maybe_federate(activity) do +         :ok <- maybe_federate(activity), +         {:ok, actor} <- User.increase_note_count(actor) do        {:ok, activity}      end    end @@ -184,7 +189,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do      with Repo.delete(object),           Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),           {:ok, activity} <- insert(data, local), -         :ok <- maybe_federate(activity) do +         :ok <- maybe_federate(activity), +         {:ok, actor} <- User.decrease_note_count(user) do        {:ok, activity}      end    end diff --git a/lib/pleroma/web/activity_pub/mrf/drop_policy.ex b/lib/pleroma/web/activity_pub/mrf/drop_policy.ex new file mode 100644 index 000000000..4333bca28 --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/drop_policy.ex @@ -0,0 +1,8 @@ +defmodule Pleroma.Web.ActivityPub.MRF.DropPolicy do +  require Logger + +  def filter(object) do +    Logger.info("REJECTING #{inspect(object)}") +    {:reject, object} +  end +end diff --git a/lib/pleroma/web/activity_pub/mrf/noop_policy.ex b/lib/pleroma/web/activity_pub/mrf/noop_policy.ex new file mode 100644 index 000000000..9dd3acb04 --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/noop_policy.ex @@ -0,0 +1,5 @@ +defmodule Pleroma.Web.ActivityPub.MRF.NoOpPolicy do +  def filter(object) do +    {:ok, object} +  end +end diff --git a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex new file mode 100644 index 000000000..4dfb0c867 --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex @@ -0,0 +1,75 @@ +defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do +  alias Pleroma.User + +  @mrf_policy Application.get_env(:pleroma, :mrf_simple) + +  @reject Keyword.get(@mrf_policy, :reject) +  defp check_reject(actor_info, object) do +    if actor_info.host in @reject do +      {:reject, nil} +    else +      {:ok, object} +    end +  end + +  @media_removal Keyword.get(@mrf_policy, :media_removal) +  defp check_media_removal(actor_info, object) do +    if actor_info.host in @media_removal do +      object = Map.delete(object, "attachments") +    end + +    {:ok, object} +  end + +  @media_nsfw Keyword.get(@mrf_policy, :media_nsfw) +  defp check_media_nsfw(actor_info, object) do +    child_object = object["object"] + +    if actor_info.host in @media_nsfw and child_object["attachment"] != nil and +         length(child_object["attachment"]) > 0 do +      tags = (child_object["tag"] || []) ++ ["nsfw"] +      child_object = Map.put(child_object, "tags", tags) +      child_object = Map.put(child_object, "sensitive", true) +      object = Map.put(object, "object", child_object) +    end + +    {:ok, object} +  end + +  @ftl_removal Keyword.get(@mrf_policy, :federated_timeline_removal) +  defp check_ftl_removal(actor_info, object) do +    if actor_info.host in @ftl_removal do +      user = User.get_by_ap_id(object["actor"]) + +      # flip to/cc relationship to make the post unlisted +      if "https://www.w3.org/ns/activitystreams#Public" in object["to"] and +           user.follower_address in object["cc"] do +        to = +          List.delete(object["to"], "https://www.w3.org/ns/activitystreams#Public") ++ +            [user.follower_address] + +        cc = +          List.delete(object["cc"], user.follower_address) ++ +            ["https://www.w3.org/ns/activitystreams#Public"] + +        object = Map.put(object, "to", to) +        object = Map.put(object, "cc", cc) +      end +    end + +    {:ok, object} +  end + +  def filter(object) do +    actor_info = URI.parse(object["actor"]) + +    with {:ok, object} <- check_reject(actor_info, object), +         {:ok, object} <- check_media_removal(actor_info, object), +         {:ok, object} <- check_media_nsfw(actor_info, object), +         {:ok, object} <- check_ftl_removal(actor_info, object) do +      {:ok, object} +    else +      e -> {:reject, nil} +    end +  end +end diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 00b9f74ff..2871a2544 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -146,7 +146,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do          %{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data        ) do      with %User{} = actor <- User.get_or_fetch_by_ap_id(actor), -         {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id), +         {:ok, object} <- +           get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),           {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do        {:ok, activity}      else @@ -158,7 +159,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do          %{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data        ) do      with %User{} = actor <- User.get_or_fetch_by_ap_id(actor), -         {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id), +         {:ok, object} <- +           get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),           {:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do        {:ok, activity}      else @@ -209,7 +211,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do        end      with %User{} = actor <- User.get_or_fetch_by_ap_id(actor), -         {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id), +         {:ok, object} <- +           get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),           {:ok, activity} <- ActivityPub.delete(object, false) do        {:ok, activity}      else diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 9e2fa1fb2..c9d0e45b2 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -175,7 +175,8 @@ defmodule Pleroma.Web.ActivityPub.Utils do    def update_element_in_object(property, element, object) do      with new_data <- -           object.data |> Map.put("#{property}_count", length(element)) +           object.data +           |> Map.put("#{property}_count", length(element))             |> Map.put("#{property}s", element),           changeset <- Changeset.change(object, data: new_data),           {:ok, object} <- Repo.update(changeset), diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 8889b9b42..ef1170010 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -113,7 +113,6 @@ defmodule Pleroma.Web.CommonAPI do            additional: %{"cc" => cc}          }) -      User.increase_note_count(user)        res      end    end diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index dfafc95f4..1a012c1b4 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -17,7 +17,7 @@ defmodule Pleroma.Web.Endpoint do      Plug.Static,      at: "/",      from: :pleroma, -    only: ~w(index.html static finmoji emoji packs sounds images instance sw.js) +    only: ~w(index.html static finmoji emoji packs sounds images instance sw.js favicon.png)    )    # Code reloading can be explicitly enabled under the diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index a49be0588..d506c4a41 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -528,7 +528,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do              ^query            ),          limit: 20, -        order_by: [desc: :inserted_at] +        order_by: [desc: :id]        )      statuses = Repo.all(q) ++ fetched diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex index 6297b7bae..11dc1806f 100644 --- a/lib/pleroma/web/oauth/oauth_controller.ex +++ b/lib/pleroma/web/oauth/oauth_controller.ex @@ -63,7 +63,8 @@ defmodule Pleroma.Web.OAuth.OAuthController do               client_secret: params["client_secret"]             ),           fixed_token = fix_padding(params["code"]), -         %Authorization{} = auth <- Repo.get_by(Authorization, token: fixed_token, app_id: app.id), +         %Authorization{} = auth <- +           Repo.get_by(Authorization, token: fixed_token, app_id: app.id),           {:ok, token} <- Token.exchange_token(app, auth) do        response = %{          token_type: "Bearer", diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index 2f28c456e..921a89bd0 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -131,7 +131,8 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do         h.(activity.data["object"]["content"] |> String.replace(~r/[\n\r]/, ""))},        {:published, h.(inserted_at)},        {:updated, h.(updated_at)}, -      {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])}, +      {:"ostatus:conversation", [ref: h.(activity.data["context"])], +       h.(activity.data["context"])},        {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}      ] ++        summary ++ @@ -162,7 +163,8 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do           # For notes, federate the object id.           {:id, h.(activity.data["object"])}         ]}, -      {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])}, +      {:"ostatus:conversation", [ref: h.(activity.data["context"])], +       h.(activity.data["context"])},        {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},        {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},        {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []} @@ -193,7 +195,8 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do        {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},        {:published, h.(inserted_at)},        {:updated, h.(updated_at)}, -      {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])}, +      {:"ostatus:conversation", [ref: h.(activity.data["context"])], +       h.(activity.data["context"])},        {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},        {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},        {:"activity:object", retweeted_xml} diff --git a/lib/pleroma/web/ostatus/handlers/note_handler.ex b/lib/pleroma/web/ostatus/handlers/note_handler.ex index b012abd51..bd6e92238 100644 --- a/lib/pleroma/web/ostatus/handlers/note_handler.ex +++ b/lib/pleroma/web/ostatus/handlers/note_handler.ex @@ -138,19 +138,15 @@ defmodule Pleroma.Web.OStatus.NoteHandler do               do: note |> Map.put("inReplyTo", inReplyTo),               else: note             ) do -      res = -        ActivityPub.create(%{ -          to: to, -          actor: actor, -          context: context, -          object: note, -          published: date, -          local: false, -          additional: %{"cc" => cc} -        }) - -      User.increase_note_count(actor) -      res +      ActivityPub.create(%{ +        to: to, +        actor: actor, +        context: context, +        object: note, +        published: date, +        local: false, +        additional: %{"cc" => cc} +      })      else        %Activity{} = activity -> {:ok, activity}        e -> {:error, e} diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index e45c0ed8d..dc9ad2014 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -81,7 +81,10 @@ defmodule Pleroma.Web.WebFinger do            "href" => user.ap_id          },          %{"rel" => "salmon", "href" => OStatus.salmon_path(user)}, -        %{"rel" => "magic-public-key", "href" => "data:application/magic-public-key,#{magic_key}"}, +        %{ +          "rel" => "magic-public-key", +          "href" => "data:application/magic-public-key,#{magic_key}" +        },          %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id},          %{            "rel" => "http://ostatus.org/schema/1.0/subscribe",  | 
