Merge remote-tracking branch 'origin/develop' into feature/local-only-scope
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 29 Oct 2020 10:51:23 +0000 (14:51 +0400)
committerEgor Kislitsyn <egor@kislitsyn.com>
Thu, 29 Oct 2020 10:51:23 +0000 (14:51 +0400)
18 files changed:
CHANGELOG.md
docs/API/differences_in_mastoapi_responses.md
lib/pleroma/activity.ex
lib/pleroma/constants.ex
lib/pleroma/web/activity_pub/builder.ex
lib/pleroma/web/activity_pub/object_validators/announce_validator.ex
lib/pleroma/web/activity_pub/pipeline.ex
lib/pleroma/web/activity_pub/utils.ex
lib/pleroma/web/activity_pub/visibility.ex
lib/pleroma/web/api_spec/operations/status_operation.ex
lib/pleroma/web/common_api.ex
lib/pleroma/web/common_api/activity_draft.ex
lib/pleroma/web/common_api/utils.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
test/pleroma/web/common_api/utils_test.exs
test/pleroma/web/common_api_test.exs
test/pleroma/web/mastodon_api/controllers/status_controller_test.exs
test/pleroma/web/mastodon_api/views/status_view_test.exs

index 03fe51f9db97bfde77e9e2a7774133bc295f0425..d2f536da8379d86d1400e74555e649a6d75efb87 100644 (file)
@@ -12,9 +12,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Media preview proxy (requires `ffmpeg` and `ImageMagick` to be installed and media proxy to be enabled; see `:media_preview_proxy` config for more details).
 - Pleroma API: Importing the mutes users from CSV files.
 - Experimental websocket-based federation between Pleroma instances.
+- Support for local-only statuses
 - App metrics: ability to restrict access to specified IP whitelist.
 - Configuration: Add `:instance, autofollowing_nicknames` setting to provide a way to make accounts automatically follow new users that register on the local Pleroma instance.
 
+
 ### Changed
 
 - **Breaking** Requires `libmagic` (or `file`) to guess file types.
index 38865dc68a8b4393a7c40d23eceacebf1a7bf434..1e932d908bb4be89e72291602f485f4c8e74c039 100644 (file)
@@ -28,6 +28,7 @@ Has these additional fields under the `pleroma` object:
 - `thread_muted`: true if the thread the post belongs to is muted
 - `emoji_reactions`: A list with emoji / reaction maps. The format is `{name: "☕", count: 1, me: true}`. Contains no information about the reacting users, for that use the `/statuses/:id/reactions` endpoint.
 - `parent_visible`: If the parent of this post is visible to the user or not.
+- `local_only`: true for local-only, non-federated posts.
 
 ## Media Attachments
 
@@ -154,6 +155,7 @@ Additional parameters can be added to the JSON body/Form data:
 - `visibility`: string, besides standard MastoAPI values (`direct`, `private`, `unlisted` or `public`) it can be used to address a List by setting it to `list:LIST_ID`.
 - `expires_in`: The number of seconds the posted activity should expire in. When a posted activity expires it will be deleted from the server, and a delete request for it will be federated. This needs to be longer than an hour.
 - `in_reply_to_conversation_id`: Will reply to a given conversation, addressing only the people who are part of the recipient set of that conversation. Sets the visibility to `direct`.
+- `local_only`: boolean, if set to `true` the post won't be federated.
 
 ## GET `/api/v1/statuses`
 
index 17af042573f22ccad27cbf29d4d87e7d149f9541..3b01f5e316a0b83d55b99ceba9d563aac60c8e41 100644 (file)
@@ -18,6 +18,8 @@ defmodule Pleroma.Activity do
   import Ecto.Changeset
   import Ecto.Query
 
+  require Pleroma.Constants
+
   @type t :: %__MODULE__{}
   @type actor :: String.t()
 
