summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config/config.exs9
-rw-r--r--docs/config.md5
-rw-r--r--lib/pleroma/user.ex56
-rw-r--r--lib/pleroma/user/info.ex2
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex13
-rw-r--r--lib/pleroma/web/twitter_api/views/activity_view.ex18
-rw-r--r--mix.exs9
-rw-r--r--mix.lock2
-rw-r--r--test/user_test.exs96
-rw-r--r--test/web/mastodon_api/mastodon_api_controller_test.exs18
-rw-r--r--test/web/twitter_api/views/activity_view_test.exs31
11 files changed, 237 insertions, 22 deletions
diff --git a/config/config.exs b/config/config.exs
index 601d9e227..4f4e2368a 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -91,6 +91,12 @@ config :logger, :console,
format: "$time $metadata[$level] $message\n",
metadata: [:request_id]
+config :logger, :ex_syslogger,
+ level: :debug,
+ ident: "Pleroma",
+ format: "$date $time $metadata[$level] $message",
+ metadata: [:request_id]
+
config :mime, :types, %{
"application/xml" => ["xml"],
"application/xrd+xml" => ["xrd+xml"],
@@ -131,7 +137,8 @@ config :pleroma, :instance,
"text/markdown"
],
finmoji_enabled: true,
- mrf_transparency: true
+ mrf_transparency: true,
+ autofollowed_nicknames: []
config :pleroma, :markup,
# XXX - unfortunately, inline images must be enabled by default right now, because
diff --git a/docs/config.md b/docs/config.md
index 1c3219efe..1a9706f5b 100644
--- a/docs/config.md
+++ b/docs/config.md
@@ -93,6 +93,11 @@ config :pleroma, Pleroma.Mailer,
* `always_show_subject_input`: When set to false, auto-hide the subject field when it's empty.
* `extended_nickname_format`: Set to `true` to use extended local nicknames format (allows underscores/dashes). This will break federation with
older software for theses nicknames.
+* `autofollowed_nicknames`: Set to nicknames of (local) users that every new user should automatically follow.
+
+## :logger
+* `backends`: `:console` is used to send logs to stdout, `{ExSyslogger, :ex_syslogger}` to log to syslog
+See: [logger’s documentation](https://hexdocs.pm/logger/Logger.html) and [ex_syslogger’s documentation](https://hexdocs.pm/ex_syslogger/)
## :fe
This section is used to configure Pleroma-FE, unless ``:managed_config`` in ``:instance`` is set to false.
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 0e5e2d943..7c2849ce2 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -44,20 +44,28 @@ defmodule Pleroma.User do
timestamps()
end
- def auth_active?(%User{} = user) do
- (user.info && !user.info.confirmation_pending) ||
- !Pleroma.Config.get([:instance, :account_activation_required])
- end
+ def auth_active?(%User{local: false}), do: true
+
+ def auth_active?(%User{info: %User.Info{confirmation_pending: false}}), do: true
+
+ def auth_active?(%User{info: %User.Info{confirmation_pending: true}}),
+ do: !Pleroma.Config.get([:instance, :account_activation_required])
+
+ def auth_active?(_), do: false
- def remote_or_auth_active?(%User{} = user), do: !user.local || auth_active?(user)
+ def visible_for?(user, for_user \\ nil)
- def visible_for?(%User{} = user, for_user \\ nil) do
- User.remote_or_auth_active?(user) || (for_user && for_user.id == user.id) ||
- User.superuser?(for_user)
+ def visible_for?(%User{id: user_id}, %User{id: for_id}) when user_id == for_id, do: true
+
+ def visible_for?(%User{} = user, for_user) do
+ auth_active?(user) || superuser?(for_user)
end
- def superuser?(nil), do: false
- def superuser?(%User{} = user), do: user.info && User.Info.superuser?(user.info)
+ def visible_for?(_, _), do: false
+
+ def superuser?(%User{local: true, info: %User.Info{is_admin: true}}), do: true
+ def superuser?(%User{local: true, info: %User.Info{is_moderator: true}}), do: true
+ def superuser?(_), do: false
def avatar_url(user) do
case user.avatar do
@@ -229,10 +237,27 @@ defmodule Pleroma.User do
end
end
+ defp autofollow_users(user) do
+ candidates = Pleroma.Config.get([:instance, :autofollowed_nicknames])
+
+ autofollowed_users =
+ from(u in User,
+ where: u.local == true,
+ where: u.nickname in ^candidates
+ )
+ |> Repo.all()
+
+ autofollowed_users
+ |> Enum.reduce({:ok, user}, fn other_user, {:ok, user} ->
+ follow(user, other_user)
+ end)
+ end
+
@doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"
def register(%Ecto.Changeset{} = changeset) do
with {:ok, user} <- Repo.insert(changeset),
- {:ok, _} = try_send_confirmation_email(user) do
+ {:ok, _} <- try_send_confirmation_email(user),
+ {:ok, user} <- autofollow_users(user) do
{:ok, user}
end
end
@@ -367,6 +392,15 @@ defmodule Pleroma.User do
Repo.get_by(User, ap_id: ap_id)
end
+ # This is mostly an SPC migration fix. This guesses the user nickname (by taking the last part of the ap_id and the domain) and tries to get that user
+ def get_by_guessed_nickname(ap_id) do
+ domain = URI.parse(ap_id).host
+ name = List.last(String.split(ap_id, "/"))
+ nickname = "#{name}@#{domain}"
+
+ get_by_nickname(nickname)
+ end
+
def update_and_set_cache(changeset) do
with {:ok, user} <- Repo.update(changeset) do
Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex
index 2f419a5a2..7c79dfcff 100644
--- a/lib/pleroma/user/info.ex
+++ b/lib/pleroma/user/info.ex
@@ -41,8 +41,6 @@ defmodule Pleroma.User.Info do
# subject _> Where is this used?
end
- def superuser?(info), do: info.is_admin || info.is_moderator
-
def set_activation_status(info, deactivated) do
params = %{deactivated: deactivated}
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 95d0f849c..f739e8f7d 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -725,11 +725,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
json(conn, %{})
end
- def status_search(query) do
+ def status_search(user, query) do
fetched =
if Regex.match?(~r/https?:/, query) do
- with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
- [Activity.get_create_activity_by_object_ap_id(object.data["id"])]
+ with {:ok, object} <- ActivityPub.fetch_object_from_id(query),
+ %Activity{} = activity <-
+ Activity.get_create_activity_by_object_ap_id(object.data["id"]),
+ true <- ActivityPub.visible_for_user?(activity, user) do
+ [activity]
else
_e -> []
end
@@ -756,7 +759,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, params["resolve"] == "true")
- statuses = status_search(query)
+ statuses = status_search(user, query)
tags_path = Web.base_url() <> "/tag/"
@@ -780,7 +783,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, params["resolve"] == "true")
- statuses = status_search(query)
+ statuses = status_search(user, query)
tags =
String.split(query)
diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex
index 61c617e87..5f4b74842 100644
--- a/lib/pleroma/web/twitter_api/views/activity_view.ex
+++ b/lib/pleroma/web/twitter_api/views/activity_view.ex
@@ -94,11 +94,27 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do
ap_id == "https://www.w3.org/ns/activitystreams#Public" ->
nil
+ user = User.get_cached_by_ap_id(ap_id) ->
+ user
+
+ user = User.get_by_guessed_nickname(ap_id) ->
+ user
+
true ->
- User.get_cached_by_ap_id(ap_id)
+ error_user(ap_id)
end
end
+ defp error_user(ap_id) do
+ %User{
+ name: ap_id,
+ ap_id: ap_id,
+ info: %User.Info{},
+ nickname: "erroruser@example.com",
+ inserted_at: NaiveDateTime.utc_now()
+ }
+ end
+
def render("index.json", opts) do
context_ids = collect_context_ids(opts.activities)
users = collect_users(opts.activities)
diff --git a/mix.exs b/mix.exs
index 0b8052d0a..ccf7790b0 100644
--- a/mix.exs
+++ b/mix.exs
@@ -32,7 +32,11 @@ defmodule Pleroma.Mixfile do
#
# Type `mix help compile.app` for more information.
def application do
- [mod: {Pleroma.Application, []}, extra_applications: [:logger, :runtime_tools, :comeonin]]
+ [
+ mod: {Pleroma.Application, []},
+ extra_applications: [:logger, :runtime_tools, :comeonin],
+ included_applications: [:ex_syslogger]
+ ]
end
# Specifies which paths to compile per environment.
@@ -76,7 +80,8 @@ defmodule Pleroma.Mixfile do
{:swoosh, "~> 0.20"},
{:gen_smtp, "~> 0.13"},
{:websocket_client, git: "https://github.com/jeremyong/websocket_client.git", only: :test},
- {:floki, "~> 0.20.0"}
+ {:floki, "~> 0.20.0"},
+ {:ex_syslogger, github: "slashmili/ex_syslogger", tag: "1.4.0"}
]
end
diff --git a/mix.lock b/mix.lock
index d6836231d..31725a477 100644
--- a/mix.lock
+++ b/mix.lock
@@ -21,6 +21,7 @@
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
"ex_machina": {:hex, :ex_machina, "2.2.0", "fec496331e04fc2db2a1a24fe317c12c0c4a50d2beb8ebb3531ed1f0d84be0ed", [:mix], [{:ecto, "~> 2.1", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm"},
"floki": {:hex, :floki, "0.20.4", "be42ac911fece24b4c72f3b5846774b6e61b83fe685c2fc9d62093277fb3bc86", [:mix], [{:html_entities, "~> 0.4.0", [hex: :html_entities, repo: "hexpm", optional: false]}, {:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm"},
+ "ex_syslogger": {:git, "https://github.com/slashmili/ex_syslogger.git", "f3963399047af17e038897c69e20d552e6899e1d", [tag: "1.4.0"]},
"gen_smtp": {:hex, :gen_smtp, "0.13.0", "11f08504c4bdd831dc520b8f84a1dce5ce624474a797394e7aafd3c29f5dcd25", [:rebar3], [], "hexpm"},
"gettext": {:hex, :gettext, "0.15.0", "40a2b8ce33a80ced7727e36768499fc9286881c43ebafccae6bab731e2b2b8ce", [:mix], [], "hexpm"},
"hackney": {:hex, :hackney, "1.14.3", "b5f6f5dcc4f1fba340762738759209e21914516df6be440d85772542d4a5e412", [:rebar3], [{:certifi, "2.4.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "1.0.2", [hex: :mimerl, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.4", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm"},
@@ -55,6 +56,7 @@
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"swoosh": {:hex, :swoosh, "0.20.0", "9a6c13822c9815993c03b6f8fccc370fcffb3c158d9754f67b1fdee6b3a5d928", [:mix], [{:cowboy, "~> 1.0.1 or ~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.12", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 1.1", [hex: :mime, repo: "hexpm", optional: false]}, {:plug, "~> 1.4", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm"},
+ "syslog": {:git, "https://github.com/Vagabond/erlang-syslog.git", "4a6c6f2c996483e86c1320e9553f91d337bcb6aa", [tag: "1.0.5"]},
"tesla": {:hex, :tesla, "1.2.1", "864783cc27f71dd8c8969163704752476cec0f3a51eb3b06393b3971dc9733ff", [:mix], [{:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm"},
"trailing_format_plug": {:hex, :trailing_format_plug, "0.0.7", "64b877f912cf7273bed03379936df39894149e35137ac9509117e59866e10e45", [:mix], [{:plug, "> 0.12.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"tzdata": {:hex, :tzdata, "0.5.17", "50793e3d85af49736701da1a040c415c97dc1caf6464112fd9bd18f425d3053b", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm"},
diff --git a/test/user_test.exs b/test/user_test.exs
index 74accb7c8..541252539 100644
--- a/test/user_test.exs
+++ b/test/user_test.exs
@@ -142,6 +142,23 @@ defmodule Pleroma.UserTest do
email: "email@example.com"
}
+ test "it autofollows accounts that are set for it" do
+ user = insert(:user)
+ remote_user = insert(:user, %{local: false})
+
+ Pleroma.Config.put([:instance, :autofollowed_nicknames], [
+ user.nickname,
+ remote_user.nickname
+ ])
+
+ cng = User.register_changeset(%User{}, @full_user_data)
+
+ {:ok, registered_user} = User.register(cng)
+
+ assert User.following?(registered_user, user)
+ refute User.following?(registered_user, remote_user)
+ end
+
test "it requires an email, name, nickname and password, bio is optional" do
@full_user_data
|> Map.keys()
@@ -767,4 +784,83 @@ defmodule Pleroma.UserTest do
|> Map.put(:search_distance, nil)
end
end
+
+ test "auth_active?/1 works correctly" do
+ Pleroma.Config.put([:instance, :account_activation_required], true)
+
+ local_user = insert(:user, local: true, info: %{confirmation_pending: true})
+ confirmed_user = insert(:user, local: true, info: %{confirmation_pending: false})
+ remote_user = insert(:user, local: false)
+
+ refute User.auth_active?(local_user)
+ assert User.auth_active?(confirmed_user)
+ assert User.auth_active?(remote_user)
+
+ Pleroma.Config.put([:instance, :account_activation_required], false)
+ end
+
+ describe "superuser?/1" do
+ test "returns false for unprivileged users" do
+ user = insert(:user, local: true)
+
+ refute User.superuser?(user)
+ end
+
+ test "returns false for remote users" do
+ user = insert(:user, local: false)
+ remote_admin_user = insert(:user, local: false, info: %{is_admin: true})
+
+ refute User.superuser?(user)
+ refute User.superuser?(remote_admin_user)
+ end
+
+ test "returns true for local moderators" do
+ user = insert(:user, local: true, info: %{is_moderator: true})
+
+ assert User.superuser?(user)
+ end
+
+ test "returns true for local admins" do
+ user = insert(:user, local: true, info: %{is_admin: true})
+
+ assert User.superuser?(user)
+ end
+ end
+
+ describe "visible_for?/2" do
+ test "returns true when the account is itself" do
+ user = insert(:user, local: true)
+
+ assert User.visible_for?(user, user)
+ end
+
+ test "returns false when the account is unauthenticated and auth is required" do
+ Pleroma.Config.put([:instance, :account_activation_required], true)
+
+ user = insert(:user, local: true, info: %{confirmation_pending: true})
+ other_user = insert(:user, local: true)
+
+ refute User.visible_for?(user, other_user)
+
+ Pleroma.Config.put([:instance, :account_activation_required], false)
+ end
+
+ test "returns true when the account is unauthenticated and auth is not required" do
+ user = insert(:user, local: true, info: %{confirmation_pending: true})
+ other_user = insert(:user, local: true)
+
+ assert User.visible_for?(user, other_user)
+ end
+
+ test "returns true when the account is unauthenticated and being viewed by a privileged account (auth required)" do
+ Pleroma.Config.put([:instance, :account_activation_required], true)
+
+ user = insert(:user, local: true, info: %{confirmation_pending: true})
+ other_user = insert(:user, local: true, info: %{is_admin: true})
+
+ assert User.visible_for?(user, other_user)
+
+ Pleroma.Config.put([:instance, :account_activation_required], false)
+ end
+ end
end
diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs
index 0136acf8c..ce87010c8 100644
--- a/test/web/mastodon_api/mastodon_api_controller_test.exs
+++ b/test/web/mastodon_api/mastodon_api_controller_test.exs
@@ -1312,6 +1312,24 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end)
end
+ test "search doesn't show statuses that it shouldn't", %{conn: conn} do
+ {:ok, activity} =
+ CommonAPI.post(insert(:user), %{
+ "status" => "This is about 2hu, but private",
+ "visibility" => "private"
+ })
+
+ capture_log(fn ->
+ conn =
+ conn
+ |> get("/api/v1/search", %{"q" => activity.data["object"]["id"]})
+
+ assert results = json_response(conn, 200)
+
+ [] = results["statuses"]
+ end)
+ end
+
test "search fetches remote accounts", %{conn: conn} do
conn =
conn
diff --git a/test/web/twitter_api/views/activity_view_test.exs b/test/web/twitter_api/views/activity_view_test.exs
index a03f48b61..5294204c6 100644
--- a/test/web/twitter_api/views/activity_view_test.exs
+++ b/test/web/twitter_api/views/activity_view_test.exs
@@ -25,6 +25,37 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
import Mock
+ test "returns a temporary ap_id based user for activities missing db users" do
+ user = insert(:user)
+
+ {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
+
+ Repo.delete(user)
+ Cachex.clear(:user_cache)
+
+ %{"user" => tw_user} = ActivityView.render("activity.json", activity: activity)
+
+ assert tw_user["screen_name"] == "erroruser@example.com"
+ assert tw_user["name"] == user.ap_id
+ assert tw_user["statusnet_profile_url"] == user.ap_id
+ end
+
+ test "tries to get a user by nickname if fetching by ap_id doesn't work" do
+ user = insert(:user)
+
+ {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
+
+ {:ok, user} =
+ user
+ |> Ecto.Changeset.change(%{ap_id: "#{user.ap_id}/extension/#{user.nickname}"})
+ |> Repo.update()
+
+ Cachex.clear(:user_cache)
+
+ result = ActivityView.render("activity.json", activity: activity)
+ assert result["user"]["id"] == user.id
+ end
+
test "a create activity with a html status" do
text = """
#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg