From 2b980dea2fe41e81ece6915bd05f3958007b424e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 17 Sep 2017 23:03:03 +0200 Subject: Remove tag links for now, they break some regular links. --- 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 b370a8fb7..06fcc9e8b 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -62,7 +62,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do |> Formatter.linkify |> String.replace("\n", "
\n") |> add_user_links(mentions) - |> add_tag_links(tags) + # |> add_tag_links(tags) end def add_tag_links(text, tags) do -- cgit v1.2.3 From b4f055b60416986220d905bbf688f8417cd8a270 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 5 Oct 2017 12:53:16 +0200 Subject: Don't reject already accepted subscriptions. --- lib/pleroma/web/websub/websub.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex index 6bbf13130..db1577a93 100644 --- a/lib/pleroma/web/websub/websub.ex +++ b/lib/pleroma/web/websub/websub.ex @@ -31,9 +31,9 @@ defmodule Pleroma.Web.Websub do do changeset = Changeset.change(subscription, %{state: "active"}) Repo.update(changeset) - else _e -> - changeset = Changeset.change(subscription, %{state: "rejected"}) - {:ok, subscription} = Repo.update(changeset) + else e -> + Logger.debug("Couldn't verify subscription") + Logger.debug(inspect(e)) {:error, subscription} end end -- cgit v1.2.3 From a17b2613795b24162e953dd457b411dddc0c902e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 18 Oct 2017 11:56:47 +0200 Subject: Simplify query. --- lib/pleroma/web/activity_pub/activity_pub.ex | 7 ++----- 1 file changed, 2 insertions(+), 5 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 31aa2c4f1..7a1566156 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -118,11 +118,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_tag(query, _), do: query defp restrict_recipients(query, recipients) do - Enum.reduce(recipients, query, fn (recipient, q) -> - map = %{ to: [recipient] } - from activity in q, - or_where: fragment(~s(? @> ?), activity.data, ^map) - end) + from activity in query, + where: fragment("?->'to' \\\?| ? ", activity.data, ^recipients) end defp restrict_local(query, %{"local_only" => true}) do -- cgit v1.2.3 From 6af164f27b5a285fb1b1c8790a86db061c8fc28a Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 19 Oct 2017 17:37:24 +0200 Subject: Add password reset. --- lib/pleroma/PasswordResetToken.ex | 44 ++++++++++++++++++++++ lib/pleroma/user.ex | 19 ++++++++++ lib/pleroma/web/router.ex | 10 +++++ .../twitter_api/util/invalid_token.html.eex | 1 + .../twitter_api/util/password_reset.html.eex | 12 ++++++ .../util/password_reset_failed.html.eex | 1 + .../util/password_reset_success.html.eex | 1 + .../web/twitter_api/controllers/util_controller.ex | 22 +++++++++++ lib/pleroma/web/twitter_api/views/util_view.ex | 4 ++ 9 files changed, 114 insertions(+) create mode 100644 lib/pleroma/PasswordResetToken.ex create mode 100644 lib/pleroma/web/templates/twitter_api/util/invalid_token.html.eex create mode 100644 lib/pleroma/web/templates/twitter_api/util/password_reset.html.eex create mode 100644 lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex create mode 100644 lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex create mode 100644 lib/pleroma/web/twitter_api/views/util_view.ex (limited to 'lib') diff --git a/lib/pleroma/PasswordResetToken.ex b/lib/pleroma/PasswordResetToken.ex new file mode 100644 index 000000000..79e60bf69 --- /dev/null +++ b/lib/pleroma/PasswordResetToken.ex @@ -0,0 +1,44 @@ +defmodule Pleroma.PasswordResetToken do + use Ecto.Schema + + import Ecto.{Changeset, Query} + + alias Pleroma.{User, PasswordResetToken, Repo} + + schema "password_reset_tokens" do + belongs_to :user, User + field :token, :string + field :used, :boolean, default: false + + timestamps() + end + + def create_token(%User{} = user) do + token = :crypto.strong_rand_bytes(32) |> Base.url_encode64 + + token = %PasswordResetToken{ + user_id: user.id, + used: false, + token: token + } + + Repo.insert(token) + end + + def used_changeset(struct) do + changeset = struct + |> cast(%{}, []) + |> put_change(:used, true) + end + + def reset_password(token, data) do + with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}), + %User{} = user <- Repo.get(User, token.user_id), + {:ok, user} <- User.reset_password(user, data), + {:ok, token} <- Repo.update(used_changeset(token)) do + {:ok, token} + else + _e -> {:error, token} + end + end +end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index a04bbe276..938b57d90 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -97,6 +97,25 @@ defmodule Pleroma.User do |> validate_length(:name, min: 1, max: 100) end + def password_update_changeset(struct, params) do + changeset = struct + |> cast(params, [:password, :password_confirmation]) + |> validate_required([:password, :password_confirmation]) + |> validate_confirmation(:password) + + if changeset.valid? do + hashed = Pbkdf2.hashpwsalt(changeset.changes[:password]) + changeset + |> put_change(:password_hash, hashed) + else + changeset + end + end + + def reset_password(user, data) do + Repo.update(password_update_changeset(user, data)) + end + def register_changeset(struct, params \\ %{}) do changeset = struct |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 6abf234c6..8497685a6 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -33,6 +33,16 @@ defmodule Pleroma.Web.Router do plug :accepts, ["html", "json"] end + pipeline :password_reset do + plug :accepts, ["html"] + end + + scope "/api/pleroma", Pleroma.Web.TwitterAPI do + pipe_through :password_reset + get "/password_reset/:token", UtilController, :show_password_reset + post "/password_reset", UtilController, :password_reset + end + scope "/oauth", Pleroma.Web.OAuth do get "/authorize", OAuthController, :authorize post "/authorize", OAuthController, :create_authorization diff --git a/lib/pleroma/web/templates/twitter_api/util/invalid_token.html.eex b/lib/pleroma/web/templates/twitter_api/util/invalid_token.html.eex new file mode 100644 index 000000000..ee84750c7 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/invalid_token.html.eex @@ -0,0 +1 @@ +

Invalid Token

diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset.html.eex b/lib/pleroma/web/templates/twitter_api/util/password_reset.html.eex new file mode 100644 index 000000000..3c7960998 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/password_reset.html.eex @@ -0,0 +1,12 @@ +

Password Reset for <%= @user.nickname %>

+<%= form_for @conn, util_path(@conn, :password_reset), [as: "data"], fn f -> %> +<%= label f, :password, "Password" %> +<%= password_input f, :password %> +
+ +<%= label f, :password_confirmation, "Confirmation" %> +<%= password_input f, :password_confirmation %> +
+<%= hidden_input f, :token, value: @token.token %> +<%= submit "Reset" %> +<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex b/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex new file mode 100644 index 000000000..58a3736fd --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/password_reset_failed.html.eex @@ -0,0 +1 @@ +

Password reset failed

diff --git a/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex b/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex new file mode 100644 index 000000000..c7dfcb6dd --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/password_reset_success.html.eex @@ -0,0 +1 @@ +

Password changed!

diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 32910d92c..11d8fa6c2 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -2,6 +2,28 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do use Pleroma.Web, :controller alias Pleroma.Web + alias Pleroma.{Repo, PasswordResetToken, User} + + def show_password_reset(conn, %{"token" => token}) do + with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}), + %User{} = user <- Repo.get(User, token.user_id) do + render conn, "password_reset.html", %{ + token: token, + user: user + } + else + _e -> render conn, "invalid_token.html" + end + end + + def password_reset(conn, %{"data" => data}) do + with {:ok, _} <- PasswordResetToken.reset_password(data["token"], data) do + render conn, "password_reset_success.html" + else + _e -> render conn, "password_reset_failed.html" + end + end + def help_test(conn, _params) do json(conn, "ok") end diff --git a/lib/pleroma/web/twitter_api/views/util_view.ex b/lib/pleroma/web/twitter_api/views/util_view.ex new file mode 100644 index 000000000..71b04e6cc --- /dev/null +++ b/lib/pleroma/web/twitter_api/views/util_view.ex @@ -0,0 +1,4 @@ +defmodule Pleroma.Web.TwitterAPI.UtilView do + use Pleroma.Web, :view + import Phoenix.HTML.Form +end -- cgit v1.2.3 From 38f3908c14d3b8fcfe522d4c92bde022dcc682e3 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 19 Oct 2017 17:56:27 +0200 Subject: Revert "Simplify query." This reverts commit a17b2613795b24162e953dd457b411dddc0c902e. --- lib/pleroma/web/activity_pub/activity_pub.ex | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 7a1566156..31aa2c4f1 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -118,8 +118,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_tag(query, _), do: query defp restrict_recipients(query, recipients) do - from activity in query, - where: fragment("?->'to' \\\?| ? ", activity.data, ^recipients) + Enum.reduce(recipients, query, fn (recipient, q) -> + map = %{ to: [recipient] } + from activity in q, + or_where: fragment(~s(? @> ?), activity.data, ^map) + end) end defp restrict_local(query, %{"local_only" => true}) do -- cgit v1.2.3 From fe7804e42d27ca46290cfe52ffc01d4e520849b5 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 19 Oct 2017 22:51:56 +0300 Subject: Add an API endpoint for emoji. --- lib/pleroma/formatter.ex | 5 +++++ lib/pleroma/web/router.ex | 7 ++++--- lib/pleroma/web/twitter_api/controllers/util_controller.ex | 5 +++++ 3 files changed, 14 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index a5eb3b268..f062d9a58 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -122,4 +122,9 @@ defmodule Pleroma.Formatter do def get_emoji(text) do Enum.filter(@emoji, fn ({emoji, _}) -> String.contains?(text, ":#{emoji}:") end) end + + def get_custom_emoji() do + @emoji + |> Enum.into %{} + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8497685a6..cb818b3cc 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -33,14 +33,15 @@ defmodule Pleroma.Web.Router do plug :accepts, ["html", "json"] end - pipeline :password_reset do - plug :accepts, ["html"] + pipeline :pleroma_api do + plug :accepts, ["html", "json"] end scope "/api/pleroma", Pleroma.Web.TwitterAPI do - pipe_through :password_reset + pipe_through :pleroma_api get "/password_reset/:token", UtilController, :show_password_reset post "/password_reset", UtilController, :password_reset + get "/emoji", UtilController, :emoji end scope "/oauth", Pleroma.Web.OAuth do diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 11d8fa6c2..792ae2a64 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -1,6 +1,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do use Pleroma.Web, :controller alias Pleroma.Web + alias Pleroma.Formatter alias Pleroma.{Repo, PasswordResetToken, User} @@ -68,4 +69,8 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do _ -> json(conn, version) end end + + def emoji(conn, _params) do + json conn, Formatter.get_custom_emoji() + end end -- cgit v1.2.3 From 64bc38e009208fa4f4409f6930d5117dc291c2f5 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 23 Oct 2017 16:27:51 +0200 Subject: MastoAPI: Add emoji output. --- lib/pleroma/web/mastodon_api/views/status_view.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index cce4f7217..272f83b2a 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -74,6 +74,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do reply_to = Activity.get_create_activity_by_object_ap_id(object["inReplyTo"]) reply_to_user = reply_to && User.get_cached_by_ap_id(reply_to.data["actor"]) + emojis = (activity.data["object"]["emoji"] || []) + |> Enum.map(fn {name, url} -> %{ shortcode: name, url: url, static_url: url } end) + %{ id: activity.id, uri: object["id"], @@ -99,7 +102,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do name: "Web", website: nil }, - language: nil + language: nil, + emojis: emojis } end -- cgit v1.2.3 From 9f417fd5e9893b095041702a6bd0190c7f1d7f22 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 23 Oct 2017 18:30:09 +0200 Subject: Speed up deletion and related queries. --- lib/pleroma/activity.ex | 9 +++++++-- lib/pleroma/web/activity_pub/activity_pub.ex | 1 - 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 9a5e6fc78..d7e31b6b4 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -16,17 +16,22 @@ defmodule Pleroma.Activity do where: fragment("(?)->>'id' = ?", activity.data, ^to_string(ap_id))) end + # TODO: + # Go through these and fix them everywhere. # Wrong name, only returns create activities def all_by_object_ap_id_q(ap_id) do from activity in Activity, - where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id)) + where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id)), + where: fragment("(?)->>'type' = 'Create'", activity.data) end + # Wrong name, returns all. def all_non_create_by_object_ap_id_q(ap_id) do from activity in Activity, - where: fragment("(?)->>'object' = ?", activity.data, ^to_string(ap_id)) + where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id)) end + # Wrong name plz fix thx def all_by_object_ap_id(ap_id) do Repo.all(all_by_object_ap_id_q(ap_id)) end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 31aa2c4f1..dcd27d04a 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -87,7 +87,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do } with Repo.delete(object), Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)), - Repo.delete_all(Activity.all_by_object_ap_id_q(id)), {:ok, activity} <- insert(data, local), :ok <- maybe_federate(activity) do {:ok, activity} -- cgit v1.2.3 From b35ff8f75592062dc8b6a4cc3a9ac0518b94b16a Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 23 Oct 2017 18:36:53 +0200 Subject: Use index in basic activity query. --- lib/pleroma/activity.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index d7e31b6b4..f43b32b56 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -38,7 +38,7 @@ defmodule Pleroma.Activity do def get_create_activity_by_object_ap_id(ap_id) do Repo.one(from activity in Activity, - where: fragment("(?)->'object'->>'id' = ?", activity.data, ^to_string(ap_id)) - and fragment("(?)->>'type' = 'Create'", activity.data)) + where: fragment("coalesce((?)->'object'->>'id', (?)->>'object') = ?", activity.data, activity.data, ^to_string(ap_id)), + where: fragment("(?)->>'type' = 'Create'", activity.data)) end end -- cgit v1.2.3 From 261ec824515c1de6d93ad1c4ca7d3cf73faa58a4 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 24 Oct 2017 08:39:24 +0200 Subject: Return nil for nil object requests. --- lib/pleroma/object.ex | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index 5b51d6be3..3c42c7cb5 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -21,6 +21,7 @@ defmodule Pleroma.Object do |> unique_constraint(:ap_id, name: :objects_unique_apid_index) end + def get_by_ap_id(nil), do: nil def get_by_ap_id(ap_id) do Repo.one(from object in Object, where: fragment("? @> ?", object.data, ^%{id: ap_id})) -- cgit v1.2.3 From ec546baee915ecd9843f3c84e5cb0dbdbdcad826 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 24 Oct 2017 10:39:23 +0200 Subject: Use different index for object fetching. --- lib/pleroma/object.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index 3c42c7cb5..a5a1d6a76 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -24,7 +24,7 @@ defmodule Pleroma.Object do def get_by_ap_id(nil), do: nil def get_by_ap_id(ap_id) do Repo.one(from object in Object, - where: fragment("? @> ?", object.data, ^%{id: ap_id})) + where: fragment("(?)->>'id' = ?", object.data, ^ap_id)) end def get_cached_by_ap_id(ap_id) do -- cgit v1.2.3 From 9af560083fcc513a7b3c41b06f4ed307c50a7529 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 24 Oct 2017 14:16:17 +0200 Subject: Use more efficient user note count update query. --- lib/pleroma/user.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 938b57d90..bfd3a3ad7 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -241,7 +241,7 @@ defmodule Pleroma.User do def update_note_count(%User{} = user) do note_count_query = from a in Object, - where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}), + where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data), select: count(a.id) note_count = Repo.one(note_count_query) -- cgit v1.2.3 From 529351673090d8165bb0c54f9e957e611fcacd72 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 24 Oct 2017 14:39:01 +0200 Subject: Use more efficient query to fetch likes. --- lib/pleroma/web/activity_pub/utils.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 4b8e6b690..4e3a7e2bd 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -90,7 +90,11 @@ defmodule Pleroma.Web.ActivityPub.Utils do """ def get_existing_like(actor, %{data: %{"id" => id}} = object) do query = from activity in Activity, - where: fragment("? @> ?", activity.data, ^%{actor: actor, object: id, type: "Like"}) + 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) end -- cgit v1.2.3 From 813d2eaaf08efcb7aa87f6270e98c760b0b60b99 Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 25 Oct 2017 21:02:42 +0300 Subject: Add mastodon API endpoint for follow --- .../web/mastodon_api/mastodon_api_controller.ex | 31 +++++++++++++++++++--- lib/pleroma/web/router.ex | 2 ++ 2 files changed, 29 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index a01a199fb..ea85e5bce 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -269,11 +269,13 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do - with %User{} = followed <- Repo.get(User, id), - {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.follow(follower, followed) do + def follow(%{assigns: %{user: follower}} = conn, params) do + with {:ok, %User{} = followed} <- get_user(params), + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "relationship.json", %{user: follower, target: followed} + else + err -> err end end @@ -338,4 +340,25 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do Logger.debug("Unimplemented, returning an empty array") json(conn, []) end + + defp get_user(params) do + case params do + %{"uri" => uri} -> + case target = Repo.get_by(User, nickname: uri) do + nil -> + {:error, "No user with such nickname"} + _ -> + {:ok, target} + end + %{"id" => id} -> + case target = Repo.get(User, id) do + nil -> + {:error, "No user with such id"} + _ -> + {:ok, target} + end + _ -> + {:error, "You need to specify uri or id"} + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index cb818b3cc..45c47eefb 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -62,6 +62,8 @@ defmodule Pleroma.Web.Router do post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop + post "/follows", MastodonAPIController, :follow + get "/blocks", MastodonAPIController, :empty_array get "/domain_blocks", MastodonAPIController, :empty_array get "/follow_requests", MastodonAPIController, :empty_array -- cgit v1.2.3 From 11d2287476fe9518cd0a3721a0fe7a0a1fd937fd Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 25 Oct 2017 21:25:37 +0300 Subject: Return error message on errors --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index ea85e5bce..5032c735d 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -275,7 +275,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "relationship.json", %{user: follower, target: followed} else - err -> err + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) end end -- cgit v1.2.3 From c56d28f96c343d445ec8d06baf351423691036f3 Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 29 Oct 2017 00:07:38 +0300 Subject: Fix return type of /api/v1/follows --- .../web/mastodon_api/mastodon_api_controller.ex | 38 +++++++++------------- lib/pleroma/web/router.ex | 2 +- 2 files changed, 16 insertions(+), 24 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 5032c735d..25c5418ab 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -269,8 +269,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def follow(%{assigns: %{user: follower}} = conn, params) do - with {:ok, %User{} = followed} <- get_user(params), + def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do + with %User{} = followed <- Repo.get(User, id), {:ok, follower} <- User.follow(follower, followed), {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "relationship.json", %{user: follower, target: followed} @@ -282,6 +282,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do + with %User{} = followed <- Repo.get_by(User, nickname: uri), + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do + render conn, AccountView, "account.json", %{user: followed} + else + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) + end + end + # TODO: Clean up and unify def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do with %User{} = followed <- Repo.get(User, id), @@ -343,25 +356,4 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do Logger.debug("Unimplemented, returning an empty array") json(conn, []) end - - defp get_user(params) do - case params do - %{"uri" => uri} -> - case target = Repo.get_by(User, nickname: uri) do - nil -> - {:error, "No user with such nickname"} - _ -> - {:ok, target} - end - %{"id" => id} -> - case target = Repo.get(User, id) do - nil -> - {:error, "No user with such id"} - _ -> - {:ok, target} - end - _ -> - {:error, "You need to specify uri or id"} - end - end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 45c47eefb..557d094b4 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -62,7 +62,7 @@ defmodule Pleroma.Web.Router do post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop - post "/follows", MastodonAPIController, :follow + post "/follows", MastodonAPIController, :follows get "/blocks", MastodonAPIController, :empty_array get "/domain_blocks", MastodonAPIController, :empty_array -- cgit v1.2.3 From 2ffc6da20799ac8e7882f5e72d3f37dadc1f2428 Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 29 Oct 2017 00:30:10 +0300 Subject: Clean style. Use 'follow' instead of 'follows' and correct indentation. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 6 +++--- lib/pleroma/web/router.ex | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 25c5418ab..971772810 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -282,10 +282,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do + def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do with %User{} = followed <- Repo.get_by(User, nickname: uri), - {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.follow(follower, followed) do + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "account.json", %{user: followed} else {:error, message} = err -> diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 557d094b4..45c47eefb 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -62,7 +62,7 @@ defmodule Pleroma.Web.Router do post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop - post "/follows", MastodonAPIController, :follows + post "/follows", MastodonAPIController, :follow get "/blocks", MastodonAPIController, :empty_array get "/domain_blocks", MastodonAPIController, :empty_array -- cgit v1.2.3 From efe12e1a73e13c9e749a4e6cc9c0c8a6988d87db Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 30 Oct 2017 03:37:07 +0000 Subject: Fix /api/v1/accounts/search by splitting account search into its own function --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 14 +++++++++++++- lib/pleroma/web/router.ex | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 971772810..dacb0ebe3 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -308,7 +308,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do + def dousersearch(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do if params["resolve"] == "true" do User.get_or_fetch_by_nickname(query) end @@ -317,6 +317,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query), limit: 20 accounts = Repo.all(q) + end + + def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do + accounts = Pleroma.Web.MastodonAPI.MastodonAPIController.dousersearch(conn, params) q = from a in Activity, where: fragment("?->>'type' = 'Create'", a.data), @@ -333,6 +337,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, res) end + def accountsearch(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do + accounts = Pleroma.Web.MastodonAPI.MastodonAPIController.dousersearch(conn, params) + + res = AccountView.render("accounts.json", users: accounts, for: user, as: :user) + + json(conn, res) + end + def favourites(%{assigns: %{user: user}} = conn, params) do params = conn |> Map.put("type", "Create") diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 45c47eefb..53f4a45e3 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -55,6 +55,7 @@ defmodule Pleroma.Web.Router do get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials get "/accounts/relationships", MastodonAPIController, :relationships + get "/accounts/search", MastodonAPIController, :accountsearch post "/accounts/:id/follow", MastodonAPIController, :follow post "/accounts/:id/unfollow", MastodonAPIController, :unfollow post "/accounts/:id/block", MastodonAPIController, :relationship_noop -- cgit v1.2.3 From 502cb38cd69f9f8c15a0ee597584364f9d36bdf1 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 30 Oct 2017 19:23:16 +0100 Subject: Move user search to User module. --- lib/pleroma/user.ex | 10 ++++++++++ lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 17 +++-------------- lib/pleroma/web/router.ex | 2 +- 3 files changed, 14 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index bfd3a3ad7..bf63a22b3 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -274,4 +274,14 @@ defmodule Pleroma.User do Repo.all(query) end + + def search(query, resolve) do + if resolve do + User.get_or_fetch_by_nickname(query) + end + q = from u in User, + where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query), + limit: 20 + Repo.all(q) + end end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index dacb0ebe3..9399dee86 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -308,19 +308,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def dousersearch(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do - if params["resolve"] == "true" do - User.get_or_fetch_by_nickname(query) - end - - q = from u in User, - where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query), - limit: 20 - accounts = Repo.all(q) - end - def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do - accounts = Pleroma.Web.MastodonAPI.MastodonAPIController.dousersearch(conn, params) + accounts = User.search(query, params["resolve"] == "true") q = from a in Activity, where: fragment("?->>'type' = 'Create'", a.data), @@ -337,8 +326,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, res) end - def accountsearch(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do - accounts = Pleroma.Web.MastodonAPI.MastodonAPIController.dousersearch(conn, params) + def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do + accounts = User.search(query, params["resolve"] == "true") res = AccountView.render("accounts.json", users: accounts, for: user, as: :user) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 53f4a45e3..1fb5eadf6 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -55,7 +55,7 @@ defmodule Pleroma.Web.Router do get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials get "/accounts/relationships", MastodonAPIController, :relationships - get "/accounts/search", MastodonAPIController, :accountsearch + get "/accounts/search", MastodonAPIController, :account_search post "/accounts/:id/follow", MastodonAPIController, :follow post "/accounts/:id/unfollow", MastodonAPIController, :unfollow post "/accounts/:id/block", MastodonAPIController, :relationship_noop -- cgit v1.2.3 From ce7f9f527c55b282a56c86a68c358a6272d01aee Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 14:40:12 +0100 Subject: MastoAPI: Always return an url for statuses. External url if available, id if not. --- lib/pleroma/web/mastodon_api/views/status_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 272f83b2a..b3bb50880 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -80,7 +80,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do %{ id: activity.id, uri: object["id"], - url: object["external_url"], + 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, -- cgit v1.2.3 From 968a546d4ac7d6b50af84aea71b3b37af8f2f669 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 14:51:41 +0100 Subject: MastoAPI: Return id as string instead of integer. --- lib/pleroma/web/mastodon_api/views/status_view.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index b3bb50880..48703e657 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -21,9 +21,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do |> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end) %{ - id: activity.id, + id: to_string(activity.id), uri: object, - url: nil, + url: nil, # TODO: This might be wrong, check with mastodon. account: AccountView.render("account.json", %{user: user}), in_reply_to_id: nil, in_reply_to_account_id: nil, @@ -78,7 +78,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do |> Enum.map(fn {name, url} -> %{ shortcode: name, url: url, static_url: url } end) %{ - id: activity.id, + id: to_string(activity.id), uri: object["id"], url: object["external_url"] || object["id"], account: AccountView.render("account.json", %{user: user}), -- cgit v1.2.3 From 3037814fde83d869f7a71567511a6aa5e0700073 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 15:26:37 +0100 Subject: Only search through last 100_000 activities for fetches. This is purely a performance enhancement --- lib/pleroma/web/activity_pub/activity_pub.ex | 9 +++++++++ 1 file changed, 9 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 dcd27d04a..71e52cb46 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -155,6 +155,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end defp restrict_favorited_by(query, _), do: query + # Only search through last 100_000 activities by default + defp restrict_recent(query, _) do + since = Repo.aggregate(Activity, :max, :id) - 100_000 + + from activity in query, + where: activity.id > ^since + end + def fetch_activities(recipients, opts \\ %{}) do base_query = from activity in Activity, limit: 20, @@ -169,6 +177,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> restrict_actor(opts) |> restrict_type(opts) |> restrict_favorited_by(opts) + |> restrict_recent(opts) |> Repo.all |> Enum.reverse end -- cgit v1.2.3 From 4dcbb64f19723334a9ef66b4ce71856d30e32796 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 16:37:11 +0100 Subject: Avoid potentially slow count queries for user note count. For a variety of reasons, posgresql won't use the available actor, type index to do an index only scan. We now just increase the user note count, which will lead to slightly wrong counts in some cases, but it's better than the potentially very slow count query. --- lib/pleroma/user.ex | 9 +++++++++ lib/pleroma/web/common_api/common_api.ex | 2 +- lib/pleroma/web/ostatus/handlers/note_handler.ex | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index bf63a22b3..5f1750035 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -239,6 +239,15 @@ defmodule Pleroma.User do {:ok, Repo.all(q)} 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) + + cs = info_changeset(user, %{info: new_info}) + + Repo.update(cs) + end + def update_note_count(%User{} = user) do note_count_query = from a in Object, where: fragment("?->>'actor' = ? and ?->>'type' = 'Note'", a.data, ^user.ap_id, a.data), diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index a865cd143..c9822dc2d 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -61,7 +61,7 @@ defmodule Pleroma.Web.CommonAPI do context <- make_context(inReplyTo), object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags) do res = ActivityPub.create(to, user, context, object) - User.update_note_count(user) + User.increase_note_count(user) res end end diff --git a/lib/pleroma/web/ostatus/handlers/note_handler.ex b/lib/pleroma/web/ostatus/handlers/note_handler.ex index dda5c7d5e..b151c118a 100644 --- a/lib/pleroma/web/ostatus/handlers/note_handler.ex +++ b/lib/pleroma/web/ostatus/handlers/note_handler.ex @@ -112,7 +112,7 @@ defmodule Pleroma.Web.OStatus.NoteHandler do note <- (if inReplyTo && !inReplyToActivity, do: note |> Map.put("inReplyTo", inReplyTo), else: note) do res = ActivityPub.create(to, actor, context, note, %{}, date, false) - User.update_note_count(actor) + User.increase_note_count(actor) res else %Activity{} = activity -> {:ok, activity} -- cgit v1.2.3 From 4cbf17dac6c9501e9fc711d04540da9afbcad717 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 17:30:46 +0100 Subject: Save cws in the activitypub data. --- lib/pleroma/web/common_api/utils.ex | 3 ++- lib/pleroma/web/ostatus/handlers/note_handler.ex | 3 ++- lib/pleroma/web/ostatus/ostatus.ex | 14 +++++++++----- 3 files changed, 13 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 06fcc9e8b..83a656011 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -94,11 +94,12 @@ defmodule Pleroma.Web.CommonAPI.Utils do end) end - def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags) do + def make_note_data(actor, to, context, content_html, attachments, inReplyTo, tags, cw \\ nil) do object = %{ "type" => "Note", "to" => to, "content" => content_html, + "summary" => cw, "context" => context, "attachment" => attachments, "actor" => actor, diff --git a/lib/pleroma/web/ostatus/handlers/note_handler.ex b/lib/pleroma/web/ostatus/handlers/note_handler.ex index b151c118a..8747dbb67 100644 --- a/lib/pleroma/web/ostatus/handlers/note_handler.ex +++ b/lib/pleroma/web/ostatus/handlers/note_handler.ex @@ -94,6 +94,7 @@ defmodule Pleroma.Web.OStatus.NoteHandler do [author] <- :xmerl_xpath.string('//author[1]', doc), {:ok, actor} <- OStatus.find_make_or_update_user(author), content_html <- OStatus.get_content(entry), + cw <- OStatus.get_cw(entry), inReplyTo <- XML.string_from_xpath("//thr:in-reply-to[1]/@ref", entry), inReplyToActivity <- fetch_replied_to_activity(entry, inReplyTo), inReplyTo <- (inReplyToActivity && inReplyToActivity.data["object"]["id"]) || inReplyTo, @@ -103,7 +104,7 @@ defmodule Pleroma.Web.OStatus.NoteHandler do mentions <- get_mentions(entry), to <- make_to_list(actor, mentions), date <- XML.string_from_xpath("//published", entry), - note <- CommonAPI.Utils.make_note_data(actor.ap_id, to, context, content_html, attachments, inReplyToActivity, []), + note <- CommonAPI.Utils.make_note_data(actor.ap_id, to, context, content_html, attachments, inReplyToActivity, [], cw), note <- note |> Map.put("id", id) |> Map.put("tag", tags), note <- note |> Map.put("published", date), note <- note |> Map.put("emoji", get_emoji(entry)), diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index bc975f82d..1e8b71357 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -150,16 +150,20 @@ defmodule Pleroma.Web.OStatus do end @doc """ - Gets the content from a an entry. Will add the cw text to the body for cw'd - Mastodon notes. + Gets the content from a an entry. """ def get_content(entry) do - base_content = string_from_xpath("//content", entry) + string_from_xpath("//content", entry) + end + @doc """ + Get the cw that mastodon uses. + """ + def get_cw(entry) do with scope when not is_nil(scope) <- string_from_xpath("//mastodon:scope", entry), cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do - "#{cw}
#{base_content}" - else _e -> base_content + cw + else _e -> nil end end -- cgit v1.2.3 From 04217f52c92d5a287b317e10be4b42bf74b30e07 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 17:43:37 +0100 Subject: TwitterAPI: Display cws. --- lib/pleroma/web/twitter_api/representers/activity_representer.ex | 6 ++++++ 1 file changed, 6 insertions(+) (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 929e26bf0..3fbeb86ba 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -135,6 +135,12 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do tags = activity.data["object"]["tag"] || [] possibly_sensitive = Enum.member?(tags, "nsfw") + content = if activity.data["object"]["summary"] do + "#{activity.data["object"]["summary"]}
#{content}" + else + content + end + html = HtmlSanitizeEx.basic_html(content) |> Formatter.emojify(object["emoji"]) %{ -- cgit v1.2.3 From 44295dd49ad004b8c7a289387e913b1234ad1e61 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 17:49:47 +0100 Subject: MastoAPI: Include CW. --- lib/pleroma/web/mastodon_api/views/status_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 48703e657..09a2ca404 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -93,7 +93,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do favourited: !!favorited, muted: false, sensitive: sensitive, - spoiler_text: "", + spoiler_text: object["summary"] || "", visibility: "public", media_attachments: attachments, mentions: mentions, -- cgit v1.2.3 From e014cc6ed8f98e20f5d64f1e666582c62de6321d Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 19:44:36 +0100 Subject: Allow using cws in mastodon api. --- lib/pleroma/web/common_api/common_api.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index c9822dc2d..32a8a08c7 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -59,7 +59,8 @@ defmodule Pleroma.Web.CommonAPI do tags <- Formatter.parse_tags(status), content_html <- make_content_html(status, mentions, attachments, tags), context <- make_context(inReplyTo), - object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags) do + cw <- data["spoiler_text"], + object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags, cw) do res = ActivityPub.create(to, user, context, object) User.increase_note_count(user) res -- cgit v1.2.3 From 6f05367325e7d773ca7199948fd5b49409f6d78d Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 31 Oct 2017 19:51:58 +0100 Subject: Federate out content warnings. --- lib/pleroma/web/ostatus/activity_representer.ex | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index 2092ab7fa..2bff57e76 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -83,6 +83,12 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do emoji_links = get_emoji_links(activity.data["object"]["content"] || "") + summary = if activity.data["object"]["summary"] do + [{:summary, [], h.(activity.data["object"]["summary"])}] + else + [] + end + [ {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']}, {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']}, @@ -93,7 +99,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do {:updated, h.(updated_at)}, {:"ostatus:conversation", [ref: h.(activity.data["context"])], h.(activity.data["context"])}, {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}, - ] ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links + ] ++ summary ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions ++ emoji_links end def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do -- cgit v1.2.3 From 9be286a9004ae60f8ea48bb0c8871d70223f082b Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 1 Nov 2017 09:33:29 +0100 Subject: Handle empty terms / tags. --- lib/pleroma/web/ostatus/ostatus.ex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 1e8b71357..308e206c5 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -169,7 +169,9 @@ defmodule Pleroma.Web.OStatus do def get_tags(entry) do :xmerl_xpath.string('//category', entry) - |> Enum.map(fn (category) -> string_from_xpath("/category/@term", category) |> String.downcase end) + |> Enum.map(fn (category) -> string_from_xpath("/category/@term", category) end) + |> Enum.filter(&(&1)) + |> Enum.map(&String.downcase/1) end def maybe_update(doc, user) do -- cgit v1.2.3 From 6a5f0871745316b50742ea9ebf3ff7c69881e105 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 21:57:37 +0100 Subject: Add blocks to User. --- lib/pleroma/user.ex | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 5f1750035..771c54e81 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -293,4 +293,28 @@ defmodule Pleroma.User do limit: 20 Repo.all(q) end + + def block(user, %{ap_id: ap_id}) do + blocks = user.info["blocks"] || [] + new_blocks = Enum.uniq([ap_id | blocks]) + new_info = Map.put(user.info, "blocks", new_blocks) + + cs = User.info_changeset(user, %{info: new_info}) + Repo.update(cs) + end + + def unblock(user, %{ap_id: ap_id}) do + blocks = user.info["blocks"] || [] + new_blocks = List.delete(blocks, ap_id) + new_info = Map.put(user.info, "blocks", new_blocks) + + cs = User.info_changeset(user, %{info: new_info}) + Repo.update(cs) + end + + def blocks?(user, %{ap_id: ap_id}) do + blocks = user.info["blocks"] || [] + Enum.member?(blocks, ap_id) + end + end -- cgit v1.2.3 From 632da6c9273b55984bdd67b8a10672733df7fae5 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 22:08:22 +0100 Subject: Don't create notifications if the user is blocked. --- lib/pleroma/notification.ex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 35f817d1d..00a382f31 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -46,9 +46,11 @@ defmodule Pleroma.Notification do # TODO move to sql, too. def create_notification(%Activity{} = activity, %User{} = user) do - notification = %Notification{user_id: user.id, activity_id: activity.id} - {:ok, notification} = Repo.insert(notification) - notification + unless User.blocks?(user, %{ap_id: activity.data["actor"]}) do + notification = %Notification{user_id: user.id, activity_id: activity.id} + {:ok, notification} = Repo.insert(notification) + notification + end end end -- cgit v1.2.3 From a47727adde426ab1e80299f5b5bdb23edabd0cd8 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 22:37:26 +0100 Subject: Don't return activities from blocked users. --- lib/pleroma/web/activity_pub/activity_pub.ex | 8 ++++++++ 1 file changed, 8 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 71e52cb46..75b59f375 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -163,6 +163,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do where: activity.id > ^since end + defp restrict_blocked(query, %{"blocking_user" => user}) do + blocks = user.info["blocks"] || [] + from activity in query, + where: fragment("not (?->>'actor' = ANY(?))", activity.data, ^blocks) + end + defp restrict_blocked(query, _), do: query + def fetch_activities(recipients, opts \\ %{}) do base_query = from activity in Activity, limit: 20, @@ -178,6 +185,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> restrict_type(opts) |> restrict_favorited_by(opts) |> restrict_recent(opts) + |> restrict_blocked(opts) |> Repo.all |> Enum.reverse end -- cgit v1.2.3 From 8ef58a266b6c73ac2ec49873d48fda82315c63fd Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 22:44:36 +0100 Subject: Don't return blocked users' activities in contexts. --- lib/pleroma/web/activity_pub/activity_pub.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 75b59f375..db986f8f2 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -93,10 +93,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - def fetch_activities_for_context(context) do + def fetch_activities_for_context(context, opts \\ %{}) do query = from activity in Activity, where: fragment("?->>'type' = ? and ?->>'context' = ?", activity.data, "Create", activity.data, ^context), order_by: [desc: :id] + query = restrict_blocked(query, opts) Repo.all(query) end -- cgit v1.2.3 From 867ac1e4f5b1a5b994b57a73bed36710100f9cf8 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 22:47:11 +0100 Subject: Handle nil cases. --- lib/pleroma/web/activity_pub/activity_pub.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 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 db986f8f2..a62be2511 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -164,8 +164,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do where: activity.id > ^since end - defp restrict_blocked(query, %{"blocking_user" => user}) do - blocks = user.info["blocks"] || [] + defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do + blocks = info["blocks"] || [] from activity in query, where: fragment("not (?->>'actor' = ANY(?))", activity.data, ^blocks) end -- cgit v1.2.3 From d89a7a0b34e62bb077a2b3e59daba802f3aa28d1 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 22:50:42 +0100 Subject: TwitterAPI: Add blocking to fetches. --- lib/pleroma/web/twitter_api/twitter_api.ex | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index d5c5cf5cf..912d5e278 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -14,17 +14,20 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end def fetch_friend_statuses(user, opts \\ %{}) do + opts = Map.put(opts, "blocking_user", user) ActivityPub.fetch_activities([user.ap_id | user.following], opts) |> activities_to_statuses(%{for: user}) end def fetch_public_statuses(user, opts \\ %{}) do opts = Map.put(opts, "local_only", true) + opts = Map.put(opts, "blocking_user", user) ActivityPub.fetch_public_activities(opts) |> activities_to_statuses(%{for: user}) end def fetch_public_and_external_statuses(user, opts \\ %{}) do + opts = Map.put(opts, "blocking_user", user) ActivityPub.fetch_public_activities(opts) |> activities_to_statuses(%{for: user}) end @@ -41,7 +44,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do def fetch_conversation(user, id) do with context when is_binary(context) <- conversation_id_to_context(id), - activities <- ActivityPub.fetch_activities_for_context(context), + activities <- ActivityPub.fetch_activities_for_context(context, %{"blocking_user" => user}), statuses <- activities |> activities_to_statuses(%{for: user}) do statuses -- cgit v1.2.3 From 9b63647aff337d8510dd7fc3ad838a365baa480f Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 2 Nov 2017 22:53:34 +0100 Subject: MastoApi: Add blocking to fetches. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 9399dee86..881bd38df 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -79,6 +79,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def home_timeline(%{assigns: %{user: user}} = conn, params) do params = params |> Map.put("type", ["Create", "Announce"]) + |> Map.put("blocking_user", user) activities = ActivityPub.fetch_activities([user.ap_id | user.following], params) |> Enum.reverse @@ -92,6 +93,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do params = params |> Map.put("type", ["Create", "Announce"]) |> Map.put("local_only", !!params["local"]) + |> Map.put("blocking_user", user) activities = ActivityPub.fetch_public_activities(params) |> Enum.reverse @@ -123,7 +125,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def get_context(%{assigns: %{user: user}} = conn, %{"id" => id}) do with %Activity{} = activity <- Repo.get(Activity, id), - activities <- ActivityPub.fetch_activities_for_context(activity.data["object"]["context"]), + activities <- ActivityPub.fetch_activities_for_context(activity.data["object"]["context"], %{"blocking_user" => user}), activities <- activities |> Enum.filter(fn (%{id: aid}) -> to_string(aid) != to_string(id) end), grouped_activities <- Enum.group_by(activities, fn (%{id: id}) -> id < activity.id end) do result = %{ @@ -246,6 +248,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do params = params |> Map.put("type", "Create") |> Map.put("local_only", !!params["local"]) + |> Map.put("blocking_user", user) activities = ActivityPub.fetch_public_activities(params) |> Enum.reverse @@ -338,6 +341,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do params = conn |> Map.put("type", "Create") |> Map.put("favorited_by", user.ap_id) + |> Map.put("blocking_user", user) activities = ActivityPub.fetch_activities([], params) |> Enum.reverse -- cgit v1.2.3 From 8a1a7191fd9348fb35fc1aa322dd90869c7dfceb Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 3 Nov 2017 08:13:39 +0100 Subject: MastoAPI: Fix mentions always being for an anonymous user. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 9399dee86..19e0be3a1 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -183,13 +183,13 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) case activity.data["type"] do "Create" -> - %{id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity})} + %{id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity, for: user})} "Like" -> liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) - %{id: id, type: "favourite", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: liked_activity})} + %{id: id, type: "favourite", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: liked_activity, for: user})} "Announce" -> announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) - %{id: id, type: "reblog", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: announced_activity})} + %{id: id, type: "reblog", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: announced_activity, for: user})} "Follow" -> %{id: id, type: "follow", created_at: created_at, account: AccountView.render("account.json", %{user: actor})} _ -> nil -- cgit v1.2.3 From 33beb51da423f5f80311453ad9025aa66984eb12 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 3 Nov 2017 08:23:31 +0100 Subject: MastoAPI: Add blocking to AccountView. --- lib/pleroma/web/mastodon_api/views/account_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index ff02587d6..cf97ab746 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -55,7 +55,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do id: target.id, following: User.following?(user, target), followed_by: User.following?(target, user), - blocking: false, + blocking: User.blocks?(user, target), muting: false, requested: false, domain_blocking: false -- cgit v1.2.3 From 5bf92e50be76b9dc2fa682b9f2ae252c0faad64e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 3 Nov 2017 08:38:05 +0100 Subject: MastoAPI: Add blocking. --- .../web/mastodon_api/mastodon_api_controller.ex | 24 ++++++++++++++++++++++ lib/pleroma/web/router.ex | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 881bd38df..5e299c976 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -311,6 +311,30 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + def block(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do + with %User{} = blocked <- Repo.get(User, id), + {:ok, blocker} <- User.block(blocker, blocked) do + render conn, AccountView, "relationship.json", %{user: blocker, target: blocked} + else + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) + end + end + + def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do + with %User{} = blocked <- Repo.get(User, id), + {:ok, blocker} <- User.unblock(blocker, blocked) do + render conn, AccountView, "relationship.json", %{user: blocker, target: blocked} + else + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) + end + end + def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do accounts = User.search(query, params["resolve"] == "true") diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 1fb5eadf6..c4bbaa265 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -58,8 +58,8 @@ defmodule Pleroma.Web.Router do get "/accounts/search", MastodonAPIController, :account_search post "/accounts/:id/follow", MastodonAPIController, :follow post "/accounts/:id/unfollow", MastodonAPIController, :unfollow - post "/accounts/:id/block", MastodonAPIController, :relationship_noop - post "/accounts/:id/unblock", MastodonAPIController, :relationship_noop + post "/accounts/:id/block", MastodonAPIController, :block + post "/accounts/:id/unblock", MastodonAPIController, :unblock post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop -- cgit v1.2.3 From c6b9b777dacef2fce51e43a25e3af9c9fac9a87e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 3 Nov 2017 08:51:17 +0100 Subject: MastoAPI: Add list of blocked users. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 9 +++++++++ lib/pleroma/web/router.ex | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 5e299c976..9c88cc4e8 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -335,6 +335,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + # TODO: Use proper query + def blocks(%{assigns: %{user: user}} = conn, _) do + with blocked_users <- user.info["blocks"] || [], + accounts <- Enum.map(blocked_users, fn (ap_id) -> User.get_cached_by_ap_id(ap_id) end) do + res = AccountView.render("accounts.json", users: accounts, for: user, as: :user) + json(conn, res) + end + end + def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do accounts = User.search(query, params["resolve"] == "true") diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index c4bbaa265..f96ec7213 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -65,7 +65,8 @@ defmodule Pleroma.Web.Router do post "/follows", MastodonAPIController, :follow - get "/blocks", MastodonAPIController, :empty_array + get "/blocks", MastodonAPIController, :blocks + get "/domain_blocks", MastodonAPIController, :empty_array get "/follow_requests", MastodonAPIController, :empty_array get "/mutes", MastodonAPIController, :empty_array -- cgit v1.2.3 From 046bee34d3b19c046dba59c1d6cb08548f978859 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 5 Nov 2017 12:05:25 +0100 Subject: Fix bug when no posts are in the db. --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 71e52cb46..d4eb50323 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -157,7 +157,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do # Only search through last 100_000 activities by default defp restrict_recent(query, _) do - since = Repo.aggregate(Activity, :max, :id) - 100_000 + since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000 from activity in query, where: activity.id > ^since -- cgit v1.2.3 From b0e27b21dd655a663969b8b179c2966974e6e046 Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 6 Nov 2017 21:51:31 +0200 Subject: Fix tootdon logins. --- lib/pleroma/web/oauth/oauth_controller.ex | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex index 3e66c3ee8..841df8c32 100644 --- a/lib/pleroma/web/oauth/oauth_controller.ex +++ b/lib/pleroma/web/oauth/oauth_controller.ex @@ -40,7 +40,8 @@ defmodule Pleroma.Web.OAuth.OAuthController do # - proper scope handling def token_exchange(conn, %{"grant_type" => "authorization_code"} = params) do with %App{} = app <- Repo.get_by(App, client_id: params["client_id"], client_secret: params["client_secret"]), - %Authorization{} = auth <- Repo.get_by(Authorization, token: params["code"], app_id: app.id), + fixed_token = fix_padding(params["code"]), + %Authorization{} = auth <- Repo.get_by(Authorization, token: fixed_token, app_id: app.id), {:ok, token} <- Token.exchange_token(app, auth) do response = %{ token_type: "Bearer", @@ -50,6 +51,14 @@ defmodule Pleroma.Web.OAuth.OAuthController do scope: "read write follow" } json(conn, response) + else + _error -> json(conn, %{error: "Invalid credentials"}) end end + + defp fix_padding(token) do + token + |> Base.url_decode64!(padding: false) + |> Base.url_encode64 + end end -- cgit v1.2.3 From f85566324ec7cf20f070850d0cd5bd3fec25445d Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 7 Nov 2017 00:33:44 +0200 Subject: Allow profile fetching for authenticated users only. --- lib/pleroma/web/router.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index f96ec7213..514320fd6 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -138,7 +138,6 @@ defmodule Pleroma.Web.Router do get "/search", TwitterAPI.Controller, :search get "/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline - get "/externalprofile/show", TwitterAPI.Controller, :external_profile end scope "/api", Pleroma.Web do @@ -176,6 +175,8 @@ defmodule Pleroma.Web.Router do get "/statuses/followers", TwitterAPI.Controller, :followers get "/statuses/friends", TwitterAPI.Controller, :friends + + get "/externalprofile/show", TwitterAPI.Controller, :external_profile end pipeline :ostatus do -- cgit v1.2.3 From 7de3a652147d9b7da9cf3fd82d8a8438df93bae8 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 7 Nov 2017 09:11:19 +0100 Subject: Longer timeouts for user fetching. --- lib/pleroma/web/web_finger/web_finger.ex | 4 ++-- lib/pleroma/web/websub/websub.ex | 6 +++++- 2 files changed, 7 insertions(+), 3 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 7cbafe11f..a54f3c54d 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -86,7 +86,7 @@ defmodule Pleroma.Web.WebFinger do end def find_lrdd_template(domain) do - with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do + with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true, timeout: 10_000, recv_timeout: 20_000) do get_template_from_xml(body) else e -> @@ -107,7 +107,7 @@ defmodule Pleroma.Web.WebFinger do with {:ok, template} <- find_lrdd_template(domain), address <- String.replace(template, "{uri}", URI.encode(account)), - response <- @httpoison.get(address, ["Accept": "application/xrd+xml"]), + response <- @httpoison.get(address, ["Accept": "application/xrd+xml"], timeout: 10_000, recv_timeout: 20_000), {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response, doc when doc != :error<- XML.parse_document(body), {:ok, data} <- webfinger_from_xml(doc) do diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex index db1577a93..41bdffb5b 100644 --- a/lib/pleroma/web/websub/websub.ex +++ b/lib/pleroma/web/websub/websub.ex @@ -142,7 +142,11 @@ defmodule Pleroma.Web.Websub do requester.(subscription) end - def gather_feed_data(topic, getter \\ &@httpoison.get/1) do + def long_get(url) do + @httpoison.get(url, [], timeout: 10_000, recv_timeout: 20_000) + end + + def gather_feed_data(topic, getter \\ &long_get/1) do with {:ok, response} <- getter.(topic), status_code when status_code in 200..299 <- response.status_code, body <- response.body, -- cgit v1.2.3 From 6dfa62800ad6cdcef9e73ecdabe45363c574a528 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 7 Nov 2017 09:41:35 +0100 Subject: Revert "Longer timeouts for user fetching." Breaks too many tests. This reverts commit 7de3a652147d9b7da9cf3fd82d8a8438df93bae8. --- lib/pleroma/web/web_finger/web_finger.ex | 4 ++-- lib/pleroma/web/websub/websub.ex | 6 +----- 2 files changed, 3 insertions(+), 7 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 a54f3c54d..7cbafe11f 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -86,7 +86,7 @@ defmodule Pleroma.Web.WebFinger do end def find_lrdd_template(domain) do - with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true, timeout: 10_000, recv_timeout: 20_000) do + with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do get_template_from_xml(body) else e -> @@ -107,7 +107,7 @@ defmodule Pleroma.Web.WebFinger do with {:ok, template} <- find_lrdd_template(domain), address <- String.replace(template, "{uri}", URI.encode(account)), - response <- @httpoison.get(address, ["Accept": "application/xrd+xml"], timeout: 10_000, recv_timeout: 20_000), + response <- @httpoison.get(address, ["Accept": "application/xrd+xml"]), {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- response, doc when doc != :error<- XML.parse_document(body), {:ok, data} <- webfinger_from_xml(doc) do diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex index 41bdffb5b..db1577a93 100644 --- a/lib/pleroma/web/websub/websub.ex +++ b/lib/pleroma/web/websub/websub.ex @@ -142,11 +142,7 @@ defmodule Pleroma.Web.Websub do requester.(subscription) end - def long_get(url) do - @httpoison.get(url, [], timeout: 10_000, recv_timeout: 20_000) - end - - def gather_feed_data(topic, getter \\ &long_get/1) do + def gather_feed_data(topic, getter \\ &@httpoison.get/1) do with {:ok, response} <- getter.(topic), status_code when status_code in 200..299 <- response.status_code, body <- response.body, -- cgit v1.2.3 From 7da978f3f54a8981c082625a8ad2fea48c918675 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 7 Nov 2017 12:06:37 +0100 Subject: Linkify fixes. --- lib/pleroma/formatter.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index f062d9a58..7ccd7e7bb 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Formatter do alias Pleroma.User - @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&]+[\w]/u + @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~]+[\w\/]/u def linkify(text) do Regex.replace(@link_regex, text, "\\0") end -- cgit v1.2.3 From 722c4614ad30d55e2c2c9a7e5f99d4c39a3ac95d Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 7 Nov 2017 16:45:27 +0100 Subject: Allow self-rt. --- lib/pleroma/web/common_api/common_api.ex | 1 - 1 file changed, 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 32a8a08c7..9ae7b095a 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -16,7 +16,6 @@ defmodule Pleroma.Web.CommonAPI do def repeat(id_or_ap_id, user) do with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id), - false <- activity.data["actor"] == user.ap_id, object <- Object.get_by_ap_id(activity.data["object"]["id"]) do ActivityPub.announce(user, object) else -- cgit v1.2.3 From c6210183e1d52b22133560eb31306919470e7a73 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 7 Nov 2017 21:28:31 +0200 Subject: Add mastodon API endpoint for custom emoji. --- lib/pleroma/formatter.ex | 1 - lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 13 +++++++++++++ lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/controllers/util_controller.ex | 2 +- 4 files changed, 15 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index f062d9a58..00be839d8 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -125,6 +125,5 @@ defmodule Pleroma.Formatter do def get_custom_emoji() do @emoji - |> Enum.into %{} end end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index b1a54a4f1..96cf39f1a 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -61,6 +61,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, response) end + def custom_emojis(conn, _params) do + mastodon_emoji = Pleroma.Formatter.get_custom_emoji() + |> Enum.map(fn {shortcode, relative_url} -> + url = to_string URI.merge(Web.base_url(), relative_url) + %{ + "shortcode" => shortcode, + "static_url" => url, + "url" => url + } + end) + json conn, mastodon_emoji + end + defp add_link_headers(conn, method, activities) do last = List.last(activities) first = List.first(activities) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index f96ec7213..b94056cd4 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -91,6 +91,7 @@ defmodule Pleroma.Web.Router do pipe_through :api get "/instance", MastodonAPIController, :masto_instance post "/apps", MastodonAPIController, :create_app + get "/custom_emojis", MastodonAPIController, :custom_emojis get "/timelines/public", MastodonAPIController, :public_timeline get "/timelines/tag/:tag", MastodonAPIController, :hashtag_timeline diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 792ae2a64..de2abd4d1 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -71,6 +71,6 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do end def emoji(conn, _params) do - json conn, Formatter.get_custom_emoji() + json conn, Enum.into(Formatter.get_custom_emoji(), %{}) end end -- cgit v1.2.3 From 6c0758e041e63d9a8792d2fe7760813f074bc06e Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 8 Nov 2017 00:04:53 +0200 Subject: TwitterAPI: Add blocks. --- lib/pleroma/web/router.ex | 2 ++ lib/pleroma/web/twitter_api/twitter_api.ex | 20 ++++++++++++++++++++ .../web/twitter_api/twitter_api_controller.ex | 16 ++++++++++++++++ 3 files changed, 38 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index b43235d8d..0a0aea966 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -164,6 +164,8 @@ defmodule Pleroma.Web.Router do post "/friendships/create", TwitterAPI.Controller, :follow post "/friendships/destroy", TwitterAPI.Controller, :unfollow + post "/blocks/create", TwitterAPI.Controller, :block + post "/blocks/destroy", TwitterAPI.Controller, :unblock post "/statusnet/media/upload", TwitterAPI.Controller, :upload post "/media/upload", TwitterAPI.Controller, :upload_json diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 912d5e278..baa3dac96 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -86,6 +86,26 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end end + def block(%User{} = blocker, params) do + with {:ok, %User{} = blocked} <- get_user(params), + {:ok, blocker} <- User.block(blocker, blocked) + do + {:ok, blocker, blocked} + else + err -> err + end + end + + def unblock(%User{} = blocker, params) do + with {:ok, %User{} = blocked} <- get_user(params), + {:ok, blocker} <- User.unblock(blocker, blocked) + do + {:ok, blocker, blocked} + else + err -> err + end + end + def repeat(%User{} = user, ap_id_or_id) do with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user), %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id), diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 6154d5ad7..2604b54dc 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -93,6 +93,22 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def block(%{assigns: %{user: user}} = conn, params) do + case TwitterAPI.block(user, params) do + {:ok, user, blocked} -> + render conn, UserView, "show.json", %{user: blocked, for: user} + {:error, msg} -> forbidden_json_reply(conn, msg) + end + end + + def unblock(%{assigns: %{user: user}} = conn, params) do + case TwitterAPI.unblock(user, params) do + {:ok, user, blocked} -> + render conn, UserView, "show.json", %{user: blocked, for: user} + {:error, msg} -> forbidden_json_reply(conn, msg) + end + end + def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do with {:ok, delete} <- CommonAPI.delete(id, user) do json = ActivityRepresenter.to_json(delete, %{user: user, for: user}) -- cgit v1.2.3 From 4656cb9b996169714e6a25c6025e96df81a34b62 Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 8 Nov 2017 13:02:00 +0200 Subject: TwitterAPI user view: add blocks. --- lib/pleroma/web/twitter_api/views/user_view.ex | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index f72e951eb..d33c054f3 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -21,6 +21,11 @@ defmodule Pleroma.Web.TwitterAPI.UserView do else false end + statusnet_blocking = if assigns[:for] do + User.blocks?(assigns[:for], user) + else + false + end user_info = User.get_cached_user_info(user) @@ -30,6 +35,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "favourites_count" => 0, "followers_count" => user_info[:follower_count], "following" => following, + "statusnet_blocking" => statusnet_blocking, "friends_count" => user_info[:following_count], "id" => user.id, "name" => user.name, -- cgit v1.2.3 From d2430d50070c290552fdeab468f2c537d7438c97 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 8 Nov 2017 17:25:18 +0100 Subject: Look through whole db for user timelines. They already have an index that's good enough. --- lib/pleroma/web/activity_pub/activity_pub.ex | 1 + lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 1 + lib/pleroma/web/twitter_api/twitter_api_controller.ex | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 4f7be4293..e7d4c48bd 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -157,6 +157,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_favorited_by(query, _), do: query # Only search through last 100_000 activities by default + defp restrict_recent(query, %{"whole_db" => true}), do: query defp restrict_recent(query, _) do since = (Repo.aggregate(Activity, :max, :id) || 0) - 100_000 diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 96cf39f1a..feaf9a900 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -122,6 +122,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do params = params |> Map.put("type", ["Create", "Announce"]) |> Map.put("actor_id", ap_id) + |> Map.put("whole_db", true) activities = ActivityPub.fetch_activities([], params) |> Enum.reverse diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 2604b54dc..125eb8a1e 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -68,7 +68,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 = Map.merge(params, %{"actor_id" => target_user.ap_id}) + params = Map.merge(params, %{"actor_id" => target_user.ap_id, "whole_db" => true}) statuses = TwitterAPI.fetch_user_statuses(user, params) conn |> json_reply(200, statuses |> Poison.encode!) -- cgit v1.2.3 From f7fc048aeb42ffbfce9f278b362c3d4766a4e9a2 Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 8 Nov 2017 19:13:03 +0200 Subject: TwitterAPI user view: add follows_you. --- lib/pleroma/web/twitter_api/views/user_view.ex | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index d33c054f3..6c5676c04 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -16,15 +16,14 @@ defmodule Pleroma.Web.TwitterAPI.UserView do def render("user.json", %{user: user = %User{}} = assigns) do image = User.avatar_url(user) - following = if assigns[:for] do - User.following?(assigns[:for], user) + {following, follows_you, statusnet_blocking} = if assigns[:for] do + { + User.following?(assigns[:for], user), + User.following?(user, assigns[:for]), + User.blocks?(assigns[:for], user) + } else - false - end - statusnet_blocking = if assigns[:for] do - User.blocks?(assigns[:for], user) - else - false + {false, false, false} end user_info = User.get_cached_user_info(user) @@ -35,6 +34,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "favourites_count" => 0, "followers_count" => user_info[:follower_count], "following" => following, + "follows_you" => follows_you, "statusnet_blocking" => statusnet_blocking, "friends_count" => user_info[:following_count], "id" => user.id, -- cgit v1.2.3 From 41b8a76e969e97aef68e7be1f1cdadcb31ea9b1d Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 9 Nov 2017 08:32:54 +0100 Subject: Re-fetch user data on salmon decode error. --- lib/pleroma/web/ostatus/ostatus.ex | 5 +++-- lib/pleroma/web/ostatus/ostatus_controller.ex | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 308e206c5..b0d2dda5d 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -221,7 +221,7 @@ defmodule Pleroma.Web.OStatus do Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname) end - def make_user(uri) do + def make_user(uri, update \\ false) do with {:ok, info} <- gather_user_info(uri) do data = %{ name: info["name"], @@ -231,7 +231,8 @@ defmodule Pleroma.Web.OStatus do avatar: info["avatar"], bio: info["bio"] } - with %User{} = user <- User.get_by_ap_id(data.ap_id) do + with false <- update, + %User{} = user <- User.get_by_ap_id(data.ap_id) do {:ok, user} else _e -> insert_or_update_user(data) end diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 4e3fbb4f6..67688ace4 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -5,6 +5,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter} alias Pleroma.Repo alias Pleroma.Web.{OStatus, Federator} + alias Pleroma.Web.XML import Ecto.Query def feed_redirect(conn, %{"nickname" => nickname}) do @@ -36,10 +37,26 @@ defmodule Pleroma.Web.OStatus.OStatusController do |> send_resp(200, response) end + defp decode_or_retry(body) do + with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body), + {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do + {:ok, doc} + else + _e -> + with [decoded | _] <- Pleroma.Web.Salmon.decode(body), + doc <- XML.parse_document(decoded), + uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc), + {:ok, user} <- Pleroma.Web.OStatus.make_user(uri, true), + {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body), + {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do + {:ok, doc} + end + end + end + def salmon_incoming(conn, params) do {:ok, body, _conn} = read_body(conn) - {:ok, magic_key} = Pleroma.Web.Salmon.fetch_magic_key(body) - {:ok, doc} = Pleroma.Web.Salmon.decode_and_validate(magic_key, body) + {:ok, doc} = decode_or_retry(body) Federator.enqueue(:incoming_doc, doc) -- cgit v1.2.3 From f1d27a5fbbe547a78f835bd65b43a652004d708e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 9 Nov 2017 10:41:19 +0100 Subject: Add actor column to activities. --- lib/pleroma/activity.ex | 1 + lib/pleroma/web/activity_pub/activity_pub.ex | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index f43b32b56..a35ccc9b4 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -6,6 +6,7 @@ defmodule Pleroma.Activity do schema "activities" do field :data, :map field :local, :boolean, default: true + field :actor, :string has_many :notifications, Notification timestamps() diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index e7d4c48bd..1624c6545 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do with nil <- Activity.get_by_ap_id(map["id"]), map <- lazy_put_activity_defaults(map), :ok <- insert_full_object(map) do - {:ok, activity} = Repo.insert(%Activity{data: map, local: local}) + {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"]}) Notification.create_notifications(activity) {:ok, activity} else @@ -137,7 +137,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_actor(query, %{"actor_id" => actor_id}) do from activity in query, - where: fragment("?->>'actor' = ?", activity.data, ^actor_id) + where: activity.actor == ^actor_id end defp restrict_actor(query, _), do: query @@ -168,14 +168,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do blocks = info["blocks"] || [] from activity in query, - where: fragment("not (?->>'actor' = ANY(?))", activity.data, ^blocks) + where: fragment("not (? = ANY(?))", activity.actor, ^blocks) end defp restrict_blocked(query, _), do: query def fetch_activities(recipients, opts \\ %{}) do base_query = from activity in Activity, limit: 20, - order_by: [desc: :id] + order_by: [fragment("? desc nulls last", activity.id)] base_query |> restrict_recipients(recipients) -- cgit v1.2.3 From e942e1e5528f587d2d5411f235d17599965f0f85 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 9 Nov 2017 14:11:37 +0200 Subject: Correct mimetype on bad uploads. --- lib/pleroma/upload.ex | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 2717377a3..d5723f5de 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -8,11 +8,19 @@ defmodule Pleroma.Upload do result_file = Path.join(upload_folder, file.filename) File.cp!(file.path, result_file) + # fix content type on some image uploads + matches = Regex.named_captures(~r/\.(?(jpg|jpeg|png|gif))$/i, file.filename) + content_type = if file.content_type == "application/octet-stream" and matches do + if matches["ext"] == "jpg", do: "image/jpeg", else: "image/#{matches["ext"]}" + else + file.content_type + end + %{ "type" => "Image", "url" => [%{ "type" => "Link", - "mediaType" => file.content_type, + "mediaType" => content_type, "href" => url_for(Path.join(uuid, :cow_uri.urlencode(file.filename))) }], "name" => file.filename, -- cgit v1.2.3 From 266d9c008d2a85395bb7ab773d5d548c89e7ed97 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 9 Nov 2017 16:48:45 +0100 Subject: MastoAPI: Fetch statuses in search. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index feaf9a900..c28e20ed1 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.TwitterAPI.TwitterAPI - alias Pleroma.Web.CommonAPI + alias Pleroma.Web.{CommonAPI, OStatus} import Ecto.Query import Logger @@ -361,11 +361,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do accounts = User.search(query, params["resolve"] == "true") + fetched = if Regex.match?(~r/https?:/, query) do + with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do + activities + else + _e -> [] + end + end || [] + q = from a in Activity, where: fragment("?->>'type' = 'Create'", a.data), where: fragment("to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)", a.data, ^query), limit: 20 - statuses = Repo.all(q) + statuses = Repo.all(q) ++ fetched res = %{ "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user), -- cgit v1.2.3 From e6a78c6ed0925c27ea4d194c0e52ab07542c444e Mon Sep 17 00:00:00 2001 From: eal Date: Fri, 10 Nov 2017 15:24:39 +0200 Subject: MastoAPI: Add notification get, clear and dismiss. --- lib/pleroma/notification.ex | 31 +++++++++++ .../web/mastodon_api/mastodon_api_controller.ex | 65 ++++++++++++++++------ lib/pleroma/web/router.ex | 3 + 3 files changed, 82 insertions(+), 17 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 00a382f31..039cc7312 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -36,6 +36,37 @@ defmodule Pleroma.Notification do Repo.all(query) end + def get(%{id: user_id} = _user, id) do + query = from n in Notification, + where: n.id == ^id, + preload: [:activity] + + notification = Repo.one(query) + case notification do + %{user_id: ^user_id} -> + {:ok, notification} + _ -> + {:error, "Cannot get notification"} + end + end + + def clear(user) do + query = from n in Notification, + where: n.user_id == ^user.id + + Repo.delete_all(query) + end + + def dismiss(%{id: user_id} = _user, id) do + notification = Repo.get(Notification, id) + case notification do + %{user_id: ^user_id} -> + Repo.delete(notification) + _ -> + {:error, "Cannot dismiss notification"} + end + end + def create_notifications(%Activity{id: id, data: %{"to" => to, "type" => type}} = activity) when type in ["Create", "Like", "Announce", "Follow"] do users = User.get_notified_from_activity(activity) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index feaf9a900..d95b18315 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -193,23 +193,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def notifications(%{assigns: %{user: user}} = conn, params) do notifications = Notification.for_user(user, params) - result = Enum.map(notifications, fn (%{id: id, activity: activity, inserted_at: created_at}) -> - actor = User.get_cached_by_ap_id(activity.data["actor"]) - created_at = NaiveDateTime.to_iso8601(created_at) - |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) - case activity.data["type"] do - "Create" -> - %{id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity, for: user})} - "Like" -> - liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) - %{id: id, type: "favourite", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: liked_activity, for: user})} - "Announce" -> - announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) - %{id: id, type: "reblog", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: announced_activity, for: user})} - "Follow" -> - %{id: id, type: "follow", created_at: created_at, account: AccountView.render("account.json", %{user: actor})} - _ -> nil - end + result = Enum.map(notifications, fn x -> + render_notification(user, x) end) |> Enum.filter(&(&1)) @@ -218,6 +203,33 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> json(result) end + def get_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, notification} <- Notification.get(user, id) do + json(conn, render_notification(user, notification)) + else + {:error, reason} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => reason})) + end + end + + def clear_notifications(%{assigns: %{user: user}} = conn, _params) do + Notification.clear(user) + json(conn, %{}) + end + + def dismiss_notification(%{assigns: %{user: user}} = conn, %{"id" => id} = _params) do + with {:ok, _notif} <- Notification.dismiss(user, id) do + json(conn, %{}) + else + {:error, reason} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => reason})) + end + end + def relationships(%{assigns: %{user: user}} = conn, %{"id" => id}) do id = List.wrap(id) q = from u in User, @@ -408,4 +420,23 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do Logger.debug("Unimplemented, returning an empty array") json(conn, []) end + + defp render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do + actor = User.get_cached_by_ap_id(activity.data["actor"]) + created_at = NaiveDateTime.to_iso8601(created_at) + |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) + case activity.data["type"] do + "Create" -> + %{id: id, type: "mention", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: activity, for: user})} + "Like" -> + liked_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) + %{id: id, type: "favourite", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: liked_activity, for: user})} + "Announce" -> + announced_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) + %{id: id, type: "reblog", created_at: created_at, account: AccountView.render("account.json", %{user: actor}), status: StatusView.render("status.json", %{activity: announced_activity, for: user})} + "Follow" -> + %{id: id, type: "follow", created_at: created_at, account: AccountView.render("account.json", %{user: actor})} + _ -> nil + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 0a0aea966..efd37ede2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -82,7 +82,10 @@ defmodule Pleroma.Web.Router do post "/statuses/:id/favourite", MastodonAPIController, :fav_status post "/statuses/:id/unfavourite", MastodonAPIController, :unfav_status + post "/notifications/clear", MastodonAPIController, :clear_notifications + post "/notifications/dismiss", MastodonAPIController, :dismiss_notification get "/notifications", MastodonAPIController, :notifications + get "/notifications/:id", MastodonAPIController, :get_notification post "/media", MastodonAPIController, :upload end -- cgit v1.2.3 From 6e9c22c0afaa67f0b94f602eceef5c57b7c9192f Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 10 Nov 2017 17:18:19 +0100 Subject: MastoAPI: Use string ids everywhere. --- lib/pleroma/web/mastodon_api/views/account_view.ex | 6 +++--- lib/pleroma/web/mastodon_api/views/status_view.ex | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index cf97ab746..16322cf21 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -18,7 +18,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do header = image_url(user.info["banner"]) || "https://placehold.it/700x335" %{ - id: user.id, + id: to_string(user.id), username: hd(String.split(user.nickname, "@")), acct: user.nickname, display_name: user.name, @@ -43,7 +43,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do def render("mention.json", %{user: user}) do %{ - id: user.id, + id: to_string(user.id), acct: user.nickname, username: hd(String.split(user.nickname, "@")), url: user.ap_id @@ -52,7 +52,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do def render("relationship.json", %{user: user, target: target}) do %{ - id: target.id, + id: to_string(target.id), following: User.following?(user, target), followed_by: User.following?(target, user), blocking: User.blocks?(user, target), diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 09a2ca404..a0acdb0ac 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -45,7 +45,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do name: "Web", website: nil }, - language: nil + language: nil, + emoji: [] } end -- cgit v1.2.3 From fd12e585c98d4cd2cca45f8ff2c368e80d21e111 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 10 Nov 2017 18:24:50 +0100 Subject: Handle existing redirect params. --- lib/pleroma/web/oauth/oauth_controller.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/oauth/oauth_controller.ex b/lib/pleroma/web/oauth/oauth_controller.ex index 841df8c32..e8483dec0 100644 --- a/lib/pleroma/web/oauth/oauth_controller.ex +++ b/lib/pleroma/web/oauth/oauth_controller.ex @@ -25,7 +25,8 @@ defmodule Pleroma.Web.OAuth.OAuthController do auth: auth } else - url = "#{redirect_uri}?code=#{auth.token}" + connector = if String.contains?(redirect_uri, "?"), do: "&", else: "?" + url = "#{redirect_uri}#{connector}code=#{auth.token}" url = if params["state"] do url <> "&state=#{params["state"]}" else -- cgit v1.2.3 From a1923d20e850c6b4f187928dd739314df84047b6 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 11 Nov 2017 11:18:05 +0100 Subject: MastoAPI: Fix reblog emojis. --- lib/pleroma/web/mastodon_api/views/status_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index a0acdb0ac..d97b2acb4 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -46,7 +46,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do website: nil }, language: nil, - emoji: [] + emojis: [] } end -- cgit v1.2.3 From bd5bdc4c247e2ebb239215540a51b69c356da65c Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 11 Nov 2017 14:59:25 +0100 Subject: MastoAPI: Basic streaming. --- lib/pleroma/application.ex | 3 +- lib/pleroma/web/activity_pub/activity_pub.ex | 3 + lib/pleroma/web/endpoint.ex | 1 + lib/pleroma/web/mastodon_api/mastodon_socket.ex | 27 +++++++++ lib/pleroma/web/streamer.ex | 45 +++++++++++++++ lib/transports.ex | 77 +++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 lib/pleroma/web/mastodon_api/mastodon_socket.ex create mode 100644 lib/pleroma/web/streamer.ex create mode 100644 lib/transports.ex (limited to 'lib') diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 1f0a05568..5422cbc28 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -19,7 +19,8 @@ defmodule Pleroma.Application do ttl_interval: 1000, limit: 2500 ]]), - worker(Pleroma.Web.Federator, []) + worker(Pleroma.Web.Federator, []), + worker(Pleroma.Web.Streamer, []) ] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 1624c6545..35536a1e4 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -22,6 +22,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional), {:ok, activity} <- insert(create_data, local), :ok <- maybe_federate(activity) do + if activity.data["type"] == "Create" and Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do + Pleroma.Web.Streamer.stream("public", activity) + end {:ok, activity} end end diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index a1b4108cd..dc1ba2a05 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -2,6 +2,7 @@ defmodule Pleroma.Web.Endpoint do use Phoenix.Endpoint, otp_app: :pleroma socket "/socket", Pleroma.Web.UserSocket + socket "/api/v1", Pleroma.Web.MastodonAPI.MastodonSocket # Serve at "/" the static files from "priv/static" directory. # diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex new file mode 100644 index 000000000..c27d025c4 --- /dev/null +++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex @@ -0,0 +1,27 @@ +defmodule Pleroma.Web.MastodonAPI.MastodonSocket do + use Phoenix.Socket + + transport :streaming, Phoenix.Transports.WebSocket.Raw + + def connect(params, socket) do + IO.inspect(params) + Pleroma.Web.Streamer.add_socket(params["stream"], socket) + {:ok, socket} + end + + def id(socket), do: nil + + def handle(:text, message, state) do + IO.inspect message + #| :ok + #| state + #| {:text, message} + #| {:text, message, state} + #| {:close, "Goodbye!"} + {:text, message} + end + + def handle(:closed, reason, _state) do + IO.inspect reason + end +end diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex new file mode 100644 index 000000000..cc3805894 --- /dev/null +++ b/lib/pleroma/web/streamer.ex @@ -0,0 +1,45 @@ +defmodule Pleroma.Web.Streamer do + use GenServer + require Logger + import Plug.Conn + + def start_link do + GenServer.start_link(__MODULE__, %{}, name: __MODULE__) + end + + def add_socket(topic, socket) do + GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic}) + end + + def stream(topic, item) do + GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item}) + end + + def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do + Logger.debug("Trying to push to #{topic}") + Logger.debug("Pushing item to #{topic}") + Enum.each(topics[topic] || [], fn (socket) -> + json = %{ + event: "update", + payload: Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: item) |> Poison.encode! + } |> Poison.encode! + + send socket.transport_pid, {:text, json} + end) + {:noreply, topics} + end + + def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do + sockets_for_topic = sockets[topic] || [] + sockets_for_topic = Enum.uniq([socket | sockets_for_topic]) + sockets = Map.put(sockets, topic, sockets_for_topic) + Logger.debug("Got new conn for #{topic}") + IO.inspect(sockets) + {:noreply, sockets} + end + + def handle_cast(m, state) do + IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}") + {:noreply, state} + end +end diff --git a/lib/transports.ex b/lib/transports.ex new file mode 100644 index 000000000..5600a4fdd --- /dev/null +++ b/lib/transports.ex @@ -0,0 +1,77 @@ +defmodule Phoenix.Transports.WebSocket.Raw do + import Plug.Conn, only: [ + fetch_query_params: 1, + send_resp: 3 + ] + alias Phoenix.Socket.Transport + + def default_config do + [ + timeout: 60_000, + transport_log: false, + cowboy: Phoenix.Endpoint.CowboyWebSocket + ] + end + + def init(%Plug.Conn{method: "GET"} = conn, {endpoint, handler, transport}) do + {_, opts} = handler.__transport__(transport) + + conn = conn + |> fetch_query_params + |> Transport.transport_log(opts[:transport_log]) + |> Transport.force_ssl(handler, endpoint, opts) + |> Transport.check_origin(handler, endpoint, opts) + + case conn do + %{halted: false} = conn -> + case Transport.connect(endpoint, handler, transport, __MODULE__, nil, conn.params) do + {:ok, socket} -> + {:ok, conn, {__MODULE__, {socket, opts}}} + :error -> + send_resp(conn, :forbidden, "") + {:error, conn} + end + _ -> + {:error, conn} + end + end + + def init(conn, _) do + send_resp(conn, :bad_request, "") + {:error, conn} + end + + def ws_init({socket, config}) do + Process.flag(:trap_exit, true) + {:ok, %{socket: socket}, config[:timeout]} + end + + def ws_handle(op, data, state) do + state.socket.handler + |> apply(:handle, [op, data, state]) + |> case do + {op, data} -> + {:reply, {op, data}, state} + {op, data, state} -> + {:reply, {op, data}, state} + %{} = state -> + {:ok, state} + _ -> + {:ok, state} + end + end + + def ws_info({op, data} = tuple, state) do + {:reply, tuple, state} + end + + def ws_info(_tuple, state), do: {:ok, state} + + def ws_close(state) do + ws_handle(:closed, :normal, state) + end + + def ws_terminate(reason, state) do + ws_handle(:closed, reason, state) + end +end -- cgit v1.2.3 From 414c52509bfcd9a4f7f4a0eecadb714ab8d46f3a Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 11 Nov 2017 20:00:11 +0100 Subject: MastoAPI: Websocket streaming for federated timeline. --- lib/pleroma/web/mastodon_api/mastodon_socket.ex | 19 ++++++++++----- lib/pleroma/web/streamer.ex | 31 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex index c27d025c4..f9c8cec32 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_socket.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex @@ -1,12 +1,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do use Phoenix.Socket - transport :streaming, Phoenix.Transports.WebSocket.Raw + transport :streaming, Phoenix.Transports.WebSocket.Raw, + timeout: :infinity # We never receive data. def connect(params, socket) do - IO.inspect(params) - Pleroma.Web.Streamer.add_socket(params["stream"], socket) - {:ok, socket} + if params["stream"] == "public" do + socket = socket + |> assign(:topic, params["stream"]) + Pleroma.Web.Streamer.add_socket(params["stream"], socket) + {:ok, socket} + else + :error + end end def id(socket), do: nil @@ -21,7 +27,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do {:text, message} end - def handle(:closed, reason, _state) do - IO.inspect reason + def handle(:closed, reason, %{socket: socket}) do + topic = socket.assigns[:topic] + Pleroma.Web.Streamer.remove_socket(topic, socket) end end diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex index cc3805894..3a7b91743 100644 --- a/lib/pleroma/web/streamer.ex +++ b/lib/pleroma/web/streamer.ex @@ -4,6 +4,10 @@ defmodule Pleroma.Web.Streamer do import Plug.Conn def start_link do + spawn(fn -> + Process.sleep(1000 * 30) # 30 seconds + GenServer.cast(__MODULE__, %{action: :ping}) + end) GenServer.start_link(__MODULE__, %{}, name: __MODULE__) end @@ -11,10 +15,28 @@ defmodule Pleroma.Web.Streamer do GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic}) end + def remove_socket(topic, socket) do + GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic}) + end + def stream(topic, item) do GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item}) end + def handle_cast(%{action: :ping}, topics) do + Map.values(topics) + |> List.flatten + |> Enum.each(fn (socket) -> + Logger.debug("Sending keepalive ping") + send socket.transport_pid, {:text, ""} + end) + spawn(fn -> + Process.sleep(1000 * 30) # 30 seconds + GenServer.cast(__MODULE__, %{action: :ping}) + end) + {:noreply, topics} + end + def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do Logger.debug("Trying to push to #{topic}") Logger.debug("Pushing item to #{topic}") @@ -38,6 +60,15 @@ defmodule Pleroma.Web.Streamer do {:noreply, sockets} end + def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do + sockets_for_topic = sockets[topic] || [] + sockets_for_topic = List.delete(sockets_for_topic, socket) + sockets = Map.put(sockets, topic, sockets_for_topic) + Logger.debug("Removed conn for #{topic}") + IO.inspect(sockets) + {:noreply, sockets} + end + def handle_cast(m, state) do IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}") {:noreply, state} -- cgit v1.2.3 From 260b148b920df030331b007d057fdfa35fa12839 Mon Sep 17 00:00:00 2001 From: eal Date: Sat, 11 Nov 2017 23:09:33 +0200 Subject: Use headers for file content type recognition. --- lib/pleroma/upload.ex | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index d5723f5de..ce6e23bae 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -9,9 +9,8 @@ defmodule Pleroma.Upload do File.cp!(file.path, result_file) # fix content type on some image uploads - matches = Regex.named_captures(~r/\.(?(jpg|jpeg|png|gif))$/i, file.filename) - content_type = if file.content_type == "application/octet-stream" and matches do - if matches["ext"] == "jpg", do: "image/jpeg", else: "image/#{matches["ext"]}" + content_type = if file.content_type == "application/octet-stream" do + get_content_type(file.path) else file.content_type end @@ -61,4 +60,30 @@ defmodule Pleroma.Upload do defp url_for(file) do "#{Web.base_url()}/media/#{file}" end + + def get_content_type(file) do + # PNG: 89 50 4E 47 0D 0A 1A 0A + # GIF: 47 49 46 38 37 61 + # GIF: 47 49 46 38 39 61 + # JPEG: FF D8 FF DB + # JPEG: FF D8 FF E0 ?? ?? 4A 46 49 46 00 01 + # JPEG: FF D8 FF E1 ?? ?? 45 78 69 66 00 00 + match = File.open(file, [:read], fn(f) -> + case IO.binread(f, 8) do + <<0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a>> -> + "image/png" + <<0x47, 0x49, 0x46, 0x38, _, 0x61, _, _>> -> + "image/gif" + <<0xff, 0xd8, 0xff, _, _, _, _, _>> -> + "image/jpeg" + _ -> + "application/octet-stream" + end + end) + + case match do + {:ok, type} -> type + _e -> "application/octet-stream" + end + end end -- cgit v1.2.3 From fc7483cb3c679040d40ea86f90384b097dcda2ca Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 12 Nov 2017 00:27:09 +0200 Subject: MastoAPI: Add update credentials endpoint. --- .../web/mastodon_api/mastodon_api_controller.ex | 51 ++++++++++++++++++++++ lib/pleroma/web/router.ex | 1 + 2 files changed, 52 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index c28e20ed1..fb06d55c1 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -23,6 +23,57 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + def update_credentials(%{assigns: %{user: user}} = conn, params) do + params = if bio = params["note"] do + Map.put(params, "bio", bio) + else + params + end + + params = if name = params["display_name"] do + Map.put(params, "name", name) + else + params + end + + user = if avatar = params["avatar"] do + with %Plug.Upload{} <- avatar, + {:ok, object} <- ActivityPub.upload(avatar), + change = Ecto.Changeset.change(user, %{avatar: object.data}), + {:ok, user} = Repo.update(change) do + user + else + _e -> user + end + else + user + end + + user = if banner = params["header"] do + with %Plug.Upload{} <- banner, + {:ok, object} <- ActivityPub.upload(banner), + new_info <- Map.put(user.info, "banner", object.data), + change <- User.info_changeset(user, %{info: new_info}), + {:ok, user} <- Repo.update(change) do + user + else + _e -> user + end + else + user + end + + with changeset <- User.update_changeset(user, params), + {:ok, user} <- Repo.update(changeset) do + json conn, AccountView.render("account.json", %{user: user}) + else + _e -> + conn + |> put_status(403) + |> json(%{error: "Invalid request"}) + end + end + def verify_credentials(%{assigns: %{user: user}} = conn, params) do account = AccountView.render("account.json", %{user: user}) json(conn, account) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 0a0aea966..4c74736e2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -53,6 +53,7 @@ defmodule Pleroma.Web.Router do scope "/api/v1", Pleroma.Web.MastodonAPI do pipe_through :authenticated_api + patch "/accounts/update_credentials", MastodonAPIController, :update_credentials get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials get "/accounts/relationships", MastodonAPIController, :relationships get "/accounts/search", MastodonAPIController, :account_search -- cgit v1.2.3 From 18a95cd3757a9b2f9f468f408da48bbd84a4d031 Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 12 Nov 2017 01:16:46 +0200 Subject: Add common video and audio types. --- lib/pleroma/upload.ex | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index ce6e23bae..3567c6c88 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -62,12 +62,6 @@ defmodule Pleroma.Upload do end def get_content_type(file) do - # PNG: 89 50 4E 47 0D 0A 1A 0A - # GIF: 47 49 46 38 37 61 - # GIF: 47 49 46 38 39 61 - # JPEG: FF D8 FF DB - # JPEG: FF D8 FF E0 ?? ?? 4A 46 49 46 00 01 - # JPEG: FF D8 FF E1 ?? ?? 45 78 69 66 00 00 match = File.open(file, [:read], fn(f) -> case IO.binread(f, 8) do <<0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a>> -> @@ -76,6 +70,16 @@ defmodule Pleroma.Upload do "image/gif" <<0xff, 0xd8, 0xff, _, _, _, _, _>> -> "image/jpeg" + <<0x1a, 0x45, 0xdf, 0xa3, _, _, _, _>> -> + "video/webm" + <<0x00, 0x00, 0x00, _, 0x66, 0x74, 0x79, 0x70>> -> + "video/mp4" + <<0x49, 0x44, 0x33, _, _, _, _, _>> -> + "audio/mpeg" + <<0x4f, 0x67, 0x67, 0x53, 0x00, 0x02, 0x00, 0x00>> -> + "audio/ogg" + <<0x52, 0x49, 0x46, 0x46, _, _, _, _>> -> + "audio/wav" _ -> "application/octet-stream" end -- cgit v1.2.3 From d293ceb1b535ab749fa841e18c1fa2ee63972afb Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 12 Nov 2017 14:23:05 +0100 Subject: Add Mastodon frontend. --- lib/pleroma/plugs/oauth_plug.ex | 8 +- lib/pleroma/web/endpoint.ex | 2 +- .../web/mastodon_api/mastodon_api_controller.ex | 115 ++++++++++++++++++++- .../web/mastodon_api/views/mastodon_view.ex | 5 + lib/pleroma/web/router.ex | 15 +++ .../templates/mastodon_api/mastodon/index.html.eex | 21 ++++ .../templates/mastodon_api/mastodon/login.html.eex | 10 ++ 7 files changed, 171 insertions(+), 5 deletions(-) create mode 100644 lib/pleroma/web/mastodon_api/views/mastodon_view.ex create mode 100644 lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex create mode 100644 lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex (limited to 'lib') diff --git a/lib/pleroma/plugs/oauth_plug.ex b/lib/pleroma/plugs/oauth_plug.ex index fc2a907a2..8366e35af 100644 --- a/lib/pleroma/plugs/oauth_plug.ex +++ b/lib/pleroma/plugs/oauth_plug.ex @@ -10,8 +10,12 @@ defmodule Pleroma.Plugs.OAuthPlug do def call(%{assigns: %{user: %User{}}} = conn, _), do: conn def call(conn, opts) do - with ["Bearer " <> header] <- get_req_header(conn, "authorization"), - %Token{user_id: user_id} <- Repo.get_by(Token, token: header), + token = case get_req_header(conn, "authorization") do + ["Bearer " <> header] -> header + _ -> get_session(conn, :oauth_token) + end + with token when not is_nil(token) <- token, + %Token{user_id: user_id} <- Repo.get_by(Token, token: token), %User{} = user <- Repo.get(User, user_id) do conn |> assign(:user, user) diff --git a/lib/pleroma/web/endpoint.ex b/lib/pleroma/web/endpoint.ex index dc1ba2a05..b57cf3917 100644 --- a/lib/pleroma/web/endpoint.ex +++ b/lib/pleroma/web/endpoint.ex @@ -12,7 +12,7 @@ defmodule Pleroma.Web.Endpoint do at: "/media", from: "uploads", gzip: false plug Plug.Static, at: "/", from: :pleroma, - only: ~w(index.html static finmoji emoji) + only: ~w(index.html static finmoji emoji packs sounds sw.js) # Code reloading can be explicitly enabled under the # :code_reloader configuration of your endpoint. diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index c28e20ed1..83003b917 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1,12 +1,13 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller alias Pleroma.{Repo, Activity, User, Notification} - alias Pleroma.Web.OAuth.App alias Pleroma.Web - alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} + alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.{CommonAPI, OStatus} + alias Pleroma.Web.OAuth.{Authorization, Token, App} + alias Comeonin.Pbkdf2 import Ecto.Query import Logger @@ -405,6 +406,116 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity}) end + def index(%{assigns: %{user: user}} = conn, _params) do + token = conn + |> get_session(:oauth_token) + + if user && token do + accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user})) + initial_state = %{ + meta: %{ + streaming_api_base_url: String.replace(Pleroma.Web.Endpoint.static_url(), "http", "ws"), + access_token: token, + locale: "en", + domain: Pleroma.Web.Endpoint.host(), + admin: "1", + me: "#{user.id}", + unfollow_modal: false, + boost_modal: false, + delete_modal: true, + auto_play_gif: false, + reduce_motion: false + }, + compose: %{ + me: "#{user.id}", + default_privacy: "public", + default_sensitive: false + }, + media_attachments: %{ + accept_content_types: [ + ".jpg", + ".jpeg", + ".png", + ".gif", + ".webm", + ".mp4", + ".m4v", + "image\/jpeg", + "image\/png", + "image\/gif", + "video\/webm", + "video\/mp4" + ] + }, + settings: %{ + onboarded: true, + home: %{ + shows: %{ + reblog: true, + reply: true + } + }, + notifications: %{ + alerts: %{ + follow: true, + favourite: true, + reblog: true, + mention: true + }, + shows: %{ + follow: true, + favourite: true, + reblog: true, + mention: true + }, + sounds: %{ + follow: true, + favourite: true, + reblog: true, + mention: true + } + } + }, + push_subscription: nil, + accounts: accounts, + custom_emojis: %{} + } |> Poison.encode! + conn + |> put_layout(false) + |> render(MastodonView, "index.html", %{initial_state: initial_state}) + else + conn + |> redirect(to: "/web/login") + end + end + + def login(conn, params) do + conn + |> render(MastodonView, "login.html") + end + + defp get_or_make_app() do + with %App{} = app <- Repo.get_by(App, client_name: "Mastodon-Local") do + {:ok, app} + else + _e -> + cs = App.register_changeset(%App{}, %{client_name: "Mastodon-Local", redirect_uris: ".", scopes: "read,write,follow"}) + Repo.insert(cs) + end + end + + def login_post(conn, %{"authorization" => %{ "name" => name, "password" => password}}) do + with %User{} = user <- User.get_cached_by_nickname(name), + true <- Pbkdf2.checkpw(password, user.password_hash), + {:ok, app} <- get_or_make_app(), + {:ok, auth} <- Authorization.create_authorization(app, user), + {:ok, token} <- Token.exchange_token(app, auth) do + conn + |> put_session(:oauth_token, token.token) + |> redirect(to: "/web/timelines/public") + end + end + def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do Logger.debug("Unimplemented, returning unmodified relationship") with %User{} = target <- Repo.get(User, id) do diff --git a/lib/pleroma/web/mastodon_api/views/mastodon_view.ex b/lib/pleroma/web/mastodon_api/views/mastodon_view.ex new file mode 100644 index 000000000..370fad374 --- /dev/null +++ b/lib/pleroma/web/mastodon_api/views/mastodon_view.ex @@ -0,0 +1,5 @@ +defmodule Pleroma.Web.MastodonAPI.MastodonView do + use Pleroma.Web, :view + import Phoenix.HTML + import Phoenix.HTML.Form +end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 0a0aea966..5c94ba392 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -21,6 +21,13 @@ defmodule Pleroma.Web.Router do plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1} end + pipeline :mastodon_html do + plug :accepts, ["html"] + plug :fetch_session + plug Pleroma.Plugs.OAuthPlug + plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} + end + pipeline :well_known do plug :accepts, ["xml", "xrd+xml"] end @@ -207,6 +214,14 @@ defmodule Pleroma.Web.Router do get "/webfinger", WebFinger.WebFingerController, :webfinger end + scope "/web", Pleroma.Web.MastodonAPI do + pipe_through :mastodon_html + + get "/login", MastodonAPIController, :login + post "/login", MastodonAPIController, :login_post + get "/*path", MastodonAPIController, :index + end + scope "/", Fallback do get "/*path", RedirectController, :redirector end diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex new file mode 100644 index 000000000..a05680205 --- /dev/null +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + +
+
+ + diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex new file mode 100644 index 000000000..6db4b05dc --- /dev/null +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex @@ -0,0 +1,10 @@ +