@@ -343,4 +345,12 @@ defmodule Pleroma.Activity do
     actor = user_actor(activity)
     activity.id in actor.pinned_activities
   end
+
+  def local_only?(activity) do
+    recipients = Enum.concat(activity.data["to"], Map.get(activity.data, "cc", []))
+    public = Pleroma.Constants.as_public()
+    local = Pleroma.Constants.as_local_public()
+
+    Enum.member?(recipients, local) and not Enum.member?(recipients, public)
+  end
 end
index 13eeaa96b4762fbb91ced797cd93c7ad2dcf9255..cf8182d55a2d11486ca99716edb48b151f3b6dab 100644 (file)
@@ -26,4 +26,6 @@ defmodule Pleroma.Constants do
     do:
       ~w(index.html robots.txt static static-fe finmoji emoji packs sounds images instance sw.js sw-pleroma.js favicon.png schemas doc embed.js embed.css)
   )
+
+  def as_local_public, do: Pleroma.Web.base_url() <> "/#Public"
 end
index 298aff6b7aac17ac04b16417000137bf12d6e1d5..c9200a3f0c880603eef78766d37472ef7b53a909 100644 (file)
@@ -222,6 +222,9 @@ defmodule Pleroma.Web.ActivityPub.Builder do
         actor.ap_id == Relay.ap_id() ->
           [actor.follower_address]
 
+        public? and Pleroma.Activity.local_only?(object) ->
+          [actor.follower_address, object.data["actor"], Pleroma.Constants.as_local_public()]
+
         public? ->
           [actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
 
index 6f757f49ce008cecede0a29646f1369c6855eb52..338957db8a10d4748284bd8665e92b86ce0d5450 100644 (file)
@@ -67,7 +67,12 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator do
          %Object{} = object <- Object.get_cached_by_ap_id(object),
          false <- Visibility.is_public?(object) do
       same_actor = object.data["actor"] == actor.ap_id
-      is_public = Pleroma.Constants.as_public() in (get_field(cng, :to) ++ get_field(cng, :cc))
+      recipients = get_field(cng, :to) ++ get_field(cng, :cc)
+      local_public = Pleroma.Constants.as_local_public()
+
+      is_public =
+        Enum.member?(recipients, Pleroma.Constants.as_public()) or
+          Enum.member?(recipients, local_public)
 
       cond do
         same_actor && is_public ->
index 2db86f116cde6ad9f540e681ff9552dd4e5fcb50..559c8387e6671938992a88927b21a0db2ca2b8fe 100644 (file)
@@ -55,7 +55,7 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do
     with {:ok, local} <- Keyword.fetch(meta, :local) do
       do_not_federate = meta[:do_not_federate] || !Config.get([:instance, :federating])
 
-      if !do_not_federate && local do
+      if !do_not_federate and local and not Activity.local_only?(activity) do
         activity =
           if object = Keyword.get(meta, :object_data) do
             %{activity | data: Map.put(activity.data, "object", object)}
index 713b0ca1f3ea1a3bcf14accd02dcf928f7373fa9..faf3bea00c86a892ea93aee17b781b651899760e 100644 (file)
@@ -175,7 +175,8 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
 
     with true <- Config.get!([:instance, :federating]),
-         true <- type != "Block" || outgoing_blocks do
+         true <- type != "Block" || outgoing_blocks,
+         false <- Activity.local_only?(activity) do
       Pleroma.Web.Federator.publish(activity)
     end
 
index 76bd54a427b552e4b87b4ec94e9f33cd8aa6267c..b3b23a38baa4df987a67204966a67dc4ff545407 100644 (file)
@@ -17,7 +17,11 @@ defmodule Pleroma.Web.ActivityPub.Visibility do
   def is_public?(%Activity{data: %{"type" => "Move"}}), do: true
   def is_public?(%Activity{data: data}), do: is_public?(data)
   def is_public?(%{"directMessage" => true}), do: false
-  def is_public?(data), do: Utils.label_in_message?(Pleroma.Constants.as_public(), data)
+
+  def is_public?(data) do
+    Utils.label_in_message?(Pleroma.Constants.as_public(), data) or
+      Utils.label_in_message?(Pleroma.Constants.as_local_public(), data)
+  end
 
   def is_private?(activity) do
     with false <- is_public?(activity),
index d7ebde6f6f5c0050e73f8f4c5c3e5433c3e1b345..e989e4f5f9ab350232b8aa8342c4eff70c8a1082 100644 (file)
@@ -475,6 +475,10 @@ defmodule Pleroma.Web.ApiSpec.StatusOperation do
           type: :string,
           description:
             "Will reply to a given conversation, addressing only the people who are part of the recipient set of that conversation. Sets the visibility to `direct`."
+        },
+        local_only: %Schema{
+          type: :boolean,
+          description: "Post the status as local only"
         }
       },
       example: %{
index 60a50b027f5c5981adc3406c0b086bdcff49e039..e5c66eea36359cd4c2fbb806c904404dccbfcc48 100644 (file)
@@ -15,6 +15,7 @@ defmodule Pleroma.Web.CommonAPI do
   alias Pleroma.Web.ActivityPub.Pipeline
   alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.Web.ActivityPub.Visibility
+  alias Pleroma.Web.CommonAPI.ActivityDraft
 
   import Pleroma.Web.Gettext
   import Pleroma.Web.CommonAPI.Utils
@@ -398,31 +399,13 @@ defmodule Pleroma.Web.CommonAPI do
   end
 
   def listen(user, data) do
-    visibility = Map.get(data, :visibility, "public")
-
-    with {to, cc} <- get_to_and_cc(user, [], nil, visibility, nil),
-         listen_data <-
-           data
-           |> Map.take([:album, :artist, :title, :length])
-           |> Map.new(fn {key, value} -> {to_string(key), value} end)
-           |> Map.put("type", "Audio")
-           |> Map.put("to", to)
-           |> Map.put("cc", cc)
-           |> Map.put("actor", user.ap_id),
-         {:ok, activity} <-
-           ActivityPub.listen(%{
-             actor: user,
-             to: to,
-             object: listen_data,
-             context: Utils.generate_context_id(),
-             additional: %{"cc" => cc}
-           }) do
-      {:ok, activity}
+    with {:ok, draft} <- ActivityDraft.listen(user, data) do
+      ActivityPub.listen(draft.changes)
     end
   end
 
   def post(user, %{status: _} = data) do
-    with {:ok, draft} <- Pleroma.Web.CommonAPI.ActivityDraft.create(user, data) do
+    with {:ok, draft} <- ActivityDraft.create(user, data) do
       ActivityPub.create(draft.changes, draft.preview?)
     end
   end
index 548f76609582c1a1b0695e276e2dd7dd4c02fef9..aa2616d9eb9dfae8dfdaff907e8cedf36d14176e 100644 (file)
@@ -22,7 +22,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
             in_reply_to_conversation: nil,
             visibility: nil,
             expires_at: nil,
-            poll: nil,
+            extra: nil,
             emoji: %{},
             content_html: nil,
             mentions: [],
@@ -35,9 +35,14 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
             preview?: false,
             changes: %{}
 
-  def create(user, params) do
+  def new(user, params) do
     %__MODULE__{user: user}
     |> put_params(params)
+  end
+
+  def create(user, params) do
+    user
+    |> new(params)
     |> status()
     |> summary()
     |> with_valid(&attachments/1)
@@ -57,6 +62,30 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     |> validate()
   end
 
+  def listen(user, params) do
+    user
+    |> new(params)
+    |> visibility()
+    |> to_and_cc()
+    |> context()
+    |> listen_object()
+    |> with_valid(&changes/1)
+    |> validate()
+  end
+
+  defp listen_object(draft) do
+    object =
+      draft.params
+      |> Map.take([:album, :artist, :title, :length])
+      |> Map.new(fn {key, value} -> {to_string(key), value} end)
+      |> Map.put("type", "Audio")
+      |> Map.put("to", draft.to)
+      |> Map.put("cc", draft.cc)
+      |> Map.put("actor", draft.user.ap_id)
+
+    %__MODULE__{draft | object: object}
+  end
+
   defp put_params(draft, params) do
     params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
     %__MODULE__{draft | params: params}
@@ -121,7 +150,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   defp poll(draft) do
     case Utils.make_poll_data(draft.params) do
       {:ok, {poll, poll_emoji}} ->
-        %__MODULE__{draft | poll: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
+        %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
 
       {:error, message} ->
         add_error(draft, message)
@@ -129,32 +158,18 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   end
 
   defp content(draft) do
-    {content_html, mentions, tags} =
-      Utils.make_content_html(
-        draft.status,
-        draft.attachments,
-        draft.params,
-        draft.visibility
-      )
-
-    %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
-  end
+    {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
 
-  defp to_and_cc(draft) do
-    addressed_users =
-      draft.mentions
+    mentions =
+      mentioned_users
       |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
       |> Utils.get_addressed_users(draft.params[:to])
 
-    {to, cc} =
-      Utils.get_to_and_cc(
-        draft.user,
-        addressed_users,
-        draft.in_reply_to,
-        draft.visibility,
-        draft.in_reply_to_conversation
-      )
+    %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
+  end
 
+  defp to_and_cc(draft) do
+    {to, cc} = Utils.get_to_and_cc(draft)
     %__MODULE__{draft | to: to, cc: cc}
   end
 
@@ -172,19 +187,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
 
     object =
-      Utils.make_note_data(
-        draft.user.ap_id,
-        draft.to,
-        draft.context,
-        draft.content_html,
-        draft.attachments,
-        draft.in_reply_to,
-        draft.tags,
-        draft.summary,
-        draft.cc,
-        draft.sensitive,
-        draft.poll
-      )
+      Utils.make_note_data(draft)
       |> Map.put("emoji", emoji)
       |> Map.put("source", draft.status)
 
index 3b71adf0e577b1fdabaa50c5b7917968c7f02681..abf6c40d5d4af93c6eaa4234b82fe087890722d2 100644 (file)
@@ -16,6 +16,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
   alias Pleroma.User
   alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.Web.ActivityPub.Visibility
+  alias Pleroma.Web.CommonAPI.ActivityDraft
   alias Pleroma.Web.MediaProxy
   alias Pleroma.Web.Plugs.AuthenticationPlug
 
@@ -50,67 +51,60 @@ defmodule Pleroma.Web.CommonAPI.Utils do
     {_, descs} = Jason.decode(descs_str)
 
     Enum.map(ids, fn media_id ->
-      case Repo.get(Object, media_id) do
-        %Object{data: data} ->
-          Map.put(data, "name", descs[media_id])
-
-        _ ->
-          nil
+      with %Object{data: data} <- Repo.get(Object, media_id) do
+        Map.put(data, "name", descs[media_id])
       end
     end)
     |> Enum.reject(&is_nil/1)
   end
 
-  @spec get_to_and_cc(
-          User.t(),
-          list(String.t()),
-          Activity.t() | nil,
-          String.t(),
-          Participation.t() | nil
-        ) :: {list(String.t()), list(String.t())}
+  @spec get_to_and_cc(ActivityDraft.t()) :: {list(String.t()), list(String.t())}
 
-  def get_to_and_cc(_, _, _, _, %Participation{} = participation) do
+  def get_to_and_cc(%{in_reply_to_conversation: %Participation{} = participation}) do
     participation = Repo.preload(participation, :recipients)
     {Enum.map(participation.recipients, & &1.ap_id), []}
   end
 
-  def get_to_and_cc(user, mentioned_users, inReplyTo, "public", _) do
-    to = [Pleroma.Constants.as_public() | mentioned_users]
-    cc = [user.follower_address]
+  def get_to_and_cc(%{visibility: "public"} = draft) do
+    to = [public_uri(draft) | draft.mentions]
+    cc = [draft.user.follower_address]
 
-    if inReplyTo do
-      {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
+    if draft.in_reply_to do
+      {Enum.uniq([draft.in_reply_to.data["actor"] | to]), cc}
     else
       {to, cc}
     end
   end
 
-  def get_to_and_cc(user, mentioned_users, inReplyTo, "unlisted", _) do
-    to = [user.follower_address | mentioned_users]
-    cc = [Pleroma.Constants.as_public()]
+  def get_to_and_cc(%{visibility: "unlisted"} = draft) do
+    to = [draft.user.follower_address | draft.mentions]
+    cc = [public_uri(draft)]
 
-    if inReplyTo do
-      {Enum.uniq([inReplyTo.data["actor"] | to]), cc}
+    if draft.in_reply_to do
+      {Enum.uniq([draft.in_reply_to.data["actor"] | to]), cc}
     else
       {to, cc}
     end
   end
 
-  def get_to_and_cc(user, mentioned_users, inReplyTo, "private", _) do
-    {to, cc} = get_to_and_cc(user, mentioned_users, inReplyTo, "direct", nil)
-    {[user.follower_address | to], cc}
+  def get_to_and_cc(%{visibility: "private"} = draft) do
+    {to, cc} = get_to_and_cc(struct(draft, visibility: "direct"))
+    {[draft.user.follower_address | to], cc}
   end
 
-  def get_to_and_cc(_user, mentioned_users, inReplyTo, "direct", _) do
+  def get_to_and_cc(%{visibility: "direct"} = draft) do
     # If the OP is a DM already, add the implicit actor.
-    if inReplyTo && Visibility.is_direct?(inReplyTo) do
-      {Enum.uniq([inReplyTo.data["actor"] | mentioned_users]), []}
+    if draft.in_reply_to && Visibility.is_direct?(draft.in_reply_to) do
+      {Enum.uniq([draft.in_reply_to.data["actor"] | draft.mentions]), []}
     else
-      {mentioned_users, []}
+      {draft.mentions, []}
     end
   end
 
-  def get_to_and_cc(_user, mentions, _inReplyTo, {:list, _}, _), do: {mentions, []}
+  def get_to_and_cc(%{visibility: {:list, _}, mentions: mentions}), do: {mentions, []}
+
+  defp public_uri(%{params: %{local_only: true}}), do: Pleroma.Constants.as_local_public()
+  defp public_uri(_), do: Pleroma.Constants.as_public()
 
   def get_addressed_users(_, to) when is_list(to) do
     User.get_ap_ids_by_nicknames(to)
@@ -203,30 +197,25 @@ defmodule Pleroma.Web.CommonAPI.Utils do
     end
   end
 
-  def make_content_html(
-        status,
-        attachments,
-        data,
-        visibility
-      ) do
+  def make_content_html(%ActivityDraft{} = draft) do
     attachment_links =
-      data
+      draft.params
       |> Map.get("attachment_links", Config.get([:instance, :attachment_links]))
       |> truthy_param?()
 
-    content_type = get_content_type(data[:content_type])
+    content_type = get_content_type(draft.params[:content_type])
 
     options =
-      if visibility == "direct" && Config.get([:instance, :safe_dm_mentions]) do
+      if draft.visibility == "direct" && Config.get([:instance, :safe_dm_mentions]) do
         [safe_mention: true]
       else
         []
       end
 
-    status
+    draft.status
     |> format_input(content_type, options)
-    |> maybe_add_attachments(attachments, attachment_links)
-    |> maybe_add_nsfw_tag(data)
+    |> maybe_add_attachments(draft.attachments, attachment_links)
+    |> maybe_add_nsfw_tag(draft.params)
   end
 
   defp get_content_type(content_type) do
@@ -308,33 +297,21 @@ defmodule Pleroma.Web.CommonAPI.Utils do
     |> Formatter.html_escape("text/html")
   end
 
-  def make_note_data(
-        actor,
-        to,
-        context,
-        content_html,
-        attachments,
-        in_reply_to,
-        tags,
-        summary \\ nil,
-        cc \\ [],
-        sensitive \\ false,
-        extra_params \\ %{}
-      ) do
+  def make_note_data(%ActivityDraft{} = draft) do
     %{
       "type" => "Note",
-      "to" => to,
-      "cc" => cc,
-      "content" => content_html,
-      "summary" => summary,
-      "sensitive" => truthy_param?(sensitive),
-      "context" => context,
-      "attachment" => attachments,
-      "actor" => actor,
-      "tag" => Keyword.values(tags) |> Enum.uniq()
+      "to" => draft.to,
+      "cc" => draft.cc,
+      "content" => draft.content_html,
+      "summary" => draft.summary,
+      "sensitive" => draft.sensitive,
+      "context" => draft.context,
+      "attachment" => draft.attachments,
+      "actor" => draft.user.ap_id,
+      "tag" => Keyword.values(draft.tags) |> Enum.uniq()
     }
-    |> add_in_reply_to(in_reply_to)
-    |> Map.merge(extra_params)
+    |> add_in_reply_to(draft.in_reply_to)
+    |> Map.merge(draft.extra)
   end
 
   defp add_in_reply_to(object, nil), do: object
index 435bcde157552b063deb98a72829c3527e6ea60f..0fc78972e055b37b6cbc47a18683180c8a0bb62d 100644 (file)
@@ -368,7 +368,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         direct_conversation_id: direct_conversation_id,
         thread_muted: thread_muted?,
         emoji_reactions: emoji_reactions,
-        parent_visible: visible_for_user?(reply_to, opts[:for])
+        parent_visible: visible_for_user?(reply_to, opts[:for]),
+        local_only: Activity.local_only?(activity)
       }
     }
   end
index e67c10b93645db18c4a2df1859330bb6f0b562b1..4d6c9ea2610786def850afffaecec5bef223ecf2 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
   alias Pleroma.Builders.UserBuilder
   alias Pleroma.Object
   alias Pleroma.Web.CommonAPI
+  alias Pleroma.Web.CommonAPI.ActivityDraft
   alias Pleroma.Web.CommonAPI.Utils
   use Pleroma.DataCase
 
@@ -235,9 +236,9 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
     test "for public posts, not a reply" do
       user = insert(:user)
       mentioned_user = insert(:user)
-      mentions = [mentioned_user.ap_id]
+      draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "public"}
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "public", nil)
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 2
       assert length(cc) == 1
