From a05cb10a95901ff0daacfc17a7709f3a277f1cd4 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Fri, 14 Dec 2018 16:38:56 +0300 Subject: [PATCH] [#114] Email confirmation route, action, node setting, User.Info fields. --- lib/pleroma/user.ex | 4 ++++ lib/pleroma/user/info.ex | 6 ++++++ lib/pleroma/web/nodeinfo/nodeinfo_controller.ex | 1 + lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 13 +++++++++++++ 5 files changed, 25 insertions(+) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 3ad1ab87a..ee0a0dfb9 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -364,6 +364,10 @@ def get_or_fetch_by_nickname(nickname) do end end + def get_by_confirmation_token(token) do + Repo.one(from(u in User, where: fragment("? ->> 'confirmation_token' = ?", u.info, ^token))) + end + def get_followers_query(%User{id: id, follower_address: follower_address}) do from( u in User, diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index a3785447c..f75984038 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -9,6 +9,8 @@ defmodule Pleroma.User.Info do field(:note_count, :integer, default: 0) field(:follower_count, :integer, default: 0) field(:locked, :boolean, default: false) + field(:confirmation_pending, :boolean, default: false) + field(:confirmation_token, :string, default: nil) field(:default_scope, :string, default: "public") field(:blocks, {:array, :string}, default: []) field(:domain_blocks, {:array, :string}, default: []) @@ -141,6 +143,10 @@ def profile_update(info, params) do ]) end + def confirmation_update(info, params) do + cast(info, params, [:confirmation_pending, :confirmation_token]) + end + def mastodon_profile_update(info, params) do info |> cast(params, [ diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex index 44c11f40a..70921605d 100644 --- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex +++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex @@ -132,6 +132,7 @@ def nodeinfo(conn, %{"version" => "2.0"}) do banner: Keyword.get(instance, :banner_upload_limit), background: Keyword.get(instance, :background_upload_limit) }, + accountActivationRequired: Keyword.get(instance, :account_activation_required, false), invitesEnabled: Keyword.get(instance, :invites_enabled, false), features: features } diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index dd1985d6e..b2fbc088d 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -281,6 +281,7 @@ defmodule Pleroma.Web.Router do post("/account/register", TwitterAPI.Controller, :register) post("/account/password_reset", TwitterAPI.Controller, :password_reset) + get("/account/confirm_email/:token", TwitterAPI.Controller, :confirm_email) 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 327620302..2680be25f 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -372,6 +372,19 @@ def password_reset(conn, params) do end end + def confirm_email(conn, %{"token" => token}) do + with %User{} = user <- User.get_by_confirmation_token(token), + true <- user.local, + new_info_fields <- %{confirmation_pending: false, confirmation_token: nil}, + info_change <- User.Info.confirmation_update(user.info, new_info_fields), + changeset <- Changeset.change(user) |> Changeset.put_embed(:info, info_change), + {:ok, _} <- User.update_and_set_cache(changeset) do + conn + |> put_flash(:info, "Email confirmed. Please sign in.") + |> redirect(to: "/") + 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})