aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/mix/pleroma.ex4
-rw-r--r--lib/mix/tasks/pleroma/config.ex6
-rw-r--r--lib/mix/tasks/pleroma/ecto/ecto.ex11
-rw-r--r--lib/mix/tasks/pleroma/ecto/migrate.ex2
-rw-r--r--lib/mix/tasks/pleroma/ecto/rollback.ex2
-rw-r--r--lib/mix/tasks/pleroma/instance.ex16
-rw-r--r--lib/mix/tasks/pleroma/user.ex4
-rw-r--r--lib/pleroma/config/transfer_task.ex19
-rw-r--r--lib/pleroma/emails/user_email.ex9
-rw-r--r--lib/pleroma/emoji.ex6
-rw-r--r--lib/pleroma/notification.ex10
-rw-r--r--lib/pleroma/password_reset_token.ex (renamed from lib/pleroma/PasswordResetToken.ex)1
-rw-r--r--lib/pleroma/release_tasks.ex2
-rw-r--r--lib/pleroma/repo_streamer.ex34
-rw-r--r--lib/pleroma/user.ex95
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex19
-rw-r--r--lib/pleroma/web/activity_pub/utils.ex18
-rw-r--r--lib/pleroma/web/admin_api/admin_api_controller.ex8
-rw-r--r--lib/pleroma/web/admin_api/config.ex88
-rw-r--r--lib/pleroma/web/admin_api/views/config_view.ex1
-rw-r--r--lib/pleroma/web/common_api/common_api.ex1
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex2
-rw-r--r--lib/pleroma/web/oauth/authorization.ex12
-rw-r--r--lib/pleroma/web/rich_media/helpers.ex42
-rw-r--r--lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex35
-rw-r--r--lib/pleroma/web/router.ex4
-rw-r--r--lib/pleroma/web/templates/twitter_api/password/invalid_token.html.eex (renamed from lib/pleroma/web/templates/twitter_api/util/invalid_token.html.eex)0
-rw-r--r--lib/pleroma/web/templates/twitter_api/password/reset.html.eex (renamed from lib/pleroma/web/templates/twitter_api/util/password_reset.html.eex)2
-rw-r--r--lib/pleroma/web/templates/twitter_api/password/reset_failed.html.eex (renamed from lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex)0
-rw-r--r--lib/pleroma/web/templates/twitter_api/password/reset_success.html.eex (renamed from lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex)0
-rw-r--r--lib/pleroma/web/twitter_api/controllers/password_controller.ex37
-rw-r--r--lib/pleroma/web/twitter_api/controllers/util_controller.ex22
-rw-r--r--lib/pleroma/web/twitter_api/views/password_view.ex8
33 files changed, 370 insertions, 150 deletions
diff --git a/lib/mix/pleroma.ex b/lib/mix/pleroma.ex
index 548c8a0a4..1b758ea33 100644
--- a/lib/mix/pleroma.ex
+++ b/lib/mix/pleroma.ex
@@ -9,6 +9,10 @@ defmodule Mix.Pleroma do
{:ok, _} = Application.ensure_all_started(:pleroma)
end
+ def load_pleroma do
+ Application.load(:pleroma)
+ end
+
def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
end
diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex
index cc5425362..4ed2c9789 100644
--- a/lib/mix/tasks/pleroma/config.ex
+++ b/lib/mix/tasks/pleroma/config.ex
@@ -24,7 +24,7 @@ defmodule Mix.Tasks.Pleroma.Config do
|> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
|> Enum.each(fn {k, v} ->
key = to_string(k) |> String.replace("Elixir.", "")
- {:ok, _} = Config.update_or_create(%{key: key, value: v})
+ {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
Mix.shell().info("#{key} is migrated.")
end)
@@ -51,7 +51,9 @@ defmodule Mix.Tasks.Pleroma.Config do
IO.write(
file,
- "config :pleroma, #{config.key}#{mark} #{inspect(Config.from_binary(config.value))}\r\n"
+ "config :#{config.group}, #{config.key}#{mark} #{
+ inspect(Config.from_binary(config.value))
+ }\r\n"
)
{:ok, _} = Repo.delete(config)
diff --git a/lib/mix/tasks/pleroma/ecto/ecto.ex b/lib/mix/tasks/pleroma/ecto/ecto.ex
index af09cb289..324f57fdd 100644
--- a/lib/mix/tasks/pleroma/ecto/ecto.ex
+++ b/lib/mix/tasks/pleroma/ecto/ecto.ex
@@ -9,6 +9,15 @@ defmodule Mix.Tasks.Pleroma.Ecto do
def ensure_migrations_path(repo, opts) do
path = opts[:migrations_path] || Path.join(source_repo_priv(repo), "migrations")
+ path =
+ case Path.type(path) do
+ :relative ->
+ Path.join(Application.app_dir(:pleroma), path)
+
+ :absolute ->
+ path
+ end
+
if not File.dir?(path) do
raise_missing_migrations(Path.relative_to_cwd(path), repo)
end
@@ -22,7 +31,7 @@ defmodule Mix.Tasks.Pleroma.Ecto do
def source_repo_priv(repo) do
config = repo.config()
priv = config[:priv] || "priv/#{repo |> Module.split() |> List.last() |> Macro.underscore()}"
- Path.join(File.cwd!(), priv)
+ Path.join(Application.app_dir(:pleroma), priv)
end
defp raise_missing_migrations(path, repo) do
diff --git a/lib/mix/tasks/pleroma/ecto/migrate.ex b/lib/mix/tasks/pleroma/ecto/migrate.ex
index 22eafe76f..855c977f6 100644
--- a/lib/mix/tasks/pleroma/ecto/migrate.ex
+++ b/lib/mix/tasks/pleroma/ecto/migrate.ex
@@ -4,6 +4,7 @@
defmodule Mix.Tasks.Pleroma.Ecto.Migrate do
use Mix.Task
+ import Mix.Pleroma
require Logger
@shortdoc "Wrapper on `ecto.migrate` task."
@@ -37,6 +38,7 @@ defmodule Mix.Tasks.Pleroma.Ecto.Migrate do
@impl true
def run(args \\ []) do
+ load_pleroma()
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
opts =
diff --git a/lib/mix/tasks/pleroma/ecto/rollback.ex b/lib/mix/tasks/pleroma/ecto/rollback.ex
index 0033ceba4..2ffb0901c 100644
--- a/lib/mix/tasks/pleroma/ecto/rollback.ex
+++ b/lib/mix/tasks/pleroma/ecto/rollback.ex
@@ -4,6 +4,7 @@
defmodule Mix.Tasks.Pleroma.Ecto.Rollback do
use Mix.Task
+ import Mix.Pleroma
require Logger
@shortdoc "Wrapper on `ecto.rollback` task"
@@ -36,6 +37,7 @@ defmodule Mix.Tasks.Pleroma.Ecto.Rollback do
@impl true
def run(args \\ []) do
+ load_pleroma()
{opts, _} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
opts =
diff --git a/lib/mix/tasks/pleroma/instance.ex b/lib/mix/tasks/pleroma/instance.ex
index c6738dbcc..997eabbeb 100644
--- a/lib/mix/tasks/pleroma/instance.ex
+++ b/lib/mix/tasks/pleroma/instance.ex
@@ -29,6 +29,7 @@ defmodule Mix.Tasks.Pleroma.Instance do
- `--dbname DBNAME` - the name of the database to use
- `--dbuser DBUSER` - the user (aka role) to use for the database connection
- `--dbpass DBPASS` - the password to use for the database connection
+ - `--rum Y/N` - Whether to enable RUM indexes
- `--indexable Y/N` - Allow/disallow indexing site by search engines
- `--db-configurable Y/N` - Allow/disallow configuring instance from admin part
- `--uploads-dir` - the directory uploads go in when using a local uploader
@@ -51,6 +52,7 @@ defmodule Mix.Tasks.Pleroma.Instance do
dbname: :string,
dbuser: :string,
dbpass: :string,
+ rum: :string,
indexable: :string,
db_configurable: :string,
uploads_dir: :string,
@@ -136,6 +138,14 @@ defmodule Mix.Tasks.Pleroma.Instance do
"autogenerated"
)
+ rum_enabled =
+ get_option(
+ options,
+ :rum,
+ "Would you like to use RUM indices?",
+ "n"
+ ) === "y"
+
uploads_dir =
get_option(
options,
@@ -175,7 +185,8 @@ defmodule Mix.Tasks.Pleroma.Instance do
web_push_private_key: Base.url_encode64(web_push_private_key, padding: false),
db_configurable?: db_configurable?,
static_dir: static_dir,
- uploads_dir: uploads_dir
+ uploads_dir: uploads_dir,
+ rum_enabled: rum_enabled
)
result_psql =
@@ -183,7 +194,8 @@ defmodule Mix.Tasks.Pleroma.Instance do
template_dir <> "/sample_psql.eex",
dbname: dbname,
dbuser: dbuser,
- dbpass: dbpass
+ dbpass: dbpass,
+ rum_enabled: rum_enabled
)
shell_info(
diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex
index ab158f57e..8a78b4fe6 100644
--- a/lib/mix/tasks/pleroma/user.ex
+++ b/lib/mix/tasks/pleroma/user.ex
@@ -204,9 +204,9 @@ defmodule Mix.Tasks.Pleroma.User do
IO.puts(
"URL: #{
- Pleroma.Web.Router.Helpers.util_url(
+ Pleroma.Web.Router.Helpers.reset_password_url(
Pleroma.Web.Endpoint,
- :show_password_reset,
+ :reset,
token.token
)
}"
diff --git a/lib/pleroma/config/transfer_task.ex b/lib/pleroma/config/transfer_task.ex
index a8cbfa52a..cf880aa22 100644
--- a/lib/pleroma/config/transfer_task.ex
+++ b/lib/pleroma/config/transfer_task.ex
@@ -11,8 +11,17 @@ defmodule Pleroma.Config.TransferTask do
def load_and_update_env do
if Pleroma.Config.get([:instance, :dynamic_configuration]) and
Ecto.Adapters.SQL.table_exists?(Pleroma.Repo, "config") do
- Pleroma.Repo.all(Config)
- |> Enum.each(&update_env(&1))
+ for_restart =
+ Pleroma.Repo.all(Config)
+ |> Enum.map(&update_env(&1))
+
+ # We need to restart applications for loaded settings take effect
+ for_restart
+ |> Enum.reject(&(&1 in [:pleroma, :ok]))
+ |> Enum.each(fn app ->
+ Application.stop(app)
+ :ok = Application.start(app)
+ end)
end
end
@@ -25,11 +34,15 @@ defmodule Pleroma.Config.TransferTask do
setting.key
end
+ group = String.to_existing_atom(setting.group)
+
Application.put_env(
- :pleroma,
+ group,
String.to_existing_atom(key),
Config.from_binary(setting.value)
)
+
+ group
rescue
e ->
require Logger
diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex
index 8502a0d0c..934620765 100644
--- a/lib/pleroma/emails/user_email.ex
+++ b/lib/pleroma/emails/user_email.ex
@@ -23,13 +23,8 @@ defmodule Pleroma.Emails.UserEmail do
defp recipient(email, name), do: {name, email}
defp recipient(%Pleroma.User{} = user), do: recipient(user.email, user.name)
- def password_reset_email(user, password_reset_token) when is_binary(password_reset_token) do
- password_reset_url =
- Router.Helpers.util_url(
- Endpoint,
- :show_password_reset,
- password_reset_token
- )
+ def password_reset_email(user, token) when is_binary(token) do
+ password_reset_url = Router.Helpers.reset_password_url(Endpoint, :reset, token)
html_body = """
<h3>Reset your password at #{instance_name()}</h3>
diff --git a/lib/pleroma/emoji.ex b/lib/pleroma/emoji.ex
index 854d46b1a..052501642 100644
--- a/lib/pleroma/emoji.ex
+++ b/lib/pleroma/emoji.ex
@@ -148,11 +148,13 @@ defmodule Pleroma.Emoji do
if File.exists?(emoji_txt) do
load_from_file(emoji_txt, emoji_groups)
else
+ extensions = Pleroma.Config.get([:emoji, :pack_extensions])
+
Logger.info(
- "No emoji.txt found for pack \"#{pack_name}\", assuming all .png files are emoji"
+ "No emoji.txt found for pack \"#{pack_name}\", assuming all #{Enum.join(extensions, ", ")} files are emoji"
)
- make_shortcode_to_file_map(pack_dir, [".png"])
+ make_shortcode_to_file_map(pack_dir, extensions)
|> Enum.map(fn {shortcode, rel_file} ->
filename = Path.join("/emoji/#{pack_name}", rel_file)
diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex
index e25692006..a414afbbf 100644
--- a/lib/pleroma/notification.ex
+++ b/lib/pleroma/notification.ex
@@ -127,8 +127,7 @@ defmodule Pleroma.Notification do
end
end
- def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
- when type in ["Create", "Like", "Announce", "Follow"] do
+ def create_notifications(%Activity{data: %{"to" => _, "type" => "Create"}} = activity) do
object = Object.normalize(activity)
unless object && object.data["type"] == "Answer" do
@@ -140,6 +139,13 @@ defmodule Pleroma.Notification do
end
end
+ def create_notifications(%Activity{data: %{"to" => _, "type" => type}} = activity)
+ when type in ["Like", "Announce", "Follow"] do
+ users = get_notified_from_activity(activity)
+ notifications = Enum.map(users, fn user -> create_notification(activity, user) end)
+ {:ok, notifications}
+ end
+
def create_notifications(_), do: {:ok, []}
# TODO move to sql, too.
diff --git a/lib/pleroma/PasswordResetToken.ex b/lib/pleroma/password_reset_token.ex
index f31ea5bc5..4a833f6a5 100644
--- a/lib/pleroma/PasswordResetToken.ex
+++ b/lib/pleroma/password_reset_token.ex
@@ -37,6 +37,7 @@ defmodule Pleroma.PasswordResetToken do
|> put_change(:used, true)
end
+ @spec reset_password(binary(), map()) :: {:ok, User.t()} | {:error, binary()}
def reset_password(token, data) do
with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
%User{} = user <- User.get_cached_by_id(token.user_id),
diff --git a/lib/pleroma/release_tasks.ex b/lib/pleroma/release_tasks.ex
index d6720cd05..8afabf463 100644
--- a/lib/pleroma/release_tasks.ex
+++ b/lib/pleroma/release_tasks.ex
@@ -44,6 +44,8 @@ defmodule Pleroma.ReleaseTasks do
end
def create do
+ Application.load(:pleroma)
+
case @repo.__adapter__.storage_up(@repo.config) do
:ok ->
IO.puts("The database for #{inspect(@repo)} has been created")
diff --git a/lib/pleroma/repo_streamer.ex b/lib/pleroma/repo_streamer.ex
new file mode 100644
index 000000000..a4b71a1bb
--- /dev/null
+++ b/lib/pleroma/repo_streamer.ex
@@ -0,0 +1,34 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.RepoStreamer do
+ alias Pleroma.Repo
+ import Ecto.Query
+
+ def chunk_stream(query, chunk_size) do
+ Stream.unfold(0, fn
+ :halt ->
+ {[], :halt}
+
+ last_id ->
+ query
+ |> order_by(asc: :id)
+ |> where([r], r.id > ^last_id)
+ |> limit(^chunk_size)
+ |> Repo.all()
+ |> case do
+ [] ->
+ {[], :halt}
+
+ records ->
+ last_id = List.last(records).id
+ {records, last_id}
+ end
+ end)
+ |> Stream.take_while(fn
+ [] -> false
+ _ -> true
+ end)
+ end
+end
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 3a9ae8d73..f7191762f 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -9,12 +9,14 @@ defmodule Pleroma.User do
import Ecto.Query
alias Comeonin.Pbkdf2
+ alias Ecto.Multi
alias Pleroma.Activity
alias Pleroma.Keys
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Registration
alias Pleroma.Repo
+ alias Pleroma.RepoStreamer
alias Pleroma.User
alias Pleroma.Web
alias Pleroma.Web.ActivityPub.ActivityPub
@@ -193,29 +195,26 @@ defmodule Pleroma.User do
end
def password_update_changeset(struct, params) do
- changeset =
- struct
- |> cast(params, [:password, :password_confirmation])
- |> validate_required([:password, :password_confirmation])
- |> validate_confirmation(:password)
-
- OAuth.Token.delete_user_tokens(struct)
- OAuth.Authorization.delete_user_authorizations(struct)
-
- if changeset.valid? do
- hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
-
- changeset
- |> put_change(:password_hash, hashed)
- else
- changeset
+ struct
+ |> cast(params, [:password, :password_confirmation])
+ |> validate_required([:password, :password_confirmation])
+ |> validate_confirmation(:password)
+ |> put_password_hash
+ end
+
+ def reset_password(%User{id: user_id} = user, data) do
+ multi =
+ Multi.new()
+ |> Multi.update(:user, password_update_changeset(user, data))
+ |> Multi.delete_all(:tokens, OAuth.Token.Query.get_by_user(user_id))
+ |> Multi.delete_all(:auth, OAuth.Authorization.delete_by_user_query(user))
+
+ case Repo.transaction(multi) do
+ {:ok, %{user: user} = _} -> set_cache(user)
+ {:error, _, changeset, _} -> {:error, changeset}
end
end
- def reset_password(user, data) do
- update_and_set_cache(password_update_changeset(user, data))
- end
-
def register_changeset(struct, params \\ %{}, opts \\ []) do
need_confirmation? =
if is_nil(opts[:need_confirmation]) do
@@ -249,12 +248,11 @@ defmodule Pleroma.User do
end
if changeset.valid? do
- hashed = Pbkdf2.hashpwsalt(changeset.changes[:password])
ap_id = User.ap_id(%User{nickname: changeset.changes[:nickname]})
followers = User.ap_followers(%User{nickname: changeset.changes[:nickname]})
changeset
- |> put_change(:password_hash, hashed)
+ |> put_password_hash
|> put_change(:ap_id, ap_id)
|> unique_constraint(:ap_id)
|> put_change(:following, [followers])
@@ -932,18 +930,24 @@ defmodule Pleroma.User do
@spec perform(atom(), User.t()) :: {:ok, User.t()}
def perform(:delete, %User{} = user) do
- {:ok, user} = User.deactivate(user)
-
# Remove all relationships
{:ok, followers} = User.get_followers(user)
- Enum.each(followers, fn follower -> User.unfollow(follower, user) end)
+ Enum.each(followers, fn follower ->
+ ActivityPub.unfollow(follower, user)
+ User.unfollow(follower, user)
+ end)
{:ok, friends} = User.get_friends(user)
- Enum.each(friends, fn followed -> User.unfollow(user, followed) end)
+ Enum.each(friends, fn followed ->
+ ActivityPub.unfollow(user, followed)
+ User.unfollow(user, followed)
+ end)
delete_user_activities(user)
+
+ {:ok, _user} = Repo.delete(user)
end
@spec perform(atom(), User.t()) :: {:ok, User.t()}
@@ -1016,18 +1020,35 @@ defmodule Pleroma.User do
])
def delete_user_activities(%User{ap_id: ap_id} = user) do
- stream =
- ap_id
- |> Activity.query_by_actor()
- |> Repo.stream()
-
- Repo.transaction(fn -> Enum.each(stream, &delete_activity(&1)) end, timeout: :infinity)
+ ap_id
+ |> Activity.query_by_actor()
+ |> RepoStreamer.chunk_stream(50)
+ |> Stream.each(fn activities ->
+ Enum.each(activities, &delete_activity(&1))
+ end)
+ |> Stream.run()
{:ok, user}
end
defp delete_activity(%{data: %{"type" => "Create"}} = activity) do
- Object.normalize(activity) |> ActivityPub.delete()
+ activity
+ |> Object.normalize()
+ |> ActivityPub.delete()
+ end
+
+ defp delete_activity(%{data: %{"type" => "Like"}} = activity) do
+ user = get_cached_by_ap_id(activity.actor)
+ object = Object.normalize(activity)
+
+ ActivityPub.unlike(user, object)
+ end
+
+ defp delete_activity(%{data: %{"type" => "Announce"}} = activity) do
+ user = get_cached_by_ap_id(activity.actor)
+ object = Object.normalize(activity)
+
+ ActivityPub.unannounce(user, object)
end
defp delete_activity(_activity), do: "Doing nothing"
@@ -1325,4 +1346,12 @@ defmodule Pleroma.User do
end
defdelegate search(query, opts \\ []), to: User.Search
+
+ defp put_password_hash(
+ %Ecto.Changeset{valid?: true, changes: %{password: password}} = changeset
+ ) do
+ change(changeset, password_hash: Pbkdf2.hashpwsalt(password))
+ end
+
+ defp put_password_hash(changeset), do: changeset
end
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index c0e3d1478..55315d66e 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -189,6 +189,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end)
end
+ def stream_out_participations(%Object{data: %{"context" => context}}, user) do
+ with %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
+ conversation = Repo.preload(conversation, :participations),
+ last_activity_id =
+ fetch_latest_activity_id_for_context(conversation.ap_id, %{
+ "user" => user,
+ "blocking_user" => user
+ }) do
+ if last_activity_id do
+ stream_out_participations(conversation.participations)
+ end
+ end
+ end
+
+ def stream_out_participations(_, _), do: :noop
+
def stream_out(activity) do
public = "https://www.w3.org/ns/activitystreams#Public"
@@ -401,7 +417,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
"to" => to,
"deleted_activity_id" => activity && activity.id
},
- {:ok, activity} <- insert(data, local),
+ {:ok, activity} <- insert(data, local, false),
+ stream_out_participations(object, user),
_ <- decrease_replies_count_if_reply(object),
# Changing note count prior to enqueuing federation task in order to avoid
# race conditions on updating user.info
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
index 10ff572a2..514266cee 100644
--- a/lib/pleroma/web/activity_pub/utils.ex
+++ b/lib/pleroma/web/activity_pub/utils.ex
@@ -151,16 +151,18 @@ defmodule Pleroma.Web.ActivityPub.Utils do
def create_context(context) do
context = context || generate_id("contexts")
- changeset = Object.context_mapping(context)
- case Repo.insert(changeset) do
- {:ok, object} ->
- object
+ # Ecto has problems accessing the constraint inside the jsonb,
+ # so we explicitly check for the existed object before insert
+ object = Object.get_cached_by_ap_id(context)
- # This should be solved by an upsert, but it seems ecto
- # has problems accessing the constraint inside the jsonb.
- {:error, _} ->
- Object.get_cached_by_ap_id(context)
+ with true <- is_nil(object),
+ changeset <- Object.context_mapping(context),
+ {:ok, inserted_object} <- Repo.insert(changeset) do
+ inserted_object
+ else
+ _ ->
+ object
end
end
diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex
index 03dfdca82..953a22ea0 100644
--- a/lib/pleroma/web/admin_api/admin_api_controller.ex
+++ b/lib/pleroma/web/admin_api/admin_api_controller.ex
@@ -377,12 +377,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
if Pleroma.Config.get([:instance, :dynamic_configuration]) do
updated =
Enum.map(configs, fn
- %{"key" => key, "value" => value} ->
- {:ok, config} = Config.update_or_create(%{key: key, value: value})
+ %{"group" => group, "key" => key, "value" => value} ->
+ {:ok, config} = Config.update_or_create(%{group: group, key: key, value: value})
config
- %{"key" => key, "delete" => "true"} ->
- {:ok, _} = Config.delete(key)
+ %{"group" => group, "key" => key, "delete" => "true"} ->
+ {:ok, _} = Config.delete(%{group: group, key: key})
nil
end)
|> Enum.reject(&is_nil(&1))
diff --git a/lib/pleroma/web/admin_api/config.ex b/lib/pleroma/web/admin_api/config.ex
index b7072f050..8b9b658a9 100644
--- a/lib/pleroma/web/admin_api/config.ex
+++ b/lib/pleroma/web/admin_api/config.ex
@@ -12,26 +12,27 @@ defmodule Pleroma.Web.AdminAPI.Config do
schema "config" do
field(:key, :string)
+ field(:group, :string)
field(:value, :binary)
timestamps()
end
- @spec get_by_key(String.t()) :: Config.t() | nil
- def get_by_key(key), do: Repo.get_by(Config, key: key)
+ @spec get_by_params(map()) :: Config.t() | nil
+ def get_by_params(params), do: Repo.get_by(Config, params)
@spec changeset(Config.t(), map()) :: Changeset.t()
def changeset(config, params \\ %{}) do
config
- |> cast(params, [:key, :value])
- |> validate_required([:key, :value])
- |> unique_constraint(:key)
+ |> cast(params, [:key, :group, :value])
+ |> validate_required([:key, :group, :value])
+ |> unique_constraint(:key, name: :config_group_key_index)
end
@spec create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
- def create(%{key: key, value: value}) do
+ def create(params) do
%Config{}
- |> changeset(%{key: key, value: transform(value)})
+ |> changeset(Map.put(params, :value, transform(params[:value])))
|> Repo.insert()
end
@@ -43,20 +44,20 @@ defmodule Pleroma.Web.AdminAPI.Config do
end
@spec update_or_create(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
- def update_or_create(%{key: key} = params) do
- with %Config{} = config <- Config.get_by_key(key) do
+ def update_or_create(params) do
+ with %Config{} = config <- Config.get_by_params(Map.take(params, [:group, :key])) do
Config.update(config, params)
else
nil -> Config.create(params)
end
end
- @spec delete(String.t()) :: {:ok, Config.t()} | {:error, Changeset.t()}
- def delete(key) do
- with %Config{} = config <- Config.get_by_key(key) do
+ @spec delete(map()) :: {:ok, Config.t()} | {:error, Changeset.t()}
+ def delete(params) do
+ with %Config{} = config <- Config.get_by_params(params) do
Repo.delete(config)
else
- nil -> {:error, "Config with key #{key} not found"}
+ nil -> {:error, "Config with params #{inspect(params)} not found"}
end
end
@@ -77,10 +78,21 @@ defmodule Pleroma.Web.AdminAPI.Config do
defp do_convert({k, v} = value) when is_tuple(value),
do: %{k => do_convert(v)}
- defp do_convert(value) when is_binary(value) or is_atom(value) or is_map(value),
- do: value
+ defp do_convert(value) when is_tuple(value), do: %{"tuple" => do_convert(Tuple.to_list(value))}
+
+ defp do_convert(value) when is_binary(value) or is_map(value) or is_number(value), do: value
+
+ defp do_convert(value) when is_atom(value) do
+ string = to_string(value)
+
+ if String.starts_with?(string, "Elixir."),
+ do: String.trim_leading(string, "Elixir."),
+ else: value
+ end
@spec transform(any()) :: binary()
+ def transform(%{"tuple" => _} = entity), do: :erlang.term_to_binary(do_transform(entity))
+
def transform(entity) when is_map(entity) do
tuples =
for {k, v} <- entity,
@@ -101,11 +113,16 @@ defmodule Pleroma.Web.AdminAPI.Config do
defp do_transform(%Regex{} = value) when is_map(value), do: value
+ defp do_transform(%{"tuple" => [k, values] = entity}) when length(entity) == 2 do
+ {do_transform(k), do_transform(values)}
+ end
+
+ defp do_transform(%{"tuple" => values}) do
+ Enum.reduce(values, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
+ end
+
defp do_transform(value) when is_map(value) do
- values =
- for {key, val} <- value,
- into: [],
- do: {String.to_atom(key), do_transform(val)}
+ values = for {key, val} <- value, into: [], do: {String.to_atom(key), do_transform(val)}
Enum.sort(values)
end
@@ -117,28 +134,27 @@ defmodule Pleroma.Web.AdminAPI.Config do
defp do_transform(entity) when is_list(entity) and length(entity) == 1, do: hd(entity)
defp do_transform(value) when is_binary(value) do
- value = String.trim(value)
+ String.trim(value)
+ |> do_transform_string()
+ end
- case String.length(value) do
- 0 ->
- nil
+ defp do_transform(value), do: value
- _ ->
- cond do
- String.starts_with?(value, "Pleroma") ->
- String.to_existing_atom("Elixir." <> value)
+ defp do_transform_string(value) when byte_size(value) == 0, do: nil
- String.starts_with?(value, ":") ->
- String.replace(value, ":", "") |> String.to_existing_atom()
+ defp do_transform_string(value) do
+ cond do
+ String.starts_with?(value, "Pleroma") or String.starts_with?(value, "Phoenix") ->
+ String.to_existing_atom("Elixir." <> value)
- String.starts_with?(value, "i:") ->
- String.replace(value, "i:", "") |> String.to_integer()
+ String.starts_with?(value, ":") ->
+ String.replace(value, ":", "") |> String.to_existing_atom()
- true ->
- value
- end
+ String.starts_with?(value, "i:") ->
+ String.replace(value, "i:", "") |> String.to_integer()
+
+ true ->
+ value
end
end
-
- defp do_transform(value), do: value
end
diff --git a/lib/pleroma/web/admin_api/views/config_view.ex b/lib/pleroma/web/admin_api/views/config_view.ex
index c8560033e..3ccc9ca46 100644
--- a/lib/pleroma/web/admin_api/views/config_view.ex
+++ b/lib/pleroma/web/admin_api/views/config_view.ex
@@ -10,6 +10,7 @@ defmodule Pleroma.Web.AdminAPI.ConfigView do
def render("show.json", %{config: config}) do
%{
key: config.key,
+ group: config.group,
value: Pleroma.Web.AdminAPI.Config.from_binary_to_map(config.value)
}
end
diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex
index 42b78494d..f8df1e2ea 100644
--- a/lib/pleroma/web/common_api/common_api.ex
+++ b/lib/pleroma/web/common_api/common_api.ex
@@ -247,6 +247,7 @@ defmodule Pleroma.Web.CommonAPI do
res
else
+ {:private_to_public, true} -> {:error, "The message visibility must be direct"}
{:error, _} = e -> e
e -> {:error, e}
end
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 0c22790f2..9b9eca2a1 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -844,7 +844,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
conn
|> put_view(AccountView)
- |> render(AccountView, "accounts.json", %{for: user, users: users, as: :user})
+ |> render("accounts.json", %{for: user, users: users, as: :user})
else
_ -> json(conn, [])
end
diff --git a/lib/pleroma/web/oauth/authorization.ex b/lib/pleroma/web/oauth/authorization.ex
index 18973413e..d53e20d12 100644
--- a/lib/pleroma/web/oauth/authorization.ex
+++ b/lib/pleroma/web/oauth/authorization.ex
@@ -76,14 +76,16 @@ defmodule Pleroma.Web.OAuth.Authorization do
def use_token(%Authorization{used: true}), do: {:error, "already used"}
@spec delete_user_authorizations(User.t()) :: {integer(), any()}
- def delete_user_authorizations(%User{id: user_id}) do
- from(
- a in Pleroma.Web.OAuth.Authorization,
- where: a.user_id == ^user_id
- )
+ def delete_user_authorizations(%User{} = user) do
+ user
+ |> delete_by_user_query
|> Repo.delete_all()
end
+ def delete_by_user_query(%User{id: user_id}) do
+ from(a in __MODULE__, where: a.user_id == ^user_id)
+ end
+
@doc "gets auth for app by token"
@spec get_by_token(App.t(), String.t()) :: {:ok, t()} | {:error, :not_found}
def get_by_token(%App{id: app_id} = _app, token) do
diff --git a/lib/pleroma/web/rich_media/helpers.ex b/lib/pleroma/web/rich_media/helpers.ex
index 94f56f70d..6506de46c 100644
--- a/lib/pleroma/web/rich_media/helpers.ex
+++ b/lib/pleroma/web/rich_media/helpers.ex
@@ -4,27 +4,53 @@
defmodule Pleroma.Web.RichMedia.Helpers do
alias Pleroma.Activity
+ alias Pleroma.Config
alias Pleroma.HTML
alias Pleroma.Object
alias Pleroma.Web.RichMedia.Parser
+ @spec validate_page_url(any()) :: :ok | :error
defp validate_page_url(page_url) when is_binary(page_url) do
validate_tld = Application.get_env(:auto_linker, :opts)[:validate_tld]
- if AutoLinker.Parser.url?(page_url, scheme: true, validate_tld: validate_tld) do
- URI.parse(page_url) |> validate_page_url
- else
- :error
+ page_url
+ |> AutoLinker.Parser.url?(scheme: true, validate_tld: validate_tld)
+ |> parse_uri(page_url)
+ end
+
+ defp validate_page_url(%URI{host: host, scheme: scheme, authority: authority})
+ when scheme == "https" and not is_nil(authority) do
+ cond do
+ host in Config.get([:rich_media, :ignore_hosts], []) ->
+ :error
+
+ get_tld(host) in Config.get([:rich_media, :ignore_tld], []) ->
+ :error
+
+ true ->
+ :ok
end
end
- defp validate_page_url(%URI{authority: nil}), do: :error
- defp validate_page_url(%URI{scheme: nil}), do: :error
- defp validate_page_url(%URI{}), do: :ok
defp validate_page_url(_), do: :error
+ defp parse_uri(true, url) do
+ url
+ |> URI.parse()
+ |> validate_page_url
+ end
+
+ defp parse_uri(_, _), do: :error
+
+ defp get_tld(host) do
+ host
+ |> String.split(".")
+ |> Enum.reverse()
+ |> hd
+ end
+
def fetch_data_for_activity(%Activity{data: %{"type" => "Create"}} = activity) do
- with true <- Pleroma.Config.get([:rich_media, :enabled]),
+ with true <- Config.get([:rich_media, :enabled]),
%Object{} = object <- Object.normalize(activity),
false <- object.data["sensitive"] || false,
{:ok, page_url} <- HTML.extract_first_external_url(object, object.data["content"]),
diff --git a/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex b/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex
index 4a7c5eae0..fb79630e4 100644
--- a/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex
+++ b/lib/pleroma/web/rich_media/parsers/meta_tags_parser.ex
@@ -1,15 +1,19 @@
defmodule Pleroma.Web.RichMedia.Parsers.MetaTagsParser do
def parse(html, data, prefix, error_message, key_name, value_name \\ "content") do
- with elements = [_ | _] <- get_elements(html, key_name, prefix),
- meta_data =
- Enum.reduce(elements, data, fn el, acc ->
- attributes = normalize_attributes(el, prefix, key_name, value_name)
+ meta_data =
+ html
+ |> get_elements(key_name, prefix)
+ |> Enum.reduce(data, fn el, acc ->
+ attributes = normalize_attributes(el, prefix, key_name, value_name)
- Map.merge(acc, attributes)
- end) do
- {:ok, meta_data}
+ Map.merge(acc, attributes)
+ end)
+ |> maybe_put_title(html)
+
+ if Enum.empty?(meta_data) do
+ {:error, error_message}
else
- _e -> {:error, error_message}
+ {:ok, meta_data}
end
end
@@ -27,4 +31,19 @@ defmodule Pleroma.Web.RichMedia.Parsers.MetaTagsParser do
%{String.to_atom(data[key_name]) => data[value_name]}
end
+
+ defp maybe_put_title(%{title: _} = meta, _), do: meta
+
+ defp maybe_put_title(meta, html) when meta != %{} do
+ case get_page_title(html) do
+ "" -> meta
+ title -> Map.put_new(meta, :title, title)
+ end
+ end
+
+ defp maybe_put_title(meta, _), do: meta
+
+ defp get_page_title(html) do
+ Floki.find(html, "title") |> Floki.text()
+ end
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 837153ed4..c504116b6 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -133,8 +133,8 @@ defmodule Pleroma.Web.Router do
scope "/api/pleroma", Pleroma.Web.TwitterAPI do
pipe_through(:pleroma_api)
- get("/password_reset/:token", UtilController, :show_password_reset)
- post("/password_reset", UtilController, :password_reset)
+ get("/password_reset/:token", PasswordController, :reset, as: :reset_password)
+ post("/password_reset", PasswordController, :do_reset, as: :reset_password)
get("/emoji", UtilController, :emoji)
get("/captcha", UtilController, :captcha)
get("/healthcheck", UtilController, :healthcheck)
diff --git a/lib/pleroma/web/templates/twitter_api/util/invalid_token.html.eex b/lib/pleroma/web/templates/twitter_api/password/invalid_token.html.eex
index ee84750c7..ee84750c7 100644
--- a/lib/pleroma/web/templates/twitter_api/util/invalid_token.html.eex
+++ b/lib/pleroma/web/templates/twitter_api/password/invalid_token.html.eex
diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset.html.eex b/lib/pleroma/web/templates/twitter_api/password/reset.html.eex
index a3facf017..7d3ef6b0d 100644
--- a/lib/pleroma/web/templates/twitter_api/util/password_reset.html.eex
+++ b/lib/pleroma/web/templates/twitter_api/password/reset.html.eex
@@ -1,5 +1,5 @@
<h2>Password Reset for <%= @user.nickname %></h2>
-<%= form_for @conn, util_path(@conn, :password_reset), [as: "data"], fn f -> %>
+<%= form_for @conn, reset_password_path(@conn, :do_reset), [as: "data"], fn f -> %>
<div class="form-row">
<%= label f, :password, "Password" %>
<%= password_input f, :password %>
diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex b/lib/pleroma/web/templates/twitter_api/password/reset_failed.html.eex
index df037c01e..df037c01e 100644
--- a/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex
+++ b/lib/pleroma/web/templates/twitter_api/password/reset_failed.html.eex
diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex b/lib/pleroma/web/templates/twitter_api/password/reset_success.html.eex
index f30ba3274..f30ba3274 100644
--- a/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex
+++ b/lib/pleroma/web/templates/twitter_api/password/reset_success.html.eex
diff --git a/lib/pleroma/web/twitter_api/controllers/password_controller.ex b/lib/pleroma/web/twitter_api/controllers/password_controller.ex
new file mode 100644
index 000000000..1941e6143
--- /dev/null
+++ b/lib/pleroma/web/twitter_api/controllers/password_controller.ex
@@ -0,0 +1,37 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.TwitterAPI.PasswordController do
+ @moduledoc """
+ The module containts functions for reset password.
+ """
+
+ use Pleroma.Web, :controller
+
+ require Logger
+
+ alias Pleroma.PasswordResetToken
+ alias Pleroma.Repo
+ alias Pleroma.User
+
+ def reset(conn, %{"token" => token}) do
+ with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
+ %User{} = user <- User.get_cached_by_id(token.user_id) do
+ render(conn, "reset.html", %{
+ token: token,
+ user: user
+ })
+ else
+ _e -> render(conn, "invalid_token.html")
+ end
+ end
+
+ def do_reset(conn, %{"data" => data}) do
+ with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
+ render(conn, "reset_success.html")
+ else
+ _e -> render(conn, "reset_failed.html")
+ end
+ end
+end
diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
index 489170d80..b1863528f 100644
--- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex
+++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex
@@ -11,8 +11,6 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
alias Pleroma.Activity
alias Pleroma.Emoji
alias Pleroma.Notification
- alias Pleroma.PasswordResetToken
- alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.Web
alias Pleroma.Web.ActivityPub.ActivityPub
@@ -20,26 +18,6 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
alias Pleroma.Web.OStatus
alias Pleroma.Web.WebFinger
- def show_password_reset(conn, %{"token" => token}) do
- with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}),
- %User{} = user <- User.get_cached_by_id(token.user_id) do
- render(conn, "password_reset.html", %{
- token: token,
- user: user
- })
- else
- _e -> render(conn, "invalid_token.html")
- end
- end
-
- def password_reset(conn, %{"data" => data}) do
- with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do
- render(conn, "password_reset_success.html")
- else
- _e -> render(conn, "password_reset_failed.html")
- end
- end
-
def help_test(conn, _params) do
json(conn, "ok")
end
diff --git a/lib/pleroma/web/twitter_api/views/password_view.ex b/lib/pleroma/web/twitter_api/views/password_view.ex
new file mode 100644
index 000000000..b166b925d
--- /dev/null
+++ b/lib/pleroma/web/twitter_api/views/password_view.ex
@@ -0,0 +1,8 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.TwitterAPI.PasswordView do
+ use Pleroma.Web, :view
+ import Phoenix.HTML.Form
+end