@@ -252,9 +253,15 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
       mentioned_user = insert(:user)
       third_user = insert(:user)
       {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
-      mentions = [mentioned_user.ap_id]
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "public", nil)
+      draft = %ActivityDraft{
+        user: user,
+        mentions: [mentioned_user.ap_id],
+        visibility: "public",
+        in_reply_to: activity
+      }
+
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 3
       assert length(cc) == 1
@@ -268,9 +275,9 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
     test "for unlisted posts, not a reply" do
       user = insert(:user)
       mentioned_user = insert(:user)
-      mentions = [mentioned_user.ap_id]
+      draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "unlisted"}
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "unlisted", nil)
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 2
       assert length(cc) == 1
@@ -285,9 +292,15 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
       mentioned_user = insert(:user)
       third_user = insert(:user)
       {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
-      mentions = [mentioned_user.ap_id]
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "unlisted", nil)
+      draft = %ActivityDraft{
+        user: user,
+        mentions: [mentioned_user.ap_id],
+        visibility: "unlisted",
+        in_reply_to: activity
+      }
+
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 3
       assert length(cc) == 1
@@ -301,9 +314,9 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
     test "for private posts, not a reply" do
       user = insert(:user)
       mentioned_user = insert(:user)
-      mentions = [mentioned_user.ap_id]
+      draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "private"}
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "private", nil)
+      {to, cc} = Utils.get_to_and_cc(draft)
       assert length(to) == 2
       assert Enum.empty?(cc)
 