Login in to Mastodon Frontend

+<%= form_for @conn, mastodon_api_path(@conn, :login), [as: "authorization"], fn f -> %> +<%= label f, :name, "Name" %> +<%= text_input f, :name %> +
+<%= label f, :password, "Password" %> +<%= password_input f, :password %> +
+<%= submit "Authorize" %> +<% end %> -- cgit v1.2.3 From 083cd169d21b0f20332eac80b4481adb41810a64 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 13 Nov 2017 09:48:56 +0100 Subject: MastoAPI: Fix repeat api bug. Documentation was wrong, reblogging actually returns the reblog, not the reblogged status. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 41fbe55e7..c15c47fa1 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -223,9 +223,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do - with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(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} + with {:ok, announce, _activity} = CommonAPI.repeat(ap_id_or_id, user) do + render conn, StatusView, "status.json", %{activity: announce, for: user, as: :activity} end end -- cgit v1.2.3 From c84723b67956f731801c98a1335128d8cdbf9291 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 15:41:16 +0200 Subject: MastoAPI: Add media timelines. --- lib/pleroma/web/activity_pub/activity_pub.ex | 7 +++++++ 1 file changed, 7 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 35536a1e4..5b02fc2c6 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -159,6 +159,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end defp restrict_favorited_by(query, _), do: query + defp restrict_media(query, %{"only_media" => true}) do + from activity in query, + where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[]) + end + defp restrict_media(query, _), do: query + # Only search through last 100_000 activities by default defp restrict_recent(query, %{"whole_db" => true}), do: query defp restrict_recent(query, _) do @@ -191,6 +197,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do |> restrict_favorited_by(opts) |> restrict_recent(opts) |> restrict_blocked(opts) + |> restrict_media(opts) |> Repo.all |> Enum.reverse end -- cgit v1.2.3 From 06c3ee3bac9abe5a369364743143618c473eb7a9 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 15:50:23 +0200 Subject: Accept 1 as true. --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 5b02fc2c6..9af0f553c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -159,7 +159,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end defp restrict_favorited_by(query, _), do: query - defp restrict_media(query, %{"only_media" => true}) do + defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do from activity in query, where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[]) end -- cgit v1.2.3 From 3533bf7eacaceb493bd4bef4ae5625f7c99021b9 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 16:04:58 +0200 Subject: TwitterAPI: Add /friends/ids. --- lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/twitter_api_controller.ex | 12 ++++++++++++ 2 files changed, 13 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8fe1d8ec6..7a5c78867 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -189,6 +189,7 @@ defmodule Pleroma.Web.Router do get "/statuses/followers", TwitterAPI.Controller, :followers get "/statuses/friends", TwitterAPI.Controller, :friends + get "/friends/ids", TwitterAPI.Controller, :friends_ids get "/externalprofile/show", TwitterAPI.Controller, :external_profile end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 125eb8a1e..887474a23 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -265,6 +265,18 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def friends_ids(%{assigns: %{user: user}} = conn, _params) do + with {:ok, friends} <- User.get_friends(user) do + ids = friends + |> Enum.map(fn x -> x.id end) + |> Poison.encode! + json(conn, ids) + else + _e -> bad_request_reply(conn, "Can't get friends") + end + end + + def update_profile(%{assigns: %{user: user}} = conn, params) do params = if bio = params["description"] do Map.put(params, "bio", bio) -- cgit v1.2.3 From 22bbe271154731902f94ce9a8b035cb18c58e437 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 16:10:13 +0200 Subject: TwitterAPI: Add unimplemented /friendships/no_retweets/ids. --- lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/twitter_api_controller.ex | 4 ++++ 2 files changed, 5 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 7a5c78867..1fb8cb51a 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -190,6 +190,7 @@ defmodule Pleroma.Web.Router do get "/statuses/followers", TwitterAPI.Controller, :followers get "/statuses/friends", TwitterAPI.Controller, :friends get "/friends/ids", TwitterAPI.Controller, :friends_ids + get "/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array get "/externalprofile/show", TwitterAPI.Controller, :external_profile end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 887474a23..9ea0773c9 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -270,12 +270,16 @@ defmodule Pleroma.Web.TwitterAPI.Controller do ids = friends |> Enum.map(fn x -> x.id end) |> Poison.encode! + json(conn, ids) else _e -> bad_request_reply(conn, "Can't get friends") end end + def empty_array(conn, _params) do + json(conn, Poison.encode!([])) + end def update_profile(%{assigns: %{user: user}} = conn, params) do params = if bio = params["description"] do -- cgit v1.2.3 From 163c65820265de5d5dbed50831c218444ebbbed1 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 16:12:52 +0200 Subject: TwitterAPI: Add unimplemented /mutes/users/ids. --- lib/pleroma/web/router.ex | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 1fb8cb51a..339c10a66 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -192,6 +192,8 @@ defmodule Pleroma.Web.Router do get "/friends/ids", TwitterAPI.Controller, :friends_ids get "/friendships/no_retweets/ids", TwitterAPI.Controller, :empty_array + get "/mutes/users/ids", TwitterAPI.Controller, :empty_array + get "/externalprofile/show", TwitterAPI.Controller, :external_profile end -- cgit v1.2.3 From f9828e578cf4c193fb35cbcc55133cd8e615755d Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 17:34:48 +0200 Subject: TwitterAPI: Add /users/show. --- lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/twitter_api_controller.ex | 13 +++++++++++++ 2 files changed, 14 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8fe1d8ec6..71b4a3692 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -140,6 +140,7 @@ defmodule Pleroma.Web.Router do get "/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline get "/qvitter/statuses/user_timeline", TwitterAPI.Controller, :user_timeline + get "/users/show", TwitterAPI.Controller, :show_user get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 125eb8a1e..5e50c3b18 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -65,6 +65,19 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, json) end + def show_user(conn, params) do + with {:ok, shown} <- TwitterAPI.get_user(params) do + if user = conn.assigns.user do + render conn, UserView, "show.json", %{user: shown, for: user} + else + render conn, UserView, "show.json", %{user: shown} + end + else + {:error, msg} -> + bad_request_reply(conn, msg) + end + end + def user_timeline(%{assigns: %{user: user}} = conn, params) do case TwitterAPI.get_user(user, params) do {:ok, target_user} -> -- cgit v1.2.3 From ced0d64d75831c68851d4deef9875343d9114eaa Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Wed, 15 Nov 2017 18:58:13 +0100 Subject: MastoAPI: Make attachment ids strings. --- lib/pleroma/web/mastodon_api/views/status_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index d97b2acb4..38abdb35f 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -120,7 +120,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do << hash_id::signed-32, _rest::binary >> = :crypto.hash(:md5, href) %{ - id: attachment["id"] || hash_id, + id: to_string(attachment["id"] || hash_id), url: href, remote_url: href, preview_url: href, -- cgit v1.2.3 From 8de890a0d9388979d7b86744dfbed7ac08635fee Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 16 Nov 2017 09:40:06 +0100 Subject: Add custom emoji to Mastodon UI initial state. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index c15c47fa1..8b5714555 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -113,8 +113,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, response) end - def custom_emojis(conn, _params) do - mastodon_emoji = Pleroma.Formatter.get_custom_emoji() + defp mastodonized_emoji do + Pleroma.Formatter.get_custom_emoji() |> Enum.map(fn {shortcode, relative_url} -> url = to_string URI.merge(Web.base_url(), relative_url) %{ @@ -123,6 +123,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do "url" => url } end) + end + + def custom_emojis(conn, _params) do + mastodon_emoji = mastodonized_emoji() json conn, mastodon_emoji end @@ -473,6 +477,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> get_session(:oauth_token) if user && token do + mastodon_emoji = mastodonized_emoji() accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user})) initial_state = %{ meta: %{ @@ -540,7 +545,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do }, push_subscription: nil, accounts: accounts, - custom_emojis: %{} + custom_emojis: mastodon_emoji } |> Poison.encode! conn |> put_layout(false) -- cgit v1.2.3 From 189513e4ffd50e7247f25ea87f91ffa42f5a253c Mon Sep 17 00:00:00 2001 From: Shpuld Shpuldson Date: Thu, 16 Nov 2017 13:26:56 +0200 Subject: Add styles to make login page resemble mastodon's --- lib/pleroma/web/templates/layout/app.html.eex | 66 +++++++++++++++++++++- .../templates/mastodon_api/mastodon/login.html.eex | 8 +-- 2 files changed, 67 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/templates/layout/app.html.eex b/lib/pleroma/web/templates/layout/app.html.eex index 6cc3b7ac5..ebb574ad6 100644 --- a/lib/pleroma/web/templates/layout/app.html.eex +++ b/lib/pleroma/web/templates/layout/app.html.eex @@ -3,9 +3,71 @@ Pleroma + -

