From 578051809f38c3d5d65155e264203c3b978a10a8 Mon Sep 17 00:00:00 2001 From: Rin Toshaka Date: Fri, 7 Dec 2018 20:03:30 +0100 Subject: Add uploadlimit to Twitter API config --- lib/pleroma/web/twitter_api/controllers/util_controller.ex | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 092779010..36ecefa76 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -165,6 +165,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do description: Keyword.get(instance, :description), server: Web.base_url(), textlimit: to_string(Keyword.get(instance, :limit)), + uploadlimit: to_string(Keyword.get(instance, :upload_limit)), closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"), private: if(Keyword.get(instance, :public, true), do: "0", else: "1"), vapidPublicKey: vapid_public_key -- cgit v1.2.3 From 15616eda5e98bb13b1382109fb1f84537c58fcb1 Mon Sep 17 00:00:00 2001 From: Rin Toshaka Date: Sat, 8 Dec 2018 21:48:49 +0100 Subject: Make uploadlimit an object that stores upload limits for avatars, banners, backgrounds, general content --- lib/pleroma/web/twitter_api/controllers/util_controller.ex | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 36ecefa76..1459f3c90 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -160,12 +160,19 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do vapid_public_key = Keyword.get(Application.get_env(:web_push_encryption, :vapid_details), :public_key) + uploadlimit = %{ + uploadlimit: to_string(Keyword.get(instance, :upload_limit)), + avatarlimit: to_string(Keyword.get(instance, :avatar_upload_limit)), + backgroundlimit: to_string(Keyword.get(instance, :background_upload_limit)), + bannerlimit: to_string(Keyword.get(instance, :banner_upload_limit)) + } + data = %{ name: Keyword.get(instance, :name), description: Keyword.get(instance, :description), server: Web.base_url(), textlimit: to_string(Keyword.get(instance, :limit)), - uploadlimit: to_string(Keyword.get(instance, :upload_limit)), + uploadlimit: uploadlimit, closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"), private: if(Keyword.get(instance, :public, true), do: "0", else: "1"), vapidPublicKey: vapid_public_key -- cgit v1.2.3 From 10c156d98fee44444ed6d1366e615ddcdb2ee68a Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Mon, 10 Dec 2018 20:20:50 +0300 Subject: [#114] SMTP deps and config. --- lib/pleroma/emails/mailer.ex | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 lib/pleroma/emails/mailer.ex (limited to 'lib') diff --git a/lib/pleroma/emails/mailer.ex b/lib/pleroma/emails/mailer.ex new file mode 100644 index 000000000..14ed32ea8 --- /dev/null +++ b/lib/pleroma/emails/mailer.ex @@ -0,0 +1,3 @@ +defmodule Pleroma.Mailer do + use Swoosh.Mailer, otp_app: :pleroma +end -- cgit v1.2.3 From 12905ce1ad39106251e401fec81b871799708cc4 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Tue, 11 Dec 2018 14:59:25 +0300 Subject: [#114] Added /dev/mailbox dev-only route (emails preview). Added mailer config examples. --- lib/pleroma/web/router.ex | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 9c06fac4f..19b8750fc 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -85,6 +85,15 @@ defmodule Pleroma.Web.Router do plug(:accepts, ["html", "json"]) end + pipeline :mailbox_preview do + plug(:accepts, ["html"]) + + plug(:put_secure_browser_headers, %{ + "content-security-policy" => + "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline' 'unsafe-eval'" + }) + end + scope "/api/pleroma", Pleroma.Web.TwitterAPI do pipe_through(:pleroma_api) get("/password_reset/:token", UtilController, :show_password_reset) @@ -268,6 +277,7 @@ defmodule Pleroma.Web.Router do get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation) post("/account/register", TwitterAPI.Controller, :register) + post("/account/reset_password", TwitterAPI.Controller, :reset_password) get("/search", TwitterAPI.Controller, :search) get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline) @@ -424,6 +434,14 @@ defmodule Pleroma.Web.Router do get("/:sig/:url/:filename", MediaProxyController, :remote) end + if Mix.env() == :dev do + scope "/dev" do + pipe_through([:mailbox_preview]) + + forward("/mailbox", Plug.Swoosh.MailboxPreview, base_path: "/dev/mailbox") + end + end + scope "/", Fallback do get("/registration/:token", RedirectController, :registration_page) get("/*path", RedirectController, :redirector) -- cgit v1.2.3 From f5afb11032913fe523bd688954b4923f357c7802 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Tue, 11 Dec 2018 20:17:49 +0300 Subject: [#114] Initial implementation of user password reset emails (user-initiated). --- lib/pleroma/emails/user_email.ex | 37 ++++++++++++++++++++++ lib/pleroma/web/router.ex | 2 +- .../web/twitter_api/twitter_api_controller.ex | 19 +++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 lib/pleroma/emails/user_email.ex (limited to 'lib') diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex new file mode 100644 index 000000000..46d8b9c2d --- /dev/null +++ b/lib/pleroma/emails/user_email.ex @@ -0,0 +1,37 @@ +defmodule Pleroma.UserEmail do + @moduledoc "User emails" + + import Swoosh.Email + + alias Pleroma.Web.{Endpoint, Router} + + defp instance_config, do: Pleroma.Config.get(:instance) + + defp instance_name, do: instance_config()[:name] + + defp from do + {instance_name(), instance_config()[:email]} + end + + def password_reset_email(user, password_reset_token) when is_binary(password_reset_token) do + password_reset_url = + Router.Helpers.util_url( + Endpoint, + :show_password_reset, + password_reset_token + ) + + html_body = """ +

Reset your password at #{instance_name()}

+

Someone has requested password change for your account at #{instance_name()}.

+

If it was you, visit the following link to proceed: reset password.

+

If it was someone else, nothing to worry about: your data is secure and your password has not been changed.

+ """ + + new() + |> to({user.name, user.email}) + |> from(from()) + |> subject("Password reset") + |> html_body(html_body) + end +end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 19b8750fc..6253a28db 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -277,7 +277,7 @@ defmodule Pleroma.Web.Router do get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation) post("/account/register", TwitterAPI.Controller, :register) - post("/account/reset_password", TwitterAPI.Controller, :reset_password) + post("/account/password_reset", TwitterAPI.Controller, :password_reset) get("/search", TwitterAPI.Controller, :search) get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 786849aa3..8837db566 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -1,5 +1,9 @@ defmodule Pleroma.Web.TwitterAPI.Controller do use Pleroma.Web, :controller + + import Pleroma.Web.ControllerHelper, only: [json_response: 3] + + alias Pleroma.Formatter alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView} alias Pleroma.Web.CommonAPI alias Pleroma.{Repo, Activity, Object, User, Notification} @@ -322,6 +326,21 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def password_reset(conn, params) do + nickname_or_email = params["email"] || params["nickname"] + + with is_binary(nickname_or_email), + %User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email) do + {:ok, token_record} = Pleroma.PasswordResetToken.create_token(user) + + user + |> Pleroma.UserEmail.password_reset_email(token_record.token) + |> Pleroma.Mailer.deliver() + + json_response(conn, :no_content, "") + end + end + def update_avatar(%{assigns: %{user: user}} = conn, params) do {:ok, object} = ActivityPub.upload(params, type: :avatar) change = Changeset.change(user, %{avatar: object.data}) -- cgit v1.2.3 From e3a21bcd45dd1f27a2f6a74041d86c64ad8256e9 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 12 Dec 2018 17:14:31 +0300 Subject: [#114] Addressed warnings. Fix of `with` statement clause in `password_reset`. --- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 8837db566..479c92381 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -3,7 +3,6 @@ defmodule Pleroma.Web.TwitterAPI.Controller do import Pleroma.Web.ControllerHelper, only: [json_response: 3] - alias Pleroma.Formatter alias Pleroma.Web.TwitterAPI.{TwitterAPI, UserView, ActivityView, NotificationView} alias Pleroma.Web.CommonAPI alias Pleroma.{Repo, Activity, Object, User, Notification} @@ -329,7 +328,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do def password_reset(conn, params) do nickname_or_email = params["email"] || params["nickname"] - with is_binary(nickname_or_email), + with true <- is_binary(nickname_or_email), %User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email) do {:ok, token_record} = Pleroma.PasswordResetToken.create_token(user) -- cgit v1.2.3 From bfff2399ff2d7b479b066c6f92bf9331f80bb914 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Wed, 12 Dec 2018 20:15:43 +0300 Subject: [#114] Routes and config for `confirm_email` and `email_invite` (Twitter API). --- lib/pleroma/web/router.ex | 3 +++ lib/pleroma/web/twitter_api/controllers/util_controller.ex | 3 +++ lib/pleroma/web/twitter_api/twitter_api_controller.ex | 4 ++++ 3 files changed, 10 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 6253a28db..6a2e0d6c4 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -278,6 +278,7 @@ defmodule Pleroma.Web.Router do post("/account/register", TwitterAPI.Controller, :register) post("/account/password_reset", TwitterAPI.Controller, :password_reset) + get("/account/confirm_email", TwitterAPI.Controller, :confirm_email) get("/search", TwitterAPI.Controller, :search) get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline) @@ -312,6 +313,8 @@ defmodule Pleroma.Web.Router do post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner) post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background) + post("/email_invite", TwitterAPI.Controller, :email_invite) + get("/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline) get("/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline) get("/statuses/mentions", TwitterAPI.Controller, :mentions_timeline) diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index b1e4c77e8..cacfbbf91 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -166,6 +166,9 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do textlimit: to_string(Keyword.get(instance, :limit)), closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"), private: if(Keyword.get(instance, :public, true), do: "0", else: "1"), + accountActivationRequired: + if(Keyword.get(instance, :account_activation_required, false), do: "1", else: "0"), + invitesEnabled: if(Keyword.get(instance, :invites_enabled, true), do: "1", else: "0"), vapidPublicKey: vapid_public_key } diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 479c92381..911dd6a48 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -340,6 +340,10 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end + def confirm_email(_conn, _params), do: :noop + + def email_invite(_conn, _params), do: :noop + def update_avatar(%{assigns: %{user: user}} = conn, params) do {:ok, object} = ActivityPub.upload(params, type: :avatar) change = Changeset.change(user, %{avatar: object.data}) -- cgit v1.2.3 From 908943352fe2d81c34323a5571ad5c1d391969e1 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Thu, 13 Dec 2018 13:17:49 +0300 Subject: [#114] Refactored `password_reset` (moved to TwitterAPI). Added homepage links to password reset result pages. --- .../twitter_api/util/password_reset_failed.html.eex | 1 + .../twitter_api/util/password_reset_success.html.eex | 1 + lib/pleroma/web/twitter_api/twitter_api.ex | 19 +++++++++++++++++++ lib/pleroma/web/twitter_api/twitter_api_controller.ex | 9 +-------- 4 files changed, 22 insertions(+), 8 deletions(-) (limited to 'lib') 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 index 58a3736fd..df037c01e 100644 --- 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 @@ -1 +1,2 @@

Password reset failed

+

Homepage

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 index c7dfcb6dd..f30ba3274 100644 --- 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 @@ -1 +1,2 @@

Password changed!

+

Homepage

diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 79ea48d86..1e764f24a 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -167,6 +167,25 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do end end + def password_reset(nickname_or_email) do + with true <- is_binary(nickname_or_email), + %User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email), + {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do + user + |> Pleroma.UserEmail.password_reset_email(token_record.token) + |> Pleroma.Mailer.deliver() + else + false -> + {:error, "bad user identifier"} + + %User{local: false} -> + {:error, "remote user"} + + nil -> + {:error, "unknown user"} + end + end + def get_by_id_or_nickname(id_or_nickname) do if !is_integer(id_or_nickname) && :error == Integer.parse(id_or_nickname) do Repo.get_by(User, nickname: id_or_nickname) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 911dd6a48..a45fdcf51 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -328,14 +328,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do def password_reset(conn, params) do nickname_or_email = params["email"] || params["nickname"] - with true <- is_binary(nickname_or_email), - %User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email) do - {:ok, token_record} = Pleroma.PasswordResetToken.create_token(user) - - user - |> Pleroma.UserEmail.password_reset_email(token_record.token) - |> Pleroma.Mailer.deliver() - + with {:ok, _} <- TwitterAPI.password_reset(nickname_or_email) do json_response(conn, :no_content, "") end end -- cgit v1.2.3 From 00744c6b03d043defcf87696f539d65e41ad6a62 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Thu, 13 Dec 2018 14:30:48 +0300 Subject: [#114] Initial implementation of user email invitations. --- lib/pleroma/emails/user_email.ex | 30 +++++++++++++++++++--- .../web/twitter_api/controllers/util_controller.ex | 2 +- .../web/twitter_api/twitter_api_controller.ex | 8 +++++- 3 files changed, 35 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index 46d8b9c2d..47dcd42e0 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -9,10 +9,13 @@ defmodule Pleroma.UserEmail do defp instance_name, do: instance_config()[:name] - defp from do + defp sender do {instance_name(), instance_config()[:email]} end + defp recipient(email, nil), do: email + defp recipient(email, name), do: {name, email} + def password_reset_email(user, password_reset_token) when is_binary(password_reset_token) do password_reset_url = Router.Helpers.util_url( @@ -29,9 +32,30 @@ defmodule Pleroma.UserEmail do """ new() - |> to({user.name, user.email}) - |> from(from()) + |> to(recipient(user.email, user.name)) + |> from(sender()) |> subject("Password reset") |> html_body(html_body) end + + def user_invitation_email(user, to_email, to_name \\ nil) do + registration_url = + Router.Helpers.redirect_url( + Endpoint, + :registration_page, + "" + ) + + html_body = """ +

You are invited to #{instance_name()}

+

#{user.name} invites you to join #{instance_name()}, an instance of Pleroma federated social networking platform.

+

Click the following link to register: accept invitation.

+ """ + + new() + |> to(recipient(to_email, to_name)) + |> from(sender()) + |> subject("Invitation to #{instance_name()}") + |> html_body(html_body) + end end diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index cacfbbf91..cbde45e52 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -168,7 +168,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do private: if(Keyword.get(instance, :public, true), do: "0", else: "1"), accountActivationRequired: if(Keyword.get(instance, :account_activation_required, false), do: "1", else: "0"), - invitesEnabled: if(Keyword.get(instance, :invites_enabled, true), do: "1", else: "0"), + invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0"), vapidPublicKey: vapid_public_key } diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index a45fdcf51..d51d71299 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -335,7 +335,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do def confirm_email(_conn, _params), do: :noop - def email_invite(_conn, _params), do: :noop + def email_invite(%{assigns: %{user: user}} = conn, %{"email" => email} = params) do + with true <- Pleroma.Config.get([:instance, :invites_enabled]), + email <- Pleroma.UserEmail.user_invitation_email(user, email, params["name"]), + {:ok, _} <- Pleroma.Mailer.deliver(email) do + json_response(conn, :no_content, "") + end + end def update_avatar(%{assigns: %{user: user}} = conn, params) do {:ok, object} = ActivityPub.upload(params, type: :avatar) -- cgit v1.2.3 From 18b9467d1a63766123f625a964303f6b4d28a39f Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Thu, 13 Dec 2018 16:22:42 +0300 Subject: [#114] Removed `email_invite` implementation (to be addressed separately). --- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index d51d71299..a45fdcf51 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -335,13 +335,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do def confirm_email(_conn, _params), do: :noop - def email_invite(%{assigns: %{user: user}} = conn, %{"email" => email} = params) do - with true <- Pleroma.Config.get([:instance, :invites_enabled]), - email <- Pleroma.UserEmail.user_invitation_email(user, email, params["name"]), - {:ok, _} <- Pleroma.Mailer.deliver(email) do - json_response(conn, :no_content, "") - end - end + def email_invite(_conn, _params), do: :noop def update_avatar(%{assigns: %{user: user}} = conn, params) do {:ok, object} = ActivityPub.upload(params, type: :avatar) -- cgit v1.2.3 From 9e689de06302df8b1d281c41f7774d43a0db3758 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Thu, 13 Dec 2018 16:22:42 +0300 Subject: [#114] Removed `email_invite` implementation (to be addressed separately). --- lib/pleroma/emails/user_email.ex | 21 --------------------- 1 file changed, 21 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/emails/user_email.ex b/lib/pleroma/emails/user_email.ex index 47dcd42e0..9cdf002f3 100644 --- a/lib/pleroma/emails/user_email.ex +++ b/lib/pleroma/emails/user_email.ex @@ -37,25 +37,4 @@ defmodule Pleroma.UserEmail do |> subject("Password reset") |> html_body(html_body) end - - def user_invitation_email(user, to_email, to_name \\ nil) do - registration_url = - Router.Helpers.redirect_url( - Endpoint, - :registration_page, - "" - ) - - html_body = """ -

You are invited to #{instance_name()}

-

#{user.name} invites you to join #{instance_name()}, an instance of Pleroma federated social networking platform.

-

Click the following link to register: accept invitation.

- """ - - new() - |> to(recipient(to_email, to_name)) - |> from(sender()) - |> subject("Invitation to #{instance_name()}") - |> html_body(html_body) - end end -- cgit v1.2.3 From 1ca080c8628d261cdb95145331aa36e631e90a3f Mon Sep 17 00:00:00 2001 From: eal Date: Fri, 14 Dec 2018 07:56:49 +0200 Subject: Prevent accidental double RTs or favorites --- lib/pleroma/web/common_api/common_api.ex | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 e3385310f..f01d36370 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -1,6 +1,7 @@ defmodule Pleroma.Web.CommonAPI do alias Pleroma.{User, Repo, Activity, Object} alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Formatter import Pleroma.Web.CommonAPI.Utils @@ -16,7 +17,8 @@ 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), - object <- Object.normalize(activity.data["object"]["id"]) do + object <- Object.normalize(activity.data["object"]["id"]), + nil <- Utils.get_existing_announce(user.ap_id, object) do ActivityPub.announce(user, object) else _ -> @@ -36,7 +38,8 @@ defmodule Pleroma.Web.CommonAPI do def favorite(id_or_ap_id, user) do with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id), - object <- Object.normalize(activity.data["object"]["id"]) do + object <- Object.normalize(activity.data["object"]["id"]), + nil <- Utils.get_existing_like(user.ap_id, object) do ActivityPub.like(user, object) else _ -> -- cgit v1.2.3 From 61ad2ce4221b86f77977d82c448d0eddb8add5aa Mon Sep 17 00:00:00 2001 From: eal Date: Fri, 14 Dec 2018 08:24:18 +0200 Subject: TwitterAPI: Include favorited post in json --- lib/pleroma/web/twitter_api/views/activity_view.ex | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/views/activity_view.ex b/lib/pleroma/web/twitter_api/views/activity_view.ex index 83e8fb765..e5caed28f 100644 --- a/lib/pleroma/web/twitter_api/views/activity_view.ex +++ b/lib/pleroma/web/twitter_api/views/activity_view.ex @@ -190,6 +190,11 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do text = "#{user.nickname} favorited a status." + favorited_status = + if liked_activity, + do: render("activity.json", Map.merge(opts, %{activity: liked_activity})), + else: nil + %{ "id" => activity.id, "user" => UserView.render("show.json", %{user: user, for: opts[:for]}), @@ -199,6 +204,7 @@ defmodule Pleroma.Web.TwitterAPI.ActivityView do "is_post_verb" => false, "uri" => "tag:#{activity.data["id"]}:objectType=Favourite", "created_at" => created_at, + "favorited_status" => favorited_status, "in_reply_to_status_id" => liked_activity_id, "external_url" => activity.data["id"], "activity_type" => "like" -- cgit v1.2.3 From f81213910fb32505b92fc19270cc483e00862d0c Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Fri, 14 Dec 2018 12:09:55 +0300 Subject: [#114] Addressed MR comments. Removed functionality to be extracted to other MRs. --- lib/pleroma/web/router.ex | 3 --- lib/pleroma/web/twitter_api/controllers/util_controller.ex | 3 --- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 4 ---- 3 files changed, 10 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 6a2e0d6c4..6253a28db 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -278,7 +278,6 @@ defmodule Pleroma.Web.Router do post("/account/register", TwitterAPI.Controller, :register) post("/account/password_reset", TwitterAPI.Controller, :password_reset) - get("/account/confirm_email", TwitterAPI.Controller, :confirm_email) get("/search", TwitterAPI.Controller, :search) get("/statusnet/tags/timeline/:tag", TwitterAPI.Controller, :public_and_external_timeline) @@ -313,8 +312,6 @@ defmodule Pleroma.Web.Router do post("/account/update_profile_banner", TwitterAPI.Controller, :update_banner) post("/qvitter/update_background_image", TwitterAPI.Controller, :update_background) - post("/email_invite", TwitterAPI.Controller, :email_invite) - get("/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline) get("/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline) get("/statuses/mentions", TwitterAPI.Controller, :mentions_timeline) diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index cbde45e52..b1e4c77e8 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -166,9 +166,6 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do textlimit: to_string(Keyword.get(instance, :limit)), closed: if(Keyword.get(instance, :registrations_open), do: "0", else: "1"), private: if(Keyword.get(instance, :public, true), do: "0", else: "1"), - accountActivationRequired: - if(Keyword.get(instance, :account_activation_required, false), do: "1", else: "0"), - invitesEnabled: if(Keyword.get(instance, :invites_enabled, false), do: "1", else: "0"), vapidPublicKey: vapid_public_key } diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index a45fdcf51..38eff8191 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -333,10 +333,6 @@ defmodule Pleroma.Web.TwitterAPI.Controller do end end - def confirm_email(_conn, _params), do: :noop - - def email_invite(_conn, _params), do: :noop - def update_avatar(%{assigns: %{user: user}} = conn, params) do {:ok, object} = ActivityPub.upload(params, type: :avatar) change = Changeset.change(user, %{avatar: object.data}) -- cgit v1.2.3 From 658edb166fc91e77399ff6022a48076db5404fb1 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Fri, 7 Dec 2018 15:55:28 +0700 Subject: fix and improve web push; add configuration docs --- lib/pleroma/plugs/oauth_plug.ex | 28 +++++++++++----------- .../mastodon_api/views/push_subscription_view.ex | 7 +++++- lib/pleroma/web/push/push.ex | 8 +++---- 3 files changed, 24 insertions(+), 19 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/plugs/oauth_plug.ex b/lib/pleroma/plugs/oauth_plug.ex index 8b99a74d1..13c914c1b 100644 --- a/lib/pleroma/plugs/oauth_plug.ex +++ b/lib/pleroma/plugs/oauth_plug.ex @@ -15,10 +15,10 @@ defmodule Pleroma.Plugs.OAuthPlug do def call(%{assigns: %{user: %User{}}} = conn, _), do: conn def call(conn, _) do - with {:ok, token} <- fetch_token(conn), - {:ok, user} <- fetch_user(token) do + with {:ok, token_str} <- fetch_token_str(conn), + {:ok, user, token_record} <- fetch_user_and_token(token_str) do conn - |> assign(:token, token) + |> assign(:token, token_record) |> assign(:user, user) else _ -> conn @@ -27,12 +27,12 @@ defmodule Pleroma.Plugs.OAuthPlug do # Gets user by token # - @spec fetch_user(String.t()) :: {:ok, User.t()} | nil - defp fetch_user(token) do + @spec fetch_user_and_token(String.t()) :: {:ok, User.t(), Token.t()} | nil + defp fetch_user_and_token(token) do query = from(q in Token, where: q.token == ^token, preload: [:user]) - with %Token{user: %{info: %{deactivated: false} = _} = user} <- Repo.one(query) do - {:ok, user} + with %Token{user: %{info: %{deactivated: false} = _} = user} = token_record <- Repo.one(query) do + {:ok, user, token_record} end end @@ -48,23 +48,23 @@ defmodule Pleroma.Plugs.OAuthPlug do # Gets token from headers # - @spec fetch_token(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()} - defp fetch_token(%Plug.Conn{} = conn) do + @spec fetch_token_str(Plug.Conn.t()) :: :no_token_found | {:ok, String.t()} + defp fetch_token_str(%Plug.Conn{} = conn) do headers = get_req_header(conn, "authorization") - with :no_token_found <- fetch_token(headers), + with :no_token_found <- fetch_token_str(headers), do: fetch_token_from_session(conn) end - @spec fetch_token(Keyword.t()) :: :no_token_found | {:ok, String.t()} - defp fetch_token([]), do: :no_token_found + @spec fetch_token_str(Keyword.t()) :: :no_token_found | {:ok, String.t()} + defp fetch_token_str([]), do: :no_token_found - defp fetch_token([token | tail]) do + defp fetch_token_str([token | tail]) do trimmed_token = String.trim(token) case Regex.run(@realm_reg, trimmed_token) do [_, match] -> {:ok, String.trim(match)} - _ -> fetch_token(tail) + _ -> fetch_token_str(tail) end end end diff --git a/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex b/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex index c8b95d14c..67e86294e 100644 --- a/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex +++ b/lib/pleroma/web/mastodon_api/views/push_subscription_view.ex @@ -5,7 +5,12 @@ defmodule Pleroma.Web.MastodonAPI.PushSubscriptionView do %{ id: to_string(subscription.id), endpoint: subscription.endpoint, - alerts: Map.get(subscription.data, "alerts") + alerts: Map.get(subscription.data, "alerts"), + server_key: server_key() } end + + defp server_key do + Keyword.get(Application.get_env(:web_push_encryption, :vapid_details), :public_key) + end end diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index 5a873ec19..fb6605f30 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -100,16 +100,16 @@ defmodule Pleroma.Web.Push do def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do %{ - title: "New Announce", - body: "@#{actor.nickname} has announced your post", + title: "New Repeat", + body: "@#{actor.nickname} has repeated your post", icon: User.avatar_url(actor) } end def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do %{ - title: "New Like", - body: "@#{actor.nickname} has liked your post", + title: "New Favorite", + body: "@#{actor.nickname} has favorited your post", icon: User.avatar_url(actor) } end -- cgit v1.2.3 From 324933a0ac8db810669375315d43fc9250a93a7b Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Fri, 7 Dec 2018 21:08:42 +0700 Subject: improve push message format (compatibility with mastodon) --- lib/pleroma/web/push/push.ex | 54 ++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 29 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index fb6605f30..4af3e159a 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -41,10 +41,10 @@ defmodule Pleroma.Web.Push do ) when type in @types do actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) - body = notification |> format(actor) |> Jason.encode!() Subscription |> where(user_id: ^user_id) + |> preload(:token) |> Repo.all() |> Enum.each(fn record -> subscription = %{ @@ -55,6 +55,16 @@ defmodule Pleroma.Web.Push do endpoint: record.endpoint } + body = + Jason.encode!(%{ + title: format_title(notification), + body: format_body(notification, actor), + notification_id: notification.id, + icon: User.avatar_url(actor), + preferred_locale: "en", + access_token: record.token.token + }) + case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do {:ok, %{status_code: code}} when 400 <= code and code < 500 -> Logger.debug("Removing subscription record") @@ -82,35 +92,21 @@ defmodule Pleroma.Web.Push do {:noreply, state} end - def format(%{activity: %{data: %{"type" => "Create"}}}, actor) do - %{ - title: "New Mention", - body: "@#{actor.nickname} has mentiond you", - icon: User.avatar_url(actor) - } - end - - def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do - %{ - title: "New Follower", - body: "@#{actor.nickname} has followed you", - icon: User.avatar_url(actor) - } - end - - def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do - %{ - title: "New Repeat", - body: "@#{actor.nickname} has repeated your post", - icon: User.avatar_url(actor) - } + defp format_title(%{activity: %{data: %{"type" => type}}}) do + case type do + "Create" -> "New Mention" + "Follow" -> "New Follower" + "Announce" -> "New Repeat" + "Like" -> "New Favorite" + end end - def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do - %{ - title: "New Favorite", - body: "@#{actor.nickname} has favorited your post", - icon: User.avatar_url(actor) - } + defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do + case type do + "Create" -> "@#{actor.nickname} has mentiond you" + "Follow" -> "@#{actor.nickname} has followed you" + "Announce" -> "@#{actor.nickname} has repeated your post" + "Like" -> "@#{actor.nickname} has favorited your post" + end end end -- cgit v1.2.3 From 7facbb2b8d68abc608d6d36f21207d5c0f131029 Mon Sep 17 00:00:00 2001 From: href Date: Sat, 8 Dec 2018 17:32:58 +0100 Subject: Push.Subscription: convert base64 to base64 urlsafe --- lib/pleroma/web/push/subscription.ex | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/push/subscription.ex b/lib/pleroma/web/push/subscription.ex index cfab7a98e..1ad405daf 100644 --- a/lib/pleroma/web/push/subscription.ex +++ b/lib/pleroma/web/push/subscription.ex @@ -37,8 +37,8 @@ defmodule Pleroma.Web.Push.Subscription do user_id: user.id, token_id: token.id, endpoint: endpoint, - key_auth: key_auth, - key_p256dh: key_p256dh, + key_auth: ensure_base64_urlsafe(key_auth), + key_p256dh: ensure_base64_urlsafe(key_p256dh), data: alerts(params) }) end @@ -63,4 +63,14 @@ defmodule Pleroma.Web.Push.Subscription do sub -> Repo.delete(sub) end end + + # Some webpush clients (e.g. iOS Toot!) use an non urlsafe base64 as an encoding for the key. + # However, the web push rfs specify to use base64 urlsafe, and the `web_push_encryption` library we use + # requires the key to be properly encoded. So we just convert base64 to urlsafe base64. + defp ensure_base64_urlsafe(string) do + string + |> String.replace("+", "-") + |> String.replace("/", "_") + |> String.replace("=", "") + end end -- cgit v1.2.3 From d8984b7bf83e1ee076a73aab682db0f2673e3735 Mon Sep 17 00:00:00 2001 From: href Date: Sat, 8 Dec 2018 17:34:13 +0100 Subject: Push: add missing notification_type field --- lib/pleroma/web/push/push.ex | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index 4af3e159a..8b59e54cb 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -58,14 +58,15 @@ defmodule Pleroma.Web.Push do body = Jason.encode!(%{ title: format_title(notification), + access_token: record.token.token, body: format_body(notification, actor), notification_id: notification.id, + notification_type: format_type(notification), icon: User.avatar_url(actor), - preferred_locale: "en", - access_token: record.token.token + preferred_locale: "en" }) - case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do + case WebPushEncryption.send_web_push(body, subscription) do {:ok, %{status_code: code}} when 400 <= code and code < 500 -> Logger.debug("Removing subscription record") Repo.delete!(record) @@ -92,6 +93,16 @@ defmodule Pleroma.Web.Push do {:noreply, state} end + # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19 + defp format_type(%{activity: %{data: %{"type" => type}}}) do + case type do + "Create" -> "mention" + "Follow" -> "follow" + "Announce" -> "reblog" + "Favorite" -> "favourite" + end + end + defp format_title(%{activity: %{data: %{"type" => type}}}) do case type do "Create" -> "New Mention" -- cgit v1.2.3 From b1bcd97a0f47832d5a87fdfa814f5e2ef54d605f Mon Sep 17 00:00:00 2001 From: href Date: Sat, 8 Dec 2018 18:07:10 +0100 Subject: Push: respect alerts settings --- lib/pleroma/web/push/push.ex | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index 8b59e54cb..d9c3410fe 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -42,45 +42,50 @@ defmodule Pleroma.Web.Push do when type in @types do actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) + type = format_type(notification) + Subscription |> where(user_id: ^user_id) |> preload(:token) |> Repo.all() - |> Enum.each(fn record -> - subscription = %{ + |> Enum.filter(fn subscription -> + get_in(subscription.data, ["alerts", type]) || false + end) + |> Enum.each(fn subscription -> + sub = %{ keys: %{ - p256dh: record.key_p256dh, - auth: record.key_auth + p256dh: subscription.key_p256dh, + auth: subscription.key_auth }, - endpoint: record.endpoint + endpoint: subscription.endpoint } body = Jason.encode!(%{ title: format_title(notification), - access_token: record.token.token, + access_token: subscription.token.token, body: format_body(notification, actor), notification_id: notification.id, - notification_type: format_type(notification), + notification_type: type, icon: User.avatar_url(actor), preferred_locale: "en" }) - case WebPushEncryption.send_web_push(body, subscription) do + case WebPushEncryption.send_web_push(body, sub) do {:ok, %{status_code: code}} when 400 <= code and code < 500 -> Logger.debug("Removing subscription record") - Repo.delete!(record) + Repo.delete!(subscription) :ok {:ok, %{status_code: code}} when 200 <= code and code < 300 -> :ok {:ok, %{status_code: code}} -> - Logger.error("Web Push Nonification failed with code: #{code}") + Logger.error("Web Push Notification failed with code: #{code}") :error _ -> - Logger.error("Web Push Nonification failed with unknown error") + Logger.error("Web Push Notification failed with unknown error") :error end end) -- cgit v1.2.3 From 68229161832fd98ee59679fb19aafc3baceea2ae Mon Sep 17 00:00:00 2001 From: href Date: Sat, 8 Dec 2018 20:02:59 +0100 Subject: Typos --- lib/pleroma/web/push/push.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index d9c3410fe..291e04a6e 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -104,7 +104,7 @@ defmodule Pleroma.Web.Push do "Create" -> "mention" "Follow" -> "follow" "Announce" -> "reblog" - "Favorite" -> "favourite" + "Like" -> "favourite" end end @@ -119,7 +119,7 @@ defmodule Pleroma.Web.Push do defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do case type do - "Create" -> "@#{actor.nickname} has mentiond you" + "Create" -> "@#{actor.nickname} has mentioned you" "Follow" -> "@#{actor.nickname} has followed you" "Announce" -> "@#{actor.nickname} has repeated your post" "Like" -> "@#{actor.nickname} has favorited your post" -- cgit v1.2.3 From 331396cbcdabe9dbfe0b84ec299a642385093605 Mon Sep 17 00:00:00 2001 From: href Date: Sun, 9 Dec 2018 13:45:21 +0100 Subject: Properly disable Web Push if no VAPID key is set --- .../web/mastodon_api/mastodon_api_controller.ex | 4 ++ lib/pleroma/web/push/push.ex | 44 +++++++++++++++------- .../web/twitter_api/controllers/util_controller.ex | 3 +- 3 files changed, 35 insertions(+), 16 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 5c8602322..e105a1e15 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1167,6 +1167,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def create_push_subscription(%{assigns: %{user: user, token: token}} = conn, params) do + true = Pleroma.Web.Push.enabled() Pleroma.Web.Push.Subscription.delete_if_exists(user, token) {:ok, subscription} = Pleroma.Web.Push.Subscription.create(user, token, params) view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) @@ -1174,6 +1175,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def get_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do + true = Pleroma.Web.Push.enabled() subscription = Pleroma.Web.Push.Subscription.get(user, token) view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) json(conn, view) @@ -1183,12 +1185,14 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do %{assigns: %{user: user, token: token}} = conn, params ) do + true = Pleroma.Web.Push.enabled() {:ok, subscription} = Pleroma.Web.Push.Subscription.update(user, token, params) view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) json(conn, view) end def delete_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do + true = Pleroma.Web.Push.enabled() {:ok, _response} = Pleroma.Web.Push.Subscription.delete(user, token) json(conn, %{}) end diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index 291e04a6e..35e14c243 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -9,32 +9,44 @@ defmodule Pleroma.Web.Push do @types ["Create", "Follow", "Announce", "Like"] - @gcm_api_key nil - def start_link() do GenServer.start_link(__MODULE__, :ok, name: __MODULE__) end - def init(:ok) do - case Application.get_env(:web_push_encryption, :vapid_details) do - nil -> - Logger.warn( - "VAPID key pair is not found. Please, add VAPID configuration to config. Run `mix web_push.gen.keypair` mix task to create a key pair" - ) - - :ignore + def vapid_config() do + Application.get_env(:web_push_encryption, :vapid_details, []) + end - _ -> - {:ok, %{}} + def enabled() do + case vapid_config() do + [] -> false + list when is_list(list) -> true + _ -> false end end def send(notification) do - if Application.get_env(:web_push_encryption, :vapid_details) do + if enabled() do GenServer.cast(Pleroma.Web.Push, {:send, notification}) end end + def init(:ok) do + if enabled() do + Logger.warn(""" + VAPID key pair is not found. If you wish to enabled web push, please run + + mix web_push.gen.keypair + + and add the resulting output to your configuration file. + """) + + :ignore + else + {:ok, nil} + end + end + def handle_cast( {:send, %{activity: %{data: %{"type" => type}}, user_id: user_id} = notification}, state @@ -71,7 +83,11 @@ defmodule Pleroma.Web.Push do preferred_locale: "en" }) - case WebPushEncryption.send_web_push(body, sub) do + case WebPushEncryption.send_web_push( + body, + sub, + Application.get_env(:web_push_encryption, :gcm_api_key) + ) do {:ok, %{status_code: code}} when 400 <= code and code < 500 -> Logger.debug("Removing subscription record") Repo.delete!(subscription) diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index b1e4c77e8..29edca9fe 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -156,8 +156,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do |> send_resp(200, response) _ -> - vapid_public_key = - Keyword.get(Application.get_env(:web_push_encryption, :vapid_details), :public_key) + vapid_public_key = Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key) data = %{ name: Keyword.get(instance, :name), -- cgit v1.2.3 From ec0e613ecaa8dc411e0c821c4c9b2ca893b45f67 Mon Sep 17 00:00:00 2001 From: href Date: Mon, 10 Dec 2018 15:50:10 +0100 Subject: Pleroma.Activity.mastodon_notification_type/1 --- lib/pleroma/activity.ex | 15 +++++ .../web/mastodon_api/mastodon_api_controller.ex | 71 +++++++++------------- lib/pleroma/web/push/push.ex | 12 +--- 3 files changed, 44 insertions(+), 54 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index c065f3b6c..200addd6e 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -3,6 +3,14 @@ defmodule Pleroma.Activity do alias Pleroma.{Repo, Activity, Notification} import Ecto.Query + # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19 + @mastodon_notification_types %{ + "Create" => "mention", + "Follow" => "follow", + "Announce" => "reblog", + "Like" => "favourite" + } + schema "activities" do field(:data, :map) field(:local, :boolean, default: true) @@ -88,4 +96,11 @@ defmodule Pleroma.Activity do end def get_in_reply_to_activity(_), do: nil + + for {ap_type, type} <- @mastodon_notification_types do + def mastodon_notification_type(%Activity{data: %{"type" => unquote(ap_type)}}), + do: unquote(type) + end + + def mastodon_notification_type(%Activity{}), do: nil end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index e105a1e15..4db7cd60f 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1055,52 +1055,37 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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"]) + parent_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"]) + mastodon_type = Activity.mastodon_notification_type(activity) - created_at = - NaiveDateTime.to_iso8601(created_at) - |> String.replace(~r/(\.\d+)?$/, ".000Z", global: false) - - id = id |> to_string + response = %{ + id: to_string(id), + type: mastodon_type, + created_at: CommonAPI.Utils.to_masto_date(activity.inserted_at), + account: AccountView.render("account.json", %{user: actor, for: user}) + } - case activity.data["type"] do - "Create" -> - %{ - id: id, - type: "mention", - created_at: created_at, - account: AccountView.render("account.json", %{user: actor, for: user}), + case mastodon_type do + "mention" -> + response + |> Map.merge(%{ 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, for: user}), - 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, for: user}), - 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, for: user}) - } + }) + + "favourite" -> + response + |> Map.merge(%{ + status: StatusView.render("status.json", %{activity: parent_activity, for: user}) + }) + + "reblog" -> + response + |> Map.merge(%{ + status: StatusView.render("status.json", %{activity: parent_activity, for: user}) + }) + + "follow" -> + response _ -> nil diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index 35e14c243..8fa28ee8e 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -54,7 +54,7 @@ defmodule Pleroma.Web.Push do when type in @types do actor = User.get_cached_by_ap_id(notification.activity.data["actor"]) - type = format_type(notification) + type = Pleroma.Activity.mastodon_notification_type(notification.activity) Subscription |> where(user_id: ^user_id) @@ -114,16 +114,6 @@ defmodule Pleroma.Web.Push do {:noreply, state} end - # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19 - defp format_type(%{activity: %{data: %{"type" => type}}}) do - case type do - "Create" -> "mention" - "Follow" -> "follow" - "Announce" -> "reblog" - "Like" -> "favourite" - end - end - defp format_title(%{activity: %{data: %{"type" => type}}}) do case type do "Create" -> "New Mention" -- cgit v1.2.3 From 0b4c61e8d5a8a24be44aa6bf48c404b47289d0a8 Mon Sep 17 00:00:00 2001 From: href Date: Fri, 14 Dec 2018 13:56:42 +0100 Subject: Fix warning --- 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 4db7cd60f..0414d73d8 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1061,7 +1061,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do response = %{ id: to_string(id), type: mastodon_type, - created_at: CommonAPI.Utils.to_masto_date(activity.inserted_at), + created_at: CommonAPI.Utils.to_masto_date(created_at), account: AccountView.render("account.json", %{user: actor, for: user}) } -- cgit v1.2.3 From 412df2cd3845802daf80c6d45db13f23f1e1b78b Mon Sep 17 00:00:00 2001 From: href Date: Fri, 14 Dec 2018 16:17:27 +0100 Subject: Warn if push is disabled.. --- lib/pleroma/web/push/push.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex index 8fa28ee8e..477943450 100644 --- a/lib/pleroma/web/push/push.ex +++ b/lib/pleroma/web/push/push.ex @@ -32,7 +32,7 @@ defmodule Pleroma.Web.Push do end def init(:ok) do - if enabled() do + if !enabled() do Logger.warn(""" VAPID key pair is not found. If you wish to enabled web push, please run -- cgit v1.2.3