aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/user.ex26
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex18
-rw-r--r--lib/pleroma/web/activity_pub/mrf/simple_policy.ex12
-rw-r--r--lib/pleroma/web/activity_pub/transmogrifier.ex12
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex56
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_socket.ex12
-rw-r--r--lib/pleroma/web/mastodon_api/views/account_view.ex2
-rw-r--r--lib/pleroma/web/router.ex7
8 files changed, 137 insertions, 8 deletions
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index c840c8529..94f16c3c0 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -512,15 +512,33 @@ defmodule Pleroma.User do
Repo.all(q)
end
- def block(user, %{ap_id: ap_id}) do
- blocks = user.info["blocks"] || []
+ def block(blocker, %User{ap_id: ap_id} = blocked) do
+ # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213)
+ blocker =
+ if following?(blocker, blocked) do
+ {:ok, blocker, _} = unfollow(blocker, blocked)
+ blocker
+ else
+ blocker
+ end
+
+ if following?(blocked, blocker) do
+ unfollow(blocked, blocker)
+ end
+
+ blocks = blocker.info["blocks"] || []
new_blocks = Enum.uniq([ap_id | blocks])
- new_info = Map.put(user.info, "blocks", new_blocks)
+ new_info = Map.put(blocker.info, "blocks", new_blocks)
- cs = User.info_changeset(user, %{info: new_info})
+ cs = User.info_changeset(blocker, %{info: new_info})
update_and_set_cache(cs)
end
+ # helper to handle the block given only an actor's AP id
+ def block(blocker, %{ap_id: ap_id}) do
+ block(blocker, User.get_by_ap_id(ap_id))
+ end
+
def unblock(user, %{ap_id: ap_id}) do
blocks = user.info["blocks"] || []
new_blocks = List.delete(blocks, ap_id)
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index e3ce5aa04..195679fad 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -65,6 +65,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
if activity.local do
Pleroma.Web.Streamer.stream("public:local", activity)
end
+
+ if activity.data["object"]["attachment"] != [] do
+ Pleroma.Web.Streamer.stream("public:media", activity)
+
+ if activity.local do
+ Pleroma.Web.Streamer.stream("public:local:media", activity)
+ end
+ end
else
if !Enum.member?(activity.data["cc"] || [], public) &&
!Enum.member?(
@@ -439,6 +447,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_media(query, _), do: query
+ defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
+ from(
+ activity in query,
+ where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
+ )
+ end
+
+ defp restrict_replies(query, _), do: query
+
# Only search through last 100_000 activities by default
defp restrict_recent(query, %{"whole_db" => true}), do: query
@@ -496,6 +513,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> restrict_blocked(opts)
|> restrict_media(opts)
|> restrict_visibility(opts)
+ |> restrict_replies(opts)
end
def fetch_activities(recipients, opts \\ %{}) do
diff --git a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex
index 8d770387d..7fecb8a4f 100644
--- a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex
+++ b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex
@@ -4,6 +4,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
@mrf_policy Application.get_env(:pleroma, :mrf_simple)
+ @accept Keyword.get(@mrf_policy, :accept)
+ defp check_accept(actor_info, object) do
+ if length(@accept) > 0 and not (actor_info.host in @accept) do
+ {:reject, nil}
+ else
+ {:ok, object}
+ end
+ end
+
@reject Keyword.get(@mrf_policy, :reject)
defp check_reject(actor_info, object) do
if actor_info.host in @reject do
@@ -74,7 +83,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
def filter(object) do
actor_info = URI.parse(object["actor"])
- with {:ok, object} <- check_reject(actor_info, object),
+ with {:ok, object} <- check_accept(actor_info, object),
+ {: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
diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex
index 300e0fcdd..30cd70fb6 100644
--- a/lib/pleroma/web/activity_pub/transmogrifier.ex
+++ b/lib/pleroma/web/activity_pub/transmogrifier.ex
@@ -24,6 +24,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|> fix_in_reply_to
|> fix_emoji
|> fix_tag
+ |> fix_content_map
end
def fix_in_reply_to(%{"inReplyTo" => in_reply_to_id} = object)
@@ -107,6 +108,17 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|> Map.put("tag", combined)
end
+ # content map usually only has one language so this will do for now.
+ def fix_content_map(%{"contentMap" => content_map} = object) do
+ content_groups = Map.to_list(content_map)
+ {_, content} = Enum.at(content_groups, 0)
+
+ object
+ |> Map.put("content", content)
+ end
+
+ def fix_content_map(object), do: object
+
# TODO: validate those with a Ecto scheme
# - tags
# - emoji
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 8a8d1e050..dab255ee2 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -621,6 +621,58 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
json(conn, %{})
end
+ def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
+ accounts = User.search(query, params["resolve"] == "true")
+
+ fetched =
+ if Regex.match?(~r/https?:/, query) do
+ with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
+ activities
+ |> Enum.filter(fn
+ %{data: %{"type" => "Create"}} -> true
+ _ -> false
+ end)
+ else
+ _e -> []
+ end
+ end || []
+
+ q =
+ from(
+ a in Activity,
+ where: fragment("?->>'type' = 'Create'", a.data),
+ where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
+ where:
+ fragment(
+ "to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
+ a.data,
+ ^query
+ ),
+ limit: 20,
+ order_by: [desc: :id]
+ )
+
+ statuses = Repo.all(q) ++ fetched
+
+ tags_path = Web.base_url() <> "/tag/"
+
+ tags =
+ String.split(query)
+ |> Enum.uniq()
+ |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
+ |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
+ |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
+
+ res = %{
+ "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
+ "statuses" =>
+ StatusView.render("index.json", activities: statuses, for: user, as: :activity),
+ "hashtags" => tags
+ }
+
+ json(conn, res)
+ end
+
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, params["resolve"] == "true")
@@ -812,7 +864,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
boost_modal: false,
delete_modal: true,
auto_play_gif: false,
- reduce_motion: false
+ display_sensitive_media: false,
+ reduce_motion: false,
+ max_toot_chars: Keyword.get(@instance, :limit)
},
compose: %{
me: "#{user.id}",
diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex
index 46648c366..174293906 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_socket.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex
@@ -15,8 +15,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do
with token when not is_nil(token) <- params["access_token"],
%Token{user_id: user_id} <- Repo.get_by(Token, token: token),
%User{} = user <- Repo.get(User, user_id),
- stream when stream in ["public", "public:local", "user", "direct", "list"] <-
- params["stream"] do
+ stream
+ when stream in [
+ "public",
+ "public:local",
+ "public:media",
+ "public:local:media",
+ "user",
+ "direct",
+ "list"
+ ] <- params["stream"] do
topic = if stream == "list", do: "list:#{params["list"]}", else: stream
socket =
diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex
index 9db683f44..f33d615cf 100644
--- a/lib/pleroma/web/mastodon_api/views/account_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/account_view.ex
@@ -30,6 +30,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
avatar_static: image,
header: header,
header_static: header,
+ emojis: [],
+ fields: [],
source: %{
note: "",
privacy: "public",
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 13bd393ab..ebe68218b 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -170,9 +170,16 @@ defmodule Pleroma.Web.Router do
get("/accounts/:id/following", MastodonAPIController, :following)
get("/accounts/:id", MastodonAPIController, :user)
+ get("/trends", MastodonAPIController, :empty_array)
+
get("/search", MastodonAPIController, :search)
end
+ scope "/api/v2", Pleroma.Web.MastodonAPI do
+ pipe_through(:api)
+ get("/search", MastodonAPIController, :search2)
+ end
+
scope "/api", Pleroma.Web do
pipe_through(:config)