From a16117225f9a4da9da08013ae256d8ac02ee3ec5 Mon Sep 17 00:00:00 2001 From: Syldexia Date: Fri, 11 May 2018 12:32:59 +0100 Subject: Added endpoint for user account deletion --- lib/pleroma/web/common_api/utils.ex | 17 +++++++++++++++++ lib/pleroma/web/router.ex | 2 ++ lib/pleroma/web/twitter_api/twitter_api_controller.ex | 13 +++++++++++++ 3 files changed, 32 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 57f8be894..5c2123f2d 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -1,7 +1,9 @@ defmodule Pleroma.Web.CommonAPI.Utils do alias Pleroma.{Repo, Object, Formatter, Activity} alias Pleroma.Web.ActivityPub.Utils + alias Pleroma.User alias Calendar.Strftime + alias Comeonin.Pbkdf2 # This is a hack for twidere. def get_by_id_or_ap_id(id) do @@ -184,4 +186,19 @@ defmodule Pleroma.Web.CommonAPI.Utils do String.slice(name, 0..30) <> "…" end end + + def confirm_current_password(user, params) do + case user do + nil -> + {:error, "Invalid credentials."} + + _ -> + with %User{local: true} = db_user <- Repo.get(User, user.id), + true <- Pbkdf2.checkpw(params["password"], db_user.password_hash) do + {:ok, db_user} + else + _ -> {:error, "Invalid password."} + end + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index c202cb810..829d9fc7b 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -211,6 +211,8 @@ defmodule Pleroma.Web.Router do post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner) post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background) + post("/account/delete_account", TwitterAPI.Controller, :delete_account) + post( "/account/most_recent_notification", TwitterAPI.Controller, diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index a99487738..a51cfa036 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -364,6 +364,19 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def delete_account(%{assigns: %{user: user}} = conn, params) do + case CommonAPI.Utils.confirm_current_password(user, params) do + {:ok, user} -> + case User.delete(user) do + :ok -> json(conn, %{status: "success"}) + :error -> error_json(conn, "Unable to delete user.") + end + + {:error, msg} -> + forbidden_json_reply(conn, msg) + end + end + def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do activities = TwitterAPI.search(user, params) -- cgit v1.2.3 From 5bfb7b4ce6c23f84c27643e9871b78b867f86b7e Mon Sep 17 00:00:00 2001 From: Syldexia Date: Sun, 13 May 2018 14:24:15 +0100 Subject: Moved account deletion stuff to somewhere that hopefully makes more sense --- lib/pleroma/web/common_api/utils.ex | 16 +++++----------- lib/pleroma/web/router.ex | 3 +-- .../web/twitter_api/controllers/util_controller.ex | 14 ++++++++++++++ lib/pleroma/web/twitter_api/twitter_api_controller.ex | 13 ------------- 4 files changed, 20 insertions(+), 26 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 5c2123f2d..d9f80ee0f 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -188,17 +188,11 @@ defmodule Pleroma.Web.CommonAPI.Utils do end def confirm_current_password(user, params) do - case user do - nil -> - {:error, "Invalid credentials."} - - _ -> - with %User{local: true} = db_user <- Repo.get(User, user.id), - true <- Pbkdf2.checkpw(params["password"], db_user.password_hash) do - {:ok, db_user} - else - _ -> {:error, "Invalid password."} - end + with %User{local: true} = db_user <- Repo.get(User, user.id), + true <- Pbkdf2.checkpw(params["password"], db_user.password_hash) do + {:ok, db_user} + else + _ -> {:error, "Invalid password."} end end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 829d9fc7b..2b5209b75 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -73,6 +73,7 @@ defmodule Pleroma.Web.Router do scope "/api/pleroma", Pleroma.Web.TwitterAPI do pipe_through(:authenticated_api) post("/follow_import", UtilController, :follow_import) + post("/delete_account", UtilController, :delete_account) end scope "/oauth", Pleroma.Web.OAuth do @@ -211,8 +212,6 @@ defmodule Pleroma.Web.Router do post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner) post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background) - post("/account/delete_account", TwitterAPI.Controller, :delete_account) - post( "/account/most_recent_notification", TwitterAPI.Controller, diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index ea540b34c..3f3ddb9e4 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do alias Pleroma.Web alias Pleroma.Web.OStatus alias Pleroma.Web.WebFinger + alias Pleroma.Web.CommonAPI alias Comeonin.Pbkdf2 alias Pleroma.Formatter alias Pleroma.Web.ActivityPub.ActivityPub @@ -195,4 +196,17 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do json(conn, "job started") end + + def delete_account(%{assigns: %{user: user}} = conn, params) do + case CommonAPI.Utils.confirm_current_password(user, params) do + {:ok, user} -> + case User.delete(user) do + :ok -> json(conn, %{status: "success"}) + :error -> json(conn, %{error: "Unable to delete user."}) + end + + {:error, msg} -> + json(conn, %{error: msg}) + end + 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 a51cfa036..a99487738 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -364,19 +364,6 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end - def delete_account(%{assigns: %{user: user}} = conn, params) do - case CommonAPI.Utils.confirm_current_password(user, params) do - {:ok, user} -> - case User.delete(user) do - :ok -> json(conn, %{status: "success"}) - :error -> error_json(conn, "Unable to delete user.") - end - - {:error, msg} -> - forbidden_json_reply(conn, msg) - end - end - def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do activities = TwitterAPI.search(user, params) -- cgit v1.2.3 From 98b36d359a1a8c10ef9877902258d46b68331363 Mon Sep 17 00:00:00 2001 From: Syldexia Date: Sun, 13 May 2018 14:56:59 +0100 Subject: Fixed formatting and test --- lib/pleroma/web/common_api/utils.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index d9f80ee0f..e774743a2 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -189,7 +189,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do def confirm_current_password(user, params) do with %User{local: true} = db_user <- Repo.get(User, user.id), - true <- Pbkdf2.checkpw(params["password"], db_user.password_hash) do + true <- Pbkdf2.checkpw(params["password"], db_user.password_hash) do {:ok, db_user} else _ -> {:error, "Invalid password."} -- cgit v1.2.3 From 93904921381300ddeefab99350cba5f551e0e2b0 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 13 May 2018 23:28:56 +0000 Subject: ActivityPub create: discard activities from deactivated users --- lib/pleroma/web/activity_pub/activity_pub.ex | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 491ad3705..76cda0b4c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -16,9 +16,20 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do (data["to"] || []) ++ (data["cc"] || []) end + defp check_actor_is_active(actor) do + user = User.get_cached_by_ap_id(actor) + + if user.info["deactivated"] == true do + :reject + else + :ok + end + end + def insert(map, local \\ true) when is_map(map) do with nil <- Activity.get_by_ap_id(map["id"]), map <- lazy_put_activity_defaults(map), + :ok <- check_actor_is_active(map["actor"]), {:ok, map} <- MRF.filter(map), :ok <- insert_full_object(map) do {:ok, activity} = -- cgit v1.2.3 From a6a6915aaf568dda8b784438b963e341c2fc0f29 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 13 May 2018 23:33:43 +0000 Subject: add mix task for deactivating a user by nickname --- lib/mix/tasks/deactivate_user.ex | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 lib/mix/tasks/deactivate_user.ex (limited to 'lib') diff --git a/lib/mix/tasks/deactivate_user.ex b/lib/mix/tasks/deactivate_user.ex new file mode 100644 index 000000000..96b3db6e4 --- /dev/null +++ b/lib/mix/tasks/deactivate_user.ex @@ -0,0 +1,13 @@ +defmodule Mix.Tasks.DeactivateUser do + use Mix.Task + alias Pleroma.User + + @shortdoc "Toggle deactivation status for a user" + def run([nickname]) do + Mix.Task.run("app.start") + + with user <- User.get_by_nickname(nickname) do + User.deactivate(user) + end + end +end -- cgit v1.2.3 From 13d4b6d2b5d17c10fb5a95e02ff668de8eeb15ea Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 18 May 2018 22:17:56 -0500 Subject: remote user deactivation: fix test failures --- lib/pleroma/web/activity_pub/activity_pub.ex | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 76cda0b4c..c026f2427 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -17,10 +17,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end defp check_actor_is_active(actor) do - user = User.get_cached_by_ap_id(actor) - - if user.info["deactivated"] == true do - :reject + if not is_nil(actor) do + with user <- User.get_cached_by_ap_id(actor), + nil <- user.info["deactivated"] do + :ok + else + _e -> :reject + end else :ok end -- cgit v1.2.3 From cce5a9cb1ce0555277d11004bcedc9e7460f4ed8 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 19 May 2018 05:43:13 +0000 Subject: webfinger: expose the application/ld+json link alongside the older application/activity+json link --- lib/pleroma/web/web_finger/web_finger.ex | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 6ffa80a43..241dfb4c8 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -86,6 +86,7 @@ defmodule Pleroma.Web.WebFinger do "href" => "data:application/magic-public-key,#{magic_key}" }, %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id}, + %{"rel" => "self", "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "href" => user.ap_id}, %{ "rel" => "http://ostatus.org/schema/1.0/subscribe", "template" => OStatus.remote_follow_path() @@ -117,6 +118,7 @@ defmodule Pleroma.Web.WebFinger do {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}, {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}}, + {:Link, %{rel: "self", type: "application/ld+json; profile="https://www.w3.org/ns/activitystreams"", href: user.ap_id}}, {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}} ] -- cgit v1.2.3 From d1f6ecf607954e725e72b8e91be5f969eebe997f Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 19 May 2018 05:46:13 +0000 Subject: webfinger: interpret application/ld+json links as an alternate to application/activity+json --- lib/pleroma/web/web_finger/web_finger.ex | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 241dfb4c8..b955bc43f 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -166,6 +166,14 @@ defmodule Pleroma.Web.WebFinger do doc ) + if ap_id == nil do + ap_id = + XML.string_from_xpath( + ~s{//Link[@rel="self" and @type="application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""]/@href}, + doc + ) + end + data = %{ "magic_key" => magic_key, "topic" => topic, @@ -185,6 +193,9 @@ defmodule Pleroma.Web.WebFinger do {"application/activity+json", "self"} -> Map.put(data, "ap_id", link["href"]) + {"application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "self"} -> + Map.put(data, "ap_id", link["href"]) + {_, "magic-public-key"} -> "data:application/magic-public-key," <> magic_key = link["href"] Map.put(data, "magic_key", magic_key) -- cgit v1.2.3 From 1a250d65afa6de25c3395fa1fad0e1d57e9ee1d2 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 19 May 2018 06:15:21 +0000 Subject: webfinger: only do ld+json on modern json webfinger --- lib/pleroma/web/web_finger/web_finger.ex | 9 --------- 1 file changed, 9 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index b955bc43f..3744dd62f 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -118,7 +118,6 @@ defmodule Pleroma.Web.WebFinger do {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}, {:Link, %{rel: "self", type: "application/activity+json", href: user.ap_id}}, - {:Link, %{rel: "self", type: "application/ld+json; profile="https://www.w3.org/ns/activitystreams"", href: user.ap_id}}, {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}} ] @@ -166,14 +165,6 @@ defmodule Pleroma.Web.WebFinger do doc ) - if ap_id == nil do - ap_id = - XML.string_from_xpath( - ~s{//Link[@rel="self" and @type="application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\""]/@href}, - doc - ) - end - data = %{ "magic_key" => magic_key, "topic" => topic, -- cgit v1.2.3 From 4d2c6707c2f5f7723f1fcf03cdbf9476f2d5b3ad Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 19 May 2018 07:03:53 +0000 Subject: activitypub: normalize the actor to ensure we have its URI --- lib/pleroma/plugs/http_signature.ex | 3 ++- lib/pleroma/web/activity_pub/utils.ex | 16 ++++++++++++++++ lib/pleroma/web/federator/federator.ex | 3 +++ lib/pleroma/web/http_signatures/http_signatures.ex | 6 +++--- 4 files changed, 24 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/plugs/http_signature.ex b/lib/pleroma/plugs/http_signature.ex index efde652f5..2d0e10cad 100644 --- a/lib/pleroma/plugs/http_signature.ex +++ b/lib/pleroma/plugs/http_signature.ex @@ -1,5 +1,6 @@ defmodule Pleroma.Web.Plugs.HTTPSignaturePlug do alias Pleroma.Web.HTTPSignatures + alias Pleroma.Web.ActivityPub.Utils import Plug.Conn require Logger @@ -12,7 +13,7 @@ defmodule Pleroma.Web.Plugs.HTTPSignaturePlug do end def call(conn, _opts) do - user = conn.params["actor"] + user = Utils.normalize_actor(conn.params["actor"]) Logger.debug("Checking sig for #{user}") [signature | _] = get_req_header(conn, "signature") diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index f98545336..d92db0d5f 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -5,6 +5,22 @@ defmodule Pleroma.Web.ActivityPub.Utils do alias Ecto.{Changeset, UUID} import Ecto.Query + # 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. + def normalize_actor(actor) do + cond do + is_binary(actor) -> + actor + + is_map(actor) -> + actor["id"] + end + end + + def normalize_params(params) do + Map.put(params, "actor", normalize_actor(params["actor"])) + end + def make_json_ld_header do %{ "@context" => [ diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex index f84af2f15..8ca530031 100644 --- a/lib/pleroma/web/federator/federator.ex +++ b/lib/pleroma/web/federator/federator.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.Federator do alias Pleroma.Web.{WebFinger, Websub} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Transmogrifier + alias Pleroma.Web.ActivityPub.Utils require Logger @websub Application.get_env(:pleroma, :websub) @@ -91,6 +92,8 @@ defmodule Pleroma.Web.Federator do def handle(:incoming_ap_doc, params) do Logger.info("Handling incoming AP activity") + params = Utils.normalize_params(params) + with {:ok, _user} <- ap_enabled_actor(params["actor"]), nil <- Activity.get_by_ap_id(params["id"]), {:ok, _activity} <- Transmogrifier.handle_incoming(params) do diff --git a/lib/pleroma/web/http_signatures/http_signatures.ex b/lib/pleroma/web/http_signatures/http_signatures.ex index 9035f5eb6..dd3f825db 100644 --- a/lib/pleroma/web/http_signatures/http_signatures.ex +++ b/lib/pleroma/web/http_signatures/http_signatures.ex @@ -1,7 +1,7 @@ # https://tools.ietf.org/html/draft-cavage-http-signatures-08 defmodule Pleroma.Web.HTTPSignatures do alias Pleroma.User - alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.ActivityPub.Utils require Logger def split_signature(sig) do @@ -31,14 +31,14 @@ defmodule Pleroma.Web.HTTPSignatures do def validate_conn(conn) do # TODO: How to get the right key and see if it is actually valid for that request. # For now, fetch the key for the actor. - with actor_id <- conn.params["actor"], + with actor_id <- Utils.normalize_actor(conn.params["actor"]), {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do if validate_conn(conn, public_key) do true else Logger.debug("Could not validate, re-fetching user and trying one more time") # Fetch user anew and try one more time - with actor_id <- conn.params["actor"], + with actor_id <- Utils.normalize_actor(conn.params["actor"]), {:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id), {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do validate_conn(conn, public_key) -- cgit v1.2.3 From 2051530868720a23715cbb0d1a1286a6ae1a6507 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 19 May 2018 07:30:02 +0000 Subject: activitypub transmogrifier: handle hubzilla AP actor quirks --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 ++ lib/pleroma/web/activity_pub/transmogrifier.ex | 13 +++++++++++++ 2 files changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 491ad3705..3e1977f96 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -401,6 +401,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do "url" => [%{"href" => data["image"]["url"]}] } + data = Transmogrifier.maybe_fix_user_object(data) + user_data = %{ ap_id: data["id"], info: %{ diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 463d1e59d..c10d27dcd 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -495,4 +495,17 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do Repo.delete_all(q) end end + + def maybe_fix_user_url(data) do + if is_map(data["url"]) do + data = Map.put(data, "url", data["url"]["href"]) + end + + data + end + + def maybe_fix_user_object(data) do + data + |> maybe_fix_user_url + end end -- cgit v1.2.3 From 6e8de2faae46f3dcaf6881ab40664da0057551e4 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 19 May 2018 08:37:04 +0000 Subject: run mix format --- lib/pleroma/user.ex | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 399a66787..6a8129ac8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -404,18 +404,22 @@ defmodule Pleroma.User do from( u in User, select_merge: %{ - search_distance: fragment( - "? <-> (? || ?)", - ^query, - u.nickname, - u.name - )} + search_distance: + fragment( + "? <-> (? || ?)", + ^query, + u.nickname, + u.name + ) + } ) - q = from(s in subquery(inner), - order_by: s.search_distance, - limit: 20 - ) + q = + from( + s in subquery(inner), + order_by: s.search_distance, + limit: 20 + ) Repo.all(q) end -- cgit v1.2.3 From 725b05d04aecaa00d5b79c81958267444bf3c91d Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 19 May 2018 08:48:15 +0000 Subject: run mix format --- lib/pleroma/user.ex | 24 ++++++++++++++---------- lib/pleroma/web/web_finger/web_finger.ex | 6 +++++- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 399a66787..6a8129ac8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -404,18 +404,22 @@ defmodule Pleroma.User do from( u in User, select_merge: %{ - search_distance: fragment( - "? <-> (? || ?)", - ^query, - u.nickname, - u.name - )} + search_distance: + fragment( + "? <-> (? || ?)", + ^query, + u.nickname, + u.name + ) + } ) - q = from(s in subquery(inner), - order_by: s.search_distance, - limit: 20 - ) + q = + from( + s in subquery(inner), + order_by: s.search_distance, + limit: 20 + ) Repo.all(q) end diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 3744dd62f..6e5fc1401 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -86,7 +86,11 @@ defmodule Pleroma.Web.WebFinger do "href" => "data:application/magic-public-key,#{magic_key}" }, %{"rel" => "self", "type" => "application/activity+json", "href" => user.ap_id}, - %{"rel" => "self", "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", "href" => user.ap_id}, + %{ + "rel" => "self", + "type" => "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"", + "href" => user.ap_id + }, %{ "rel" => "http://ostatus.org/schema/1.0/subscribe", "template" => OStatus.remote_follow_path() -- cgit v1.2.3 From df95118c819ae15f0de43519f2f9f9753ac60ec2 Mon Sep 17 00:00:00 2001 From: lain Date: Sat, 19 May 2018 11:27:14 +0200 Subject: Fix linking problem. --- lib/pleroma/formatter.ex | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 456416fbd..395a0ac55 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -160,6 +160,7 @@ defmodule Pleroma.Formatter do links = Regex.scan(@link_regex, text) |> Enum.map(fn [url] -> {Ecto.UUID.generate(), url} end) + |> Enum.sort_by(fn ({_, url}) -> -String.length(url) end) uuid_text = links -- cgit v1.2.3 From d1366f8d46959229fdae398fe7920f6894d9d02a Mon Sep 17 00:00:00 2001 From: Syldexia Date: Sat, 19 May 2018 13:35:49 +0100 Subject: Modified deleting an account to run as a task --- lib/pleroma/web/twitter_api/controllers/util_controller.ex | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 3f3ddb9e4..23e7408a0 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -200,10 +200,8 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do def delete_account(%{assigns: %{user: user}} = conn, params) do case CommonAPI.Utils.confirm_current_password(user, params) do {:ok, user} -> - case User.delete(user) do - :ok -> json(conn, %{status: "success"}) - :error -> json(conn, %{error: "Unable to delete user."}) - end + Task.start(fn -> User.delete(user) end) + json(conn, %{status: "success"}) {:error, msg} -> json(conn, %{error: msg}) -- cgit v1.2.3 From 6f39ecc41be77298ae375ca0a2f51ae7dc29fbbf Mon Sep 17 00:00:00 2001 From: Thog Date: Sat, 19 May 2018 15:22:43 +0200 Subject: Support Undo like activities (Fix #139) --- lib/pleroma/formatter.ex | 2 +- lib/pleroma/web/activity_pub/activity_pub.ex | 18 +++++++++++++----- lib/pleroma/web/activity_pub/transmogrifier.ex | 18 ++++++++++++++++++ lib/pleroma/web/activity_pub/utils.ex | 17 +++++++++++++++++ .../web/mastodon_api/mastodon_api_controller.ex | 2 +- lib/pleroma/web/twitter_api/twitter_api.ex | 4 ++-- 6 files changed, 52 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 395a0ac55..53e2c204f 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -160,7 +160,7 @@ defmodule Pleroma.Formatter do links = Regex.scan(@link_regex, text) |> Enum.map(fn [url] -> {Ecto.UUID.generate(), url} end) - |> Enum.sort_by(fn ({_, url}) -> -String.length(url) end) + |> Enum.sort_by(fn {_, url} -> -String.length(url) end) uuid_text = links diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 973d18e52..4e97693a2 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -131,11 +131,19 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - def unlike(%User{} = actor, %Object{} = object) do - with %Activity{} = activity <- get_existing_like(actor.ap_id, object), - {:ok, _activity} <- Repo.delete(activity), - {:ok, object} <- remove_like_from_object(activity, object) do - {:ok, object} + 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), + {:ok, _activity} <- Repo.delete(like_activity), + {:ok, object} <- remove_like_from_object(like_activity, object), + :ok <- maybe_federate(unlike_activity) do + {:ok, unlike_activity, like_activity, object} else _e -> {:ok, object} end diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index c10d27dcd..a31452a63 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -241,6 +241,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do end end + def handle_incoming( + %{ + "type" => "Undo", + "object" => %{"type" => "Like", "object" => object_id}, + "actor" => actor, + "id" => id + } = data + ) 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.unlike(actor, object, id, false) do + {:ok, activity} + else + e -> :error + end + end + # TODO # Accept # Undo for non-Announce diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index d92db0d5f..937f032c3 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -315,6 +315,23 @@ defmodule Pleroma.Web.ActivityPub.Utils do if activity_id, do: Map.put(data, "id", activity_id), else: data end + def make_unlike_data( + %User{ap_id: ap_id} = user, + %Activity{data: %{"context" => context}} = activity, + activity_id + ) do + data = %{ + "type" => "Undo", + "actor" => ap_id, + "object" => activity.data, + "to" => [user.follower_address, activity.data["actor"]], + "cc" => ["https://www.w3.org/ns/activitystreams#Public"], + "context" => context + } + + if activity_id, do: Map.put(data, "id", activity_id), else: data + end + def add_announce_to_object(%Activity{data: %{"actor" => actor}}, object) do with announcements <- [actor | object.data["announcements"] || []] |> Enum.uniq() do update_element_in_object("announcement", announcements, object) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 5475cb505..b218c269d 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -323,7 +323,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def unfav_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do - with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user), + with {:ok, _, _, %{data: %{"id" => id}}} = CommonAPI.unfavorite(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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 8177a4988..722e436e2 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -82,14 +82,14 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end def fav(%User{} = user, ap_id_or_id) do - with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user), + with {:ok, _fav, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user), %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do {:ok, activity} end end def unfav(%User{} = user, ap_id_or_id) do - with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user), + with {:ok, _unfav, _fav, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user), %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do {:ok, activity} end -- cgit v1.2.3 From 434601a5c367bb2d41115e3060f5d5b70572da39 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 20 May 2018 16:15:18 +0200 Subject: Return private / direct posts on user timelines, too. --- lib/pleroma/web/activity_pub/activity_pub.ex | 19 +++++++++++++++++++ .../web/mastodon_api/mastodon_api_controller.ex | 15 ++++----------- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 8 +------- 3 files changed, 24 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 4e97693a2..24b4f045a 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -260,6 +260,25 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> Enum.reverse() end + def fetch_user_activities(user, reading_user, params \\ %{}) do + params = + params + |> Map.put("type", ["Create", "Announce"]) + |> Map.put("actor_id", user.ap_id) + |> Map.put("whole_db", true) + + recipients = + if reading_user do + ["https://www.w3.org/ns/activitystreams#Public"] ++ + [reading_user.ap_id | reading_user.following] + else + ["https://www.w3.org/ns/activitystreams#Public"] + end + + fetch_activities(recipients, params) + |> Enum.reverse() + end + defp restrict_since(query, %{"since_id" => since_id}) do from(activity in query, where: activity.id > ^since_id) end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index b218c269d..85f9c5b7b 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -204,21 +204,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity}) end - def user_statuses(%{assigns: %{user: user}} = conn, params) do - with %User{ap_id: ap_id} <- Repo.get(User, params["id"]) do - params = - params - |> Map.put("type", ["Create", "Announce"]) - |> Map.put("actor_id", ap_id) - |> Map.put("whole_db", true) - + def user_statuses(%{assigns: %{user: reading_user}} = conn, params) do + with %User{} = user <- Repo.get(User, params["id"]) do + # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here activities = if params["pinned"] == "true" do - # Since Pleroma has no "pinned" posts feature, we'll just set an empty list here [] else - ActivityPub.fetch_public_activities(params) - |> Enum.reverse() + ActivityPub.fetch_user_activities(user, reading_user, params) end conn diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index a99487738..dd1dc241d 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -96,13 +96,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do def user_timeline(%{assigns: %{user: user}} = conn, params) do case TwitterAPI.get_user(user, params) do {:ok, target_user} -> - params = - params - |> Map.put("type", ["Create", "Announce"]) - |> Map.put("actor_id", target_user.ap_id) - |> Map.put("whole_db", true) - - activities = ActivityPub.fetch_public_activities(params) + activities = ActivityPub.fetch_user_activities(target_user, user, params) conn |> render(ActivityView, "index.json", %{activities: activities, for: user}) -- cgit v1.2.3 From ff007af0c2f229fa20cd4b83b94de89cd9fce29c Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 20 May 2018 18:01:24 +0200 Subject: Return visilility in twitter api, too. --- lib/pleroma/web/twitter_api/views/activity_view.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index 580d4648c..62ce3b7b5 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -262,7 +262,8 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do "external_url" => object["external_url"] || object["id"], "tags" => tags, "activity_type" => "post", - "possibly_sensitive" => possibly_sensitive + "possibly_sensitive" => possibly_sensitive, + "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object) } end end -- cgit v1.2.3 From dca26f3655b97893513b96a5d17858241dca3a81 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 20 May 2018 19:22:26 +0200 Subject: Fix specs. --- lib/pleroma/web/twitter_api/representers/activity_representer.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index 9a4954de8..c2e1f07a5 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -197,7 +197,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do "external_url" => object["external_url"] || object["id"], "tags" => tags, "activity_type" => "post", - "possibly_sensitive" => possibly_sensitive + "possibly_sensitive" => possibly_sensitive, + "visibility" => Pleroma.Web.MastodonAPI.StatusView.get_visibility(object) } end -- cgit v1.2.3