aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorIvan Tashkinov <ivantashkinov@gmail.com>2019-08-31 14:25:43 +0300
committerIvan Tashkinov <ivantashkinov@gmail.com>2019-08-31 14:25:43 +0300
commite890ea7e821d61fca75084d46f70ed125acf1fc8 (patch)
tree7994c0be9f90b39a9ed46bbfd9f04fce70a14671 /lib
parentcd78e63a2528ab813088d5e44a026f6bb05b344b (diff)
parent6d33c89c4d27a1b52e69e1c14b408726410a6326 (diff)
downloadpleroma-e890ea7e821d61fca75084d46f70ed125acf1fc8.tar.gz
[#1149] Added Oban job for "activity_expiration". Merged remote-tracking branch 'remotes/upstream/develop' into 1149-oban-job-queue
# Conflicts: # config/config.exs
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/activity.ex3
-rw-r--r--lib/pleroma/activity/queries.ex49
-rw-r--r--lib/pleroma/activity_expiration.ex68
-rw-r--r--lib/pleroma/activity_expiration_worker.ex71
-rw-r--r--lib/pleroma/application.ex3
-rw-r--r--lib/pleroma/list.ex22
-rw-r--r--lib/pleroma/moderation_log.ex433
-rw-r--r--lib/pleroma/object.ex2
-rw-r--r--lib/pleroma/object/fetcher.ex4
-rw-r--r--lib/pleroma/signature.ex6
-rw-r--r--lib/pleroma/user.ex8
-rw-r--r--lib/pleroma/user/info.ex6
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex9
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub_controller.ex100
-rw-r--r--lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex12
-rw-r--r--lib/pleroma/web/activity_pub/publisher.ex4
-rw-r--r--lib/pleroma/web/activity_pub/relay.ex28
-rw-r--r--lib/pleroma/web/activity_pub/transmogrifier.ex6
-rw-r--r--lib/pleroma/web/activity_pub/utils.ex144
-rw-r--r--lib/pleroma/web/admin_api/admin_api_controller.ex241
-rw-r--r--lib/pleroma/web/admin_api/views/account_view.ex46
-rw-r--r--lib/pleroma/web/admin_api/views/moderation_log_view.ex26
-rw-r--r--lib/pleroma/web/common_api/common_api.ex46
-rw-r--r--lib/pleroma/web/common_api/utils.ex3
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/fallback_controller.ex34
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/list_controller.ex84
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex (renamed from lib/pleroma/web/mastodon_api/mastodon_api_controller.ex)127
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/search_controller.ex (renamed from lib/pleroma/web/mastodon_api/search_controller.ex)0
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/subscription_controller.ex (renamed from lib/pleroma/web/mastodon_api/subscription_controller.ex)4
-rw-r--r--lib/pleroma/web/mastodon_api/views/list_view.ex6
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex11
-rw-r--r--lib/pleroma/web/ostatus/ostatus_controller.ex3
-rw-r--r--lib/pleroma/web/router.ex33
-rw-r--r--lib/pleroma/workers/background_worker.ex7
34 files changed, 1294 insertions, 355 deletions
diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex
index 35612c882..2d4e9da0c 100644
--- a/lib/pleroma/activity.ex
+++ b/lib/pleroma/activity.ex
@@ -6,6 +6,7 @@ defmodule Pleroma.Activity do
use Ecto.Schema
alias Pleroma.Activity
+ alias Pleroma.ActivityExpiration
alias Pleroma.Bookmark
alias Pleroma.Notification
alias Pleroma.Object
@@ -59,6 +60,8 @@ defmodule Pleroma.Activity do
# typical case.
has_one(:object, Object, on_delete: :nothing, foreign_key: :id)
+ has_one(:expiration, ActivityExpiration, on_delete: :delete_all)
+
timestamps()
end
diff --git a/lib/pleroma/activity/queries.ex b/lib/pleroma/activity/queries.ex
new file mode 100644
index 000000000..aa5b29566
--- /dev/null
+++ b/lib/pleroma/activity/queries.ex
@@ -0,0 +1,49 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Activity.Queries do
+ @moduledoc """
+ Contains queries for Activity.
+ """
+
+ import Ecto.Query, only: [from: 2]
+
+ @type query :: Ecto.Queryable.t() | Activity.t()
+
+ alias Pleroma.Activity
+
+ @spec by_actor(query, String.t()) :: query
+ def by_actor(query \\ Activity, actor) do
+ from(
+ activity in query,
+ where: fragment("(?)->>'actor' = ?", activity.data, ^actor)
+ )
+ end
+
+ @spec by_object_id(query, String.t()) :: query
+ def by_object_id(query \\ Activity, object_id) do
+ from(activity in query,
+ where:
+ fragment(
+ "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
+ activity.data,
+ activity.data,
+ ^object_id
+ )
+ )
+ end
+
+ @spec by_type(query, String.t()) :: query
+ def by_type(query \\ Activity, activity_type) do
+ from(
+ activity in query,
+ where: fragment("(?)->>'type' = ?", activity.data, ^activity_type)
+ )
+ end
+
+ @spec limit(query, pos_integer()) :: query
+ def limit(query \\ Activity, limit) do
+ from(activity in query, limit: ^limit)
+ end
+end
diff --git a/lib/pleroma/activity_expiration.ex b/lib/pleroma/activity_expiration.ex
new file mode 100644
index 000000000..bf57abca4
--- /dev/null
+++ b/lib/pleroma/activity_expiration.ex
@@ -0,0 +1,68 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.ActivityExpiration do
+ use Ecto.Schema
+
+ alias Pleroma.Activity
+ alias Pleroma.ActivityExpiration
+ alias Pleroma.FlakeId
+ alias Pleroma.Repo
+
+ import Ecto.Changeset
+ import Ecto.Query
+
+ @type t :: %__MODULE__{}
+ @min_activity_lifetime :timer.hours(1)
+
+ schema "activity_expirations" do
+ belongs_to(:activity, Activity, type: FlakeId)
+ field(:scheduled_at, :naive_datetime)
+ end
+
+ def changeset(%ActivityExpiration{} = expiration, attrs) do
+ expiration
+ |> cast(attrs, [:scheduled_at])
+ |> validate_required([:scheduled_at])
+ |> validate_scheduled_at()
+ end
+
+ def get_by_activity_id(activity_id) do
+ ActivityExpiration
+ |> where([exp], exp.activity_id == ^activity_id)
+ |> Repo.one()
+ end
+
+ def create(%Activity{} = activity, scheduled_at) do
+ %ActivityExpiration{activity_id: activity.id}
+ |> changeset(%{scheduled_at: scheduled_at})
+ |> Repo.insert()
+ end
+
+ def due_expirations(offset \\ 0) do
+ naive_datetime =
+ NaiveDateTime.utc_now()
+ |> NaiveDateTime.add(offset, :millisecond)
+
+ ActivityExpiration
+ |> where([exp], exp.scheduled_at < ^naive_datetime)
+ |> Repo.all()
+ end
+
+ def validate_scheduled_at(changeset) do
+ validate_change(changeset, :scheduled_at, fn _, scheduled_at ->
+ if not expires_late_enough?(scheduled_at) do
+ [scheduled_at: "an ephemeral activity must live for at least one hour"]
+ else
+ []
+ end
+ end)
+ end
+
+ def expires_late_enough?(scheduled_at) do
+ now = NaiveDateTime.utc_now()
+ diff = NaiveDateTime.diff(scheduled_at, now, :millisecond)
+ diff >= @min_activity_lifetime
+ end
+end
diff --git a/lib/pleroma/activity_expiration_worker.ex b/lib/pleroma/activity_expiration_worker.ex
new file mode 100644
index 000000000..5c0c53232
--- /dev/null
+++ b/lib/pleroma/activity_expiration_worker.ex
@@ -0,0 +1,71 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.ActivityExpirationWorker do
+ alias Pleroma.Activity
+ alias Pleroma.ActivityExpiration
+ alias Pleroma.Config
+ alias Pleroma.Repo
+ alias Pleroma.User
+ alias Pleroma.Web.CommonAPI
+ alias Pleroma.Workers.BackgroundWorker
+
+ require Logger
+ use GenServer
+ import Ecto.Query
+
+ defdelegate worker_args(queue), to: Pleroma.Workers.Helper
+
+ @schedule_interval :timer.minutes(1)
+
+ def start_link(_) do
+ GenServer.start_link(__MODULE__, nil)
+ end
+
+ @impl true
+ def init(_) do
+ if Config.get([ActivityExpiration, :enabled]) do
+ schedule_next()
+ {:ok, nil}
+ else
+ :ignore
+ end
+ end
+
+ def perform(:execute, expiration_id) do
+ try do
+ expiration =
+ ActivityExpiration
+ |> where([e], e.id == ^expiration_id)
+ |> Repo.one!()
+
+ activity = Activity.get_by_id_with_object(expiration.activity_id)
+ user = User.get_by_ap_id(activity.object.data["actor"])
+ CommonAPI.delete(activity.id, user)
+ rescue
+ error ->
+ Logger.error("#{__MODULE__} Couldn't delete expired activity: #{inspect(error)}")
+ end
+ end
+
+ @impl true
+ def handle_info(:perform, state) do
+ ActivityExpiration.due_expirations(@schedule_interval)
+ |> Enum.each(fn expiration ->
+ %{
+ "op" => "activity_expiration",
+ "activity_expiration_id" => expiration.id
+ }
+ |> BackgroundWorker.new(worker_args(:activity_expiration))
+ |> Repo.insert()
+ end)
+
+ schedule_next()
+ {:noreply, state}
+ end
+
+ defp schedule_next do
+ Process.send_after(self(), :perform, @schedule_interval)
+ end
+end
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
index ce2d3ab59..7d38ed5c4 100644
--- a/lib/pleroma/application.ex
+++ b/lib/pleroma/application.ex
@@ -36,7 +36,8 @@ defmodule Pleroma.Application do
Pleroma.Emoji,
Pleroma.Captcha,
Pleroma.FlakeId,
- Pleroma.ScheduledActivityWorker
+ Pleroma.ScheduledActivityWorker,
+ Pleroma.ActivityExpirationWorker
] ++
cachex_children() ++
hackney_pool_children() ++
diff --git a/lib/pleroma/list.ex b/lib/pleroma/list.ex
index 1d320206e..c572380c2 100644
--- a/lib/pleroma/list.ex
+++ b/lib/pleroma/list.ex
@@ -109,15 +109,19 @@ defmodule Pleroma.List do
end
def create(title, %User{} = creator) do
- list = %Pleroma.List{user_id: creator.id, title: title}
-
- Repo.transaction(fn ->
- list = Repo.insert!(list)
-
- list
- |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
- |> Repo.update!()
- end)
+ changeset = title_changeset(%Pleroma.List{user_id: creator.id}, %{title: title})
+
+ if changeset.valid? do
+ Repo.transaction(fn ->
+ list = Repo.insert!(changeset)
+
+ list
+ |> change(ap_id: "#{creator.ap_id}/lists/#{list.id}")
+ |> Repo.update!()
+ end)
+ else
+ {:error, changeset}
+ end
end
def follow(%Pleroma.List{following: following} = list, %User{} = followed) do
diff --git a/lib/pleroma/moderation_log.ex b/lib/pleroma/moderation_log.ex
new file mode 100644
index 000000000..1ef6fe67a
--- /dev/null
+++ b/lib/pleroma/moderation_log.ex
@@ -0,0 +1,433 @@
+defmodule Pleroma.ModerationLog do
+ use Ecto.Schema
+
+ alias Pleroma.Activity
+ alias Pleroma.ModerationLog
+ alias Pleroma.Repo
+ alias Pleroma.User
+
+ import Ecto.Query
+
+ schema "moderation_log" do
+ field(:data, :map)
+
+ timestamps()
+ end
+
+ def get_all(page, page_size) do
+ from(q in __MODULE__,
+ order_by: [desc: q.inserted_at],
+ limit: ^page_size,
+ offset: ^((page - 1) * page_size)
+ )
+ |> Repo.all()
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ subject: %User{} = subject,
+ action: action,
+ permission: permission
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ subject: user_to_map(subject),
+ action: action,
+ permission: permission
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ action: "report_update",
+ subject: %Activity{data: %{"type" => "Flag"}} = subject
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: "report_update",
+ subject: report_to_map(subject)
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ action: "report_response",
+ subject: %Activity{} = subject,
+ text: text
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: "report_response",
+ subject: report_to_map(subject),
+ text: text
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ action: "status_update",
+ subject: %Activity{} = subject,
+ sensitive: sensitive,
+ visibility: visibility
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: "status_update",
+ subject: status_to_map(subject),
+ sensitive: sensitive,
+ visibility: visibility
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ action: "status_delete",
+ subject_id: subject_id
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: "status_delete",
+ subject_id: subject_id
+ }
+ })
+ end
+
+ @spec insert_log(%{actor: User, subject: User, action: String.t()}) ::
+ {:ok, ModerationLog} | {:error, any}
+ def insert_log(%{actor: %User{} = actor, subject: subject, action: action}) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: action,
+ subject: user_to_map(subject)
+ }
+ })
+ end
+
+ @spec insert_log(%{actor: User, subjects: [User], action: String.t()}) ::
+ {:ok, ModerationLog} | {:error, any}
+ def insert_log(%{actor: %User{} = actor, subjects: subjects, action: action}) do
+ subjects = Enum.map(subjects, &user_to_map/1)
+
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: action,
+ subjects: subjects
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ followed: %User{} = followed,
+ follower: %User{} = follower,
+ action: "follow"
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: "follow",
+ followed: user_to_map(followed),
+ follower: user_to_map(follower)
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ followed: %User{} = followed,
+ follower: %User{} = follower,
+ action: "unfollow"
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: "unfollow",
+ followed: user_to_map(followed),
+ follower: user_to_map(follower)
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ nicknames: nicknames,
+ tags: tags,
+ action: action
+ }) do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ nicknames: nicknames,
+ tags: tags,
+ action: action
+ }
+ })
+ end
+
+ def insert_log(%{
+ actor: %User{} = actor,
+ action: action,
+ target: target
+ })
+ when action in ["relay_follow", "relay_unfollow"] do
+ Repo.insert(%ModerationLog{
+ data: %{
+ actor: user_to_map(actor),
+ action: action,
+ target: target
+ }
+ })
+ end
+
+ defp user_to_map(%User{} = user) do
+ user
+ |> Map.from_struct()
+ |> Map.take([:id, :nickname])
+ |> Map.put(:type, "user")
+ end
+
+ defp report_to_map(%Activity{} = report) do
+ %{
+ type: "report",
+ id: report.id,
+ state: report.data["state"]
+ }
+ end
+
+ defp status_to_map(%Activity{} = status) do
+ %{
+ type: "status",
+ id: status.id
+ }
+ end
+
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => action,
+ "followed" => %{"nickname" => followed_nickname},
+ "follower" => %{"nickname" => follower_nickname}
+ }
+ }) do
+ "@#{actor_nickname} made @#{follower_nickname} #{action} @#{followed_nickname}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "delete",
+ "subject" => %{"nickname" => subject_nickname, "type" => "user"}
+ }
+ }) do
+ "@#{actor_nickname} deleted user @#{subject_nickname}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "create",
+ "subjects" => subjects
+ }
+ }) do
+ nicknames =
+ subjects
+ |> Enum.map(&"@#{&1["nickname"]}")
+ |> Enum.join(", ")
+
+ "@#{actor_nickname} created users: #{nicknames}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "activate",
+ "subject" => %{"nickname" => subject_nickname, "type" => "user"}
+ }
+ }) do
+ "@#{actor_nickname} activated user @#{subject_nickname}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "deactivate",
+ "subject" => %{"nickname" => subject_nickname, "type" => "user"}
+ }
+ }) do
+ "@#{actor_nickname} deactivated user @#{subject_nickname}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "nicknames" => nicknames,
+ "tags" => tags,
+ "action" => "tag"
+ }
+ }) do
+ nicknames_string =
+ nicknames
+ |> Enum.map(&"@#{&1}")
+ |> Enum.join(", ")
+
+ tags_string = tags |> Enum.join(", ")
+
+ "@#{actor_nickname} added tags: #{tags_string} to users: #{nicknames_string}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "nicknames" => nicknames,
+ "tags" => tags,
+ "action" => "untag"
+ }
+ }) do
+ nicknames_string =
+ nicknames
+ |> Enum.map(&"@#{&1}")
+ |> Enum.join(", ")
+
+ tags_string = tags |> Enum.join(", ")
+
+ "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_string}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "grant",
+ "subject" => %{"nickname" => subject_nickname},
+ "permission" => permission
+ }
+ }) do
+ "@#{actor_nickname} made @#{subject_nickname} #{permission}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "revoke",
+ "subject" => %{"nickname" => subject_nickname},
+ "permission" => permission
+ }
+ }) do
+ "@#{actor_nickname} revoked #{permission} role from @#{subject_nickname}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "relay_follow",
+ "target" => target
+ }
+ }) do
+ "@#{actor_nickname} followed relay: #{target}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "relay_unfollow",
+ "target" => target
+ }
+ }) do
+ "@#{actor_nickname} unfollowed relay: #{target}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "report_update",
+ "subject" => %{"id" => subject_id, "state" => state, "type" => "report"}
+ }
+ }) do
+ "@#{actor_nickname} updated report ##{subject_id} with '#{state}' state"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "report_response",
+ "subject" => %{"id" => subject_id, "type" => "report"},
+ "text" => text
+ }
+ }) do
+ "@#{actor_nickname} responded with '#{text}' to report ##{subject_id}"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "status_update",
+ "subject" => %{"id" => subject_id, "type" => "status"},
+ "sensitive" => nil,
+ "visibility" => visibility
+ }
+ }) do
+ "@#{actor_nickname} updated status ##{subject_id}, set visibility: '#{visibility}'"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "status_update",
+ "subject" => %{"id" => subject_id, "type" => "status"},
+ "sensitive" => sensitive,
+ "visibility" => nil
+ }
+ }) do
+ "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}'"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "status_update",
+ "subject" => %{"id" => subject_id, "type" => "status"},
+ "sensitive" => sensitive,
+ "visibility" => visibility
+ }
+ }) do
+ "@#{actor_nickname} updated status ##{subject_id}, set sensitive: '#{sensitive}', visibility: '#{
+ visibility
+ }'"
+ end
+
+ @spec get_log_entry_message(ModerationLog) :: String.t()
+ def get_log_entry_message(%ModerationLog{
+ data: %{
+ "actor" => %{"nickname" => actor_nickname},
+ "action" => "status_delete",
+ "subject_id" => subject_id
+ }
+ }) do
+ "@#{actor_nickname} deleted status ##{subject_id}"
+ end
+end
diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex
index c8d339c19..d58eb7f7d 100644
--- a/lib/pleroma/object.ex
+++ b/lib/pleroma/object.ex
@@ -150,8 +150,6 @@ defmodule Pleroma.Object do
def update_and_set_cache(changeset) do
with {:ok, object} <- Repo.update(changeset) do
set_cache(object)
- else
- e -> e
end
end
diff --git a/lib/pleroma/object/fetcher.ex b/lib/pleroma/object/fetcher.ex
index 8d79ddb1f..c1795ae0f 100644
--- a/lib/pleroma/object/fetcher.ex
+++ b/lib/pleroma/object/fetcher.ex
@@ -117,9 +117,7 @@ defmodule Pleroma.Object.Fetcher do
def fetch_and_contain_remote_object_from_id(id) when is_binary(id) do
Logger.info("Fetching object #{id} via AP")
- date =
- NaiveDateTime.utc_now()
- |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
+ date = Pleroma.Signature.signed_date()
headers =
[{:Accept, "application/activity+json"}]
diff --git a/lib/pleroma/signature.ex b/lib/pleroma/signature.ex
index 15bf3c317..f20aeb0d5 100644
--- a/lib/pleroma/signature.ex
+++ b/lib/pleroma/signature.ex
@@ -53,4 +53,10 @@ defmodule Pleroma.Signature do
HTTPSignatures.sign(private_key, user.ap_id <> "#main-key", headers)
end
end
+
+ def signed_date, do: signed_date(NaiveDateTime.utc_now())
+
+ def signed_date(%NaiveDateTime{} = date) do
+ Timex.format!(date, "{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
+ end
end
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 61a3d5911..18bba0fbb 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -333,7 +333,13 @@ defmodule Pleroma.User do
@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, user} <- autofollow_users(user),
+ {:ok, user} <- post_register_action(user) do
+ {:ok, user}
+ end
+ end
+
+ def post_register_action(%User{} = user) do
+ with {:ok, user} <- autofollow_users(user),
{:ok, user} <- set_cache(user),
{:ok, _} <- User.WelcomeMessage.post_welcome_message_to_user(user),
{:ok, _} <- try_send_confirmation_email(user) do
diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex
index 45a39924b..779bfbc18 100644
--- a/lib/pleroma/user/info.ex
+++ b/lib/pleroma/user/info.ex
@@ -49,7 +49,7 @@ defmodule Pleroma.User.Info do
field(:mascot, :map, default: nil)
field(:emoji, {:array, :map}, default: [])
field(:pleroma_settings_store, :map, default: %{})
- field(:fields, {:array, :map}, default: [])
+ field(:fields, {:array, :map}, default: nil)
field(:raw_fields, {:array, :map}, default: [])
field(:notification_settings, :map,
@@ -422,7 +422,7 @@ defmodule Pleroma.User.Info do
# ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
# For example: [{"name": "Pronoun", "value": "she/her"}, …]
- def fields(%{fields: [], source_data: %{"attachment" => attachment}}) do
+ def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
attachment
@@ -431,6 +431,8 @@ defmodule Pleroma.User.Info do
|> Enum.take(limit)
end
+ def fields(%{fields: nil}), do: []
+
def fields(%{fields: fields}), do: fields
def follow_information_update(info, params) do
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index 07652fae4..50279cca5 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -142,7 +142,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
# Splice in the child object if we have one.
activity =
- if !is_nil(object) do
+ if not is_nil(object) do
Map.put(activity, :object, object)
else
activity
@@ -336,12 +336,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end
end
- def unlike(
- %User{} = actor,
- %Object{} = object,
- activity_id \\ nil,
- local \\ true
- ) do
+ def unlike(%User{} = actor, %Object{} = object, activity_id \\ nil, local \\ true) do
with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
unlike_data <- make_unlike_data(actor, like_activity, activity_id),
{:ok, unlike_activity} <- insert(unlike_data, local),
diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
index 133a726c5..08bf1c752 100644
--- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
@@ -41,7 +41,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{:ok, user} <- User.ensure_keys_present(user) do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("user.json", %{user: user}))
else
nil -> {:error, :not_found}
@@ -53,7 +53,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
%Object{} = object <- Object.get_cached_by_ap_id(ap_id),
{_, true} <- {:public?, Visibility.is_public?(object)} do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(ObjectView.render("object.json", %{object: object}))
else
{:public?, false} ->
@@ -69,7 +69,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{page, _} = Integer.parse(page)
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(ObjectView.render("likes.json", ap_id, likes, page))
else
{:public?, false} ->
@@ -83,7 +83,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{_, true} <- {:public?, Visibility.is_public?(object)},
likes <- Utils.get_object_likes(object) do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(ObjectView.render("likes.json", ap_id, likes))
else
{:public?, false} ->
@@ -96,7 +96,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
%Activity{} = activity <- Activity.normalize(ap_id),
{_, true} <- {:public?, Visibility.is_public?(activity)} do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(ObjectView.render("object.json", %{object: activity}))
else
{:public?, false} ->
@@ -104,6 +104,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
end
end
+ # GET /relay/following
+ def following(%{assigns: %{relay: true}} = conn, _params) do
+ conn
+ |> put_resp_content_type("application/activity+json")
+ |> json(UserView.render("following.json", %{user: Relay.get_actor()}))
+ end
+
def following(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user),
@@ -112,12 +119,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{page, _} = Integer.parse(page)
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("following.json", %{user: user, page: page, for: for_user}))
else
{:show_follows, _} ->
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> send_resp(403, "")
end
end
@@ -126,11 +133,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("following.json", %{user: user, for: for_user}))
end
end
+ # GET /relay/followers
+ def followers(%{assigns: %{relay: true}} = conn, _params) do
+ conn
+ |> put_resp_content_type("application/activity+json")
+ |> json(UserView.render("followers.json", %{user: Relay.get_actor()}))
+ end
+
def followers(%{assigns: %{user: for_user}} = conn, %{"nickname" => nickname, "page" => page}) do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user),
@@ -139,12 +153,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
{page, _} = Integer.parse(page)
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("followers.json", %{user: user, page: page, for: for_user}))
else
{:show_followers, _} ->
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> send_resp(403, "")
end
end
@@ -153,7 +167,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{user, for_user} <- ensure_user_keys_present_and_maybe_refresh_for_user(user, for_user) do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("followers.json", %{user: user, for: for_user}))
end
end
@@ -162,7 +176,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{:ok, user} <- User.ensure_keys_present(user) do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("outbox.json", %{user: user, max_id: params["max_id"]}))
end
end
@@ -210,7 +224,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
defp represent_service_actor(%User{} = user, conn) do
with {:ok, user} <- User.ensure_keys_present(user) do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("user.json", %{user: user}))
else
nil -> {:error, :not_found}
@@ -231,7 +245,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("user.json", %{user: user}))
end
@@ -240,7 +254,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
def read_inbox(%{assigns: %{user: user}} = conn, %{"nickname" => nickname} = params) do
if nickname == user.nickname do
conn
- |> put_resp_header("content-type", "application/activity+json")
+ |> put_resp_content_type("application/activity+json")
|> json(UserView.render("inbox.json", %{user: user, max_id: params["max_id"]}))
else
err =
@@ -295,42 +309,42 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
end
def update_outbox(
- %{assigns: %{user: user}} = conn,
+ %{assigns: %{user: %User{nickname: nickname} = user}} = conn,
%{"nickname" => nickname} = params
) do
- if nickname == user.nickname do
- actor = user.ap_id()
+ actor = user.ap_id()
- params =
- params
- |> Map.drop(["id"])
- |> Map.put("actor", actor)
- |> Transmogrifier.fix_addressing()
-
- with {:ok, %Activity{} = activity} <- handle_user_activity(user, params) do
- conn
- |> put_status(:created)
- |> put_resp_header("location", activity.data["id"])
- |> json(activity.data)
- else
- {:error, message} ->
- conn
- |> put_status(:bad_request)
- |> json(message)
- end
- else
- err =
- dgettext("errors", "can't update outbox of %{nickname} as %{as_nickname}",
- nickname: nickname,
- as_nickname: user.nickname
- )
+ params =
+ params
+ |> Map.drop(["id"])
+ |> Map.put("actor", actor)
+ |> Transmogrifier.fix_addressing()
+ with {:ok, %Activity{} = activity} <- handle_user_activity(user, params) do
conn
- |> put_status(:forbidden)
- |> json(err)
+ |> put_status(:created)
+ |> put_resp_header("location", activity.data["id"])
+ |> json(activity.data)
+ else
+ {:error, message} ->
+ conn
+ |> put_status(:bad_request)
+ |> json(message)
end
end
+ def update_outbox(%{assigns: %{user: user}} = conn, %{"nickname" => nickname} = _) do
+ err =
+ dgettext("errors", "can't update outbox of %{nickname} as %{as_nickname}",
+ nickname: nickname,
+ as_nickname: user.nickname
+ )
+
+ conn
+ |> put_status(:forbidden)
+ |> json(err)
+ end
+
def errors(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
diff --git a/lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex b/lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex
index de1eb4aa5..b3547ecd4 100644
--- a/lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex
+++ b/lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex
@@ -25,11 +25,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
defp score_displayname(_), do: 0.0
defp determine_if_followbot(%User{nickname: nickname, name: displayname}) do
- # nickname will always be a binary string because it's generated by Pleroma.
+ # nickname will be a binary string except when following a relay
nick_score =
- nickname
- |> String.downcase()
- |> score_nickname()
+ if is_binary(nickname) do
+ nickname
+ |> String.downcase()
+ |> score_nickname()
+ else
+ 0.0
+ end
# displayname will either be a binary string or nil, if a displayname isn't set.
name_score =
diff --git a/lib/pleroma/web/activity_pub/publisher.ex b/lib/pleroma/web/activity_pub/publisher.ex
index 03deec5f4..24d101dc8 100644
--- a/lib/pleroma/web/activity_pub/publisher.ex
+++ b/lib/pleroma/web/activity_pub/publisher.ex
@@ -50,9 +50,7 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
- date =
- NaiveDateTime.utc_now()
- |> Timex.format!("{WDshort}, {0D} {Mshort} {YYYY} {h24}:{m}:{s} GMT")
+ date = Pleroma.Signature.signed_date()
signature =
Pleroma.Signature.sign(actor, %{
diff --git a/lib/pleroma/web/activity_pub/relay.ex b/lib/pleroma/web/activity_pub/relay.ex
index 5f18cc64a..c2ac38907 100644
--- a/lib/pleroma/web/activity_pub/relay.ex
+++ b/lib/pleroma/web/activity_pub/relay.ex
@@ -22,13 +22,7 @@ defmodule Pleroma.Web.ActivityPub.Relay do
Logger.info("relay: followed instance: #{target_instance}; id=#{activity.data["id"]}")
{:ok, activity}
else
- {:error, _} = error ->
- Logger.error("error: #{inspect(error)}")
- error
-
- e ->
- Logger.error("error: #{inspect(e)}")
- {:error, e}
+ error -> format_error(error)
end
end
@@ -37,16 +31,11 @@ defmodule Pleroma.Web.ActivityPub.Relay do
with %User{} = local_user <- get_actor(),
{:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_instance),
{:ok, activity} <- ActivityPub.unfollow(local_user, target_user) do
+ User.unfollow(local_user, target_user)
Logger.info("relay: unfollowed instance: #{target_instance}: id=#{activity.data["id"]}")
{:ok, activity}
else
- {:error, _} = error ->
- Logger.error("error: #{inspect(error)}")
- error
-
- e ->
- Logger.error("error: #{inspect(e)}")
- {:error, e}
+ error -> format_error(error)
end
end
@@ -56,11 +45,16 @@ defmodule Pleroma.Web.ActivityPub.Relay do
%Object{} = object <- Object.normalize(activity) do
ActivityPub.announce(user, object, nil, true, false)
else
- e ->
- Logger.error("error: #{inspect(e)}")
- {:error, inspect(e)}
+ error -> format_error(error)
end
end
def publish(_), do: {:error, "Not implemented"}
+
+ defp format_error({:error, error}), do: format_error(error)
+
+ defp format_error(error) do
+ Logger.error("error: #{inspect(error)}")
+ {:error, error}
+ end
end
diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex
index 7a03914bb..b068d28a7 100644
--- a/lib/pleroma/web/activity_pub/transmogrifier.ex
+++ b/lib/pleroma/web/activity_pub/transmogrifier.ex
@@ -467,8 +467,10 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
%{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
_options
) do
- with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
- {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
+ with %User{local: true} = followed <-
+ User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
+ {:ok, %User{} = follower} <-
+ User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
{:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
{_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
index 8502ca3be..52f4b0194 100644
--- a/lib/pleroma/web/activity_pub/utils.ex
+++ b/lib/pleroma/web/activity_pub/utils.ex
@@ -166,6 +166,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
@doc """
Enqueues an activity for federation if it's local
"""
+ @spec maybe_federate(any()) :: :ok
def maybe_federate(%Activity{local: true} = activity) do
if Pleroma.Config.get!([:instance, :federating]) do
Pleroma.Web.Federator.publish(activity)
@@ -249,46 +250,27 @@ defmodule Pleroma.Web.ActivityPub.Utils do
@doc """
Returns an existing like if a user already liked an object
"""
+ @spec get_existing_like(String.t(), map()) :: Activity.t() | nil
def get_existing_like(actor, %{data: %{"id" => id}}) do
- query =
- from(
- activity in Activity,
- where: fragment("(?)->>'actor' = ?", activity.data, ^actor),
- # this is to use the index
- where:
- fragment(
- "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
- activity.data,
- activity.data,
- ^id
- ),
- where: fragment("(?)->>'type' = 'Like'", activity.data)
- )
-
- Repo.one(query)
+ actor
+ |> Activity.Queries.by_actor()
+ |> Activity.Queries.by_object_id(id)
+ |> Activity.Queries.by_type("Like")
+ |> Activity.Queries.limit(1)
+ |> Repo.one()
end
@doc """
Returns like activities targeting an object
"""
def get_object_likes(%{data: %{"id" => id}}) do
- query =
- from(
- activity in Activity,
- # this is to use the index
- where:
- fragment(
- "coalesce((?)->'object'->>'id', (?)->>'object') = ?",
- activity.data,
- activity.data,
- ^id
- ),
- where: fragment("(?)->>'type' = 'Like'", activity.data)
- )
-
- Repo.all(query)
+ id
+ |> Activity.Queries.by_object_id()
+ |> Activity.Queries.by_type("Like")
+ |> Repo.all()
end
+ @spec make_like_data(User.t(), map(), String.t()) :: map()
def make_like_data(
%User{ap_id: ap_id} = actor,
%{data: %{"actor" => object_actor_id, "id" => id}} = object,
@@ -308,7 +290,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|> List.delete(actor.ap_id)
|> List.delete(object_actor.follower_address)
- data = %{
+ %{
"type" => "Like",
"actor" => ap_id,
"object" => id,
@@ -316,38 +298,49 @@ defmodule Pleroma.Web.ActivityPub.Utils do
"cc" => cc,
"context" => object.data["context"]
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
+ @spec update_element_in_object(String.t(), list(any), Object.t()) ::
+ {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
def update_element_in_object(property, element, object) do
- with new_data <-
- object.data
- |> Map.put("#{property}_count", length(element))
- |> Map.put("#{property}s", element),
- changeset <- Changeset.change(object, data: new_data),
- {:ok, object} <- Object.update_and_set_cache(changeset) do
- {:ok, object}
- end
- end
+ data =
+ Map.merge(
+ object.data,
+ %{"#{property}_count" => length(element), "#{property}s" => element}
+ )
- def update_likes_in_object(likes, object) do
- update_element_in_object("like", likes, object)
+ object
+ |> Changeset.change(data: data)
+ |> Object.update_and_set_cache()
end
+ @spec add_like_to_object(Activity.t(), Object.t()) ::
+ {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
- likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
-
- with likes <- [actor | likes] |> Enum.uniq() do
- update_likes_in_object(likes, object)
- end
+ [actor | fetch_likes(object)]
+ |> Enum.uniq()
+ |> update_likes_in_object(object)
end
+ @spec remove_like_from_object(Activity.t(), Object.t()) ::
+ {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
- likes = if is_list(object.data["likes"]), do: object.data["likes"], else: []
+ object
+ |> fetch_likes()
+ |> List.delete(actor)
+ |> update_likes_in_object(object)
+ end
- with likes <- likes |> List.delete(actor) do
- update_likes_in_object(likes, object)
+ defp update_likes_in_object(likes, object) do
+ update_element_in_object("like", likes, object)
+ end
+
+ defp fetch_likes(object) do
+ if is_list(object.data["likes"]) do
+ object.data["likes"]
+ else
+ []
end
end
@@ -398,7 +391,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
%User{ap_id: followed_id} = _followed,
activity_id
) do
- data = %{
+ %{
"type" => "Follow",
"actor" => follower_id,
"to" => [followed_id],
@@ -406,10 +399,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
"object" => followed_id,
"state" => "pending"
}
-
- data = if activity_id, do: Map.put(data, "id", activity_id), else: data
-
- data
+ |> maybe_put("id", activity_id)
end
def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
@@ -471,7 +461,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
activity_id,
false
) do
- data = %{
+ %{
"type" => "Announce",
"actor" => ap_id,
"object" => id,
@@ -479,8 +469,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
"cc" => [],
"context" => object.data["context"]
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
def make_announce_data(
@@ -489,7 +478,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
activity_id,
true
) do
- data = %{
+ %{
"type" => "Announce",
"actor" => ap_id,
"object" => id,
@@ -497,8 +486,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
"cc" => [Pleroma.Constants.as_public()],
"context" => object.data["context"]
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
@doc """
@@ -509,7 +497,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
%Activity{data: %{"context" => context}} = activity,
activity_id
) do
- data = %{
+ %{
"type" => "Undo",
"actor" => ap_id,
"object" => activity.data,
@@ -517,8 +505,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
"cc" => [Pleroma.Constants.as_public()],
"context" => context
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
def make_unlike_data(
@@ -526,7 +513,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
%Activity{data: %{"context" => context}} = activity,
activity_id
) do
- data = %{
+ %{
"type" => "Undo",
"actor" => ap_id,
"object" => activity.data,
@@ -534,8 +521,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
"cc" => [Pleroma.Constants.as_public()],
"context" => context
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
def add_announce_to_object(
@@ -566,14 +552,13 @@ defmodule Pleroma.Web.ActivityPub.Utils do
#### Unfollow-related helpers
def make_unfollow_data(follower, followed, follow_activity, activity_id) do
- data = %{
+ %{
"type" => "Undo",
"actor" => follower.ap_id,
"to" => [followed.ap_id],
"object" => follow_activity.data
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
#### Block-related helpers
@@ -603,25 +588,23 @@ defmodule Pleroma.Web.ActivityPub.Utils do
end
def make_block_data(blocker, blocked, activity_id) do
- data = %{
+ %{
"type" => "Block",
"actor" => blocker.ap_id,
"to" => [blocked.ap_id],
"object" => blocked.ap_id
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
def make_unblock_data(blocker, blocked, block_activity, activity_id) do
- data = %{
+ %{
"type" => "Undo",
"actor" => blocker.ap_id,
"to" => [blocked.ap_id],
"object" => block_activity.data
}
-
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ |> maybe_put("id", activity_id)
end
#### Create-related helpers
@@ -792,4 +775,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
Repo.all(query)
end
+
+ defp maybe_put(map, _key, nil), do: map
+ defp maybe_put(map, key, value), do: Map.put(map, key, value)
end
diff --git a/lib/pleroma/web/admin_api/admin_api_controller.ex b/lib/pleroma/web/admin_api/admin_api_controller.ex
index 2d3d0adc4..544b9d7d8 100644
--- a/lib/pleroma/web/admin_api/admin_api_controller.ex
+++ b/lib/pleroma/web/admin_api/admin_api_controller.ex
@@ -5,6 +5,7 @@
defmodule Pleroma.Web.AdminAPI.AdminAPIController do
use Pleroma.Web, :controller
alias Pleroma.Activity
+ alias Pleroma.ModerationLog
alias Pleroma.User
alias Pleroma.UserInviteToken
alias Pleroma.Web.ActivityPub.ActivityPub
@@ -12,6 +13,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
alias Pleroma.Web.AdminAPI.AccountView
alias Pleroma.Web.AdminAPI.Config
alias Pleroma.Web.AdminAPI.ConfigView
+ alias Pleroma.Web.AdminAPI.ModerationLogView
alias Pleroma.Web.AdminAPI.ReportView
alias Pleroma.Web.AdminAPI.Search
alias Pleroma.Web.CommonAPI
@@ -25,52 +27,113 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
action_fallback(:errors)
- def user_delete(conn, %{"nickname" => nickname}) do
- User.get_cached_by_nickname(nickname)
- |> User.delete()
+ def user_delete(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
+ user = User.get_cached_by_nickname(nickname)
+ User.delete(user)
+
+ ModerationLog.insert_log(%{
+ actor: admin,
+ subject: user,
+ action: "delete"
+ })
conn
|> json(nickname)
end
- def user_follow(conn, %{"follower" => follower_nick, "followed" => followed_nick}) do
+ def user_follow(%{assigns: %{user: admin}} = conn, %{
+ "follower" => follower_nick,
+ "followed" => followed_nick
+ }) do
with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
%User{} = followed <- User.get_cached_by_nickname(followed_nick) do
User.follow(follower, followed)
+
+ ModerationLog.insert_log(%{
+ actor: admin,
+ followed: followed,
+ follower: follower,
+ action: "follow"
+ })
end
conn
|> json("ok")
end
- def user_unfollow(conn, %{"follower" => follower_nick, "followed" => followed_nick}) do
+ def user_unfollow(%{assigns: %{user: admin}} = conn, %{
+ "follower" => follower_nick,
+ "followed" => followed_nick
+ }) do
with %User{} = follower <- User.get_cached_by_nickname(follower_nick),
%User{} = followed <- User.get_cached_by_nickname(followed_nick) do
User.unfollow(follower, followed)
+
+ ModerationLog.insert_log(%{
+ actor: admin,
+ followed: followed,
+ follower: follower,
+ action: "unfollow"
+ })
end
conn
|> json("ok")
end
- def user_create(
- conn,
- %{"nickname" => nickname, "email" => email, "password" => password}
- ) do
- user_data = %{
- nickname: nickname,
- name: nickname,
- email: email,
- password: password,
- password_confirmation: password,
- bio: "."
- }
+ def users_create(%{assigns: %{user: admin}} = conn, %{"users" => users}) do
+ changesets =
+ Enum.map(users, fn %{"nickname" => nickname, "email" => email, "password" => password} ->
+ user_data = %{
+ nickname: nickname,
+ name: nickname,
+ email: email,
+ password: password,
+ password_confirmation: password,
+ bio: "."
+ }
- changeset = User.register_changeset(%User{}, user_data, need_confirmation: false)
- {:ok, user} = User.register(changeset)
+ User.register_changeset(%User{}, user_data, need_confirmation: false)
+ end)
+ |> Enum.reduce(Ecto.Multi.new(), fn changeset, multi ->
+ Ecto.Multi.insert(multi, Ecto.UUID.generate(), changeset)
+ end)
+
+ case Pleroma.Repo.transaction(changesets) do
+ {:ok, users} ->
+ res =
+ users
+ |> Map.values()
+ |> Enum.map(fn user ->
+ {:ok, user} = User.post_register_action(user)
+
+ user
+ end)
+ |> Enum.map(&AccountView.render("created.json", %{user: &1}))
- conn
- |> json(user.nickname)
+ ModerationLog.insert_log(%{
+ actor: admin,
+ subjects: Map.values(users),
+ action: "create"
+ })
+
+ conn
+ |> json(res)
+
+ {:error, id, changeset, _} ->
+ res =
+ Enum.map(changesets.operations, fn
+ {current_id, {:changeset, _current_changeset, _}} when current_id == id ->
+ AccountView.render("create-error.json", %{changeset: changeset})
+
+ {_, {:changeset, current_changeset, _}} ->
+ AccountView.render("create-error.json", %{changeset: current_changeset})
+ end)
+
+ conn
+ |> put_status(:conflict)
+ |> json(res)
+ end
end
def user_show(conn, %{"nickname" => nickname}) do
@@ -101,23 +164,47 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end
end
- def user_toggle_activation(conn, %{"nickname" => nickname}) do
+ def user_toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
user = User.get_cached_by_nickname(nickname)
{:ok, updated_user} = User.deactivate(user, !user.info.deactivated)
+ action = if user.info.deactivated, do: "activate", else: "deactivate"
+
+ ModerationLog.insert_log(%{
+ actor: admin,
+ subject: user,
+ action: action
+ })
+
conn
|> json(AccountView.render("show.json", %{user: updated_user}))
end
- def tag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
- with {:ok, _} <- User.tag(nicknames, tags),
- do: json_response(conn, :no_content, "")
+ def tag_users(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames, "tags" => tags}) do
+ with {:ok, _} <- User.tag(nicknames, tags) do
+ ModerationLog.insert_log(%{
+ actor: admin,
+ nicknames: nicknames,
+ tags: tags,
+ action: "tag"
+ })
+
+ json_response(conn, :no_content, "")
+ end
end
- def untag_users(conn, %{"nicknames" => nicknames, "tags" => tags}) do
- with {:ok, _} <- User.untag(nicknames, tags),
- do: json_response(conn, :no_content, "")
+ def untag_users(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames, "tags" => tags}) do
+ with {:ok, _} <- User.untag(nicknames, tags) do
+ ModerationLog.insert_log(%{
+ actor: admin,
+ nicknames: nicknames,
+ tags: tags,
+ action: "untag"
+ })
+
+ json_response(conn, :no_content, "")
+ end
end
def list_users(conn, params) do
@@ -158,7 +245,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|> Enum.into(%{}, &{&1, true})
end
- def right_add(conn, %{"permission_group" => permission_group, "nickname" => nickname})
+ def right_add(%{assigns: %{user: admin}} = conn, %{
+ "permission_group" => permission_group,
+ "nickname" => nickname
+ })
when permission_group in ["moderator", "admin"] do
user = User.get_cached_by_nickname(nickname)
@@ -173,6 +263,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_embed(:info, info_cng)
+ ModerationLog.insert_log(%{
+ action: "grant",
+ actor: admin,
+ subject: user,
+ permission: permission_group
+ })
+
{:ok, _user} = User.update_and_set_cache(cng)
json(conn, info)
@@ -193,7 +290,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end
def right_delete(
- %{assigns: %{user: %User{:nickname => admin_nickname}}} = conn,
+ %{assigns: %{user: %User{:nickname => admin_nickname} = admin}} = conn,
%{
"permission_group" => permission_group,
"nickname" => nickname
@@ -217,6 +314,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
{:ok, _user} = User.update_and_set_cache(cng)
+ ModerationLog.insert_log(%{
+ action: "revoke",
+ actor: admin,
+ subject: user,
+ permission: permission_group
+ })
+
json(conn, info)
end
end
@@ -225,15 +329,33 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
render_error(conn, :not_found, "No such permission_group")
end
- def set_activation_status(conn, %{"nickname" => nickname, "status" => status}) do
+ def set_activation_status(%{assigns: %{user: admin}} = conn, %{
+ "nickname" => nickname,
+ "status" => status
+ }) do
with {:ok, status} <- Ecto.Type.cast(:boolean, status),
%User{} = user <- User.get_cached_by_nickname(nickname),
- {:ok, _} <- User.deactivate(user, !status),
- do: json_response(conn, :no_content, "")
+ {:ok, _} <- User.deactivate(user, !status) do
+ action = if(user.info.deactivated, do: "activate", else: "deactivate")
+
+ ModerationLog.insert_log(%{
+ actor: admin,
+ subject: user,
+ action: action
+ })
+
+ json_response(conn, :no_content, "")
+ end
end
- def relay_follow(conn, %{"relay_url" => target}) do
+ def relay_follow(%{assigns: %{user: admin}} = conn, %{"relay_url" => target}) do
with {:ok, _message} <- Relay.follow(target) do
+ ModerationLog.insert_log(%{
+ action: "relay_follow",
+ actor: admin,
+ target: target
+ })
+
json(conn, target)
else
_ ->
@@ -243,8 +365,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end
end
- def relay_unfollow(conn, %{"relay_url" => target}) do
+ def relay_unfollow(%{assigns: %{user: admin}} = conn, %{"relay_url" => target}) do
with {:ok, _message} <- Relay.unfollow(target) do
+ ModerationLog.insert_log(%{
+ action: "relay_unfollow",
+ actor: admin,
+ target: target
+ })
+
json(conn, target)
else
_ ->
@@ -335,8 +463,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end
end
- def report_update_state(conn, %{"id" => id, "state" => state}) do
+ def report_update_state(%{assigns: %{user: admin}} = conn, %{"id" => id, "state" => state}) do
with {:ok, report} <- CommonAPI.update_report_state(id, state) do
+ ModerationLog.insert_log(%{
+ action: "report_update",
+ actor: admin,
+ subject: report
+ })
+
conn
|> put_view(ReportView)
|> render("show.json", %{report: report})
@@ -353,6 +487,13 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
{:ok, activity} = CommonAPI.post(user, params)
+ ModerationLog.insert_log(%{
+ action: "report_response",
+ actor: user,
+ subject: activity,
+ text: params["status"]
+ })
+
conn
|> put_view(StatusView)
|> render("status.json", %{activity: activity})
@@ -365,8 +506,18 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
end
end
- def status_update(conn, %{"id" => id} = params) do
+ def status_update(%{assigns: %{user: admin}} = conn, %{"id" => id} = params) do
with {:ok, activity} <- CommonAPI.update_activity_scope(id, params) do
+ {:ok, sensitive} = Ecto.Type.cast(:boolean, params["sensitive"])
+
+ ModerationLog.insert_log(%{
+ action: "status_update",
+ actor: admin,
+ subject: activity,
+ sensitive: sensitive,
+ visibility: params["visibility"]
+ })
+
conn
|> put_view(StatusView)
|> render("status.json", %{activity: activity})
@@ -375,10 +526,26 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
def status_delete(%{assigns: %{user: user}} = conn, %{"id" => id}) do
with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
+ ModerationLog.insert_log(%{
+ action: "status_delete",
+ actor: user,
+ subject_id: id
+ })
+
json(conn, %{})
end
end
+ def list_log(conn, params) do
+ {page, page_size} = page_params(params)
+
+ log = ModerationLog.get_all(page, page_size)
+
+ conn
+ |> put_view(ModerationLogView)
+ |> render("index.json", %{log: log})
+ end
+
def migrate_to_db(conn, _params) do
Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
json(conn, %{})
diff --git a/lib/pleroma/web/admin_api/views/account_view.ex b/lib/pleroma/web/admin_api/views/account_view.ex
index 7e1b9c431..a96affd40 100644
--- a/lib/pleroma/web/admin_api/views/account_view.ex
+++ b/lib/pleroma/web/admin_api/views/account_view.ex
@@ -52,4 +52,50 @@ defmodule Pleroma.Web.AdminAPI.AccountView do
invites: render_many(invites, AccountView, "invite.json", as: :invite)
}
end
+
+ def render("created.json", %{user: user}) do
+ %{
+ type: "success",
+ code: 200,
+ data: %{
+ nickname: user.nickname,
+ email: user.email
+ }
+ }
+ end
+
+ def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
+ %{
+ type: "error",
+ code: 409,
+ error: parse_error(errors),
+ data: %{
+ nickname: Map.get(changes, :nickname),
+ email: Map.get(changes, :email)
+ }
+ }
+ end
+
+ defp parse_error([]), do: ""
+
+ defp parse_error(errors) do
+ ## when nickname is duplicate ap_id constraint error is raised
+ nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
+ email_error = Keyword.get(errors, :email)
+ password_error = Keyword.get(errors, :password)
+
+ cond do
+ nickname_error ->
+ "nickname #{elem(nickname_error, 0)}"
+
+ email_error ->
+ "email #{elem(email_error, 0)}"
+
+ password_error ->
+ "password #{elem(password_error, 0)}"
+
+ true ->
+ ""
+ end
+ end
end
diff --git a/lib/pleroma/web/admin_api/views/moderation_log_view.ex b/lib/pleroma/web/admin_api/views/moderation_log_view.ex
new file mode 100644
index 000000000..b3fc7cfe5
--- /dev/null
+++ b/lib/pleroma/web/admin_api/views/moderation_log_view.ex
@@ -0,0 +1,26 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.AdminAPI.ModerationLogView do
+ use Pleroma.Web, :view
+
+ alias Pleroma.ModerationLog
+
+ def render("index.json", %{log: log}) do
+ render_many(log, __MODULE__, "show.json", as: :log_entry)
+ end
+
+ def render("show.json", %{log_entry: log_entry}) do
+ time =
+ log_entry.inserted_at
+ |> DateTime.from_naive!("Etc/UTC")
+ |> DateTime.to_unix()
+
+ %{
+ data: log_entry.data,
+ time: time,
+ message: ModerationLog.get_log_entry_message(log_entry)
+ }
+ end
+end
diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex
index 72da46263..5faddc9f4 100644
--- a/lib/pleroma/web/common_api/common_api.ex
+++ b/lib/pleroma/web/common_api/common_api.ex
@@ -4,6 +4,7 @@
defmodule Pleroma.Web.CommonAPI do
alias Pleroma.Activity
+ alias Pleroma.ActivityExpiration
alias Pleroma.Conversation.Participation
alias Pleroma.Formatter
alias Pleroma.Object
@@ -200,6 +201,23 @@ defmodule Pleroma.Web.CommonAPI do
end
end
+ defp check_expiry_date({:ok, nil} = res), do: res
+
+ defp check_expiry_date({:ok, in_seconds}) do
+ expiry = NaiveDateTime.utc_now() |> NaiveDateTime.add(in_seconds)
+
+ if ActivityExpiration.expires_late_enough?(expiry) do
+ {:ok, expiry}
+ else
+ {:error, "Expiry date is too soon"}
+ end
+ end
+
+ defp check_expiry_date(expiry_str) do
+ Ecto.Type.cast(:integer, expiry_str)
+ |> check_expiry_date()
+ end
+
def post(user, %{"status" => status} = data) do
limit = Pleroma.Config.get([:instance, :limit])
@@ -226,6 +244,7 @@ defmodule Pleroma.Web.CommonAPI do
context <- make_context(in_reply_to, in_reply_to_conversation),
cw <- data["spoiler_text"] || "",
sensitive <- data["sensitive"] || Enum.member?(tags, {"#nsfw", "nsfw"}),
+ {:ok, expires_at} <- check_expiry_date(data["expires_in"]),
full_payload <- String.trim(status <> cw),
:ok <- validate_character_limit(full_payload, attachments, limit),
object <-
@@ -251,15 +270,24 @@ defmodule Pleroma.Web.CommonAPI do
preview? = Pleroma.Web.ControllerHelper.truthy_param?(data["preview"]) || false
direct? = visibility == "direct"
- %{
- to: to,
- actor: user,
- context: context,
- object: object,
- additional: %{"cc" => cc, "directMessage" => direct?}
- }
- |> maybe_add_list_data(user, visibility)
- |> ActivityPub.create(preview?)
+ result =
+ %{
+ to: to,
+ actor: user,
+ context: context,
+ object: object,
+ additional: %{"cc" => cc, "directMessage" => direct?}
+ }
+ |> maybe_add_list_data(user, visibility)
+ |> ActivityPub.create(preview?)
+
+ if expires_at do
+ with {:ok, activity} <- result do
+ {:ok, _} = ActivityExpiration.create(activity, expires_at)
+ end
+ end
+
+ result
else
{:private_to_public, true} ->
{:error, dgettext("errors", "The message visibility must be direct")}
diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex
index 61b96aba9..6958c7511 100644
--- a/lib/pleroma/web/common_api/utils.ex
+++ b/lib/pleroma/web/common_api/utils.ex
@@ -93,8 +93,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do
Activity.t() | nil,
String.t(),
Participation.t() | nil
- ) ::
- {list(String.t()), list(String.t())}
+ ) :: {list(String.t()), list(String.t())}
def get_to_and_cc(_, _, _, _, %Participation{} = participation) do
participation = Repo.preload(participation, :recipients)
diff --git a/lib/pleroma/web/mastodon_api/controllers/fallback_controller.ex b/lib/pleroma/web/mastodon_api/controllers/fallback_controller.ex
new file mode 100644
index 000000000..41243d5e7
--- /dev/null
+++ b/lib/pleroma/web/mastodon_api/controllers/fallback_controller.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.Web.MastodonAPI.FallbackController do
+ use Pleroma.Web, :controller
+
+ def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
+ error_message =
+ changeset
+ |> Ecto.Changeset.traverse_errors(fn {message, _opt} -> message end)
+ |> Enum.map_join(", ", fn {_k, v} -> v end)
+
+ conn
+ |> put_status(:unprocessable_entity)
+ |> json(%{error: error_message})
+ end
+
+ def call(conn, {:error, :not_found}) do
+ render_error(conn, :not_found, "Record not found")
+ end
+
+ def call(conn, {:error, error_message}) do
+ conn
+ |> put_status(:bad_request)
+ |> json(%{error: error_message})
+ end
+
+ def call(conn, _) do
+ conn
+ |> put_status(:internal_server_error)
+ |> json(dgettext("errors", "Something went wrong"))
+ end
+end
diff --git a/lib/pleroma/web/mastodon_api/controllers/list_controller.ex b/lib/pleroma/web/mastodon_api/controllers/list_controller.ex
new file mode 100644
index 000000000..2873deda8
--- /dev/null
+++ b/lib/pleroma/web/mastodon_api/controllers/list_controller.ex
@@ -0,0 +1,84 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.MastodonAPI.ListController do
+ use Pleroma.Web, :controller
+
+ alias Pleroma.User
+ alias Pleroma.Web.MastodonAPI.AccountView
+
+ plug(:list_by_id_and_user when action not in [:index, :create])
+
+ action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
+
+ # GET /api/v1/lists
+ def index(%{assigns: %{user: user}} = conn, opts) do
+ lists = Pleroma.List.for_user(user, opts)
+ render(conn, "index.json", lists: lists)
+ end
+
+ # POST /api/v1/lists
+ def create(%{assigns: %{user: user}} = conn, %{"title" => title}) do
+ with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
+ render(conn, "show.json", list: list)
+ end
+ end
+
+ # GET /api/v1/lists/:id
+ def show(%{assigns: %{list: list}} = conn, _) do
+ render(conn, "show.json", list: list)
+ end
+
+ # PUT /api/v1/lists/:id
+ def update(%{assigns: %{list: list}} = conn, %{"title" => title}) do
+ with {:ok, list} <- Pleroma.List.rename(list, title) do
+ render(conn, "show.json", list: list)
+ end
+ end
+
+ # DELETE /api/v1/lists/:id
+ def delete(%{assigns: %{list: list}} = conn, _) do
+ with {:ok, _list} <- Pleroma.List.delete(list) do
+ json(conn, %{})
+ end
+ end
+
+ # GET /api/v1/lists/:id/accounts
+ def list_accounts(%{assigns: %{user: user, list: list}} = conn, _) do
+ with {:ok, users} <- Pleroma.List.get_following(list) do
+ conn
+ |> put_view(AccountView)
+ |> render("accounts.json", for: user, users: users, as: :user)
+ end
+ end
+
+ # POST /api/v1/lists/:id/accounts
+ def add_to_list(%{assigns: %{list: list}} = conn, %{"account_ids" => account_ids}) do
+ Enum.each(account_ids, fn account_id ->
+ with %User{} = followed <- User.get_cached_by_id(account_id) do
+ Pleroma.List.follow(list, followed)
+ end
+ end)
+
+ json(conn, %{})
+ end
+
+ # DELETE /api/v1/lists/:id/accounts
+ def remove_from_list(%{assigns: %{list: list}} = conn, %{"account_ids" => account_ids}) do
+ Enum.each(account_ids, fn account_id ->
+ with %User{} = followed <- User.get_cached_by_id(account_id) do
+ Pleroma.List.unfollow(list, followed)
+ end
+ end)
+
+ json(conn, %{})
+ end
+
+ defp list_by_id_and_user(%{assigns: %{user: user}, params: %{"id" => id}} = conn, _) do
+ case Pleroma.List.get(id, user) do
+ %Pleroma.List{} = list -> assign(conn, :list, list)
+ nil -> conn |> render_error(:not_found, "List not found") |> halt()
+ end
+ end
+end
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
index 53cf95fbb..83e877c0e 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
@@ -83,7 +83,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
@local_mastodon_name "Mastodon-Local"
- action_fallback(:errors)
+ action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
def create_app(conn, params) do
scopes = Scopes.fetch_scopes(params, ["read"])
@@ -189,7 +189,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
info_cng = User.Info.profile_update(user.info, info_params)
with changeset <- User.update_changeset(user, user_params),
- changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
+ changeset <- Changeset.put_embed(changeset, :info, info_cng),
{:ok, user} <- User.update_and_set_cache(changeset) do
if original_user != user do
CommonAPI.update(user)
@@ -225,7 +225,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def update_banner(%{assigns: %{user: user}} = conn, %{"banner" => ""}) do
with new_info <- %{"banner" => %{}},
info_cng <- User.Info.profile_update(user.info, new_info),
- changeset <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
+ changeset <- Changeset.change(user) |> Changeset.put_embed(:info, info_cng),
{:ok, user} <- User.update_and_set_cache(changeset) do
CommonAPI.update(user)
@@ -237,7 +237,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}, type: :banner),
new_info <- %{"banner" => object.data},
info_cng <- User.Info.profile_update(user.info, new_info),
- changeset <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
+ changeset <- Changeset.change(user) |> Changeset.put_embed(:info, info_cng),
{:ok, user} <- User.update_and_set_cache(changeset) do
CommonAPI.update(user)
%{"url" => [%{"href" => href} | _]} = object.data
@@ -249,7 +249,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def update_background(%{assigns: %{user: user}} = conn, %{"img" => ""}) do
with new_info <- %{"background" => %{}},
info_cng <- User.Info.profile_update(user.info, new_info),
- changeset <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
+ changeset <- Changeset.change(user) |> Changeset.put_embed(:info, info_cng),
{:ok, _user} <- User.update_and_set_cache(changeset) do
json(conn, %{url: nil})
end
@@ -259,7 +259,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
with {:ok, object} <- ActivityPub.upload(params, type: :background),
new_info <- %{"background" => object.data},
info_cng <- User.Info.profile_update(user.info, new_info),
- changeset <- Ecto.Changeset.change(user) |> Ecto.Changeset.put_embed(:info, info_cng),
+ changeset <- Changeset.change(user) |> Changeset.put_embed(:info, info_cng),
{:ok, _user} <- User.update_and_set_cache(changeset) do
%{"url" => [%{"href" => href} | _]} = object.data
@@ -806,8 +806,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
user_changeset =
user
- |> Ecto.Changeset.change()
- |> Ecto.Changeset.put_embed(:info, info_changeset)
+ |> Changeset.change()
+ |> Changeset.put_embed(:info, info_changeset)
{:ok, _user} = User.update_and_set_cache(user_changeset)
@@ -1205,88 +1205,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> render("index.json", %{activities: activities, for: user, as: :activity})
end
- def get_lists(%{assigns: %{user: user}} = conn, opts) do
- lists = Pleroma.List.for_user(user, opts)
- res = ListView.render("lists.json", lists: lists)
- json(conn, res)
- end
-
- def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with %Pleroma.List{} = list <- Pleroma.List.get(id, user) do
- res = ListView.render("list.json", list: list)
- json(conn, res)
- else
- _e -> render_error(conn, :not_found, "Record not found")
- end
- end
-
def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do
lists = Pleroma.List.get_lists_account_belongs(user, account_id)
res = ListView.render("lists.json", lists: lists)
json(conn, res)
end
- def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
- {:ok, _list} <- Pleroma.List.delete(list) do
- json(conn, %{})
- else
- _e ->
- json(conn, dgettext("errors", "error"))
- end
- end
-
- def create_list(%{assigns: %{user: user}} = conn, %{"title" => title}) do
- with {:ok, %Pleroma.List{} = list} <- Pleroma.List.create(title, user) do
- res = ListView.render("list.json", list: list)
- json(conn, res)
- end
- end
-
- def add_to_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
- accounts
- |> Enum.each(fn account_id ->
- with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
- %User{} = followed <- User.get_cached_by_id(account_id) do
- Pleroma.List.follow(list, followed)
- end
- end)
-
- json(conn, %{})
- end
-
- def remove_from_list(%{assigns: %{user: user}} = conn, %{"id" => id, "account_ids" => accounts}) do
- accounts
- |> Enum.each(fn account_id ->
- with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
- %User{} = followed <- User.get_cached_by_id(account_id) do
- Pleroma.List.unfollow(list, followed)
- end
- end)
-
- json(conn, %{})
- end
-
- def list_accounts(%{assigns: %{user: user}} = conn, %{"id" => id}) do
- with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
- {:ok, users} = Pleroma.List.get_following(list) do
- conn
- |> put_view(AccountView)
- |> render("accounts.json", %{for: user, users: users, as: :user})
- end
- end
-
- def rename_list(%{assigns: %{user: user}} = conn, %{"id" => id, "title" => title}) do
- with %Pleroma.List{} = list <- Pleroma.List.get(id, user),
- {:ok, list} <- Pleroma.List.rename(list, title) do
- res = ListView.render("list.json", list: list)
- json(conn, res)
- else
- _e ->
- json(conn, dgettext("errors", "error"))
- end
- end
-
def list_timeline(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
params =
@@ -1420,8 +1344,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
info_cng = User.Info.mastodon_settings_update(user.info, settings)
- with changeset <- Ecto.Changeset.change(user),
- changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
+ with changeset <- Changeset.change(user),
+ changeset <- Changeset.put_embed(changeset, :info, info_cng),
{:ok, _user} <- User.update_and_set_cache(changeset) do
json(conn, %{})
else
@@ -1485,7 +1409,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
{:ok, app}
else
app
- |> Ecto.Changeset.change(%{scopes: scopes})
+ |> Changeset.change(%{scopes: scopes})
|> Repo.update()
end
@@ -1587,35 +1511,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
json(conn, %{})
end
- # fallback action
- #
- def errors(conn, {:error, %Changeset{} = changeset}) do
- error_message =
- changeset
- |> Changeset.traverse_errors(fn {message, _opt} -> message end)
- |> Enum.map_join(", ", fn {_k, v} -> v end)
-
- conn
- |> put_status(:unprocessable_entity)
- |> json(%{error: error_message})
- end
-
- def errors(conn, {:error, :not_found}) do
- render_error(conn, :not_found, "Record not found")
- end
-
- def errors(conn, {:error, error_message}) do
- conn
- |> put_status(:bad_request)
- |> json(%{error: error_message})
- end
-
- def errors(conn, _) do
- conn
- |> put_status(:internal_server_error)
- |> json(dgettext("errors", "Something went wrong"))
- end
-
def suggestions(%{assigns: %{user: user}} = conn, _) do
suggestions = Config.get(:suggestions)
diff --git a/lib/pleroma/web/mastodon_api/search_controller.ex b/lib/pleroma/web/mastodon_api/controllers/search_controller.ex
index 9072aa7a4..9072aa7a4 100644
--- a/lib/pleroma/web/mastodon_api/search_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/search_controller.ex
diff --git a/lib/pleroma/web/mastodon_api/subscription_controller.ex b/lib/pleroma/web/mastodon_api/controllers/subscription_controller.ex
index 255ee2f18..e2b17aab1 100644
--- a/lib/pleroma/web/mastodon_api/subscription_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/subscription_controller.ex
@@ -64,8 +64,6 @@ defmodule Pleroma.Web.MastodonAPI.SubscriptionController do
end
def errors(conn, _) do
- conn
- |> put_status(:internal_server_error)
- |> json(dgettext("errors", "Something went wrong"))
+ Pleroma.Web.MastodonAPI.FallbackController.call(conn, nil)
end
end
diff --git a/lib/pleroma/web/mastodon_api/views/list_view.ex b/lib/pleroma/web/mastodon_api/views/list_view.ex
index 0f86e2512..bfda6f5b3 100644
--- a/lib/pleroma/web/mastodon_api/views/list_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/list_view.ex
@@ -6,11 +6,11 @@ defmodule Pleroma.Web.MastodonAPI.ListView do
use Pleroma.Web, :view
alias Pleroma.Web.MastodonAPI.ListView
- def render("lists.json", %{lists: lists} = opts) do
- render_many(lists, ListView, "list.json", opts)
+ def render("index.json", %{lists: lists} = opts) do
+ render_many(lists, ListView, "show.json", opts)
end
- def render("list.json", %{list: list}) do
+ def render("show.json", %{list: list}) do
%{
id: to_string(list.id),
title: list.title
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index 42fbdf51b..a4ee0b5dd 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -8,6 +8,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
require Pleroma.Constants
alias Pleroma.Activity
+ alias Pleroma.ActivityExpiration
alias Pleroma.Conversation
alias Pleroma.Conversation.Participation
alias Pleroma.HTML
@@ -177,6 +178,15 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
bookmarked = Activity.get_bookmark(activity, opts[:for]) != nil
+ client_posted_this_activity = opts[:for] && user.id == opts[:for].id
+
+ expires_at =
+ with true <- client_posted_this_activity,
+ expiration when not is_nil(expiration) <-
+ ActivityExpiration.get_by_activity_id(activity.id) do
+ expiration.scheduled_at
+ end
+
thread_muted? =
case activity.thread_muted? do
thread_muted? when is_boolean(thread_muted?) -> thread_muted?
@@ -288,6 +298,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
in_reply_to_account_acct: reply_to_user && reply_to_user.nickname,
content: %{"text/plain" => content_plaintext},
spoiler_text: %{"text/plain" => summary_plaintext},
+ expires_at: expires_at,
direct_conversation_id: direct_conversation_id
}
}
diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex
index fdba0f77f..07e2a4c2d 100644
--- a/lib/pleroma/web/ostatus/ostatus_controller.ex
+++ b/lib/pleroma/web/ostatus/ostatus_controller.ex
@@ -37,8 +37,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
action_fallback(:errors)
def feed_redirect(%{assigns: %{format: "html"}} = conn, %{"nickname" => nickname}) do
- with {_, %User{} = user} <-
- {:fetch_user, User.get_cached_by_nickname_or_id(nickname)} do
+ with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname_or_id(nickname)} do
RedirectController.redirector_with_meta(conn, %{user: user})
end
end
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 1eb6f7b9d..969dc66fd 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -133,6 +133,10 @@ defmodule Pleroma.Web.Router do
})
end
+ pipeline :http_signature do
+ plug(Pleroma.Web.Plugs.HTTPSignaturePlug)
+ end
+
scope "/api/pleroma", Pleroma.Web.TwitterAPI do
pipe_through(:pleroma_api)
@@ -155,7 +159,7 @@ defmodule Pleroma.Web.Router do
post("/users/unfollow", AdminAPIController, :user_unfollow)
delete("/users", AdminAPIController, :user_delete)
- post("/users", AdminAPIController, :user_create)
+ post("/users", AdminAPIController, :users_create)
patch("/users/:nickname/toggle_activation", AdminAPIController, :user_toggle_activation)
put("/users/tag", AdminAPIController, :tag_users)
delete("/users/tag", AdminAPIController, :untag_users)
@@ -198,6 +202,8 @@ defmodule Pleroma.Web.Router do
post("/config", AdminAPIController, :config_update)
get("/config/migrate_to_db", AdminAPIController, :migrate_to_db)
get("/config/migrate_from_db", AdminAPIController, :migrate_from_db)
+
+ get("/moderation_log", AdminAPIController, :list_log)
end
scope "/", Pleroma.Web.TwitterAPI do
@@ -306,9 +312,9 @@ defmodule Pleroma.Web.Router do
get("/scheduled_statuses", MastodonAPIController, :scheduled_statuses)
get("/scheduled_statuses/:id", MastodonAPIController, :show_scheduled_status)
- get("/lists", MastodonAPIController, :get_lists)
- get("/lists/:id", MastodonAPIController, :get_list)
- get("/lists/:id/accounts", MastodonAPIController, :list_accounts)
+ get("/lists", ListController, :index)
+ get("/lists/:id", ListController, :show)
+ get("/lists/:id/accounts", ListController, :list_accounts)
get("/domain_blocks", MastodonAPIController, :domain_blocks)
@@ -349,12 +355,12 @@ defmodule Pleroma.Web.Router do
post("/media", MastodonAPIController, :upload)
put("/media/:id", MastodonAPIController, :update_media)
- delete("/lists/:id", MastodonAPIController, :delete_list)
- post("/lists", MastodonAPIController, :create_list)
- put("/lists/:id", MastodonAPIController, :rename_list)
+ delete("/lists/:id", ListController, :delete)
+ post("/lists", ListController, :create)
+ put("/lists/:id", ListController, :update)
- post("/lists/:id/accounts", MastodonAPIController, :add_to_list)
- delete("/lists/:id/accounts", MastodonAPIController, :remove_from_list)
+ post("/lists/:id/accounts", ListController, :add_to_list)
+ delete("/lists/:id/accounts", ListController, :remove_from_list)
post("/filters", MastodonAPIController, :create_filter)
get("/filters/:id", MastodonAPIController, :get_filter)
@@ -686,7 +692,14 @@ defmodule Pleroma.Web.Router do
pipe_through(:ap_service_actor)
get("/", ActivityPubController, :relay)
- post("/inbox", ActivityPubController, :inbox)
+
+ scope [] do
+ pipe_through(:http_signature)
+ post("/inbox", ActivityPubController, :inbox)
+ end
+
+ get("/following", ActivityPubController, :following, assigns: %{relay: true})
+ get("/followers", ActivityPubController, :followers, assigns: %{relay: true})
end
scope "/internal/fetch", Pleroma.Web.ActivityPub do
diff --git a/lib/pleroma/workers/background_worker.ex b/lib/pleroma/workers/background_worker.ex
index 3c021b9b4..fbce7d789 100644
--- a/lib/pleroma/workers/background_worker.ex
+++ b/lib/pleroma/workers/background_worker.ex
@@ -69,4 +69,11 @@ defmodule Pleroma.Workers.BackgroundWorker do
activity = Activity.get_by_id(activity_id)
Pleroma.Web.RichMedia.Helpers.perform(:fetch, activity)
end
+
+ def perform(
+ %{"op" => "activity_expiration", "activity_expiration_id" => activity_expiration_id},
+ _job
+ ) do
+ Pleroma.ActivityExpirationWorker.perform(:execute, activity_expiration_id)
+ end
end