diff options
-rw-r--r-- | lib/pleroma/application.ex | 12 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/mrf/simple_policy.ex | 42 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/transmogrifier.ex | 9 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/views/user_view.ex | 9 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 20 | ||||
-rw-r--r-- | lib/pleroma/web/streamer.ex | 34 | ||||
-rw-r--r-- | lib/pleroma/web/web_finger/web_finger.ex | 13 | ||||
-rw-r--r-- | priv/static/index.html | 2 | ||||
-rw-r--r-- | test/web/mastodon_api/mastodon_api_controller_test.exs | 19 | ||||
-rw-r--r-- | test/web/streamer_test.exs | 63 |
10 files changed, 165 insertions, 58 deletions
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 89826f515..e1e3bcd63 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -23,6 +23,18 @@ defmodule Pleroma.Application do limit: 2500 ] ]), + worker( + Cachex, + [ + :idempotency_cache, + [ + default_ttl: :timer.seconds(6 * 60 * 60), + ttl_interval: :timer.seconds(60), + limit: 2500 + ] + ], + id: :cachex_idem + ), worker(Pleroma.Web.Federator, []), worker(Pleroma.Gopher.Server, []), worker(Pleroma.Stats, []) diff --git a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex index ad28cbfe3..d840d759d 100644 --- a/lib/pleroma/web/activity_pub/mrf/simple_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/simple_policy.ex @@ -17,9 +17,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do if actor_info.host in @media_removal do child_object = Map.delete(object["object"], "attachment") object = Map.put(object, "object", child_object) + {:ok, object} + else + {:ok, object} end - - {:ok, object} end @media_nsfw Keyword.get(@mrf_policy, :media_nsfw) @@ -32,9 +33,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do child_object = Map.put(child_object, "tags", tags) child_object = Map.put(child_object, "sensitive", true) object = Map.put(object, "object", child_object) + {:ok, object} + else + {:ok, object} end - - {:ok, object} end @ftl_removal Keyword.get(@mrf_policy, :federated_timeline_removal) @@ -43,22 +45,28 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy 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] + object = + 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"] + 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 + object + |> Map.put("to", to) + |> Map.put("cc", cc) + else + object + end - {:ok, object} + {:ok, object} + else + {:ok, object} + end end def filter(object) do diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index c6b99da2e..a0e45510c 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -72,9 +72,12 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do |> Enum.reduce(%{}, fn data, mapping -> name = data["name"] - if String.starts_with?(name, ":") do - name = name |> String.slice(1..-2) - end + name = + if String.starts_with?(name, ":") do + name = name |> String.slice(1..-2) + else + name + end mapping |> Map.put(name, data["icon"]["url"]) end) diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 156a12f26..a1f0be9ed 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -104,9 +104,12 @@ defmodule Pleroma.Web.ActivityPub.UserView do "limit" => "10" } - if max_qid != nil do - params = Map.put(params, "max_id", max_qid) - end + params = + if max_qid != nil do + Map.put(params, "max_id", max_qid) + else + params + end activities = ActivityPub.fetch_public_activities(params) min_id = Enum.at(activities, 0).id diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index bbd16482a..9f4261143 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -112,7 +112,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do version: "#{@mastodon_api_level} (compatible; #{Keyword.get(@instance, :version)})", email: Keyword.get(@instance, :email), urls: %{ - streaming_api: String.replace(Web.base_url(), ["http", "https"], "wss") + streaming_api: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws") }, stats: Stats.get_stats(), thumbnail: Web.base_url() <> "/instance/thumbnail.jpeg", @@ -212,14 +212,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> Map.put("actor_id", ap_id) |> Map.put("whole_db", true) - if params["pinned"] == "true" do - # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here - activities = [] - else - activities = + activities = + if params["pinned"] == "true" do + # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here + [] + else ActivityPub.fetch_public_activities(params) |> Enum.reverse() - end + end conn |> add_link_headers(:user_statuses, activities, params["id"]) @@ -283,13 +283,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do {:ok, activity} = Cachex.get!( - :user_cache, - "idem:#{idempotency_key}", + :idempotency_cache, + idempotency_key, fallback: fn _ -> CommonAPI.post(user, params) end ) - Cachex.expire(:user_cache, "idem:#{idempotency_key}", :timer.seconds(5 * 60)) - render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}) end diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex index 10670e71f..3375af841 100644 --- a/lib/pleroma/web/streamer.ex +++ b/lib/pleroma/web/streamer.ex @@ -110,20 +110,26 @@ defmodule Pleroma.Web.Streamer do def push_to_socket(topics, topic, item) do Enum.each(topics[topic] || [], fn socket -> - json = - %{ - event: "update", - payload: - Pleroma.Web.MastodonAPI.StatusView.render( - "status.json", - activity: item, - for: socket.assigns[:user] - ) - |> Jason.encode!() - } - |> Jason.encode!() - - send(socket.transport_pid, {:text, json}) + # Get the current user so we have up-to-date blocks etc. + user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id) + blocks = user.info["blocks"] || [] + + unless item.actor in blocks do + json = + %{ + event: "update", + payload: + Pleroma.Web.MastodonAPI.StatusView.render( + "status.json", + activity: item, + for: user + ) + |> Jason.encode!() + } + |> Jason.encode!() + + send(socket.transport_pid, {:text, json}) + end end) end diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 9fe3b2ca1..6ffa80a43 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -239,13 +239,14 @@ defmodule Pleroma.Web.WebFinger do URI.parse(account).host end - case find_lrdd_template(domain) do - {:ok, template} -> - address = String.replace(template, "{uri}", URI.encode(account)) + address = + case find_lrdd_template(domain) do + {:ok, template} -> + String.replace(template, "{uri}", URI.encode(account)) - _ -> - address = "http://#{domain}/.well-known/webfinger?resource=acct:#{account}" - end + _ -> + "http://#{domain}/.well-known/webfinger?resource=acct:#{account}" + end with response <- @httpoison.get( diff --git a/priv/static/index.html b/priv/static/index.html index 795c9e18d..cf1c24b0e 100644 --- a/priv/static/index.html +++ b/priv/static/index.html @@ -1 +1 @@ -<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Pleroma</title><link rel=stylesheet href=/static/font/css/fontello.css><link rel=stylesheet href=/static/font/css/animation.css><link href=/static/css/app.6c8da1b0ace79ad8881a0e5e716ec818.css rel=stylesheet></head><body style="display: none"><div id=app></div><script type=text/javascript src=/static/js/manifest.38e369a50eccc2857845.js></script><script type=text/javascript src=/static/js/vendor.ef2aee0b2db579c3a86a.js></script><script type=text/javascript src=/static/js/app.af121efa5ff89725b4c6.js></script></body></html>
\ No newline at end of file +<!DOCTYPE html><html lang=en><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>Pleroma</title><link rel=icon type=image/png href=/favicon.png><link rel=stylesheet href=/static/font/css/fontello.css><link rel=stylesheet href=/static/font/css/animation.css><link href=/static/css/app.6c8da1b0ace79ad8881a0e5e716ec818.css rel=stylesheet></head><body style="display: none"><div id=app></div><script type=text/javascript src=/static/js/manifest.38e369a50eccc2857845.js></script><script type=text/javascript src=/static/js/vendor.ef2aee0b2db579c3a86a.js></script><script type=text/javascript src=/static/js/app.af121efa5ff89725b4c6.js></script></body></html>
\ No newline at end of file diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 69a0299ac..883ebc61e 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -75,9 +75,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do "sensitive" => "false" }) - {:ok, ttl} = Cachex.ttl(:user_cache, "idem:#{idempotency_key}") - # 5 Minutes - assert ttl > :timer.seconds(5 * 60 - 1) + {:ok, ttl} = Cachex.ttl(:idempotency_cache, idempotency_key) + # Six hours + assert ttl > :timer.seconds(6 * 60 * 60 - 1) assert %{"content" => "cofe", "id" => id, "spoiler_text" => "2hu", "sensitive" => false} = json_response(conn_one, 200) @@ -97,6 +97,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert %{"id" => second_id} = json_response(conn_two, 200) assert id == second_id + + conn_three = + conn + |> assign(:user, user) + |> post("/api/v1/statuses", %{ + "status" => "cofe", + "spoiler_text" => "2hu", + "sensitive" => "false" + }) + + assert %{"id" => third_id} = json_response(conn_three, 200) + + refute id == third_id end test "posting a sensitive status", %{conn: conn} do diff --git a/test/web/streamer_test.exs b/test/web/streamer_test.exs new file mode 100644 index 000000000..47d491d1b --- /dev/null +++ b/test/web/streamer_test.exs @@ -0,0 +1,63 @@ +defmodule Pleroma.Web.StreamerTest do + use Pleroma.DataCase + + alias Pleroma.Web.Streamer + alias Pleroma.User + alias Pleroma.Web.CommonAPI + import Pleroma.Factory + + test "it sends to public" do + user = insert(:user) + other_user = insert(:user) + + task = + Task.async(fn -> + assert_receive {:text, _}, 4_000 + end) + + fake_socket = %{ + transport_pid: task.pid, + assigns: %{ + user: user + } + } + + {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"}) + + topics = %{ + "public" => [fake_socket] + } + + Streamer.push_to_socket(topics, "public", activity) + + Task.await(task) + end + + test "it doesn't send to blocked users" do + user = insert(:user) + blocked_user = insert(:user) + {:ok, user} = User.block(user, blocked_user) + + task = + Task.async(fn -> + refute_receive {:text, _}, 1_000 + end) + + fake_socket = %{ + transport_pid: task.pid, + assigns: %{ + user: user + } + } + + {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"}) + + topics = %{ + "public" => [fake_socket] + } + + Streamer.push_to_socket(topics, "public", activity) + + Task.await(task) + end +end |