@@ -316,9 +329,15 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
       mentioned_user = insert(:user)
       third_user = insert(:user)
       {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
-      mentions = [mentioned_user.ap_id]
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "private", nil)
+      draft = %ActivityDraft{
+        user: user,
+        mentions: [mentioned_user.ap_id],
+        visibility: "private",
+        in_reply_to: activity
+      }
+
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 2
       assert Enum.empty?(cc)
@@ -330,9 +349,9 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
     test "for direct posts, not a reply" do
       user = insert(:user)
       mentioned_user = insert(:user)
-      mentions = [mentioned_user.ap_id]
+      draft = %ActivityDraft{user: user, mentions: [mentioned_user.ap_id], visibility: "direct"}
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, nil, "direct", nil)
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 1
       assert Enum.empty?(cc)
@@ -345,9 +364,15 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
       mentioned_user = insert(:user)
       third_user = insert(:user)
       {:ok, activity} = CommonAPI.post(third_user, %{status: "uguu"})
-      mentions = [mentioned_user.ap_id]
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, activity, "direct", nil)
+      draft = %ActivityDraft{
+        user: user,
+        mentions: [mentioned_user.ap_id],
+        visibility: "direct",
+        in_reply_to: activity
+      }
+
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 1
       assert Enum.empty?(cc)