Welcome to Pleroma

- <%= render @view_module, @view_template, assigns %> +
+

Pleroma

+ <%= render @view_module, @view_template, assigns %> +
diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex index 6db4b05dc..89d97561b 100644 --- a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex @@ -1,10 +1,8 @@

Login in to Mastodon Frontend

<%= form_for @conn, mastodon_api_path(@conn, :login), [as: "authorization"], fn f -> %> -<%= label f, :name, "Name" %> -<%= text_input f, :name %> +<%= text_input f, :name, placeholder: "Username" %>
-<%= label f, :password, "Password" %> -<%= password_input f, :password %> +<%= password_input f, :password, placeholder: "Password" %>
-<%= submit "Authorize" %> +<%= submit "Log in" %> <% end %> -- cgit v1.2.3 From 43499848d06348b24620f470d187847db5931bc7 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 16 Nov 2017 12:58:33 +0100 Subject: Small css fix. --- lib/pleroma/web/templates/layout/app.html.eex | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/templates/layout/app.html.eex b/lib/pleroma/web/templates/layout/app.html.eex index ebb574ad6..2a8dede80 100644 --- a/lib/pleroma/web/templates/layout/app.html.eex +++ b/lib/pleroma/web/templates/layout/app.html.eex @@ -8,6 +8,7 @@ background-color: #282c37; font-family: sans-serif; color:white; + text-align: center; } .container { @@ -35,7 +36,7 @@ } input { - box-sizing: padding-box; + box-sizing: border-box; width: 100%; padding: 10px; margin-top: 20px; @@ -51,6 +52,7 @@ } button { + box-sizing: border-box; width: 100%; color: white; background-color: #419bdd; -- cgit v1.2.3 From 5719f69ae338bce2419a6ea572f34a68fda5d23c Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 16 Nov 2017 13:48:58 +0100 Subject: MastodonAPI: Stream fixes. --- lib/pleroma/web/activity_pub/activity_pub.ex | 3 +++ lib/pleroma/web/mastodon_api/mastodon_socket.ex | 11 +++++++++-- lib/pleroma/web/streamer.ex | 2 +- 3 files changed, 13 insertions(+), 3 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 9af0f553c..5cbf14868 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -24,6 +24,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do :ok <- maybe_federate(activity) do if activity.data["type"] == "Create" and Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do Pleroma.Web.Streamer.stream("public", activity) + if local do + Pleroma.Web.Streamer.stream("public:local", activity) + end end {:ok, activity} end diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex index f9c8cec32..af76c8701 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_socket.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex @@ -1,17 +1,24 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do use Phoenix.Socket + alias Pleroma.Web.OAuth.Token + alias Pleroma.{User, Repo} + transport :streaming, Phoenix.Transports.WebSocket.Raw, timeout: :infinity # We never receive data. def connect(params, socket) do - if params["stream"] == "public" 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"] <- params["stream"] do socket = socket |> assign(:topic, params["stream"]) + |> assign(:user, user) Pleroma.Web.Streamer.add_socket(params["stream"], socket) {:ok, socket} else - :error + _e -> :error end end diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex index 3a7b91743..3b2938676 100644 --- a/lib/pleroma/web/streamer.ex +++ b/lib/pleroma/web/streamer.ex @@ -43,7 +43,7 @@ defmodule Pleroma.Web.Streamer do Enum.each(topics[topic] || [], fn (socket) -> json = %{ event: "update", - payload: Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: item) |> Poison.encode! + payload: Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: item, for: socket.assigns[:user]) |> Poison.encode! } |> Poison.encode! send socket.transport_pid, {:text, json} -- cgit v1.2.3 From a743940463a7d0a7346f77792310dff6a98e7f31 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 16 Nov 2017 16:49:51 +0100 Subject: MastoAPI: Implement all streaming functions. --- lib/pleroma/notification.ex | 3 +- lib/pleroma/user.ex | 11 ++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 1 + .../web/mastodon_api/mastodon_api_controller.ex | 2 +- lib/pleroma/web/mastodon_api/mastodon_socket.ex | 2 +- lib/pleroma/web/streamer.ex | 42 ++++++++++++++++++++-- 6 files changed, 55 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 039cc7312..65e3265d4 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -78,8 +78,9 @@ defmodule Pleroma.Notification do # TODO move to sql, too. def create_notification(%Activity{} = activity, %User{} = user) do unless User.blocks?(user, %{ap_id: activity.data["actor"]}) do - notification = %Notification{user_id: user.id, activity_id: activity.id} + notification = %Notification{user_id: user.id, activity: activity} {:ok, notification} = Repo.insert(notification) + Pleroma.Web.Streamer.stream("user", notification) notification end end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 771c54e81..56502e897 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -284,6 +284,17 @@ defmodule Pleroma.User do Repo.all(query) end + def get_recipients_from_activity(%Activity{data: %{"to" => to}} = activity) do + query = from u in User, + where: u.local == true + + query = from u in query, + where: u.ap_id in ^to, + or_where: fragment("? \\\?| ?", u.following, ^to) + + Repo.all(query) + end + def search(query, resolve) do if resolve do User.get_or_fetch_by_nickname(query) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 5cbf14868..b4e59050b 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -24,6 +24,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do :ok <- maybe_federate(activity) do if activity.data["type"] == "Create" and Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do Pleroma.Web.Streamer.stream("public", activity) + Pleroma.Web.Streamer.stream("user", activity) if local do Pleroma.Web.Streamer.stream("public:local", activity) end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 8b5714555..bbd003b06 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -595,7 +595,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, []) end - defp render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do + def render_notification(user, %{id: id, activity: activity, inserted_at: created_at} = _params) do actor = User.get_cached_by_ap_id(activity.data["actor"]) created_at = NaiveDateTime.to_iso8601(created_at) |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex index af76c8701..1d276e64a 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_socket.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex @@ -11,7 +11,7 @@ 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"] <- params["stream"] do + stream when stream in ["public", "public:local", "user"] <- params["stream"] do socket = socket |> assign(:topic, params["stream"]) |> assign(:user, user) diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex index 3b2938676..9f1080015 100644 --- a/lib/pleroma/web/streamer.ex +++ b/lib/pleroma/web/streamer.ex @@ -2,6 +2,7 @@ defmodule Pleroma.Web.Streamer do use GenServer require Logger import Plug.Conn + alias Pleroma.{User, Notification} def start_link do spawn(fn -> @@ -37,9 +38,7 @@ defmodule Pleroma.Web.Streamer do {:noreply, topics} end - def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do - Logger.debug("Trying to push to #{topic}") - Logger.debug("Pushing item to #{topic}") + def push_to_socket(topics, topic, item) do Enum.each(topics[topic] || [], fn (socket) -> json = %{ event: "update", @@ -48,10 +47,46 @@ defmodule Pleroma.Web.Streamer do send socket.transport_pid, {:text, json} end) + end + + def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do + topic = "user:#{item.user_id}" + Enum.each(topics[topic] || [], fn (socket) -> + json = %{ + event: "notification", + payload: Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(socket.assigns["user"], item) |> Poison.encode! + } |> Poison.encode! + + send socket.transport_pid, {:text, json} + end) + {:noreply, topics} + end + + def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do + Logger.debug("Trying to push to users") + recipient_topics = User.get_recipients_from_activity(item) + |> Enum.map(fn (%{id: id}) -> "user:#{id}" end) + + Enum.each(recipient_topics, fn (topic) -> + push_to_socket(topics, topic, item) + end) + {:noreply, topics} + end + + def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do + Logger.debug("Trying to push to #{topic}") + Logger.debug("Pushing item to #{topic}") + push_to_socket(topics, topic, item) {:noreply, topics} end + defp internal_topic("user", socket) do + "user:#{socket.assigns[:user].id}" + end + defp internal_topic(topic, socket), do: topic + def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do + topic = internal_topic(topic, socket) sockets_for_topic = sockets[topic] || [] sockets_for_topic = Enum.uniq([socket | sockets_for_topic]) sockets = Map.put(sockets, topic, sockets_for_topic) @@ -61,6 +96,7 @@ defmodule Pleroma.Web.Streamer do end def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do + topic = internal_topic(topic, socket) sockets_for_topic = sockets[topic] || [] sockets_for_topic = List.delete(sockets_for_topic, socket) sockets = Map.put(sockets, topic, sockets_for_topic) -- cgit v1.2.3 From 9c60cc88dfd1401ddc7d67ea7ec6d0220f11c242 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 16 Nov 2017 20:47:44 +0200 Subject: Redirect to Getting Started on login. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index bbd003b06..e1256c7b6 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -579,7 +579,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do {:ok, token} <- Token.exchange_token(app, auth) do conn |> put_session(:oauth_token, token.token) - |> redirect(to: "/web/timelines/public") + |> redirect(to: "/web/getting-started") end end -- cgit v1.2.3 From 4647bcd6e647ad31ba492a6e712721b58bf47e83 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 18 Nov 2017 12:22:07 +0100 Subject: Don't start streamer during tests. --- lib/pleroma/application.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 5422cbc28..bfe16e13a 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -20,8 +20,8 @@ defmodule Pleroma.Application do limit: 2500 ]]), worker(Pleroma.Web.Federator, []), - worker(Pleroma.Web.Streamer, []) ] + ++ if Mix.env == :test, do: [], else: [worker(Pleroma.Web.Streamer, [])] # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html # for other strategies and supported options -- cgit v1.2.3 From 66e78c3ec4e524a31a4c12f4dbe682ccbbc0025d Mon Sep 17 00:00:00 2001 From: eal Date: Sat, 18 Nov 2017 14:43:41 +0200 Subject: Escape HTML instead of discarding it. --- lib/pleroma/web/common_api/utils.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 83a656011..21b6226b1 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -58,7 +58,8 @@ defmodule Pleroma.Web.CommonAPI.Utils do end def format_input(text, mentions, tags) do - HtmlSanitizeEx.strip_tags(text) + Phoenix.HTML.html_escape(text) + |> elem(1) |> Formatter.linkify |> String.replace("\n", "
\n") |> add_user_links(mentions) -- cgit v1.2.3 From fb118b2978686a44a15534b638ab7887fb38c03d Mon Sep 17 00:00:00 2001 From: eal Date: Sat, 18 Nov 2017 14:46:54 +0200 Subject: Don't insert newlines to generated HTML. MastoFE doesn't like them. --- lib/pleroma/web/common_api/utils.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 21b6226b1..7cce77b10 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -54,14 +54,14 @@ defmodule Pleroma.Web.CommonAPI.Utils do "#{shortname(name)}" _ -> "" end) - Enum.join([text | attachment_text], "
\n") + Enum.join([text | attachment_text], "
") end def format_input(text, mentions, tags) do Phoenix.HTML.html_escape(text) |> elem(1) |> Formatter.linkify - |> String.replace("\n", "
\n") + |> String.replace("\n", "
") |> add_user_links(mentions) # |> add_tag_links(tags) end -- cgit v1.2.3 From 31e4277ba5a2a793a0bc94f5d7682a48349583a3 Mon Sep 17 00:00:00 2001 From: eal Date: Sat, 18 Nov 2017 15:25:22 +0200 Subject: Don't add summary if empty. --- lib/pleroma/web/twitter_api/representers/activity_representer.ex | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (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 3fbeb86ba..b17013d87 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -135,8 +135,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do tags = activity.data["object"]["tag"] || [] possibly_sensitive = Enum.member?(tags, "nsfw") - content = if activity.data["object"]["summary"] do - "#{activity.data["object"]["summary"]}
#{content}" + summary = activity.data["object"]["summary"] + content = if !!summary and summary != "" do + "#{activity.data["object"]["summary"]}
#{content}" else content end -- cgit v1.2.3 From f53cdabcdf5e64c862e0532e6d5ad26666e9c83f Mon Sep 17 00:00:00 2001 From: eal Date: Sat, 18 Nov 2017 16:30:18 +0200 Subject: Add #nsfw tag if sensitive content bit is set --- lib/pleroma/formatter.ex | 3 ++- lib/pleroma/web/common_api/common_api.ex | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 27e2adc16..fbcbca979 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -7,9 +7,10 @@ defmodule Pleroma.Formatter do end @tag_regex ~r/\#\w+/u - def parse_tags(text) do + def parse_tags(text, data \\ %{}) do Regex.scan(@tag_regex, text) |> Enum.map(fn (["#" <> tag = full_tag]) -> {full_tag, String.downcase(tag)} end) + |> (fn map -> if data["sensitive"], do: [{"#nsfw", "nsfw"}] ++ map, else: map end).() end def parse_mentions(text) do diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 9ae7b095a..9bc7f2ce6 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -55,7 +55,7 @@ defmodule Pleroma.Web.CommonAPI do mentions <- Formatter.parse_mentions(status), inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]), to <- to_for_user_and_mentions(user, mentions, inReplyTo), - tags <- Formatter.parse_tags(status), + tags <- Formatter.parse_tags(status, data), content_html <- make_content_html(status, mentions, attachments, tags), context <- make_context(inReplyTo), cw <- data["spoiler_text"], -- cgit v1.2.3 From 59770c3f5c9a038dbde6b2e1cd1551a7b8f1672b Mon Sep 17 00:00:00 2001 From: Thog Date: Sun, 19 Nov 2017 02:22:07 +0100 Subject: Fix all compilation warnings --- lib/pleroma/PasswordResetToken.ex | 6 ++-- lib/pleroma/notification.ex | 2 +- lib/pleroma/object.ex | 2 +- lib/pleroma/plugs/oauth_plug.ex | 2 +- lib/pleroma/user.ex | 11 ++++--- lib/pleroma/web/activity_pub/activity_pub.ex | 7 ++--- lib/pleroma/web/activity_pub/utils.ex | 6 ++-- lib/pleroma/web/common_api/utils.ex | 2 +- lib/pleroma/web/federator/federator.ex | 12 ++++---- .../web/mastodon_api/mastodon_api_controller.ex | 27 +++++++++-------- lib/pleroma/web/mastodon_api/mastodon_socket.ex | 6 ++-- lib/pleroma/web/mastodon_api/views/account_view.ex | 2 +- lib/pleroma/web/ostatus/activity_representer.ex | 8 ++--- lib/pleroma/web/ostatus/feed_representer.ex | 2 +- lib/pleroma/web/ostatus/handlers/delete_handler.ex | 6 ++-- lib/pleroma/web/ostatus/ostatus.ex | 5 ++-- lib/pleroma/web/ostatus/ostatus_controller.ex | 4 +-- lib/pleroma/web/streamer.ex | 34 +++++++++++----------- .../representers/activity_representer.ex | 2 +- lib/pleroma/web/twitter_api/twitter_api.ex | 5 ++-- .../web/twitter_api/twitter_api_controller.ex | 16 +++++----- lib/pleroma/web/twitter_api/views/user_view.ex | 6 ++-- lib/pleroma/web/web_finger/web_finger.ex | 2 +- lib/pleroma/web/xml/xml.ex | 4 +-- lib/transports.ex | 2 +- lib/xml_builder.ex | 2 +- 26 files changed, 89 insertions(+), 94 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/PasswordResetToken.ex b/lib/pleroma/PasswordResetToken.ex index 79e60bf69..52b1fcd50 100644 --- a/lib/pleroma/PasswordResetToken.ex +++ b/lib/pleroma/PasswordResetToken.ex @@ -1,7 +1,7 @@ defmodule Pleroma.PasswordResetToken do use Ecto.Schema - import Ecto.{Changeset, Query} + import Ecto.Changeset alias Pleroma.{User, PasswordResetToken, Repo} @@ -26,7 +26,7 @@ defmodule Pleroma.PasswordResetToken do end def used_changeset(struct) do - changeset = struct + struct |> cast(%{}, []) |> put_change(:used, true) end @@ -34,7 +34,7 @@ defmodule Pleroma.PasswordResetToken do def reset_password(token, data) do with %{used: false} = token <- Repo.get_by(PasswordResetToken, %{token: token}), %User{} = user <- Repo.get(User, token.user_id), - {:ok, user} <- User.reset_password(user, data), + {:ok, _user} <- User.reset_password(user, data), {:ok, token} <- Repo.update(used_changeset(token)) do {:ok, token} else diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 65e3265d4..241d6a9e0 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -67,7 +67,7 @@ defmodule Pleroma.Notification do end end - def create_notifications(%Activity{id: id, data: %{"to" => to, "type" => type}} = activity) when type in ["Create", "Like", "Announce", "Follow"] do + def create_notifications(%Activity{id: _, data: %{"to" => _, "type" => type}} = activity) when type in ["Create", "Like", "Announce", "Follow"] do users = User.get_notified_from_activity(activity) notifications = Enum.map(users, fn (user) -> create_notification(activity, user) end) diff --git a/lib/pleroma/object.ex b/lib/pleroma/object.ex index a5a1d6a76..30ba7b57a 100644 --- a/lib/pleroma/object.ex +++ b/lib/pleroma/object.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Object do end def change(struct, params \\ %{}) do - changeset = struct + struct |> cast(params, [:data]) |> validate_required([:data]) |> unique_constraint(:ap_id, name: :objects_unique_apid_index) diff --git a/lib/pleroma/plugs/oauth_plug.ex b/lib/pleroma/plugs/oauth_plug.ex index 8366e35af..775423bb1 100644 --- a/lib/pleroma/plugs/oauth_plug.ex +++ b/lib/pleroma/plugs/oauth_plug.ex @@ -9,7 +9,7 @@ defmodule Pleroma.Plugs.OAuthPlug do end def call(%{assigns: %{user: %User{}}} = conn, _), do: conn - def call(conn, opts) do + def call(conn, _) do token = case get_req_header(conn, "authorization") do ["Bearer " <> header] -> header _ -> get_session(conn, :oauth_token) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 56502e897..fbeeef003 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -5,7 +5,6 @@ defmodule Pleroma.User do alias Pleroma.{Repo, User, Object, Web, Activity, Notification} alias Comeonin.Pbkdf2 alias Pleroma.Web.{OStatus, Websub} - alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.Utils schema "users" do @@ -89,7 +88,7 @@ defmodule Pleroma.User do end def update_changeset(struct, params \\ %{}) do - changeset = struct + struct |> cast(params, [:bio, :name]) |> unique_constraint(:nickname) |> validate_format(:nickname, ~r/^[a-zA-Z\d]+$/) @@ -159,7 +158,7 @@ defmodule Pleroma.User do |> follow_changeset(%{following: following}) |> Repo.update - {:ok, followed} = update_follower_count(followed) + {:ok, _} = update_follower_count(followed) follower end @@ -214,7 +213,7 @@ defmodule Pleroma.User do with %User{} = user <- get_by_nickname(nickname) do user else _e -> - with [nick, domain] <- String.split(nickname, "@"), + with [_nick, _domain] <- String.split(nickname, "@"), {:ok, user} <- OStatus.make_user(nickname) do user else _e -> nil @@ -276,7 +275,7 @@ defmodule Pleroma.User do Repo.update(cs) end - def get_notified_from_activity(%Activity{data: %{"to" => to}} = activity) do + def get_notified_from_activity(%Activity{data: %{"to" => to}}) do query = from u in User, where: u.ap_id in ^to, where: u.local == true @@ -284,7 +283,7 @@ defmodule Pleroma.User do Repo.all(query) end - def get_recipients_from_activity(%Activity{data: %{"to" => to}} = activity) do + def get_recipients_from_activity(%Activity{data: %{"to" => to}}) do query = from u in User, where: u.local == true diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index b4e59050b..4aade6e40 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1,6 +1,5 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do - alias Pleroma.{Activity, Repo, Object, Upload, User, Web, Notification} - alias Ecto.{Changeset, UUID} + alias Pleroma.{Activity, Repo, Object, Upload, User, Notification} import Ecto.Query import Pleroma.Web.ActivityPub.Utils require Logger @@ -34,7 +33,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end # TODO: This is weird, maybe we shouldn't check here if we can make the activity. - def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do + def like(%User{ap_id: ap_id} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do with nil <- get_existing_like(ap_id, object), like_data <- make_like_data(user, object, activity_id), {:ok, activity} <- insert(like_data, local), @@ -56,7 +55,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - def announce(%User{ap_id: ap_id} = user, %Object{data: %{"id" => id}} = object, activity_id \\ nil, local \\ true) do + def announce(%User{ap_id: _} = user, %Object{data: %{"id" => _}} = object, activity_id \\ nil, local \\ true) do with announce_data <- make_announce_data(user, object, activity_id), {:ok, activity} <- insert(announce_data, local), {:ok, object} <- add_announce_to_object(activity, object), diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 4e3a7e2bd..51fac6fe2 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -64,7 +64,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do Inserts a full object if it is contained in an activity. """ def insert_full_object(%{"object" => object_data}) when is_map(object_data) do - with {:ok, object} <- Object.create(object_data) do + with {:ok, _} <- Object.create(object_data) do :ok end end @@ -88,7 +88,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do @doc """ Returns an existing like if a user already liked an object """ - def get_existing_like(actor, %{data: %{"id" => id}} = object) do + 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 @@ -201,7 +201,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do def make_create_data(params, additional) do published = params.published || make_date() - activity = %{ + %{ "type" => "Create", "to" => params.to |> Enum.uniq, "actor" => params.actor.ap_id, diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 7cce77b10..1a23b1ad2 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -57,7 +57,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do Enum.join([text | attachment_text], "
") end - def format_input(text, mentions, tags) do + def format_input(text, mentions, _tags) do Phoenix.HTML.html_escape(text) |> elem(1) |> Formatter.linkify diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex index 4d6ebff8e..eccade883 100644 --- a/lib/pleroma/web/federator/federator.ex +++ b/lib/pleroma/web/federator/federator.ex @@ -71,7 +71,7 @@ defmodule Pleroma.Web.Federator do end end - def handle(type, payload) do + def handle(type, _) do Logger.debug(fn -> "Unknown task: #{type}" end) {:error, "Don't know what do do with this"} end @@ -101,14 +101,14 @@ defmodule Pleroma.Web.Federator do {:noreply, {running_jobs, queue}} end + def handle_cast(m, state) do + IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}") + {:noreply, state} + end + def handle_info({:DOWN, ref, :process, _pid, _reason}, {running_jobs, queue}) do running_jobs = :sets.del_element(ref, running_jobs) {running_jobs, queue} = maybe_start_job(running_jobs, queue) {:noreply, {running_jobs, queue}} end - - def handle_cast(m, state) do - IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}") - {:noreply, state} - end end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index e1256c7b6..a3587ff44 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -4,12 +4,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do alias Pleroma.Web alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView} alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.{CommonAPI, OStatus} alias Pleroma.Web.OAuth.{Authorization, Token, App} alias Comeonin.Pbkdf2 import Ecto.Query - import Logger + require Logger def create_app(conn, params) do with cs <- App.register_changeset(%App{}, params) |> IO.inspect, @@ -75,7 +74,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def verify_credentials(%{assigns: %{user: user}} = conn, params) do + def verify_credentials(%{assigns: %{user: user}} = conn, _) do account = AccountView.render("account.json", %{user: user}) json(conn, account) end @@ -207,7 +206,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def post_status(%{assigns: %{user: user}} = conn, %{"status" => status} = params) do + def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do params = params |> Map.put("in_reply_to_status_id", params["in_reply_to_id"]) @@ -293,7 +292,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do render conn, AccountView, "relationships.json", %{user: user, targets: targets} end - def upload(%{assigns: %{user: user}} = conn, %{"file" => file}) do + def upload(%{assigns: %{user: _}} = conn, %{"file" => file}) do with {:ok, object} <- ActivityPub.upload(file) do data = object.data |> Map.put("id", object.id) @@ -303,7 +302,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def favourited_by(conn, %{"id" => id}) do - with %Activity{data: %{"object" => %{"likes" => likes} = data}} <- Repo.get(Activity, id) do + with %Activity{data: %{"object" => %{"likes" => likes}}} <- Repo.get(Activity, id) do q = from u in User, where: u.ap_id in ^likes users = Repo.all(q) @@ -356,10 +355,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do with %User{} = followed <- Repo.get(User, id), {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.follow(follower, followed) do + {:ok, _activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "relationship.json", %{user: follower, target: followed} else - {:error, message} = err -> + {:error, message} -> conn |> put_resp_content_type("application/json") |> send_resp(403, Poison.encode!(%{"error" => message})) @@ -369,10 +368,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do with %User{} = followed <- Repo.get_by(User, nickname: uri), {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.follow(follower, followed) do + {:ok, _activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "account.json", %{user: followed} else - {:error, message} = err -> + {:error, message} -> conn |> put_resp_content_type("application/json") |> send_resp(403, Poison.encode!(%{"error" => message})) @@ -397,7 +396,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do {:ok, blocker} <- User.block(blocker, blocked) do render conn, AccountView, "relationship.json", %{user: blocker, target: blocked} else - {:error, message} = err -> + {:error, message} -> conn |> put_resp_content_type("application/json") |> send_resp(403, Poison.encode!(%{"error" => message})) @@ -409,7 +408,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do {:ok, blocker} <- User.unblock(blocker, blocked) do render conn, AccountView, "relationship.json", %{user: blocker, target: blocked} else - {:error, message} = err -> + {:error, message} -> conn |> put_resp_content_type("application/json") |> send_resp(403, Poison.encode!(%{"error" => message})) @@ -459,7 +458,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, res) end - def favourites(%{assigns: %{user: user}} = conn, params) do + def favourites(%{assigns: %{user: user}} = conn, _) do params = conn |> Map.put("type", "Create") |> Map.put("favorited_by", user.ap_id) @@ -556,7 +555,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end - def login(conn, params) do + def login(conn, _) do conn |> render(MastodonView, "login.html") end diff --git a/lib/pleroma/web/mastodon_api/mastodon_socket.ex b/lib/pleroma/web/mastodon_api/mastodon_socket.ex index 1d276e64a..fe71ea271 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_socket.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_socket.ex @@ -22,9 +22,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do end end - def id(socket), do: nil + def id(_), do: nil - def handle(:text, message, state) do + def handle(:text, message, _state) do IO.inspect message #| :ok #| state @@ -34,7 +34,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonSocket do {:text, message} end - def handle(:closed, reason, %{socket: socket}) do + def handle(:closed, _, %{socket: socket}) do topic = socket.assigns[:topic] Pleroma.Web.Streamer.remove_socket(topic, socket) end diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 16322cf21..02f1e60bb 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -4,7 +4,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.CommonAPI.Utils - defp image_url(%{"url" => [ %{ "href" => href } | t ]}), do: href + defp image_url(%{"url" => [ %{ "href" => href } | _ ]}), do: href defp image_url(_), do: nil def render("accounts.json", %{users: users} = opts) do diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index 2bff57e76..cfc342fca 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -108,7 +108,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do updated_at = activity.data["published"] inserted_at = activity.data["published"] - in_reply_to = get_in_reply_to(activity.data) + _in_reply_to = get_in_reply_to(activity.data) author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: [] mentions = activity.data["to"] |> get_mentions @@ -136,7 +136,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do updated_at = activity.data["published"] inserted_at = activity.data["published"] - in_reply_to = get_in_reply_to(activity.data) + _in_reply_to = get_in_reply_to(activity.data) author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: [] retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) @@ -233,6 +233,8 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do ] ++ author end + def to_simple_form(_, _, _), do: nil + def wrap_with_entry(simple_form) do [{ :entry, [ @@ -244,6 +246,4 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do ], simple_form }] end - - def to_simple_form(_, _, _), do: nil end diff --git a/lib/pleroma/web/ostatus/feed_representer.ex b/lib/pleroma/web/ostatus/feed_representer.ex index 6b67b8ddf..08710f246 100644 --- a/lib/pleroma/web/ostatus/feed_representer.ex +++ b/lib/pleroma/web/ostatus/feed_representer.ex @@ -2,7 +2,7 @@ defmodule Pleroma.Web.OStatus.FeedRepresenter do alias Pleroma.Web.OStatus alias Pleroma.Web.OStatus.{UserRepresenter, ActivityRepresenter} - def to_simple_form(user, activities, users) do + def to_simple_form(user, activities, _users) do most_recent_update = (List.first(activities) || user).updated_at |> NaiveDateTime.to_iso8601 diff --git a/lib/pleroma/web/ostatus/handlers/delete_handler.ex b/lib/pleroma/web/ostatus/handlers/delete_handler.ex index 29fe4052c..4f3016b65 100644 --- a/lib/pleroma/web/ostatus/handlers/delete_handler.ex +++ b/lib/pleroma/web/ostatus/handlers/delete_handler.ex @@ -1,10 +1,10 @@ defmodule Pleroma.Web.OStatus.DeleteHandler do require Logger - alias Pleroma.Web.{XML, OStatus} - alias Pleroma.{Activity, Object, Repo} + alias Pleroma.Web.XML + alias Pleroma.Object alias Pleroma.Web.ActivityPub.ActivityPub - def handle_delete(entry, doc \\ nil) do + def handle_delete(entry, _doc \\ nil) do with id <- XML.string_from_xpath("//id", entry), object when not is_nil(object) <- Object.get_by_ap_id(id), {:ok, delete} <- ActivityPub.delete(object, false) do diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index b0d2dda5d..745539b3e 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -7,7 +7,6 @@ defmodule Pleroma.Web.OStatus do alias Pleroma.{Repo, User, Web, Object, Activity} alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.{WebFinger, Websub} alias Pleroma.Web.OStatus.{FollowHandler, NoteHandler, DeleteHandler} @@ -112,7 +111,7 @@ defmodule Pleroma.Web.OStatus do with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry), %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do {:ok, activity} - else e -> + else _ -> Logger.debug("Couldn't get, will try to fetch") with href when not is_nil(href) <- string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry), {:ok, [favorited_activity]} <- fetch_activity_from_url(href) do @@ -191,7 +190,7 @@ defmodule Pleroma.Web.OStatus do false <- new_data == old_data do change = Ecto.Changeset.change(user, new_data) Repo.update(change) - else e -> + else _ -> {:ok, user} end end diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 67688ace4..1ac07546f 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -46,7 +46,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do with [decoded | _] <- Pleroma.Web.Salmon.decode(body), doc <- XML.parse_document(decoded), uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc), - {:ok, user} <- Pleroma.Web.OStatus.make_user(uri, true), + {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true), {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body), {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do {:ok, doc} @@ -54,7 +54,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do end end - def salmon_incoming(conn, params) do + def salmon_incoming(conn, _) do {:ok, body, _conn} = read_body(conn) {:ok, doc} = decode_or_retry(body) diff --git a/lib/pleroma/web/streamer.ex b/lib/pleroma/web/streamer.ex index 9f1080015..d64e6c393 100644 --- a/lib/pleroma/web/streamer.ex +++ b/lib/pleroma/web/streamer.ex @@ -1,7 +1,6 @@ defmodule Pleroma.Web.Streamer do use GenServer require Logger - import Plug.Conn alias Pleroma.{User, Notification} def start_link do @@ -38,17 +37,6 @@ defmodule Pleroma.Web.Streamer do {:noreply, topics} end - def push_to_socket(topics, topic, item) do - Enum.each(topics[topic] || [], fn (socket) -> - json = %{ - event: "update", - payload: Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: item, for: socket.assigns[:user]) |> Poison.encode! - } |> Poison.encode! - - send socket.transport_pid, {:text, json} - end) - end - def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do topic = "user:#{item.user_id}" Enum.each(topics[topic] || [], fn (socket) -> @@ -80,11 +68,6 @@ defmodule Pleroma.Web.Streamer do {:noreply, topics} end - defp internal_topic("user", socket) do - "user:#{socket.assigns[:user].id}" - end - defp internal_topic(topic, socket), do: topic - def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do topic = internal_topic(topic, socket) sockets_for_topic = sockets[topic] || [] @@ -109,4 +92,21 @@ defmodule Pleroma.Web.Streamer do IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}") {:noreply, state} end + + def push_to_socket(topics, topic, item) do + Enum.each(topics[topic] || [], fn (socket) -> + json = %{ + event: "update", + payload: Pleroma.Web.MastodonAPI.StatusView.render("status.json", activity: item, for: socket.assigns[:user]) |> Poison.encode! + } |> Poison.encode! + + send socket.transport_pid, {:text, json} + end) + end + + defp internal_topic("user", socket) do + "user:#{socket.assigns[:user].id}" + end + + defp internal_topic(topic, _), do: topic end diff --git a/lib/pleroma/web/twitter_api/representers/activity_representer.ex b/lib/pleroma/web/twitter_api/representers/activity_representer.ex index b17013d87..1f11bc9ac 100644 --- a/lib/pleroma/web/twitter_api/representers/activity_representer.ex +++ b/lib/pleroma/web/twitter_api/representers/activity_representer.ex @@ -97,7 +97,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do } end - def to_map(%Activity{data: %{"type" => "Delete", "published" => created_at, "object" => deleted_object }} = activity, %{user: user} = opts) do + def to_map(%Activity{data: %{"type" => "Delete", "published" => created_at, "object" => _ }} = activity, %{user: user} = opts) do created_at = created_at |> Utils.date_to_asctime %{ diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index baa3dac96..d04a81cd4 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -4,12 +4,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter alias Pleroma.Web.TwitterAPI.UserView alias Pleroma.Web.{OStatus, CommonAPI} - alias Pleroma.Formatter import Ecto.Query @httpoison Application.get_env(:pleroma, :httpoison) - def create_status(%User{} = user, %{"status" => status} = data) do + def create_status(%User{} = user, %{"status" => _} = data) do CommonAPI.post(user, data) end @@ -216,7 +215,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end end - defp parse_int(string, default \\ nil) + defp parse_int(string, default) defp parse_int(string, default) when is_binary(string) do with {n, _} <- Integer.parse(string) do n diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index e319cfb8c..1a706029a 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -3,7 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView} alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter alias Pleroma.Web.CommonAPI - alias Pleroma.{Repo, Activity, User, Object} + alias Pleroma.{Repo, Activity, User} alias Pleroma.Web.ActivityPub.ActivityPub alias Ecto.Changeset @@ -13,7 +13,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do render(conn, UserView, "show.json", %{user: user}) end - def status_update(%{assigns: %{user: user}} = conn, %{"status" => status_text} = status_data) do + def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do with media_ids <- extract_media_ids(status_data), {:ok, activity} <- TwitterAPI.create_status(user, Map.put(status_data, "media_ids", media_ids)) do conn @@ -215,8 +215,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}), new_info <- Map.put(user.info, "banner", object.data), change <- User.info_changeset(user, %{info: new_info}), - {:ok, user} <- Repo.update(change) do - %{"url" => [ %{ "href" => href } | t ]} = object.data + {:ok, _user} <- Repo.update(change) do + %{"url" => [ %{ "href" => href } | _ ]} = object.data response = %{ url: href } |> Poison.encode! conn |> json_reply(200, response) @@ -227,8 +227,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do with {:ok, object} <- ActivityPub.upload(params), new_info <- Map.put(user.info, "background", object.data), change <- User.info_changeset(user, %{info: new_info}), - {:ok, user} <- Repo.update(change) do - %{"url" => [ %{ "href" => href } | t ]} = object.data + {:ok, _user} <- Repo.update(change) do + %{"url" => [ %{ "href" => href } | _ ]} = object.data response = %{ url: href } |> Poison.encode! conn |> json_reply(200, response) @@ -254,7 +254,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do mrn <- max(id, user.info["most_recent_notification"] || 0), updated_info <- Map.put(info, "most_recent_notification", mrn), changeset <- User.info_changeset(user, %{info: updated_info}), - {:ok, user} <- Repo.update(changeset) do + {:ok, _user} <- Repo.update(changeset) do conn |> json_reply(200, Poison.encode!(mrn)) else @@ -311,7 +311,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end - def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do + def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) do conn |> json(TwitterAPI.search(user, params)) end diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 6c5676c04..3dc18eff8 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -11,9 +11,6 @@ defmodule Pleroma.Web.TwitterAPI.UserView do render_many(users, Pleroma.Web.TwitterAPI.UserView, "user.json", for: user) end - defp image_url(%{"url" => [ %{ "href" => href } | t ]}), do: href - defp image_url(_), do: nil - def render("user.json", %{user: user = %User{}} = assigns) do image = User.avatar_url(user) {following, follows_you, statusnet_blocking} = if assigns[:for] do @@ -63,4 +60,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "screen_name" => nickname } end + + defp image_url(%{"url" => [ %{ "href" => href } | _ ]}), do: href + defp image_url(_), do: nil end diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 7cbafe11f..026d2f98b 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -89,7 +89,7 @@ defmodule Pleroma.Web.WebFinger do with {:ok, %{status_code: status_code, body: body}} when status_code in 200..299 <- @httpoison.get("http://#{domain}/.well-known/host-meta", [], follow_redirect: true) do get_template_from_xml(body) else - e -> + _ -> with {:ok, %{body: body}} <- @httpoison.get("https://#{domain}/.well-known/host-meta", []) do get_template_from_xml(body) else diff --git a/lib/pleroma/web/xml/xml.ex b/lib/pleroma/web/xml/xml.ex index 63580c1f8..026672ad1 100644 --- a/lib/pleroma/web/xml/xml.ex +++ b/lib/pleroma/web/xml/xml.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Web.XML do require Logger - def string_from_xpath(xpath, :error), do: nil + def string_from_xpath(_, :error), do: nil def string_from_xpath(xpath, doc) do {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc) @@ -20,7 +20,7 @@ defmodule Pleroma.Web.XML do doc catch - :exit, error -> + :exit, _error -> Logger.debug("Couldn't parse xml: #{inspect(text)}") :error end diff --git a/lib/transports.ex b/lib/transports.ex index 5600a4fdd..a820aa778 100644 --- a/lib/transports.ex +++ b/lib/transports.ex @@ -61,7 +61,7 @@ defmodule Phoenix.Transports.WebSocket.Raw do end end - def ws_info({op, data} = tuple, state) do + def ws_info({_,_} = tuple, state) do {:reply, tuple, state} end diff --git a/lib/xml_builder.ex b/lib/xml_builder.ex index c6d144903..52358c437 100644 --- a/lib/xml_builder.ex +++ b/lib/xml_builder.ex @@ -37,6 +37,6 @@ defmodule Pleroma.XmlBuilder do "#{attribute}=\"#{value}\"" end |> Enum.join(" ") - [tag, attributes_string] |> Enum.join(" ") |> String.strip + [tag, attributes_string] |> Enum.join(" ") |> String.trim end end -- cgit v1.2.3 From c336a13d2c7cf873ef12c30973c833a2c53f4dff Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 19 Nov 2017 12:59:23 +0100 Subject: MastoAPI: Only return create activties in contexts. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index a3587ff44..ca1e4c8d5 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -196,6 +196,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do with %Activity{} = activity <- Repo.get(Activity, id), activities <- ActivityPub.fetch_activities_for_context(activity.data["object"]["context"], %{"blocking_user" => user}), activities <- activities |> Enum.filter(fn (%{id: aid}) -> to_string(aid) != to_string(id) end), + activities <- activities |> Enum.filter(fn (%{data: %{"type" => type}}) -> type == "Create" end), grouped_activities <- Enum.group_by(activities, fn (%{id: id}) -> id < activity.id end) do result = %{ ancestors: StatusView.render("index.json", for: user, activities: grouped_activities[true] || [], as: :activity) |> Enum.reverse, -- cgit v1.2.3 From 0f099dac673e707726b8498d6b56fa6f509e9467 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 19 Nov 2017 13:23:16 +0100 Subject: MastodonAPI: Add sign out. Close #79 --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 6 ++++++ lib/pleroma/web/router.ex | 9 +++++---- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index ca1e4c8d5..9c50e850b 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -583,6 +583,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + def logout(conn, _) do + conn + |> clear_session + |> redirect(to: "/") + end + def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do Logger.debug("Unimplemented, returning unmodified relationship") with %User{} = target <- Repo.get(User, id) do diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index beca1581e..f3c476fdc 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -223,12 +223,13 @@ defmodule Pleroma.Web.Router do get "/webfinger", WebFinger.WebFingerController, :webfinger end - scope "/web", Pleroma.Web.MastodonAPI do + scope "/", Pleroma.Web.MastodonAPI do pipe_through :mastodon_html - get "/login", MastodonAPIController, :login - post "/login", MastodonAPIController, :login_post - get "/*path", MastodonAPIController, :index + get "/web/login", MastodonAPIController, :login + post "/web/login", MastodonAPIController, :login_post + get "/web/*path", MastodonAPIController, :index + delete "/auth/sign_out", MastodonAPIController, :logout end scope "/", Fallback do -- cgit v1.2.3 From 5ff5d583b69a1f68231b274de879826704d434f0 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 19 Nov 2017 13:47:50 +0100 Subject: MastodonAPI: Streaming fixes. Now shows repeats in the home tl. --- lib/pleroma/web/activity_pub/activity_pub.ex | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 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 4aade6e40..421fd5cd7 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -10,6 +10,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do :ok <- insert_full_object(map) do {:ok, activity} = Repo.insert(%Activity{data: map, local: local, actor: map["actor"]}) Notification.create_notifications(activity) + stream_out(activity) {:ok, activity} else %Activity{} = activity -> {:ok, activity} @@ -17,17 +18,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end end - def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do - with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional), - {:ok, activity} <- insert(create_data, local), - :ok <- maybe_federate(activity) do - if activity.data["type"] == "Create" and Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do + def stream_out(activity) do + if activity.data["type"] in ["Create", "Announce"] do + Pleroma.Web.Streamer.stream("user", activity) + if Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") do Pleroma.Web.Streamer.stream("public", activity) - Pleroma.Web.Streamer.stream("user", activity) - if local do + if activity.local do Pleroma.Web.Streamer.stream("public:local", activity) end end + end + end + + def create(to, actor, context, object, additional \\ %{}, published \\ nil, local \\ true) do + with create_data <- make_create_data(%{to: to, actor: actor, published: published, context: context, object: object}, additional), + {:ok, activity} <- insert(create_data, local), + :ok <- maybe_federate(activity) do {:ok, activity} end end -- cgit v1.2.3 From 52aa65fe6ebfd6730fa611e25dd0e9c0f7503654 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 19 Nov 2017 15:10:51 +0100 Subject: Federator Queue: Add seperate in/out queues. --- lib/pleroma/web/federator/federator.ex | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex index eccade883..9f6f983aa 100644 --- a/lib/pleroma/web/federator/federator.ex +++ b/lib/pleroma/web/federator/federator.ex @@ -14,7 +14,10 @@ defmodule Pleroma.Web.Federator do Process.sleep(1000 * 60 * 1) # 1 minute enqueue(:refresh_subscriptions, nil) end) - GenServer.start_link(__MODULE__, {:sets.new(), :queue.new()}, name: __MODULE__) + GenServer.start_link(__MODULE__, %{ + in: {:sets.new(), :queue.new()}, + out: {:sets.new(), :queue.new()} + }, name: __MODULE__) end def handle(:refresh_subscriptions, _) do @@ -95,10 +98,18 @@ defmodule Pleroma.Web.Federator do end end - def handle_cast({:enqueue, type, payload}, {running_jobs, queue}) do - queue = :queue.in({type, payload}, queue) - {running_jobs, queue} = maybe_start_job(running_jobs, queue) - {:noreply, {running_jobs, queue}} + def handle_cast({:enqueue, type, payload}, state) when type in [:incoming_doc] do + %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state + i_queue = :queue.in({type, payload}, i_queue) + {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue) + {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}} + end + + def handle_cast({:enqueue, type, payload}, state) do + %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state + o_queue = :queue.in({type, payload}, o_queue) + {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue) + {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}} end def handle_cast(m, state) do @@ -106,9 +117,13 @@ defmodule Pleroma.Web.Federator do {:noreply, state} end - def handle_info({:DOWN, ref, :process, _pid, _reason}, {running_jobs, queue}) do - running_jobs = :sets.del_element(ref, running_jobs) - {running_jobs, queue} = maybe_start_job(running_jobs, queue) - {:noreply, {running_jobs, queue}} + def handle_info({:DOWN, ref, :process, _pid, _reason}, state) do + %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state + i_running_jobs = :sets.del_element(ref, i_running_jobs) + o_running_jobs = :sets.del_element(ref, o_running_jobs) + {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue) + {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue) + + {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}} end end -- cgit v1.2.3 From 36011fee4e70435a38552bc300bf6008df83f75f Mon Sep 17 00:00:00 2001 From: "Hyper! (Stitch)" Date: Sun, 19 Nov 2017 19:07:19 +0000 Subject: Add Custom Pleroma-dark theme --- lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex index 334dc4f98..ac50ad46b 100644 --- a/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/index.html.eex @@ -5,6 +5,7 @@ + -- cgit v1.2.3 From aadf54e0df39aa2c1bce2c9df076c91ca7fd8167 Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 20 Nov 2017 00:31:39 +0200 Subject: Don't show the user in their own following count. --- lib/pleroma/user.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index fbeeef003..dde51bd30 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -61,8 +61,9 @@ defmodule Pleroma.User do end def user_info(%User{} = user) do + oneself = if user.local, do: 1, else: 0 %{ - following_count: length(user.following), + following_count: length(user.following) - oneself, note_count: user.info["note_count"] || 0, follower_count: user.info["follower_count"] || 0 } -- cgit v1.2.3 From 4db5954786b60df84b833feb360fd8c81be956d9 Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 20 Nov 2017 01:21:53 +0200 Subject: Don't let the user unfollow their own account. --- lib/pleroma/user.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index dde51bd30..59d4fb839 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -167,7 +167,7 @@ defmodule Pleroma.User do def unfollow(%User{} = follower, %User{} = followed) do ap_followers = followed.follower_address - if following?(follower, followed) do + if following?(follower, followed) and follower.ap_id != followed.ap_id do following = follower.following |> List.delete(ap_followers) -- cgit v1.2.3 From bd921ca5d7830efc9dde68b763412fdc03725adc Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 20 Nov 2017 07:58:43 +0200 Subject: Fix posts being streamed to non-local websocket channels. --- lib/pleroma/user.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 59d4fb839..f80c0ae33 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -286,12 +286,12 @@ defmodule Pleroma.User do def get_recipients_from_activity(%Activity{data: %{"to" => to}}) do query = from u in User, - where: u.local == true - - query = from u in query, where: u.ap_id in ^to, or_where: fragment("? \\\?| ?", u.following, ^to) + query = from u in query, + where: u.local == true + Repo.all(query) end -- cgit v1.2.3 From cf0bbf320d78ee116739d65a52b5f0a8b5772a6e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 20 Nov 2017 08:54:47 +0100 Subject: MastoAPI: Add max_toot_chars. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 9c50e850b..fc7f21096 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -106,7 +106,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do user_count: 1, status_count: 2, domain_count: 3 - } + }, + max_toot_chars: Keyword.get(@instance, :limit) } json(conn, response) -- cgit v1.2.3 From 747a68a075d90f14ce764208c0a409c3a8ec3393 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Mon, 20 Nov 2017 17:53:21 +0100 Subject: Treat internal emoji like external. --- lib/pleroma/web/common_api/common_api.ex | 3 ++- lib/pleroma/web/ostatus/activity_representer.ex | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index 9bc7f2ce6..dc94e5377 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -59,7 +59,8 @@ defmodule Pleroma.Web.CommonAPI do content_html <- make_content_html(status, mentions, attachments, tags), context <- make_context(inReplyTo), cw <- data["spoiler_text"], - object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags, cw) do + object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags, cw), + object <- Map.put(object, "emoji", Formatter.get_emoji(status) |> Enum.reduce(%{}, fn({name, file}, acc) -> Map.put(acc, name, "#{Pleroma.Web.Endpoint.static_url}#{file}") end)) do res = ActivityPub.create(to, user, context, object) User.increase_note_count(user) res diff --git a/lib/pleroma/web/ostatus/activity_representer.ex b/lib/pleroma/web/ostatus/activity_representer.ex index cfc342fca..aa2b1df39 100644 --- a/lib/pleroma/web/ostatus/activity_representer.ex +++ b/lib/pleroma/web/ostatus/activity_representer.ex @@ -56,9 +56,9 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do defp get_links(_activity), do: [] - defp get_emoji_links(content) do - Enum.map(Formatter.get_emoji(content), fn({emoji, file}) -> - {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist("#{Pleroma.Web.Endpoint.static_url}#{file}")], []} + defp get_emoji_links(emojis) do + Enum.map(emojis, fn({emoji, file}) -> + {:link, [name: to_charlist(emoji), rel: 'emoji', href: to_charlist(file)], []} end) end @@ -81,7 +81,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do categories = (activity.data["object"]["tag"] || []) |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end) - emoji_links = get_emoji_links(activity.data["object"]["content"] || "") + emoji_links = get_emoji_links(activity.data["object"]["emoji"] || %{}) summary = if activity.data["object"]["summary"] do [{:summary, [], h.(activity.data["object"]["summary"])}] -- cgit v1.2.3 From fa19de97ba6b135ac09dfede2dba33c40520dcf1 Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 20 Nov 2017 22:49:20 +0200 Subject: MastoAPI: no more than 4 attachments. --- lib/pleroma/web/mastodon_api/views/status_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index 38abdb35f..5585a5605 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -96,7 +96,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do sensitive: sensitive, spoiler_text: object["summary"] || "", visibility: "public", - media_attachments: attachments, + media_attachments: attachments |> Enum.take(4), mentions: mentions, tags: [], # fix, application: %{ -- cgit v1.2.3 From e9037ffc8d38d905c746a30128ab8ca930acbcd1 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 21 Nov 2017 15:33:09 +0200 Subject: Do not include user in their own follower count. --- lib/pleroma/user.ex | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index f80c0ae33..68ffe184b 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -265,6 +265,7 @@ defmodule Pleroma.User do def update_follower_count(%User{} = user) do follower_count_query = from u in User, where: fragment("? @> ?", u.following, ^user.follower_address), + where: u.id != ^user.id, select: count(u.id) follower_count = Repo.one(follower_count_query) -- cgit v1.2.3 From d16b6139299e47777c68eca819e75f85f71a8156 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 23 Nov 2017 12:06:14 +0100 Subject: MastodonUI login: Show error message on error. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 6 +++++- lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index fc7f21096..82887966c 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -559,7 +559,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def login(conn, _) do conn - |> render(MastodonView, "login.html") + |> render(MastodonView, "login.html", %{error: false}) end defp get_or_make_app() do @@ -581,6 +581,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do conn |> put_session(:oauth_token, token.token) |> redirect(to: "/web/getting-started") + else + _e -> + conn + |> render(MastodonView, "login.html", %{error: "Wrong username or password"}) end end diff --git a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex index 89d97561b..2ef67b901 100644 --- a/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex +++ b/lib/pleroma/web/templates/mastodon_api/mastodon/login.html.eex @@ -1,4 +1,7 @@

