aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorsquidboi <squidboi@waifu.club>2018-06-16 15:37:16 -0700
committersquidboi <squidboi@waifu.club>2018-06-16 15:37:16 -0700
commit2e294ee44a1baa7c0d3ac6b2905a70ed4e05cffb (patch)
tree740d660abc0ca455368144abc89c8a398e85caf0 /lib
parent4f9ecfc77a54eef23741c89206b4cbce924f7d76 (diff)
parent1ea4a18ad859600841860cdd1a981da868aa18a0 (diff)
downloadpleroma-2e294ee44a1baa7c0d3ac6b2905a70ed4e05cffb.tar.gz
Merge branch 'develop' into feature/configurable-blocks
Diffstat (limited to 'lib')
-rw-r--r--lib/mix/tasks/make_moderator.ex4
-rw-r--r--lib/mix/tasks/sample_config.eex3
-rw-r--r--lib/mix/tasks/set_locked.ex30
-rw-r--r--lib/pleroma/list.ex15
-rw-r--r--lib/pleroma/upload.ex136
-rw-r--r--lib/pleroma/user.ex72
-rw-r--r--lib/pleroma/web/activity_pub/activity_pub.ex10
-rw-r--r--lib/pleroma/web/activity_pub/mrf/reject_non_public.ex26
-rw-r--r--lib/pleroma/web/activity_pub/transmogrifier.ex89
-rw-r--r--lib/pleroma/web/activity_pub/utils.ex25
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_api_controller.ex78
-rw-r--r--lib/pleroma/web/mastodon_api/mastodon_socket.ex7
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex4
-rw-r--r--lib/pleroma/web/oauth/oauth_controller.ex16
-rw-r--r--lib/pleroma/web/router.ex17
-rw-r--r--lib/pleroma/web/streamer.ex58
-rw-r--r--lib/pleroma/web/twitter_api/twitter_api.ex16
-rw-r--r--lib/pleroma/web/twitter_api/twitter_api_controller.ex70
-rw-r--r--lib/pleroma/web/twitter_api/views/user_view.ex3
-rw-r--r--lib/pleroma/web/web_finger/web_finger.ex30
20 files changed, 598 insertions, 111 deletions
diff --git a/lib/mix/tasks/make_moderator.ex b/lib/mix/tasks/make_moderator.ex
index 20f04c54c..a454a958e 100644
--- a/lib/mix/tasks/make_moderator.ex
+++ b/lib/mix/tasks/make_moderator.ex
@@ -5,7 +5,7 @@ defmodule Mix.Tasks.SetModerator do
@shortdoc "Set moderator status"
def run([nickname | rest]) do
- ensure_started(Repo, [])
+ Application.ensure_all_started(:pleroma)
moderator =
case rest do
@@ -19,7 +19,7 @@ defmodule Mix.Tasks.SetModerator do
|> Map.put("is_moderator", !!moderator)
cng = User.info_changeset(user, %{info: info})
- user = Repo.update!(cng)
+ {:ok, user} = User.update_and_set_cache(cng)
IO.puts("Moderator status of #{nickname}: #{user.info["is_moderator"]}")
else
diff --git a/lib/mix/tasks/sample_config.eex b/lib/mix/tasks/sample_config.eex
index e37c864c0..6db36fa09 100644
--- a/lib/mix/tasks/sample_config.eex
+++ b/lib/mix/tasks/sample_config.eex
@@ -8,7 +8,8 @@ config :pleroma, :instance,
name: "<%= name %>",
email: "<%= email %>",
limit: 5000,
- registrations_open: true
+ registrations_open: true,
+ dedupe_media: false
config :pleroma, :media_proxy,
enabled: false,
diff --git a/lib/mix/tasks/set_locked.ex b/lib/mix/tasks/set_locked.ex
new file mode 100644
index 000000000..2b3b18b09
--- /dev/null
+++ b/lib/mix/tasks/set_locked.ex
@@ -0,0 +1,30 @@
+defmodule Mix.Tasks.SetLocked do
+ use Mix.Task
+ import Mix.Ecto
+ alias Pleroma.{Repo, User}
+
+ @shortdoc "Set locked status"
+ def run([nickname | rest]) do
+ ensure_started(Repo, [])
+
+ locked =
+ case rest do
+ [locked] -> locked == "true"
+ _ -> true
+ end
+
+ with %User{local: true} = user <- User.get_by_nickname(nickname) do
+ info =
+ user.info
+ |> Map.put("locked", !!locked)
+
+ cng = User.info_changeset(user, %{info: info})
+ user = Repo.update!(cng)
+
+ IO.puts("locked status of #{nickname}: #{user.info["locked"]}")
+ else
+ _ ->
+ IO.puts("No local user #{nickname}")
+ end
+ end
+end
diff --git a/lib/pleroma/list.ex b/lib/pleroma/list.ex
index 9d0b9285b..53d98665b 100644
--- a/lib/pleroma/list.ex
+++ b/lib/pleroma/list.ex
@@ -1,7 +1,7 @@
defmodule Pleroma.List do
use Ecto.Schema
import Ecto.{Changeset, Query}
- alias Pleroma.{User, Repo}
+ alias Pleroma.{User, Repo, Activity}
schema "lists" do
belongs_to(:user, Pleroma.User)
@@ -56,6 +56,19 @@ defmodule Pleroma.List do
{:ok, Repo.all(q)}
end
+ # Get lists the activity should be streamed to.
+ def get_lists_from_activity(%Activity{actor: ap_id}) do
+ actor = User.get_cached_by_ap_id(ap_id)
+
+ query =
+ from(
+ l in Pleroma.List,
+ where: fragment("? && ?", l.following, ^[actor.follower_address])
+ )
+
+ Repo.all(query)
+ end
+
def rename(%Pleroma.List{} = list, title) do
list
|> title_changeset(%{title: title})
diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex
index e5df94009..43df0d418 100644
--- a/lib/pleroma/upload.ex
+++ b/lib/pleroma/upload.ex
@@ -2,20 +2,21 @@ defmodule Pleroma.Upload do
alias Ecto.UUID
alias Pleroma.Web
- def store(%Plug.Upload{} = file) do
- uuid = UUID.generate()
- upload_folder = Path.join(upload_path(), uuid)
+ def store(%Plug.Upload{} = file, should_dedupe) do
+ content_type = get_content_type(file.path)
+ uuid = get_uuid(file, should_dedupe)
+ name = get_name(file, uuid, content_type, should_dedupe)
+ upload_folder = get_upload_path(uuid, should_dedupe)
+ url_path = get_url(name, uuid, should_dedupe)
+
File.mkdir_p!(upload_folder)
- result_file = Path.join(upload_folder, file.filename)
- File.cp!(file.path, result_file)
+ result_file = Path.join(upload_folder, name)
- # fix content type on some image uploads
- content_type =
- if file.content_type in [nil, "application/octet-stream"] do
- get_content_type(file.path)
- else
- file.content_type
- end
+ if File.exists?(result_file) do
+ File.rm!(file.path)
+ else
+ File.cp!(file.path, result_file)
+ end
%{
"type" => "Image",
@@ -23,26 +24,48 @@ defmodule Pleroma.Upload do
%{
"type" => "Link",
"mediaType" => content_type,
- "href" => url_for(Path.join(uuid, :cow_uri.urlencode(file.filename)))
+ "href" => url_path
}
],
- "name" => file.filename,
- "uuid" => uuid
+ "name" => name
}
end
- def store(%{"img" => "data:image/" <> image_data}) do
+ def store(%{"img" => "data:image/" <> image_data}, should_dedupe) do
parsed = Regex.named_captures(~r/(?<filetype>jpeg|png|gif);base64,(?<data>.*)/, image_data)
- data = Base.decode64!(parsed["data"])
+ data = Base.decode64!(parsed["data"], ignore: :whitespace)
uuid = UUID.generate()
- upload_folder = Path.join(upload_path(), uuid)
- File.mkdir_p!(upload_folder)
- filename = Base.encode16(:crypto.hash(:sha256, data)) <> ".#{parsed["filetype"]}"
- result_file = Path.join(upload_folder, filename)
+ uuidpath = Path.join(upload_path(), uuid)
+ uuid = UUID.generate()
+
+ File.mkdir_p!(upload_path())
+
+ File.write!(uuidpath, data)
+
+ content_type = get_content_type(uuidpath)
- File.write!(result_file, data)
+ name =
+ create_name(
+ String.downcase(Base.encode16(:crypto.hash(:sha256, data))),
+ parsed["filetype"],
+ content_type
+ )
- content_type = "image/#{parsed["filetype"]}"
+ upload_folder = get_upload_path(uuid, should_dedupe)
+ url_path = get_url(name, uuid, should_dedupe)
+
+ File.mkdir_p!(upload_folder)
+ result_file = Path.join(upload_folder, name)
+
+ if should_dedupe do
+ if !File.exists?(result_file) do
+ File.rename(uuidpath, result_file)
+ else
+ File.rm!(uuidpath)
+ end
+ else
+ File.rename(uuidpath, result_file)
+ end
%{
"type" => "Image",
@@ -50,11 +73,10 @@ defmodule Pleroma.Upload do
%{
"type" => "Link",
"mediaType" => content_type,
- "href" => url_for(Path.join(uuid, :cow_uri.urlencode(filename)))
+ "href" => url_path
}
],
- "name" => filename,
- "uuid" => uuid
+ "name" => name
}
end
@@ -63,6 +85,65 @@ defmodule Pleroma.Upload do
Keyword.fetch!(settings, :uploads)
end
+ defp create_name(uuid, ext, type) do
+ case type do
+ "application/octet-stream" ->
+ String.downcase(Enum.join([uuid, ext], "."))
+
+ "audio/mpeg" ->
+ String.downcase(Enum.join([uuid, "mp3"], "."))
+
+ _ ->
+ String.downcase(Enum.join([uuid, List.last(String.split(type, "/"))], "."))
+ end
+ end
+
+ defp get_uuid(file, should_dedupe) do
+ if should_dedupe do
+ Base.encode16(:crypto.hash(:sha256, File.read!(file.path)))
+ else
+ UUID.generate()
+ end
+ end
+
+ defp get_name(file, uuid, type, should_dedupe) do
+ if should_dedupe do
+ create_name(uuid, List.last(String.split(file.filename, ".")), type)
+ else
+ unless String.contains?(file.filename, ".") do
+ case type do
+ "image/png" -> file.filename <> ".png"
+ "image/jpeg" -> file.filename <> ".jpg"
+ "image/gif" -> file.filename <> ".gif"
+ "video/webm" -> file.filename <> ".webm"
+ "video/mp4" -> file.filename <> ".mp4"
+ "audio/mpeg" -> file.filename <> ".mp3"
+ "audio/ogg" -> file.filename <> ".ogg"
+ "audio/wav" -> file.filename <> ".wav"
+ _ -> file.filename
+ end
+ else
+ file.filename
+ end
+ end
+ end
+
+ defp get_upload_path(uuid, should_dedupe) do
+ if should_dedupe do
+ upload_path()
+ else
+ Path.join(upload_path(), uuid)
+ end
+ end
+
+ defp get_url(name, uuid, should_dedupe) do
+ if should_dedupe do
+ url_for(:cow_uri.urlencode(name))
+ else
+ url_for(Path.join(uuid, :cow_uri.urlencode(name)))
+ end
+ end
+
defp url_for(file) do
"#{Web.base_url()}/media/#{file}"
end
@@ -89,6 +170,9 @@ defmodule Pleroma.Upload do
<<0x49, 0x44, 0x33, _, _, _, _, _>> ->
"audio/mpeg"
+ <<255, 251, _, 68, 0, 0, 0, 0>> ->
+ "audio/mpeg"
+
<<0x4F, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> ->
"audio/ogg"
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index dd645b2e5..1fcec479f 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -201,6 +201,14 @@ defmodule Pleroma.User do
end
end
+ def maybe_follow(%User{} = follower, %User{info: info} = followed) do
+ if not following?(follower, followed) do
+ follow(follower, followed)
+ else
+ {:ok, follower}
+ end
+ end
+
@user_config Application.get_env(:pleroma, :user)
@deny_follow_blocked Keyword.get(@user_config, :deny_follow_blocked)
@@ -259,6 +267,10 @@ defmodule Pleroma.User do
Enum.member?(follower.following, followed.follower_address)
end
+ def locked?(%User{} = user) do
+ user.info["locked"] || false
+ end
+
def get_by_ap_id(ap_id) do
Repo.get_by(User, ap_id: ap_id)
end
@@ -356,6 +368,40 @@ defmodule Pleroma.User do
{:ok, Repo.all(q)}
end
+ def get_follow_requests_query(%User{} = user) do
+ from(
+ a in Activity,
+ where:
+ fragment(
+ "? ->> 'type' = 'Follow'",
+ a.data
+ ),
+ where:
+ fragment(
+ "? ->> 'state' = 'pending'",
+ a.data
+ ),
+ where:
+ fragment(
+ "? @> ?",
+ a.data,
+ ^%{"object" => user.ap_id}
+ )
+ )
+ end
+
+ def get_follow_requests(%User{} = user) do
+ q = get_follow_requests_query(user)
+ reqs = Repo.all(q)
+
+ users =
+ Enum.map(reqs, fn req -> req.actor end)
+ |> Enum.uniq()
+ |> Enum.map(fn ap_id -> get_by_ap_id(ap_id) end)
+
+ {:ok, users}
+ end
+
def increase_note_count(%User{} = user) do
note_count = (user.info["note_count"] || 0) + 1
new_info = Map.put(user.info, "note_count", note_count)
@@ -486,7 +532,31 @@ defmodule Pleroma.User do
def blocks?(user, %{ap_id: ap_id}) do
blocks = user.info["blocks"] || []
- Enum.member?(blocks, ap_id)
+ domain_blocks = user.info["domain_blocks"] || []
+ %{host: host} = URI.parse(ap_id)
+
+ Enum.member?(blocks, ap_id) ||
+ Enum.any?(domain_blocks, fn domain ->
+ host == domain
+ end)
+ end
+
+ def block_domain(user, domain) do
+ domain_blocks = user.info["domain_blocks"] || []
+ new_blocks = Enum.uniq([domain | domain_blocks])
+ new_info = Map.put(user.info, "domain_blocks", new_blocks)
+
+ cs = User.info_changeset(user, %{info: new_info})
+ update_and_set_cache(cs)
+ end
+
+ def unblock_domain(user, domain) do
+ blocks = user.info["domain_blocks"] || []
+ new_blocks = List.delete(blocks, domain)
+ new_info = Map.put(user.info, "domain_blocks", new_blocks)
+
+ cs = User.info_changeset(user, %{info: new_info})
+ update_and_set_cache(cs)
end
def local_user_query() do
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex
index a12bd5b58..b174af7ce 100644
--- a/lib/pleroma/web/activity_pub/activity_pub.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub.ex
@@ -57,6 +57,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
if activity.data["type"] in ["Create", "Announce"] do
Pleroma.Web.Streamer.stream("user", activity)
+ Pleroma.Web.Streamer.stream("list", activity)
if Enum.member?(activity.data["to"], public) do
Pleroma.Web.Streamer.stream("public", activity)
@@ -198,7 +199,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
:ok <- maybe_federate(unannounce_activity),
{:ok, _activity} <- Repo.delete(announce_activity),
{:ok, object} <- remove_announce_from_object(announce_activity, object) do
- {:ok, unannounce_activity, announce_activity, object}
+ {:ok, unannounce_activity, object}
else
_e -> {:ok, object}
end
@@ -214,6 +215,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
+ {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
{:ok, activity} <- insert(unfollow_data, local),
:ok <- maybe_federate(activity) do
@@ -449,11 +451,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
blocks = info["blocks"] || []
+ domain_blocks = info["domain_blocks"] || []
from(
activity in query,
where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
- where: fragment("not (?->'to' \\?| ?)", activity.data, ^blocks)
+ where: fragment("not (?->'to' \\?| ?)", activity.data, ^blocks),
+ where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks)
)
end
@@ -502,7 +506,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end
def upload(file) do
- data = Upload.store(file)
+ data = Upload.store(file, Application.get_env(:pleroma, :instance)[:dedupe_media])
Repo.insert(%Object{data: data})
end
diff --git a/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex b/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex
index 879cbe6de..b6936fe90 100644
--- a/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex
+++ b/lib/pleroma/web/activity_pub/mrf/reject_non_public.ex
@@ -2,6 +2,10 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublic do
alias Pleroma.User
@behaviour Pleroma.Web.ActivityPub.MRF
+ @mrf_rejectnonpublic Application.get_env(:pleroma, :mrf_rejectnonpublic)
+ @allow_followersonly Keyword.get(@mrf_rejectnonpublic, :allow_followersonly)
+ @allow_direct Keyword.get(@mrf_rejectnonpublic, :allow_direct)
+
@impl true
def filter(object) do
if object["type"] == "Create" do
@@ -18,9 +22,25 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublic do
end
case visibility do
- "public" -> {:ok, object}
- "unlisted" -> {:ok, object}
- _ -> {:reject, nil}
+ "public" ->
+ {:ok, object}
+
+ "unlisted" ->
+ {:ok, object}
+
+ "followers" ->
+ with true <- @allow_followersonly do
+ {:ok, object}
+ else
+ _e -> {:reject, nil}
+ end
+
+ "direct" ->
+ with true <- @allow_direct do
+ {:ok, object}
+ else
+ _e -> {:reject, nil}
+ end
end
else
{:ok, object}
diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex
index 75ba36729..300e0fcdd 100644
--- a/lib/pleroma/web/activity_pub/transmogrifier.ex
+++ b/lib/pleroma/web/activity_pub/transmogrifier.ex
@@ -30,14 +30,19 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
when not is_nil(in_reply_to_id) do
case ActivityPub.fetch_object_from_id(in_reply_to_id) do
{:ok, replied_object} ->
- activity = Activity.get_create_activity_by_object_ap_id(replied_object.data["id"])
-
- object
- |> Map.put("inReplyTo", replied_object.data["id"])
- |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
- |> Map.put("inReplyToStatusId", activity.id)
- |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
- |> Map.put("context", replied_object.data["context"] || object["conversation"])
+ with %Activity{} = activity <-
+ Activity.get_create_activity_by_object_ap_id(replied_object.data["id"]) do
+ object
+ |> Map.put("inReplyTo", replied_object.data["id"])
+ |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
+ |> Map.put("inReplyToStatusId", activity.id)
+ |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
+ |> Map.put("context", replied_object.data["context"] || object["conversation"])
+ else
+ e ->
+ Logger.error("Couldn't fetch #{object["inReplyTo"]} #{inspect(e)}")
+ object
+ end
e ->
Logger.error("Couldn't fetch #{object["inReplyTo"]} #{inspect(e)}")
@@ -137,9 +142,17 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
%User{} = follower <- User.get_or_fetch_by_ap_id(follower),
{:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
- ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
+ if not User.locked?(followed) do
+ ActivityPub.accept(%{
+ to: [follower.ap_id],
+ actor: followed.ap_id,
+ object: data,
+ local: true
+ })
+
+ User.follow(follower, followed)
+ end
- User.follow(follower, followed)
{:ok, activity}
else
_e -> :error
@@ -252,7 +265,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
{:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
banner = new_user_data[:info]["banner"]
- locked = new_user_data[:info]["locked"]
+ locked = new_user_data[:info]["locked"] || false
update_data =
new_user_data
@@ -304,7 +317,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
{:ok, object} <-
get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
- {:ok, activity, _, _} <- ActivityPub.unannounce(actor, object, id, false) do
+ {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
{:ok, activity}
else
_e -> :error
@@ -432,6 +445,58 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
{:ok, data}
end
+ # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
+ # because of course it does.
+ def prepare_outgoing(%{"type" => "Accept"} = data) do
+ follow_activity_id =
+ if is_binary(data["object"]) do
+ data["object"]
+ else
+ data["object"]["id"]
+ end
+
+ with follow_activity <- Activity.get_by_ap_id(follow_activity_id) do
+ object = %{
+ "actor" => follow_activity.actor,
+ "object" => follow_activity.data["object"],
+ "id" => follow_activity.data["id"],
+ "type" => "Follow"
+ }
+
+ data =
+ data
+ |> Map.put("object", object)
+ |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
+
+ {:ok, data}
+ end
+ end
+
+ def prepare_outgoing(%{"type" => "Reject"} = data) do
+ follow_activity_id =
+ if is_binary(data["object"]) do
+ data["object"]
+ else
+ data["object"]["id"]
+ end
+
+ with follow_activity <- Activity.get_by_ap_id(follow_activity_id) do
+ object = %{
+ "actor" => follow_activity.actor,
+ "object" => follow_activity.data["object"],
+ "id" => follow_activity.data["id"],
+ "type" => "Follow"
+ }
+
+ data =
+ data
+ |> Map.put("object", object)
+ |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
+
+ {:ok, data}
+ end
+ end
+
def prepare_outgoing(%{"type" => _type} = data) do
data =
data
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
index 56b80a8db..64329b710 100644
--- a/lib/pleroma/web/activity_pub/utils.ex
+++ b/lib/pleroma/web/activity_pub/utils.ex
@@ -4,6 +4,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
alias Pleroma.Web.Endpoint
alias Ecto.{Changeset, UUID}
import Ecto.Query
+ require Logger
# Some implementations send the actor URI as the actor field, others send the entire actor object,
# so figure out what the actor's URI is based on what we have.
@@ -217,9 +218,26 @@ defmodule Pleroma.Web.ActivityPub.Utils do
#### Follow-related helpers
@doc """
+ Updates a follow activity's state (for locked accounts).
+ """
+ def update_follow_state(%Activity{} = activity, state) do
+ with new_data <-
+ activity.data
+ |> Map.put("state", state),
+ changeset <- Changeset.change(activity, data: new_data),
+ {:ok, activity} <- Repo.update(changeset) do
+ {:ok, activity}
+ end
+ end
+
+ @doc """
Makes a follow activity data for the given follower and followed
"""
- def make_follow_data(%User{ap_id: follower_id}, %User{ap_id: followed_id}, activity_id) do
+ def make_follow_data(
+ %User{ap_id: follower_id},
+ %User{ap_id: followed_id} = followed,
+ activity_id
+ ) do
data = %{
"type" => "Follow",
"actor" => follower_id,
@@ -228,7 +246,10 @@ defmodule Pleroma.Web.ActivityPub.Utils do
"object" => followed_id
}
- if activity_id, do: Map.put(data, "id", activity_id), else: data
+ data = if activity_id, do: Map.put(data, "id", activity_id), else: data
+ data = if User.locked?(followed), do: Map.put(data, "state", "pending"), else: data
+
+ data
end
def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index 974da5203..8a8d1e050 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -4,6 +4,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
alias Pleroma.Web
alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView, ListView}
alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.{CommonAPI, OStatus}
alias Pleroma.Web.OAuth.{Authorization, Token, App}
alias Comeonin.Pbkdf2
@@ -71,6 +72,20 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
user
end
+ user =
+ if locked = params["locked"] do
+ with locked <- locked == "true",
+ new_info <- Map.put(user.info, "locked", locked),
+ change <- User.info_changeset(user, %{info: new_info}),
+ {:ok, user} <- User.update_and_set_cache(change) do
+ user
+ else
+ _e -> user
+ end
+ else
+ user
+ end
+
with changeset <- User.update_changeset(user, params),
{:ok, user} <- User.update_and_set_cache(changeset) do
if original_user != user do
@@ -345,7 +360,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
- with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
+ with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
%Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end
@@ -476,6 +491,53 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
end
+ def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
+ with {:ok, follow_requests} <- User.get_follow_requests(followed) do
+ render(conn, AccountView, "accounts.json", %{users: follow_requests, as: :user})
+ end
+ end
+
+ def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
+ with %User{} = follower <- Repo.get(User, id),
+ {:ok, follower} <- User.maybe_follow(follower, followed),
+ %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
+ {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
+ {:ok, _activity} <-
+ ActivityPub.accept(%{
+ to: [follower.ap_id],
+ actor: followed.ap_id,
+ object: follow_activity.data["id"],
+ type: "Accept"
+ }) do
+ render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
+ else
+ {:error, message} ->
+ conn
+ |> put_resp_content_type("application/json")
+ |> send_resp(403, Jason.encode!(%{"error" => message}))
+ end
+ end
+
+ def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
+ with %User{} = follower <- Repo.get(User, id),
+ %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
+ {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
+ {:ok, _activity} <-
+ ActivityPub.reject(%{
+ to: [follower.ap_id],
+ actor: followed.ap_id,
+ object: follow_activity.data["id"],
+ type: "Reject"
+ }) do
+ render(conn, AccountView, "relationship.json", %{user: followed, target: follower})
+ else
+ {:error, message} ->
+ conn
+ |> put_resp_content_type("application/json")
+ |> send_resp(403, Jason.encode!(%{"error" => message}))
+ end
+ end
+
def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
with %User{} = followed <- Repo.get(User, id),
{:ok, follower} <- User.maybe_direct_follow(follower, followed),
@@ -545,6 +607,20 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
end
+ def domain_blocks(%{assigns: %{user: %{info: info}}} = conn, _) do
+ json(conn, info["domain_blocks"] || [])
+ end
+
+ def block_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
+ User.block_domain(blocker, domain)
+ json(conn, %{})
+ end
+
+ def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) do
+ User.unblock_domain(blocker, domain)
+ json(conn, %{})
+ end
+
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, params["resolve"] == "true")
diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex
index 080f62b31..46648c366 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_socket.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex
@@ -15,10 +15,13 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do
with token when not is_nil(token) <- params["access_token"],
%Token{user_id: user_id} <- Repo.get_by(Token, token: token),
%User{} = user <- Repo.get(User, user_id),
- stream when stream in ["public", "public:local", "user", "direct"] <- params["stream"] do
+ stream when stream in ["public", "public:local", "user", "direct", "list"] <-
+ params["stream"] do
+ topic = if stream == "list", do: "list:#{params["list"]}", else: stream
+
socket =
socket
- |> assign(:topic, params["stream"])
+ |> assign(:topic, topic)
|> assign(:user, user)
Pleroma.Web.Streamer.add_socket(params["stream"], socket)
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index d1d48cd0a..59898457b 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -125,8 +125,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
uri: object["id"],
url: object["external_url"] || object["id"],
account: AccountView.render("account.json", %{user: user}),
- in_reply_to_id: reply_to && reply_to.id,
- in_reply_to_account_id: reply_to_user && reply_to_user.id,
+ in_reply_to_id: reply_to && to_string(reply_to.id),
+ in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
reblog: nil,
content: HtmlSanitizeEx.basic_html(object["content"]),
created_at: created_at,
diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex
index 3dd87d0ab..a5fb32a4e 100644
--- a/lib/pleroma/web/oauth/oauth_controller.ex
+++ b/lib/pleroma/web/oauth/oauth_controller.ex
@@ -81,10 +81,10 @@ defmodule Pleroma.Web.OAuth.OAuthController do
# - investigate a way to verify the user wants to grant read/write/follow once scope handling is done
def token_exchange(
conn,
- %{"grant_type" => "password", "name" => name, "password" => password} = params
+ %{"grant_type" => "password", "username" => name, "password" => password} = params
) do
with %App{} = app <- get_app_from_request(conn, params),
- %User{} = user <- User.get_cached_by_nickname(name),
+ %User{} = user <- User.get_by_nickname_or_email(name),
true <- Pbkdf2.checkpw(password, user.password_hash),
{:ok, auth} <- Authorization.create_authorization(app, user),
{:ok, token} <- Token.exchange_token(app, auth) do
@@ -104,6 +104,18 @@ defmodule Pleroma.Web.OAuth.OAuthController do
end
end
+ def token_exchange(
+ conn,
+ %{"grant_type" => "password", "name" => name, "password" => password} = params
+ ) do
+ params =
+ params
+ |> Map.delete("name")
+ |> Map.put("username", name)
+
+ token_exchange(conn, params)
+ end
+
defp fix_padding(token) do
token
|> Base.url_decode64!(padding: false)
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index 924254895..13bd393ab 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -41,7 +41,7 @@ defmodule Pleroma.Web.Router do
end
pipeline :well_known do
- plug(:accepts, ["xml", "xrd+xml", "json", "jrd+json"])
+ plug(:accepts, ["json", "jrd+json", "xml", "xrd+xml"])
end
pipeline :config do
@@ -97,12 +97,14 @@ defmodule Pleroma.Web.Router do
post("/accounts/:id/mute", MastodonAPIController, :relationship_noop)
post("/accounts/:id/unmute", MastodonAPIController, :relationship_noop)
+ get("/follow_requests", MastodonAPIController, :follow_requests)
+ post("/follow_requests/:id/authorize", MastodonAPIController, :authorize_follow_request)
+ post("/follow_requests/:id/reject", MastodonAPIController, :reject_follow_request)
+
post("/follows", MastodonAPIController, :follow)
get("/blocks", MastodonAPIController, :blocks)
- get("/domain_blocks", MastodonAPIController, :empty_array)
- get("/follow_requests", MastodonAPIController, :empty_array)
get("/mutes", MastodonAPIController, :empty_array)
get("/timelines/home", MastodonAPIController, :home_timeline)
@@ -134,6 +136,10 @@ defmodule Pleroma.Web.Router do
get("/lists/:id/accounts", MastodonAPIController, :list_accounts)
post("/lists/:id/accounts", MastodonAPIController, :add_to_list)
delete("/lists/:id/accounts", MastodonAPIController, :remove_from_list)
+
+ get("/domain_blocks", MastodonAPIController, :domain_blocks)
+ post("/domain_blocks", MastodonAPIController, :block_domain)
+ delete("/domain_blocks", MastodonAPIController, :unblock_domain)
end
scope "/api/web", Pleroma.Web.MastodonAPI do
@@ -238,8 +244,13 @@ defmodule Pleroma.Web.Router do
post("/statuses/update", TwitterAPI.Controller, :status_update)
post("/statuses/retweet/:id", TwitterAPI.Controller, :retweet)
+ post("/statuses/unretweet/:id", TwitterAPI.Controller, :unretweet)
post("/statuses/destroy/:id", TwitterAPI.Controller, :delete_post)
+ get("/pleroma/friend_requests", TwitterAPI.Controller, :friend_requests)
+ post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request)
+ post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request)
+
post("/friendships/create", TwitterAPI.Controller, :follow)
post("/friendships/destroy", TwitterAPI.Controller, :unfollow)
post("/blocks/create", TwitterAPI.Controller, :block)
diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex
index 6ed9035fb..ce38f3cc3 100644
--- a/lib/pleroma/web/streamer.ex
+++ b/lib/pleroma/web/streamer.ex
@@ -1,7 +1,7 @@
defmodule Pleroma.Web.Streamer do
use GenServer
require Logger
- alias Pleroma.{User, Notification}
+ alias Pleroma.{User, Notification, Activity, Object}
def init(args) do
{:ok, args}
@@ -59,6 +59,19 @@ defmodule Pleroma.Web.Streamer do
{:noreply, topics}
end
+ def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
+ recipient_topics =
+ Pleroma.List.get_lists_from_activity(item)
+ |> Enum.map(fn %{id: id} -> "list:#{id}" end)
+
+ Enum.each(recipient_topics || [], fn list_topic ->
+ Logger.debug("Trying to push message to #{list_topic}\n\n")
+ push_to_socket(topics, list_topic, item)
+ end)
+
+ {:noreply, topics}
+ end
+
def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
topic = "user:#{item.user_id}"
@@ -125,6 +138,34 @@ defmodule Pleroma.Web.Streamer do
{:noreply, state}
end
+ defp represent_update(%Activity{} = activity, %User{} = user) do
+ %{
+ event: "update",
+ payload:
+ Pleroma.Web.MastodonAPI.StatusView.render(
+ "status.json",
+ activity: activity,
+ for: user
+ )
+ |> Jason.encode!()
+ }
+ |> Jason.encode!()
+ end
+
+ def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
+ Enum.each(topics[topic] || [], fn socket ->
+ # Get the current user so we have up-to-date blocks etc.
+ user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
+ blocks = user.info["blocks"] || []
+
+ parent = Object.get_by_ap_id(item.data["object"])
+
+ unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
+ send(socket.transport_pid, {:text, represent_update(item, user)})
+ end
+ end)
+ end
+
def push_to_socket(topics, topic, item) do
Enum.each(topics[topic] || [], fn socket ->
# Get the current user so we have up-to-date blocks etc.
@@ -132,20 +173,7 @@ defmodule Pleroma.Web.Streamer do
blocks = user.info["blocks"] || []
unless item.actor in blocks do
- json =
- %{
- event: "update",
- payload:
- Pleroma.Web.MastodonAPI.StatusView.render(
- "status.json",
- activity: item,
- for: user
- )
- |> Jason.encode!()
- }
- |> Jason.encode!()
-
- send(socket.transport_pid, {:text, json})
+ send(socket.transport_pid, {:text, represent_update(item, user)})
end
end)
end
diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex
index ccc6fe8e7..c23b3c2c4 100644
--- a/lib/pleroma/web/twitter_api/twitter_api.ex
+++ b/lib/pleroma/web/twitter_api/twitter_api.ex
@@ -12,14 +12,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end
def delete(%User{} = user, id) do
- # TwitterAPI does not have an "unretweet" endpoint; instead this is done
- # via the "destroy" endpoint. Therefore, we need to handle
- # when the status to "delete" is actually an Announce (repeat) object.
- with %Activity{data: %{"type" => type}} <- Repo.get(Activity, id) do
- case type do
- "Announce" -> unrepeat(user, id)
- _ -> CommonAPI.delete(id, user)
- end
+ with %Activity{data: %{"type" => type}} <- Repo.get(Activity, id),
+ {:ok, activity} <- CommonAPI.delete(id, user) do
+ {:ok, activity}
end
end
@@ -70,8 +65,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end
end
- defp unrepeat(%User{} = user, ap_id_or_id) do
- with {:ok, _unannounce, activity, _object} <- CommonAPI.unrepeat(ap_id_or_id, user) do
+ def unrepeat(%User{} = user, ap_id_or_id) do
+ with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
{:ok, activity}
end
end
diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
index d53dd0c44..ff5921807 100644
--- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex
+++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex
@@ -4,6 +4,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
alias Pleroma.Web.CommonAPI
alias Pleroma.{Repo, Activity, User, Notification}
alias Pleroma.Web.ActivityPub.ActivityPub
+ alias Pleroma.Web.ActivityPub.Utils
alias Ecto.Changeset
require Logger
@@ -240,6 +241,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end
end
+ def unretweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
+ with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
+ {:ok, activity} <- TwitterAPI.unrepeat(user, id) do
+ render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
+ end
+ end
+
def register(conn, params) do
with {:ok, user} <- TwitterAPI.register_user(params) do
render(conn, UserView, "show.json", %{user: user})
@@ -331,6 +339,54 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end
end
+ def friend_requests(conn, params) do
+ with {:ok, user} <- TwitterAPI.get_user(conn.assigns[:user], params),
+ {:ok, friend_requests} <- User.get_follow_requests(user) do
+ render(conn, UserView, "index.json", %{users: friend_requests, for: conn.assigns[:user]})
+ else
+ _e -> bad_request_reply(conn, "Can't get friend requests")
+ end
+ end
+
+ def approve_friend_request(conn, %{"user_id" => uid} = params) do
+ with followed <- conn.assigns[:user],
+ uid when is_number(uid) <- String.to_integer(uid),
+ %User{} = follower <- Repo.get(User, uid),
+ {:ok, follower} <- User.maybe_follow(follower, followed),
+ %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
+ {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "accept"),
+ {:ok, _activity} <-
+ ActivityPub.accept(%{
+ to: [follower.ap_id],
+ actor: followed.ap_id,
+ object: follow_activity.data["id"],
+ type: "Accept"
+ }) do
+ render(conn, UserView, "show.json", %{user: follower, for: followed})
+ else
+ e -> bad_request_reply(conn, "Can't approve user: #{inspect(e)}")
+ end
+ end
+
+ def deny_friend_request(conn, %{"user_id" => uid} = params) do
+ with followed <- conn.assigns[:user],
+ uid when is_number(uid) <- String.to_integer(uid),
+ %User{} = follower <- Repo.get(User, uid),
+ %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
+ {:ok, follow_activity} <- Utils.update_follow_state(follow_activity, "reject"),
+ {:ok, _activity} <-
+ ActivityPub.reject(%{
+ to: [follower.ap_id],
+ actor: followed.ap_id,
+ object: follow_activity.data["id"],
+ type: "Reject"
+ }) do
+ render(conn, UserView, "show.json", %{user: follower, for: followed})
+ else
+ e -> bad_request_reply(conn, "Can't deny user: #{inspect(e)}")
+ end
+ end
+
def friends_ids(%{assigns: %{user: user}} = conn, _params) do
with {:ok, friends} <- User.get_friends(user) do
ids =
@@ -357,6 +413,20 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
params
end
+ user =
+ if locked = params["locked"] do
+ with locked <- locked == "true",
+ new_info <- Map.put(user.info, "locked", locked),
+ change <- User.info_changeset(user, %{info: new_info}),
+ {:ok, user} <- User.update_and_set_cache(change) do
+ user
+ else
+ _e -> user
+ end
+ else
+ user
+ end
+
with changeset <- User.update_changeset(user, params),
{:ok, user} <- User.update_and_set_cache(changeset) do
CommonAPI.update(user)
diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex
index 31527caae..711008973 100644
--- a/lib/pleroma/web/twitter_api/views/user_view.ex
+++ b/lib/pleroma/web/twitter_api/views/user_view.ex
@@ -51,7 +51,8 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
"statusnet_profile_url" => user.ap_id,
"cover_photo" => User.banner_url(user) |> MediaProxy.url(),
"background_image" => image_url(user.info["background"]) |> MediaProxy.url(),
- "is_local" => user.local
+ "is_local" => user.local,
+ "locked" => !!user.info["locked"]
}
if assigns[:token] do
diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex
index e7ee810f9..9f554d286 100644
--- a/lib/pleroma/web/web_finger/web_finger.ex
+++ b/lib/pleroma/web/web_finger/web_finger.ex
@@ -25,35 +25,17 @@ defmodule Pleroma.Web.WebFinger do
|> XmlBuilder.to_doc()
end
- def webfinger(resource, "JSON") do
+ def webfinger(resource, fmt) when fmt in ["XML", "JSON"] do
host = Pleroma.Web.Endpoint.host()
regex = ~r/(acct:)?(?<username>\w+)@#{host}/
- with %{"username" => username} <- Regex.named_captures(regex, resource) do
- user = User.get_by_nickname(username)
- {:ok, represent_user(user, "JSON")}
+ with %{"username" => username} <- Regex.named_captures(regex, resource),
+ %User{} = user <- User.get_by_nickname(username) do
+ {:ok, represent_user(user, fmt)}
else
_e ->
- with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
- {:ok, represent_user(user, "JSON")}
- else
- _e ->
- {:error, "Couldn't find user"}
- end
- end
- end
-
- def webfinger(resource, "XML") do
- host = Pleroma.Web.Endpoint.host()
- regex = ~r/(acct:)?(?<username>\w+)@#{host}/
-
- with %{"username" => username} <- Regex.named_captures(regex, resource) do
- user = User.get_by_nickname(username)
- {:ok, represent_user(user, "XML")}
- else
- _e ->
- with user when not is_nil(user) <- User.get_cached_by_ap_id(resource) do
- {:ok, represent_user(user, "XML")}
+ with %User{} = user <- User.get_cached_by_ap_id(resource) do
+ {:ok, represent_user(user, fmt)}
else
_e ->
{:error, "Couldn't find user"}