@@ -356,7 +381,14 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
 
       {:ok, direct_activity} = CommonAPI.post(third_user, %{status: "uguu", visibility: "direct"})
 
-      {to, cc} = Utils.get_to_and_cc(user, mentions, direct_activity, "direct", nil)
+      draft = %ActivityDraft{
+        user: user,
+        mentions: [mentioned_user.ap_id],
+        visibility: "direct",
+        in_reply_to: direct_activity
+      }
+
+      {to, cc} = Utils.get_to_and_cc(draft)
 
       assert length(to) == 2
       assert Enum.empty?(cc)
@@ -532,26 +564,26 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do
     end
   end
 
-  describe "make_note_data/11" do
+  describe "make_note_data/1" do
     test "returns note data" do
       user = insert(:user)
       note = insert(:note)
       user2 = insert(:user)
       user3 = insert(:user)
 
-      assert Utils.make_note_data(
-               user.ap_id,
-               [user2.ap_id],
-               "2hu",
-               "<h1>This is :moominmamma: note</h1>",
-               [],
-               note.id,
-               [name: "jimm"],
-               "test summary",
-               [user3.ap_id],
-               false,
-               %{"custom_tag" => "test"}
-             ) == %{
+      draft = %ActivityDraft{
+        user: user,
+        to: [user2.ap_id],
+        context: "2hu",
+        content_html: "<h1>This is :moominmamma: note</h1>",
+        in_reply_to: note.id,
+        tags: [name: "jimm"],
+        summary: "test summary",
+        cc: [user3.ap_id],
+        extra: %{"custom_tag" => "test"}
+      }
+
+      assert Utils.make_note_data(draft) == %{
                "actor" => user.ap_id,
                "attachment" => [],
                "cc" => [user3.ap_id],
index c5b90ad84297b7d569eac29dd92069a6f9125328..e1dddd21aeae73bf4b8abddd3595f532fe785acd 100644 (file)
@@ -1255,4 +1255,128 @@ defmodule Pleroma.Web.CommonAPITest do
              } = CommonAPI.get_user("")
     end
   end
+
+  describe "with `local_only` enabled" do
+    setup do: clear_config([:instance, :federating], true)
+
+    test "post" do
+      user = insert(:user)
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        {:ok, activity} = CommonAPI.post(user, %{status: "#2hu #2HU", local_only: true})
+
+        assert Activity.local_only?(activity)
+        assert_not_called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "delete" do
+      user = insert(:user)
+
+      {:ok, %Activity{id: activity_id}} =
+        CommonAPI.post(user, %{status: "#2hu #2HU", local_only: true})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"deleted_activity_id" => ^activity_id}} = activity} =
+                 CommonAPI.delete(activity_id, user)
+
+        assert Activity.local_only?(activity)
+        assert_not_called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "repeat" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, %Activity{id: activity_id}} =
+        CommonAPI.post(other_user, %{status: "cofe", local_only: true})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Announce"}} = activity} =
+                 CommonAPI.repeat(activity_id, user)
+
+        assert Activity.local_only?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "unrepeat" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, %Activity{id: activity_id}} =
+        CommonAPI.post(other_user, %{status: "cofe", local_only: true})
+
+      assert {:ok, _} = CommonAPI.repeat(activity_id, user)
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Undo"}} = activity} =
+                 CommonAPI.unrepeat(activity_id, user)
+
+        assert Activity.local_only?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "favorite" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", local_only: true})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Like"}} = activity} =
+                 CommonAPI.favorite(user, activity.id)
+
+        assert Activity.local_only?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "unfavorite" do
+      user = insert(:user)
+      other_user = insert(:user)
+
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", local_only: true})
+
+      {:ok, %Activity{}} = CommonAPI.favorite(user, activity.id)
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, activity} = CommonAPI.unfavorite(activity.id, user)
+        assert Activity.local_only?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "react_with_emoji" do
+      user = insert(:user)
+      other_user = insert(:user)
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", local_only: true})
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "EmojiReact"}} = activity} =
+                 CommonAPI.react_with_emoji(activity.id, user, "👍")
+
+        assert Activity.local_only?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+
+    test "unreact_with_emoji" do
+      user = insert(:user)
+      other_user = insert(:user)
+      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe", local_only: true})
+
+      {:ok, _reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍")
+
+      with_mock Pleroma.Web.Federator, publish: fn _ -> :ok end do
+        assert {:ok, %Activity{data: %{"type" => "Undo"}} = activity} =
+                 CommonAPI.unreact_with_emoji(activity.id, user, "👍")
+
+        assert Activity.local_only?(activity)
+        refute called(Pleroma.Web.Federator.publish(activity))
+      end
+    end
+  end
 end
index 436608e515094a3733a02d5ec294b25e6cac582d..ddddd0ea06744851c34e9378441ce9b844465194 100644 (file)
@@ -1740,4 +1740,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
              |> get("/api/v1/statuses/#{activity.id}")
              |> json_response_and_validate_schema(:ok)
   end
+
+  test "posting a local only status" do
+    %{user: _user, conn: conn} = oauth_access(["write:statuses"])
+
+    conn_one =
+      conn
+      |> put_req_header("content-type", "application/json")
+      |> post("/api/v1/statuses", %{
+        "status" => "cofe",
+        "local_only" => "true"
+      })
+
+    local = Pleroma.Constants.as_local_public()
+
+    assert %{"content" => "cofe", "id" => id, "pleroma" => %{"local_only" => true}} =
+             json_response(conn_one, 200)
+
+    assert %Activity{id: ^id, data: %{"to" => [^local]}} = Activity.get_by_id(id)
+  end
 end
index 70d829979bb0cde71f47aec6fee8c92be1158196..03b0cdf152175255556c35f6421468a566bc0e1e 100644 (file)
@@ -245,7 +245,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
         direct_conversation_id: nil,
         thread_muted: false,
         emoji_reactions: [],
-        parent_visible: false
+        parent_visible: false,
+        local_only: false
       }
     }