Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
authorlain <lain@soykaf.club>
Sun, 17 May 2020 10:14:49 +0000 (12:14 +0200)
committerlain <lain@soykaf.club>
Sun, 17 May 2020 10:14:49 +0000 (12:14 +0200)
1  2 
lib/pleroma/web/common_api/common_api.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/mastodon_api/views/notification_view.ex
lib/pleroma/web/router.ex
test/web/common_api/common_api_test.exs
test/web/mastodon_api/views/account_view_test.exs
test/web/mastodon_api/views/notification_view_test.exs

index 7008cea4425f1ef6d27470de94a6f4db9e57c420,7c94f16b61b5110139370a505a610db404847698..ce5dad4482ec1cf163b78c1c44b2672c8d9d63b2
@@@ -25,47 -24,8 +25,47 @@@ defmodule Pleroma.Web.CommonAPI d
    require Pleroma.Constants
    require Logger
  
 +  def post_chat_message(%User{} = user, %User{} = recipient, content, opts \\ []) do
 +    with maybe_attachment <- opts[:media_id] && Object.get_by_id(opts[:media_id]),
 +         :ok <- validate_chat_content_length(content, !!maybe_attachment),
 +         {_, {:ok, chat_message_data, _meta}} <-
 +           {:build_object,
 +            Builder.chat_message(
 +              user,
 +              recipient.ap_id,
 +              content |> format_chat_content,
 +              attachment: maybe_attachment
 +            )},
 +         {_, {:ok, create_activity_data, _meta}} <-
 +           {:build_create_activity, Builder.create(user, chat_message_data, [recipient.ap_id])},
 +         {_, {:ok, %Activity{} = activity, _meta}} <-
 +           {:common_pipeline,
 +            Pipeline.common_pipeline(create_activity_data,
 +              local: true
 +            )} do
 +      {:ok, activity}
 +    end
 +  end
 +
 +  defp format_chat_content(nil), do: nil
 +
 +  defp format_chat_content(content) do
 +    content |> Formatter.html_escape("text/plain")
 +  end
 +
 +  defp validate_chat_content_length(_, true), do: :ok
 +  defp validate_chat_content_length(nil, false), do: {:error, :no_content}
 +
 +  defp validate_chat_content_length(content, _) do
 +    if String.length(content) <= Pleroma.Config.get([:instance, :chat_limit]) do
 +      :ok
 +    else
 +      {:error, :content_too_long}
 +    end
 +  end
 +
    def unblock(blocker, blocked) do
-     with %Activity{} = block <- Utils.fetch_latest_block(blocker, blocked),
+     with {_, %Activity{} = block} <- {:fetch_block, Utils.fetch_latest_block(blocker, blocked)},
           {:ok, unblock_data, _} <- Builder.undo(blocker, block),
           {:ok, unblock, _} <- Pipeline.common_pipeline(unblock_data, local: true) do
        {:ok, unblock}
index 2a99518317007c6bd52cc1e5a6dedaf149a8b3b3,c46ddcf5578f456b30c18cdce99ff3a9b0baccd0..07d55a3e9c18fd6046e7a6824f0f031c7330e0b2
@@@ -83,26 -79,10 +81,24 @@@ defmodule Pleroma.Web.MastodonAPI.Notif
        end
      end
  
 -    mastodon_type = Activity.mastodon_notification_type(activity)
 +    # This returns the notification type by activity, but both chats and statuses
 +    # are in "Create" activities.
 +    mastodon_type =
 +      case Activity.mastodon_notification_type(activity) do
 +        "mention" ->
 +          object = Object.normalize(activity)
 +
 +          case object do
 +            %{data: %{"type" => "ChatMessage"}} -> "pleroma:chat_mention"
 +            _ -> "mention"
 +          end
 +
 +        type ->
 +          type
 +      end
  
