aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/constants.ex2
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub_controller.ex10
-rw-r--r--lib/pleroma/web/activity_pub/builder.ex3
-rw-r--r--lib/pleroma/web/activity_pub/object_validators/announce_validator.ex7
-rw-r--r--lib/pleroma/web/activity_pub/pipeline.ex3
-rw-r--r--lib/pleroma/web/activity_pub/utils.ex3
-rw-r--r--lib/pleroma/web/activity_pub/visibility.ex17
-rw-r--r--lib/pleroma/web/api_spec/schemas/visibility_scope.ex2
-rw-r--r--lib/pleroma/web/common_api.ex27
-rw-r--r--lib/pleroma/web/common_api/activity_draft.ex77
-rw-r--r--lib/pleroma/web/common_api/utils.ex113
11 files changed, 131 insertions, 133 deletions
diff --git a/lib/pleroma/constants.ex b/lib/pleroma/constants.ex
index 13eeaa96b..cf8182d55 100644
--- a/lib/pleroma/constants.ex
+++ b/lib/pleroma/constants.ex
@@ -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
diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
index 31df80adb..7e5647f8f 100644
--- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
@@ -82,7 +82,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def object(conn, _) do
with ap_id <- Endpoint.url() <> conn.request_path,
%Object{} = object <- Object.get_cached_by_ap_id(ap_id),
- {_, true} <- {:public?, Visibility.is_public?(object)} do
+ {_, true} <- {:public?, Visibility.is_public?(object)},
+ {_, false} <- {:local?, Visibility.is_local_public?(object)} do
conn
|> assign(:tracking_fun_data, object.id)
|> set_cache_ttl_for(object)
@@ -92,6 +93,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
else
{:public?, false} ->
{:error, :not_found}
+
+ {:local?, true} ->
+ {:error, :not_found}
end
end
@@ -108,7 +112,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def activity(conn, _params) do
with ap_id <- Endpoint.url() <> conn.request_path,
%Activity{} = activity <- Activity.normalize(ap_id),
- {_, true} <- {:public?, Visibility.is_public?(activity)} do
+ {_, true} <- {:public?, Visibility.is_public?(activity)},
+ {_, false} <- {:local?, Visibility.is_local_public?(activity)} do
conn
|> maybe_set_tracking_data(activity)
|> set_cache_ttl_for(activity)
@@ -117,6 +122,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|> render("object.json", object: activity)
else
{:public?, false} -> {:error, :not_found}
+ {:local?, true} -> {:error, :not_found}
nil -> {:error, :not_found}
end
end
diff --git a/lib/pleroma/web/activity_pub/builder.ex b/lib/pleroma/web/activity_pub/builder.ex
index 298aff6b7..e99f6fd83 100644
--- a/lib/pleroma/web/activity_pub/builder.ex
+++ b/lib/pleroma/web/activity_pub/builder.ex
@@ -222,6 +222,9 @@ defmodule Pleroma.Web.ActivityPub.Builder do
actor.ap_id == Relay.ap_id() ->
[actor.follower_address]
+ public? and Visibility.is_local_public?(object) ->
+ [actor.follower_address, object.data["actor"], Pleroma.Constants.as_local_public()]
+
public? ->
[actor.follower_address, object.data["actor"], Pleroma.Constants.as_public()]
diff --git a/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex b/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex
index 6f757f49c..338957db8 100644
--- a/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex
+++ b/lib/pleroma/web/activity_pub/object_validators/announce_validator.ex
@@ -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 ->
diff --git a/lib/pleroma/web/activity_pub/pipeline.ex b/lib/pleroma/web/activity_pub/pipeline.ex
index 2db86f116..98c32a42b 100644
--- a/lib/pleroma/web/activity_pub/pipeline.ex
+++ b/lib/pleroma/web/activity_pub/pipeline.ex
@@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.Pipeline do
alias Pleroma.Web.ActivityPub.MRF
alias Pleroma.Web.ActivityPub.ObjectValidator
alias Pleroma.Web.ActivityPub.SideEffects
+ alias Pleroma.Web.ActivityPub.Visibility
alias Pleroma.Web.Federator
@spec common_pipeline(map(), keyword()) ::
@@ -55,7 +56,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 Visibility.is_local_public?(activity) do
activity =
if object = Keyword.get(meta, :object_data) do
%{activity | data: Map.put(activity.data, "object", object)}
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
index 713b0ca1f..46002bec2 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
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 <- Visibility.is_local_public?(activity) do
Pleroma.Web.Federator.publish(activity)
end
diff --git a/lib/pleroma/web/activity_pub/visibility.ex b/lib/pleroma/web/activity_pub/visibility.ex
index 76bd54a42..2cb5a2bd0 100644
--- a/lib/pleroma/web/activity_pub/visibility.ex
+++ b/lib/pleroma/web/activity_pub/visibility.ex
@@ -17,7 +17,19 @@ 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_local_public?(%Object{data: data}), do: is_local_public?(data)
+ def is_local_public?(%Activity{data: data}), do: is_local_public?(data)
+
+ def is_local_public?(data) do
+ Utils.label_in_message?(Pleroma.Constants.as_local_public(), data) and
+ not Utils.label_in_message?(Pleroma.Constants.as_public(), data)
+ end
def is_private?(activity) do
with false <- is_public?(activity),
@@ -114,6 +126,9 @@ defmodule Pleroma.Web.ActivityPub.Visibility do
Pleroma.Constants.as_public() in cc ->
"unlisted"
+ Pleroma.Constants.as_local_public() in to ->
+ "local"
+
# this should use the sql for the object's activity
Enum.any?(to, &String.contains?(&1, "/followers")) ->
"private"
diff --git a/lib/pleroma/web/api_spec/schemas/visibility_scope.ex b/lib/pleroma/web/api_spec/schemas/visibility_scope.ex
index 831734e27..633269a92 100644
--- a/lib/pleroma/web/api_spec/schemas/visibility_scope.ex
+++ b/lib/pleroma/web/api_spec/schemas/visibility_scope.ex
@@ -9,6 +9,6 @@ defmodule Pleroma.Web.ApiSpec.Schemas.VisibilityScope do
title: "VisibilityScope",
description: "Status visibility",
type: :string,
- enum: ["public", "unlisted", "private", "direct", "list"]
+ enum: ["public", "unlisted", "local", "private", "direct", "list"]
})
end
diff --git a/lib/pleroma/web/common_api.ex b/lib/pleroma/web/common_api.ex
index 0ab1b115d..e59254791 100644
--- a/lib/pleroma/web/common_api.ex
+++ b/lib/pleroma/web/common_api.ex
@@ -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
@@ -358,7 +359,7 @@ defmodule Pleroma.Web.CommonAPI do
def get_visibility(_, _, %Participation{}), do: {"direct", "direct"}
def get_visibility(%{visibility: visibility}, in_reply_to, _)
- when visibility in ~w{public unlisted private direct},
+ when visibility in ~w{public local unlisted private direct},
do: {visibility, get_replied_to_visibility(in_reply_to)}
def get_visibility(%{visibility: "list:" <> list_id}, in_reply_to, _) do
@@ -399,31 +400,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
diff --git a/lib/pleroma/web/common_api/activity_draft.ex b/lib/pleroma/web/common_api/activity_draft.ex
index 548f76609..aa2616d9e 100644
--- a/lib/pleroma/web/common_api/activity_draft.ex
+++ b/lib/pleroma/web/common_api/activity_draft.ex
@@ -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)
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index 3b71adf0e..1c74ea787 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -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,62 @@ 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: visibility} = draft) when visibility in ["public", "local"] do
+ to =
+ case visibility do
+ "public" -> [Pleroma.Constants.as_public() | draft.mentions]
+ "local" -> [Pleroma.Constants.as_local_public() | draft.mentions]
+ end
+
+ 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]
+ def get_to_and_cc(%{visibility: "unlisted"} = draft) do
+ to = [draft.user.follower_address | draft.mentions]
cc = [Pleroma.Constants.as_public()]
- 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, []}
def get_addressed_users(_, to) when is_list(to) do
User.get_ap_ids_by_nicknames(to)
@@ -203,30 +199,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 +299,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