[#114] Naive implementation of email invitations.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Thu, 13 Dec 2018 13:30:10 +0000 (16:30 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 14 Dec 2018 09:28:14 +0000 (12:28 +0300)
lib/pleroma/emails/user_email.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_controller_test.exs

index 9cdf002f30eaf2b8b7e50c47dd2bcb09eb93ea4f..47dcd42e0ca9decad6ab4c57cd174f67cc1a6597 100644 (file)
@@ -37,4 +37,25 @@ 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 = """
+    <h3>You are invited to #{instance_name()}</h3>
+    <p>#{user.name} invites you to join #{instance_name()}, an instance of Pleroma federated social networking platform.</p>
+    <p>Click the following link to register: <a href="#{registration_url}">accept invitation</a>.</p>
+    """
+
+    new()
+    |> to(recipient(to_email, to_name))
+    |> from(sender())
+    |> subject("Invitation to #{instance_name()}")
+    |> html_body(html_body)
+  end
 end
index 38eff8191785da354ed9b5dedeb8d83092855cb6..d51d712991fadb57aaf014bbcc57418cbaf596b8 100644 (file)
@@ -333,6 +333,16 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     end
   end
 
+  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 update_avatar(%{assigns: %{user: user}} = conn, params) do
     {:ok, object} = ActivityPub.upload(params, type: :avatar)
     change = Changeset.change(user, %{avatar: object.data})
index c16c0cdc0f32759bcead129ccf2ca72cbed7669f..cbb5f77962b0d0a9cc7d1087d0da5ffa2700ae38 100644 (file)
@@ -873,6 +873,38 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "POST /api/email_invite, with valid parameters" do
+    setup [:valid_user]
+
+    setup do
+      invites_enabled = Pleroma.Config.get([:instance, :invites_enabled])
+      Pleroma.Config.put([:instance, :invites_enabled], true)
+
+      on_exit(fn ->
+        Pleroma.Config.put([:instance, :invites_enabled], invites_enabled)
+        :ok
+      end)
+
+      :ok
+    end
+
+    test "it returns 204", %{conn: conn, user: user} do
+      recipient_email = "foo@bar.com"
+      recipient_name = "J. D."
+
+      conn =
+        conn
+        |> assign(:user, user)
+        |> post("/api/email_invite?email=#{recipient_email}&name=#{recipient_name}")
+
+      assert json_response(conn, :no_content)
+
+      Swoosh.TestAssertions.assert_email_sent(
+        Pleroma.UserEmail.user_invitation_email(user, recipient_email, recipient_name)
+      )
+    end
+  end
+
   describe "GET /api/externalprofile/show" do
     test "it returns the user", %{conn: conn} do
       user = insert(:user)