-     render_opts = %{
-       relationships: opts[:relationships],
-       skip_relationships: opts[:skip_relationships]
-     }
+     # Note: :relationships contain user mutes (needed for :muted flag in :status)
+     status_render_opts = %{relationships: opts[:relationships]}
  
      with %{id: _} = account <-
             AccountView.render(
  
          "pleroma:emoji_reaction" ->
            response
-           |> put_status(parent_activity_fn.(), reading_user, render_opts)
+           |> put_status(parent_activity_fn.(), reading_user, status_render_opts)
            |> put_emoji(activity)
  
-           put_chat_message(response, activity, reading_user, render_opts)
 +        "pleroma:chat_mention" ->
++          put_chat_message(response, activity, reading_user, status_render_opts)
 +
          type when type in ["follow", "follow_request"] ->
            response
  
Simple merge
index 46ffd2888a198c8b7232407e18399db531608686,fd829901350e17eae2840dd12ee84456403bda48..35396988526b9d037490de1f8799b1c71617bb95
@@@ -24,80 -23,18 +24,92 @@@ defmodule Pleroma.Web.CommonAPITest d
    setup do: clear_config([:instance, :limit])
    setup do: clear_config([:instance, :max_pinned_statuses])
  
 +  describe "posting chat messages" do
 +    setup do: clear_config([:instance, :chat_limit])
 +
 +    test "it posts a chat message without content but with an attachment" do
 +      author = insert(:user)
 +      recipient = insert(:user)
 +
 +      file = %Plug.Upload{
 +        content_type: "image/jpg",
 +        path: Path.absname("test/fixtures/image.jpg"),
 +        filename: "an_image.jpg"
 +      }
 +
 +      {:ok, upload} = ActivityPub.upload(file, actor: author.ap_id)
 +
 +      {:ok, activity} =
 +        CommonAPI.post_chat_message(
 +          author,
 +          recipient,
 +          nil,
 +          media_id: upload.id
 +        )
 +
 +      assert activity
 +    end
 +
 +    test "it posts a chat message" do
 +      author = insert(:user)
 +      recipient = insert(:user)
 +
 +      {:ok, activity} =
 +        CommonAPI.post_chat_message(
 +          author,
 +          recipient,
 +          "a test message <script>alert('uuu')</script> :firefox:"
 +        )
 +
 +      assert activity.data["type"] == "Create"
 +      assert activity.local
 +      object = Object.normalize(activity)
 +
 +      assert object.data["type"] == "ChatMessage"
 +      assert object.data["to"] == [recipient.ap_id]
 +
 +      assert object.data["content"] ==
 +               "a test message &lt;script&gt;alert(&#39;uuu&#39;)&lt;/script&gt; :firefox:"
 +
 +      assert object.data["emoji"] == %{
 +               "firefox" => "http://localhost:4001/emoji/Firefox.gif"
 +             }
 +
 +      assert Chat.get(author.id, recipient.ap_id)
 +      assert Chat.get(recipient.id, author.ap_id)
 +
 +      assert :ok == Pleroma.Web.Federator.perform(:publish, activity)
 +    end
 +
 +    test "it reject messages over the local limit" do
 +      Pleroma.Config.put([:instance, :chat_limit], 2)
 +
 +      author = insert(:user)
 +      recipient = insert(:user)
 +
 +      {:error, message} =
 +        CommonAPI.post_chat_message(
 +          author,
 +          recipient,
 +          "123"
 +        )
 +
 +      assert message == :content_too_long
 +    end
 +  end
 +
+   describe "unblocking" do
+     test "it works even without an existing block activity" do
+       blocked = insert(:user)
+       blocker = insert(:user)
+       User.block(blocker, blocked)
+       assert User.blocks?(blocker, blocked)
+       assert {:ok, :no_activity} == CommonAPI.unblock(blocker, blocked)
+       refute User.blocks?(blocker, blocked)
+     end
+   end
    describe "deletion" do
      test "it works with pruned objects" do
        user = insert(:user)