Login in to Mastodon Frontend

+<%= if @error do %> +

<%= @error %>

+<% end %> <%= form_for @conn, mastodon_api_path(@conn, :login), [as: "authorization"], fn f -> %> <%= text_input f, :name, placeholder: "Username" %>
-- cgit v1.2.3 From 44dc6948823f5e26ede427d3348fef72129f3f4d Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Thu, 23 Nov 2017 16:22:20 +0100 Subject: Add configuration generation tool, update readme. --- lib/mix/tasks/generate_config.ex | 17 +++++++++++++++++ lib/mix/tasks/sample_config.eex | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 lib/mix/tasks/generate_config.ex create mode 100644 lib/mix/tasks/sample_config.eex (limited to 'lib') diff --git a/lib/mix/tasks/generate_config.ex b/lib/mix/tasks/generate_config.ex new file mode 100644 index 000000000..b3bc14d5b --- /dev/null +++ b/lib/mix/tasks/generate_config.ex @@ -0,0 +1,17 @@ +defmodule Mix.Tasks.GenerateConfig do + use Mix.Task + + @shortdoc "Generates a new config" + def run(_) do + IO.puts("Answer a few questions to generate a new config\n") + IO.puts("--- THIS WILL OVERWRITE YOUR config/generated_config.exs! ---\n") + domain = IO.gets("What is your domain name? (e.g. pleroma.soykaf.com): ") |> String.trim + name = IO.gets("What is the name of your instance? (e.g. Pleroma/Soykaf): ") |> String.trim + email = IO.gets("What's your admin email address: ") |> String.trim + secret = :crypto.strong_rand_bytes(64) |> Base.encode64 |> binary_part(0, 64) + + result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret]) + IO.puts("\nWriting config to config/generated_config.exs.\n\nCheck it and configure your database, then copy it to either config/dev.secret.exs or config/prod.secret.exs") + File.write("config/generated_config.exs", result) + end +end diff --git a/lib/mix/tasks/sample_config.eex b/lib/mix/tasks/sample_config.eex new file mode 100644 index 000000000..62a9804fe --- /dev/null +++ b/lib/mix/tasks/sample_config.eex @@ -0,0 +1,20 @@ +use Mix.Config + +config :pleroma, Pleroma.Web.Endpoint, + url: [host: "<%= domain %>", scheme: "https", port: 443], + secret_key_base: "<%= secret %>" + +config :pleroma, :instance, + name: "<%= name %>", + email: "<%= email %>", + limit: 5000, + registrations_open: true + +# Configure your database +config :pleroma, Pleroma.Repo, + adapter: Ecto.Adapters.Postgres, + username: "postgres", + password: "postgres", + database: "pleroma_dev", + hostname: "localhost", + pool_size: 10 -- cgit v1.2.3 From c680ae581d028144ebea414b4137433ac7ca8e6a Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Mon, 27 Nov 2017 17:24:52 +0100 Subject: Return xml notice at /notice path. --- lib/pleroma/web/ostatus/ostatus_controller.ex | 13 +++++++++++++ lib/pleroma/web/router.ex | 1 + 2 files changed, 14 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index 1ac07546f..d442d16fd 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -86,6 +86,19 @@ defmodule Pleroma.Web.OStatus.OStatusController do end end + def notice(conn, %{"id" => id}) do + with %Activity{} = activity <- Repo.get(Activity, id), + %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do + case get_format(conn) do + "html" -> + conn + |> put_resp_content_type("text/html") + |> send_file(200, "priv/static/index.html") + _ -> represent_activity(conn, activity, user) + end + end + end + defp represent_activity(conn, activity, user) do response = activity |> ActivityRepresenter.to_simple_form(user, true) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index f3c476fdc..6806e8a75 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -207,6 +207,7 @@ defmodule Pleroma.Web.Router do get "/objects/:uuid", OStatus.OStatusController, :object get "/activities/:uuid", OStatus.OStatusController, :activity + get "/notice/:id", OStatus.OStatusController, :notice get "/users/:nickname/feed", OStatus.OStatusController, :feed get "/users/:nickname", OStatus.OStatusController, :feed_redirect -- cgit v1.2.3 From 4524721fba0582774b7c38fdc1f270c43fd8d53b Mon Sep 17 00:00:00 2001 From: href Date: Tue, 28 Nov 2017 16:50:12 +0100 Subject: Salmon: generate private key with native crypto if available. --- lib/pleroma/web/salmon/salmon.ex | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/salmon/salmon.ex b/lib/pleroma/web/salmon/salmon.ex index 4f6dfed65..81b864582 100644 --- a/lib/pleroma/web/salmon/salmon.ex +++ b/lib/pleroma/web/salmon/salmon.ex @@ -73,17 +73,30 @@ defmodule Pleroma.Web.Salmon do "RSA.#{modulus_enc}.#{exponent_enc}" end - def generate_rsa_pem do - port = Port.open({:spawn, "openssl genrsa"}, [:binary]) - {:ok, pem} = receive do - {^port, {:data, pem}} -> {:ok, pem} - end - Port.close(port) - if Regex.match?(~r/RSA PRIVATE KEY/, pem) do + # Native generation of RSA keys is only available since OTP 20+ and in default build conditions + # We try at compile time to generate natively an RSA key otherwise we fallback on the old way. + try do + _ = :public_key.generate_key({:rsa, 2048, 65537}) + def generate_rsa_pem do + key = :public_key.generate_key({:rsa, 2048, 65537}) + entry = :public_key.pem_entry_encode(:RSAPrivateKey, key) + pem = :public_key.pem_encode([entry]) |> String.trim_trailing {:ok, pem} - else - :error end + rescue + _ -> + def generate_rsa_pem do + port = Port.open({:spawn, "openssl genrsa"}, [:binary]) + {:ok, pem} = receive do + {^port, {:data, pem}} -> {:ok, pem} + end + Port.close(port) + if Regex.match?(~r/RSA PRIVATE KEY/, pem) do + {:ok, pem} + else + :error + end + end end def keys_from_pem(pem) do -- cgit v1.2.3 From 5637d163e6eb365c69f0e79e43306156241f6494 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Thu, 30 Nov 2017 14:59:44 +0100 Subject: MastodonAPI: Add proper user count. --- lib/pleroma/user.ex | 5 +++++ lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 68ffe184b..afc62f265 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -329,4 +329,9 @@ defmodule Pleroma.User do Enum.member?(blocks, ap_id) end + def local_user_query() do + from u in User, + where: u.local == true + end + end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 82887966c..61bf8b4b8 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -93,6 +93,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do @instance Application.get_env(:pleroma, :instance) def masto_instance(conn, _params) do + user_count = Repo.aggregate(User.local_user_query, :count, :id) response = %{ uri: Web.base_url, title: Keyword.get(@instance, :name), @@ -103,8 +104,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do streaming_api: String.replace(Web.base_url, ["http","https"], "wss") }, stats: %{ - user_count: 1, status_count: 2, + user_count: user_count, domain_count: 3 }, max_toot_chars: Keyword.get(@instance, :limit) -- cgit v1.2.3 From d08a34e88b0d42f403755e1bdbaf015784ebbe74 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 3 Dec 2017 13:34:17 +0100 Subject: OStatus: Add user bio as summary field Fixes problem with bio federation. --- lib/pleroma/web/ostatus/user_representer.ex | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/user_representer.ex b/lib/pleroma/web/ostatus/user_representer.ex index 14f78a4ed..20ebb3e08 100644 --- a/lib/pleroma/web/ostatus/user_representer.ex +++ b/lib/pleroma/web/ostatus/user_representer.ex @@ -19,6 +19,7 @@ defmodule Pleroma.Web.OStatus.UserRepresenter do {:"poco:preferredUsername", [nickname]}, {:"poco:displayName", [name]}, {:"poco:note", [bio]}, + {:summary, [bio]}, {:name, [nickname]}, {:link, [rel: 'avatar', href: avatar_url], []} ] ++ banner -- cgit v1.2.3 From 0673511d3939c04efd0e3fd350c47b4bfb3e322e Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 26 Nov 2017 20:57:49 +0300 Subject: first version of safe DB setup --- lib/mix/tasks/generate_config.ex | 13 +++++++++---- lib/mix/tasks/sample_config.eex | 4 ++-- lib/mix/tasks/sample_psql.eex | 6 ++++++ 3 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 lib/mix/tasks/sample_psql.eex (limited to 'lib') diff --git a/lib/mix/tasks/generate_config.ex b/lib/mix/tasks/generate_config.ex index b3bc14d5b..f9399b114 100644 --- a/lib/mix/tasks/generate_config.ex +++ b/lib/mix/tasks/generate_config.ex @@ -4,14 +4,19 @@ defmodule Mix.Tasks.GenerateConfig do @shortdoc "Generates a new config" def run(_) do IO.puts("Answer a few questions to generate a new config\n") - IO.puts("--- THIS WILL OVERWRITE YOUR config/generated_config.exs! ---\n") + IO.puts("--- THIS WILL OVERWRITE YOUR config/dev.secret.exs AND config/prod.secret.exs! ---\n") domain = IO.gets("What is your domain name? (e.g. pleroma.soykaf.com): ") |> String.trim name = IO.gets("What is the name of your instance? (e.g. Pleroma/Soykaf): ") |> String.trim email = IO.gets("What's your admin email address: ") |> String.trim secret = :crypto.strong_rand_bytes(64) |> Base.encode64 |> binary_part(0, 64) + dbpass = :crypto.strong_rand_bytes(64) |> Base.encode64 |> binary_part(0, 64) - result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret]) - IO.puts("\nWriting config to config/generated_config.exs.\n\nCheck it and configure your database, then copy it to either config/dev.secret.exs or config/prod.secret.exs") - File.write("config/generated_config.exs", result) + resultSql = EEx.eval_file("lib/mix/tasks/sample_psql.eex", [dbpass: dbpass]) + result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, dbpass: dbpass]) + IO.puts("\nWriting config to config/dev.secret.exs and config/prod.secret.exs") + IO.puts("\nWriting setup_db.psql, please run it as postgre superuser, i.e. su - postgre -c 'psql -f setup_db.psql'") + File.write("config/dev.secret.exs", result) + File.write("config/prod.secret.exs", result) + File.write("config/setup_db.psql", resultSql) end end diff --git a/lib/mix/tasks/sample_config.eex b/lib/mix/tasks/sample_config.eex index 62a9804fe..85a7c554e 100644 --- a/lib/mix/tasks/sample_config.eex +++ b/lib/mix/tasks/sample_config.eex @@ -13,8 +13,8 @@ config :pleroma, :instance, # Configure your database config :pleroma, Pleroma.Repo, adapter: Ecto.Adapters.Postgres, - username: "postgres", - password: "postgres", + username: "pleroma", + password: "<%= dbpass %>", database: "pleroma_dev", hostname: "localhost", pool_size: 10 diff --git a/lib/mix/tasks/sample_psql.eex b/lib/mix/tasks/sample_psql.eex new file mode 100644 index 000000000..5353fa216 --- /dev/null +++ b/lib/mix/tasks/sample_psql.eex @@ -0,0 +1,6 @@ +CREATE USER pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>' CREATEDB; +CREATE DATABASE pleroma_dev; +ALTER DATABASE pleroma_dev OWNER TO pleroma; +\c pleroma_dev; +--Extensions made by ecto.migrate that need superuser access +CREATE EXTENSION citext; -- cgit v1.2.3 From 40714031176f28ed1af2ed83f01b1d066badb871 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Tue, 28 Nov 2017 00:42:03 +0300 Subject: fixes --- lib/mix/tasks/generate_config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/mix/tasks/generate_config.ex b/lib/mix/tasks/generate_config.ex index f9399b114..d06cc1a84 100644 --- a/lib/mix/tasks/generate_config.ex +++ b/lib/mix/tasks/generate_config.ex @@ -14,7 +14,7 @@ defmodule Mix.Tasks.GenerateConfig do resultSql = EEx.eval_file("lib/mix/tasks/sample_psql.eex", [dbpass: dbpass]) result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, dbpass: dbpass]) IO.puts("\nWriting config to config/dev.secret.exs and config/prod.secret.exs") - IO.puts("\nWriting setup_db.psql, please run it as postgre superuser, i.e. su - postgre -c 'psql -f setup_db.psql'") + IO.puts("\nWriting setup_db.psql, please run it as postgre superuser, i.e. su - postgres -c 'psql -f config/setup_db.psql'") File.write("config/dev.secret.exs", result) File.write("config/prod.secret.exs", result) File.write("config/setup_db.psql", resultSql) -- cgit v1.2.3 From a884e08271ec29ed42011c450655861b870f4745 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Tue, 28 Nov 2017 00:44:14 +0300 Subject: fix --- lib/mix/tasks/generate_config.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/mix/tasks/generate_config.ex b/lib/mix/tasks/generate_config.ex index d06cc1a84..95cf394be 100644 --- a/lib/mix/tasks/generate_config.ex +++ b/lib/mix/tasks/generate_config.ex @@ -14,7 +14,7 @@ defmodule Mix.Tasks.GenerateConfig do resultSql = EEx.eval_file("lib/mix/tasks/sample_psql.eex", [dbpass: dbpass]) result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, dbpass: dbpass]) IO.puts("\nWriting config to config/dev.secret.exs and config/prod.secret.exs") - IO.puts("\nWriting setup_db.psql, please run it as postgre superuser, i.e. su - postgres -c 'psql -f config/setup_db.psql'") + IO.puts("\nWriting setup_db.psql, please run it as postgre superuser, i.e.: sudo su postgres -c 'psql -f config/setup_db.psql'") File.write("config/dev.secret.exs", result) File.write("config/prod.secret.exs", result) File.write("config/setup_db.psql", resultSql) -- cgit v1.2.3 From 071f518028e20d1f22811d6a10f120bebe26c2ce Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 4 Dec 2017 02:01:45 +0300 Subject: reverts config gen --- lib/mix/tasks/generate_config.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/mix/tasks/generate_config.ex b/lib/mix/tasks/generate_config.ex index 95cf394be..f20f93e4d 100644 --- a/lib/mix/tasks/generate_config.ex +++ b/lib/mix/tasks/generate_config.ex @@ -4,7 +4,7 @@ defmodule Mix.Tasks.GenerateConfig do @shortdoc "Generates a new config" def run(_) do IO.puts("Answer a few questions to generate a new config\n") - IO.puts("--- THIS WILL OVERWRITE YOUR config/dev.secret.exs AND config/prod.secret.exs! ---\n") + IO.puts("--- THIS WILL OVERWRITE YOUR config/generated_config.exs! ---\n") domain = IO.gets("What is your domain name? (e.g. pleroma.soykaf.com): ") |> String.trim name = IO.gets("What is the name of your instance? (e.g. Pleroma/Soykaf): ") |> String.trim email = IO.gets("What's your admin email address: ") |> String.trim @@ -13,10 +13,10 @@ defmodule Mix.Tasks.GenerateConfig do resultSql = EEx.eval_file("lib/mix/tasks/sample_psql.eex", [dbpass: dbpass]) result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, dbpass: dbpass]) - IO.puts("\nWriting config to config/dev.secret.exs and config/prod.secret.exs") + + IO.puts("\nWriting config to config/generated_config.exs.\n\nCheck it and configure your database, then copy it to either config/dev.secret.exs or config/prod.secret.exs") + File.write("config/generated_config.exs", result) IO.puts("\nWriting setup_db.psql, please run it as postgre superuser, i.e.: sudo su postgres -c 'psql -f config/setup_db.psql'") - File.write("config/dev.secret.exs", result) - File.write("config/prod.secret.exs", result) File.write("config/setup_db.psql", resultSql) end end -- cgit v1.2.3 From 6b60f5f64ab9d7598ccdb2545b0e5560991ff5f3 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 4 Dec 2017 02:06:05 +0300 Subject: made psql script compatible with migrations or generate_config reruns --- lib/mix/tasks/sample_psql.eex | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/mix/tasks/sample_psql.eex b/lib/mix/tasks/sample_psql.eex index 5353fa216..9eb7f65b0 100644 --- a/lib/mix/tasks/sample_psql.eex +++ b/lib/mix/tasks/sample_psql.eex @@ -1,6 +1,8 @@ -CREATE USER pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>' CREATEDB; -CREATE DATABASE pleroma_dev; +CREATE USER IF NOT EXISTS pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>' CREATEDB; +-- in case someone runs this second time accidentally +ALTER USER pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>' CREATEDB; +CREATE DATABASE IF NOT EXISTS pleroma_dev; ALTER DATABASE pleroma_dev OWNER TO pleroma; \c pleroma_dev; --Extensions made by ecto.migrate that need superuser access -CREATE EXTENSION citext; +CREATE EXTENSION IF NOT EXISTS citext; -- cgit v1.2.3 From 964e2dd2f055207ac8fa7f8b3612e50860a0472a Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 4 Dec 2017 04:06:52 +0300 Subject: unfuck --- lib/mix/tasks/sample_psql.eex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/mix/tasks/sample_psql.eex b/lib/mix/tasks/sample_psql.eex index 9eb7f65b0..18e322efc 100644 --- a/lib/mix/tasks/sample_psql.eex +++ b/lib/mix/tasks/sample_psql.eex @@ -1,7 +1,7 @@ -CREATE USER IF NOT EXISTS pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>' CREATEDB; +CREATE USER pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>' CREATEDB; -- in case someone runs this second time accidentally ALTER USER pleroma WITH ENCRYPTED PASSWORD '<%= dbpass %>' CREATEDB; -CREATE DATABASE IF NOT EXISTS pleroma_dev; +CREATE DATABASE pleroma_dev; ALTER DATABASE pleroma_dev OWNER TO pleroma; \c pleroma_dev; --Extensions made by ecto.migrate that need superuser access -- cgit v1.2.3 From 5c4098612067abf16e5c10e878656dc412304cd3 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Mon, 4 Dec 2017 19:10:15 +0100 Subject: Basic backend chat. --- lib/pleroma/web/channels/user_socket.ex | 12 ++++++++++-- lib/pleroma/web/chat_channel.ex | 14 ++++++++++++++ lib/pleroma/web/twitter_api/twitter_api_controller.ex | 3 ++- lib/pleroma/web/twitter_api/views/user_view.ex | 8 +++++++- 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 lib/pleroma/web/chat_channel.ex (limited to 'lib') diff --git a/lib/pleroma/web/channels/user_socket.ex b/lib/pleroma/web/channels/user_socket.ex index 7aa8e556e..d8171cabd 100644 --- a/lib/pleroma/web/channels/user_socket.ex +++ b/lib/pleroma/web/channels/user_socket.ex @@ -1,8 +1,11 @@ defmodule Pleroma.Web.UserSocket do use Phoenix.Socket + alias Pleroma.User + alias Comeonin.Pbkdf2 ## Channels # channel "room:*", Pleroma.Web.RoomChannel + channel "chat:*", Pleroma.Web.ChatChannel ## Transports transport :websocket, Phoenix.Transports.WebSocket @@ -19,8 +22,13 @@ defmodule Pleroma.Web.UserSocket do # # See `Phoenix.Token` documentation for examples in # performing token verification on connect. - def connect(_params, socket) do - {:ok, socket} + def connect(%{"token" => token}, socket) do + with {:ok, user_id} <- Phoenix.Token.verify(socket, "user socket", token, max_age: 84600), + %User{} = user <- Pleroma.Repo.get(User, user_id) do + {:ok, assign(socket, :user, user)} + else + _e -> :error + end end # Socket id's are topics that allow you to identify all sockets for a given user: diff --git a/lib/pleroma/web/chat_channel.ex b/lib/pleroma/web/chat_channel.ex new file mode 100644 index 000000000..60558cb8e --- /dev/null +++ b/lib/pleroma/web/chat_channel.ex @@ -0,0 +1,14 @@ +defmodule Pleroma.Web.ChatChannel do + use Phoenix.Channel + + def join("chat:public", _message, socket) do + {:ok, socket} + end + + def handle_in("new_msg", %{"text" => text}, socket) do + author = socket.assigns[:user] + author = Pleroma.Web.MastodonAPI.AccountView.render("account.json", user: author) + broadcast! socket, "new_msg", %{text: text, author: author} + {:noreply, socket} + 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 1a706029a..73d96c73d 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -10,7 +10,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do require Logger def verify_credentials(%{assigns: %{user: user}} = conn, _params) do - render(conn, UserView, "show.json", %{user: user}) + token = Phoenix.Token.sign(conn, "user socket", user.id) + render(conn, UserView, "show.json", %{user: user, token: token}) end def status_update(%{assigns: %{user: user}} = conn, %{"status" => _} = status_data) do diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 3dc18eff8..d1c7e6fbd 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -25,7 +25,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do user_info = User.get_cached_user_info(user) - %{ + data = %{ "created_at" => user.inserted_at |> Utils.format_naive_asctime, "description" => HtmlSanitizeEx.strip_tags(user.bio), "favourites_count" => 0, @@ -47,6 +47,12 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "cover_photo" => image_url(user.info["banner"]), "background_image" => image_url(user.info["background"]) } + + if assigns[:token] do + Map.put(data, "token", assigns[:token]) + else + data + end end def render("short.json", %{user: %User{ -- cgit v1.2.3 From c1fa1e8844c8eae1ad7638a2d7f9d00e8cd07ce8 Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 4 Dec 2017 22:45:16 +0200 Subject: Fix basic auth for passwords with a colon. --- lib/pleroma/plugs/authentication_plug.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/plugs/authentication_plug.ex b/lib/pleroma/plugs/authentication_plug.ex index 14654f2e6..beb02eb88 100644 --- a/lib/pleroma/plugs/authentication_plug.ex +++ b/lib/pleroma/plugs/authentication_plug.ex @@ -44,7 +44,7 @@ defmodule Pleroma.Plugs.AuthenticationPlug do defp decode_header(conn) do with ["Basic " <> header] <- get_req_header(conn, "authorization"), {:ok, userinfo} <- Base.decode64(header), - [username, password] <- String.split(userinfo, ":") + [username, password] <- String.split(userinfo, ":", parts: 2) do {:ok, username, password} end -- cgit v1.2.3 From 69f1024bb0c6fef509cb38aabcbb1482055cde05 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 5 Dec 2017 09:36:26 +0100 Subject: Add basic channel state. --- lib/pleroma/application.ex | 1 + lib/pleroma/web/chat_channel.ex | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index bfe16e13a..2969ca3c4 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -20,6 +20,7 @@ defmodule Pleroma.Application do limit: 2500 ]]), worker(Pleroma.Web.Federator, []), + worker(Pleroma.Web.ChatChannel.ChatChannelState, []), ] ++ if Mix.env == :test, do: [], else: [worker(Pleroma.Web.Streamer, [])] diff --git a/lib/pleroma/web/chat_channel.ex b/lib/pleroma/web/chat_channel.ex index 60558cb8e..c2277d73d 100644 --- a/lib/pleroma/web/chat_channel.ex +++ b/lib/pleroma/web/chat_channel.ex @@ -1,14 +1,45 @@ defmodule Pleroma.Web.ChatChannel do use Phoenix.Channel + alias Pleroma.Web.ChatChannel.ChatChannelState def join("chat:public", _message, socket) do + send(self(), :after_join) {:ok, socket} end + def handle_info(:after_join, socket) do + push socket, "messages", %{messages: ChatChannelState.messages()} + {:noreply, socket} + end + def handle_in("new_msg", %{"text" => text}, socket) do author = socket.assigns[:user] author = Pleroma.Web.MastodonAPI.AccountView.render("account.json", user: author) - broadcast! socket, "new_msg", %{text: text, author: author} + message= ChatChannelState.add_message(%{text: text, author: author}) + + broadcast! socket, "new_msg", message {:noreply, socket} end end + +defmodule Pleroma.Web.ChatChannel.ChatChannelState do + use Agent + @max_messages 20 + + def start_link do + Agent.start_link(fn -> %{max_id: 1, messages: []} end, name: __MODULE__) + end + + def add_message(message) do + Agent.get_and_update(__MODULE__, fn state -> + id = state[:max_id] + 1 + message = Map.put(message, "id", id) + messages = [message | state[:messages]] |> Enum.take(@max_messages) + {message, %{max_id: id, messages: messages}} + end) + end + + def messages() do + Agent.get(__MODULE__, fn state -> state[:messages] |> Enum.reverse end) + end +end -- cgit v1.2.3 From 5945ec84e9c6333b0ad4fb09ef9453de603447a1 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 5 Dec 2017 10:01:36 +0100 Subject: Don't save user in socket, just save the name. --- lib/pleroma/web/channels/user_socket.ex | 2 +- lib/pleroma/web/chat_channel.ex | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/channels/user_socket.ex b/lib/pleroma/web/channels/user_socket.ex index d8171cabd..4a9bb8e22 100644 --- a/lib/pleroma/web/channels/user_socket.ex +++ b/lib/pleroma/web/channels/user_socket.ex @@ -25,7 +25,7 @@ defmodule Pleroma.Web.UserSocket do def connect(%{"token" => token}, socket) do with {:ok, user_id} <- Phoenix.Token.verify(socket, "user socket", token, max_age: 84600), %User{} = user <- Pleroma.Repo.get(User, user_id) do - {:ok, assign(socket, :user, user)} + {:ok, assign(socket, :user_name, user.nickname)} else _e -> :error end diff --git a/lib/pleroma/web/chat_channel.ex b/lib/pleroma/web/chat_channel.ex index c2277d73d..268bef17d 100644 --- a/lib/pleroma/web/chat_channel.ex +++ b/lib/pleroma/web/chat_channel.ex @@ -1,6 +1,7 @@ defmodule Pleroma.Web.ChatChannel do use Phoenix.Channel alias Pleroma.Web.ChatChannel.ChatChannelState + alias Pleroma.User def join("chat:public", _message, socket) do send(self(), :after_join) @@ -12,10 +13,10 @@ defmodule Pleroma.Web.ChatChannel do {:noreply, socket} end - def handle_in("new_msg", %{"text" => text}, socket) do - author = socket.assigns[:user] + def handle_in("new_msg", %{"text" => text}, %{assigns: %{user_name: user_name}} = socket) do + author = User.get_cached_by_nickname(user_name) author = Pleroma.Web.MastodonAPI.AccountView.render("account.json", user: author) - message= ChatChannelState.add_message(%{text: text, author: author}) + message = ChatChannelState.add_message(%{text: text, author: author}) broadcast! socket, "new_msg", message {:noreply, socket} -- cgit v1.2.3 From 66c3813ea6388e9933af2b15e903f1cf6254cd3a Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Tue, 5 Dec 2017 18:21:30 +0100 Subject: Add basic queue prioritization. --- lib/pleroma/web/federator/federator.ex | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex index 9f6f983aa..f384b313c 100644 --- a/lib/pleroma/web/federator/federator.ex +++ b/lib/pleroma/web/federator/federator.ex @@ -15,8 +15,8 @@ defmodule Pleroma.Web.Federator do enqueue(:refresh_subscriptions, nil) end) GenServer.start_link(__MODULE__, %{ - in: {:sets.new(), :queue.new()}, - out: {:sets.new(), :queue.new()} + in: {:sets.new(), [], + out: {:sets.new(), []} }, name: __MODULE__) end @@ -88,8 +88,8 @@ defmodule Pleroma.Web.Federator do end def maybe_start_job(running_jobs, queue) do - if (:sets.size(running_jobs) < @max_jobs) && !:queue.is_empty(queue) do - {{:value, {type, payload}}, queue} = :queue.out(queue) + if (:sets.size(running_jobs) < @max_jobs) && queue != [] do + {{:value, {type, payload}}, queue} = queue_pop(queue) {:ok, pid} = Task.start(fn -> handle(type, payload) end) mref = Process.monitor(pid) {:sets.add_element(mref, running_jobs), queue} @@ -100,14 +100,14 @@ defmodule Pleroma.Web.Federator do def handle_cast({:enqueue, type, payload}, state) when type in [:incoming_doc] do %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state - i_queue = :queue.in({type, payload}, i_queue) + i_queue = enqueue_sorted(i_queue, {type, payload}, 1) {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue) {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}} end def handle_cast({:enqueue, type, payload}, state) do %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state - o_queue = :queue.in({type, payload}, o_queue) + o_queue = enqueue_sorted(o_queue, {type, payload}, 1) {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue) {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}} end @@ -126,4 +126,13 @@ defmodule Pleroma.Web.Federator do {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}} end + + def enqueue_sorted(queue, element, priority) do + [%{item: element, priority: priority} | queue] + |> Enum.sort_by(fn (%{priority: priority}) -> priority end) + end + + def queue_pop([%{item: element} | queue]) do + {element, queue} + end end -- cgit v1.2.3 From e31a5ff4af04156b9db8f032cf184e1da540f025 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Wed, 6 Dec 2017 16:51:11 +0100 Subject: Priority queue fixes. --- lib/pleroma/web/activity_pub/utils.ex | 7 ++++++- lib/pleroma/web/federator/federator.ex | 12 ++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 51fac6fe2..ac20a2822 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -29,7 +29,12 @@ defmodule Pleroma.Web.ActivityPub.Utils do Enqueues an activity for federation if it's local """ def maybe_federate(%Activity{local: true} = activity) do - Pleroma.Web.Federator.enqueue(:publish, activity) + priority = case activity.data["type"] do + "Delete" -> 10 + "Create" -> 1 + _ -> 5 + end + Pleroma.Web.Federator.enqueue(:publish, activity, priority) :ok end def maybe_federate(_), do: :ok diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex index f384b313c..b23ed5fcc 100644 --- a/lib/pleroma/web/federator/federator.ex +++ b/lib/pleroma/web/federator/federator.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Web.Federator do enqueue(:refresh_subscriptions, nil) end) GenServer.start_link(__MODULE__, %{ - in: {:sets.new(), [], + in: {:sets.new(), []}, out: {:sets.new(), []} }, name: __MODULE__) end @@ -79,17 +79,17 @@ defmodule Pleroma.Web.Federator do {:error, "Don't know what do do with this"} end - def enqueue(type, payload) do + def enqueue(type, payload, priority \\ 1) do if Mix.env == :test do handle(type, payload) else - GenServer.cast(__MODULE__, {:enqueue, type, payload}) + GenServer.cast(__MODULE__, {:enqueue, type, payload, priority}) end end def maybe_start_job(running_jobs, queue) do if (:sets.size(running_jobs) < @max_jobs) && queue != [] do - {{:value, {type, payload}}, queue} = queue_pop(queue) + {{type, payload}, queue} = queue_pop(queue) {:ok, pid} = Task.start(fn -> handle(type, payload) end) mref = Process.monitor(pid) {:sets.add_element(mref, running_jobs), queue} @@ -98,14 +98,14 @@ defmodule Pleroma.Web.Federator do end end - def handle_cast({:enqueue, type, payload}, state) when type in [:incoming_doc] do + def handle_cast({:enqueue, type, payload, priority}, state) when type in [:incoming_doc] do %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state i_queue = enqueue_sorted(i_queue, {type, payload}, 1) {i_running_jobs, i_queue} = maybe_start_job(i_running_jobs, i_queue) {:noreply, %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}}} end - def handle_cast({:enqueue, type, payload}, state) do + def handle_cast({:enqueue, type, payload, priority}, state) do %{in: {i_running_jobs, i_queue}, out: {o_running_jobs, o_queue}} = state o_queue = enqueue_sorted(o_queue, {type, payload}, 1) {o_running_jobs, o_queue} = maybe_start_job(o_running_jobs, o_queue) -- cgit v1.2.3 From 0ec5aeb8a76653935caefa0de92861269f98f343 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Thu, 7 Dec 2017 17:41:34 +0100 Subject: Don't log in deactivated users. --- lib/pleroma/plugs/authentication_plug.ex | 1 + lib/pleroma/plugs/oauth_plug.ex | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/plugs/authentication_plug.ex b/lib/pleroma/plugs/authentication_plug.ex index beb02eb88..60f6faf49 100644 --- a/lib/pleroma/plugs/authentication_plug.ex +++ b/lib/pleroma/plugs/authentication_plug.ex @@ -12,6 +12,7 @@ defmodule Pleroma.Plugs.AuthenticationPlug do def call(conn, opts) do with {:ok, username, password} <- decode_header(conn), {:ok, user} <- opts[:fetcher].(username), + false <- !!user.info["deactivated"], saved_user_id <- get_session(conn, :user_id), {:ok, verified_user} <- verify(user, password, saved_user_id) do diff --git a/lib/pleroma/plugs/oauth_plug.ex b/lib/pleroma/plugs/oauth_plug.ex index 775423bb1..be737dc9a 100644 --- a/lib/pleroma/plugs/oauth_plug.ex +++ b/lib/pleroma/plugs/oauth_plug.ex @@ -16,7 +16,8 @@ defmodule Pleroma.Plugs.OAuthPlug do end with token when not is_nil(token) <- token, %Token{user_id: user_id} <- Repo.get_by(Token, token: token), - %User{} = user <- Repo.get(User, user_id) do + %User{} = user <- Repo.get(User, user_id), + false <- !!user.info["deactivated"] do conn |> assign(:user, user) else -- cgit v1.2.3 From b727ecc5e75388e2b2d67c76cab6df1a7e6719b3 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Thu, 7 Dec 2017 17:47:23 +0100 Subject: Add function to deactivate users. --- lib/pleroma/user.ex | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index afc62f265..779a89a12 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -334,4 +334,9 @@ defmodule Pleroma.User do where: u.local == true end + def deactivate (%User{} = user) do + new_info = Map.put(user.info, "deactivated", true) + cs = User.info_changeset(user, %{info: new_info}) + Repo.update(cs) + end end -- cgit v1.2.3 From a78ae2a685769cbf7459eb347aca581d389018ad Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Thu, 7 Dec 2017 17:51:55 +0100 Subject: Don't follow deactivated users. --- lib/pleroma/user.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 779a89a12..b21caba9d 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -142,9 +142,9 @@ defmodule Pleroma.User do end end - def follow(%User{} = follower, %User{} = followed) do + def follow(%User{} = follower, %User{info: info} = followed) do ap_followers = followed.follower_address - if following?(follower, followed) do + if following?(follower, followed) or info["deactivated"] do {:error, "Could not follow user: #{followed.nickname} is already on your list."} else -- cgit v1.2.3 From 6df6ad0b429150b00b065d95890bd62cd2778fad Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Thu, 7 Dec 2017 18:13:05 +0100 Subject: User deletion: Remove relationships. --- lib/pleroma/user.ex | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index b21caba9d..021207de9 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -339,4 +339,19 @@ defmodule Pleroma.User do cs = User.info_changeset(user, %{info: new_info}) Repo.update(cs) end + + def delete (%User{} = user) do + {:ok, user} = User.deactivate(user) + + # Remove all relationships + {:ok, followers } = User.get_followers(user) + followers + |> Enum.each(fn (follower) -> User.unfollow(follower, user) end) + + {:ok, friends} = User.get_friends(user) + friends + |> Enum.each(fn (followed) -> User.unfollow(user, followed) end) + + :ok + end end -- cgit v1.2.3 From 5436dbaeaf0765c5d651cd16b02018edbc6793d4 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 7 Dec 2017 20:44:09 +0200 Subject: MastoAPI: don't add attachment links. --- lib/pleroma/web/common_api/common_api.ex | 2 +- lib/pleroma/web/common_api/utils.ex | 8 ++++++-- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index dc94e5377..d3a9f7b85 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -56,7 +56,7 @@ defmodule Pleroma.Web.CommonAPI do inReplyTo <- get_replied_to_activity(data["in_reply_to_status_id"]), to <- to_for_user_and_mentions(user, mentions, inReplyTo), tags <- Formatter.parse_tags(status, data), - content_html <- make_content_html(status, mentions, attachments, tags), + content_html <- make_content_html(status, mentions, attachments, tags, data["no_attachment_links"]), context <- make_context(inReplyTo), cw <- data["spoiler_text"], object <- make_note_data(user.ap_id, to, context, content_html, attachments, inReplyTo, tags, cw), diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 1a23b1ad2..2d9fdaf6c 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -38,15 +38,19 @@ defmodule Pleroma.Web.CommonAPI.Utils do end end - def make_content_html(status, mentions, attachments, tags) do + def make_content_html(status, mentions, attachments, tags, no_attachment_links \\ false) do status |> format_input(mentions, tags) - |> add_attachments(attachments) + |> maybe_add_attachments(attachments, no_attachment_links) end def make_context(%Activity{data: %{"context" => context}}), do: context def make_context(_), do: Utils.generate_context_id + def maybe_add_attachments(text, attachments, _no_links = true), do: text + def maybe_add_attachments(text, attachments, _no_links) do + add_attachments(text, attachments) + end def add_attachments(text, attachments) do attachment_text = Enum.map(attachments, fn (%{"url" => [%{"href" => href} | _]}) -> diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 61bf8b4b8..e50f53ba4 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -212,6 +212,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def post_status(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do params = params |> Map.put("in_reply_to_status_id", params["in_reply_to_id"]) + |> Map.put("no_attachment_links", true) {:ok, activity} = CommonAPI.post(user, params) render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity} -- cgit v1.2.3 From bf91e5659ffd03b15110b6f1094b30aed940e45b Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 7 Dec 2017 21:34:25 +0200 Subject: Fix HTML escape breaking some links. --- lib/pleroma/formatter.ex | 9 +++++++++ lib/pleroma/web/common_api/utils.ex | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index fbcbca979..275c60f32 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -24,6 +24,15 @@ defmodule Pleroma.Formatter do |> Enum.filter(fn ({_match, user}) -> user end) end + def html_escape(text) do + Regex.split(@link_regex, text, include_captures: true) + |> Enum.map_every(2, fn chunk -> + {:safe, part} = Phoenix.HTML.html_escape(chunk) + part + end) + |> Enum.join("") + end + @finmoji [ "a_trusted_friend", "alandislands", diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 1a23b1ad2..f46db4cf0 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -58,8 +58,8 @@ defmodule Pleroma.Web.CommonAPI.Utils do end def format_input(text, mentions, _tags) do - Phoenix.HTML.html_escape(text) - |> elem(1) + text + |> Formatter.html_escape |> Formatter.linkify |> String.replace("\n", "
") |> add_user_links(mentions) -- cgit v1.2.3 From a3e68f02330a3c38f1d3f568ad507e5111b82600 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 7 Dec 2017 21:38:31 +0200 Subject: Allow parentheses in links. --- lib/pleroma/formatter.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index 275c60f32..c98db2d94 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -1,7 +1,7 @@ defmodule Pleroma.Formatter do alias Pleroma.User - @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~]+[\w\/]/u + @link_regex ~r/https?:\/\/[\w\.\/?=\-#%&@~\(\)]+[\w\/]/u def linkify(text) do Regex.replace(@link_regex, text, "\\0") end -- cgit v1.2.3 From bad499b3fd95d1643890feaedea07123c004be57 Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Fri, 8 Dec 2017 17:50:11 +0100 Subject: Basic user deletion. --- lib/pleroma/user.ex | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 021207de9..09bcf0cb4 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -5,7 +5,7 @@ defmodule Pleroma.User do alias Pleroma.{Repo, User, Object, Web, Activity, Notification} alias Comeonin.Pbkdf2 alias Pleroma.Web.{OStatus, Websub} - alias Pleroma.Web.ActivityPub.Utils + alias Pleroma.Web.ActivityPub.{Utils, ActivityPub} schema "users" do field :bio, :string @@ -113,7 +113,7 @@ defmodule Pleroma.User do end def reset_password(user, data) do - Repo.update(password_update_changeset(user, data)) + update_and_set_cache(password_update_changeset(user, data)) end def register_changeset(struct, params \\ %{}) do @@ -157,7 +157,7 @@ defmodule Pleroma.User do follower = follower |> follow_changeset(%{following: following}) - |> Repo.update + |> update_and_set_cache {:ok, _} = update_follower_count(followed) @@ -173,7 +173,7 @@ defmodule Pleroma.User do { :ok, follower } = follower |> follow_changeset(%{following: following}) - |> Repo.update + |> update_and_set_cache {:ok, followed} = update_follower_count(followed) @@ -191,6 +191,17 @@ defmodule Pleroma.User do Repo.get_by(User, ap_id: ap_id) end + def update_and_set_cache(changeset) do + with {:ok, user} <- Repo.update(changeset) do + Cachex.set(:user_cache, "ap_id:#{user.ap_id}", user) + Cachex.set(:user_cache, "nickname:#{user.nickname}", user) + Cachex.set(:user_cache, "user_info:#{user.id}", user_info(user)) + {:ok, user} + else + e -> e + end + end + def get_cached_by_ap_id(ap_id) do key = "ap_id:#{ap_id}" Cachex.get!(:user_cache, key, fallback: fn(_) -> get_by_ap_id(ap_id) end) @@ -245,7 +256,7 @@ defmodule Pleroma.User do cs = info_changeset(user, %{info: new_info}) - Repo.update(cs) + update_and_set_cache(cs) end def update_note_count(%User{} = user) do @@ -259,7 +270,7 @@ defmodule Pleroma.User do cs = info_changeset(user, %{info: new_info}) - Repo.update(cs) + update_and_set_cache(cs) end def update_follower_count(%User{} = user) do @@ -274,7 +285,7 @@ defmodule Pleroma.User do cs = info_changeset(user, %{info: new_info}) - Repo.update(cs) + update_and_set_cache(cs) end def get_notified_from_activity(%Activity{data: %{"to" => to}}) do @@ -312,7 +323,7 @@ defmodule Pleroma.User do new_info = Map.put(user.info, "blocks", new_blocks) cs = User.info_changeset(user, %{info: new_info}) - Repo.update(cs) + update_and_set_cache(cs) end def unblock(user, %{ap_id: ap_id}) do @@ -321,7 +332,7 @@ defmodule Pleroma.User do new_info = Map.put(user.info, "blocks", new_blocks) cs = User.info_changeset(user, %{info: new_info}) - Repo.update(cs) + update_and_set_cache(cs) end def blocks?(user, %{ap_id: ap_id}) do @@ -337,7 +348,7 @@ defmodule Pleroma.User do def deactivate (%User{} = user) do new_info = Map.put(user.info, "deactivated", true) cs = User.info_changeset(user, %{info: new_info}) - Repo.update(cs) + update_and_set_cache(cs) end def delete (%User{} = user) do @@ -352,6 +363,17 @@ defmodule Pleroma.User do friends |> Enum.each(fn (followed) -> User.unfollow(user, followed) end) + query = from a in Activity, + where: a.actor == ^user.ap_id + + Repo.all(query) + |> Enum.each(fn (activity) -> + case activity.data["type"] do + "Create" -> ActivityPub.delete(Object.get_by_ap_id(activity.data["object"]["id"])) + _ -> "Doing nothing" # TODO: Do something with likes, follows, repeats. + end + end) + :ok end end -- cgit v1.2.3 From ef2322bdb7d46882eeae0271c4381efda6835c3c Mon Sep 17 00:00:00 2001 From: Lain Iwakura Date: Fri, 8 Dec 2017 18:17:30 +0100 Subject: Delete notifications on activity deletion. --- lib/pleroma/activity.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index a35ccc9b4..afd09982f 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -7,7 +7,7 @@ defmodule Pleroma.Activity do field :data, :map field :local, :boolean, default: true field :actor, :string - has_many :notifications, Notification + has_many :notifications, Notification, on_delete: :delete_all timestamps() end -- cgit v1.2.3