diff --git a/lib/chess/emails.ex b/lib/chess/emails.ex index 1c3395f..717a159 100644 --- a/lib/chess/emails.ex +++ b/lib/chess/emails.ex @@ -2,6 +2,10 @@ defmodule Chess.Emails do @moduledoc false import Bamboo.Email + import ChessWeb.GameView, only: [opponent: 2] + + alias Chess.Repo + alias Chess.Store.User def new_game_email(conn, game) do new_email() @@ -14,4 +18,19 @@ defmodule Chess.Emails do Game link: #{ChessWeb.Router.Helpers.game_url(conn, :show, game)} """) end + + def opponent_moved_email(socket, game) do + user = Repo.get(User, socket.assigns.user_id) + opponent = opponent(game, socket.assigns.user_id) + + new_email() + |> to(opponent) + |> from({"64squares", "games@64squares.club"}) + |> subject( + "[64squares] #{user.name} has moved." + ) + |> text_body(""" + Game link: #{ChessWeb.Router.Helpers.game_url(socket, :show, game)} + """) + end end diff --git a/lib/chess_web/channels/game_channel.ex b/lib/chess_web/channels/game_channel.ex index aab0e28..5eff7c2 100644 --- a/lib/chess_web/channels/game_channel.ex +++ b/lib/chess_web/channels/game_channel.ex @@ -6,6 +6,8 @@ defmodule ChessWeb.GameChannel do import ChessWeb.GameView, only: [player: 2, opponent: 2] alias Chess.Board + alias Chess.Emails + alias Chess.Mailer alias Chess.MoveList alias Chess.Moves alias Chess.Repo.Queries @@ -42,14 +44,17 @@ defmodule ChessWeb.GameChannel do end def handle_in("game:move", params, socket) do + game = + socket.assigns.user_id + |> Queries.game_for_info(socket.assigns.game_id) + move_params = convert_params(params) - socket.assigns.user_id - |> Queries.game_with_moves(socket.assigns.game_id) + game |> Moves.make_move(move_params) |> case do {:ok, _} -> - send_update(socket) + update_opponent(socket, game) {:noreply, socket} {:error, :game, changeset, _} -> @@ -80,6 +85,24 @@ defmodule ChessWeb.GameChannel do {:reply, {:ok, reply}, socket} end + def update_opponent(socket, game) do + opponent_id = + opponent(game, socket.assigns.user_id).id + |> Integer.to_string + + "game:#{game.id}" + |> Presence.list + |> case do + %{^opponent_id => _} -> + send_update(socket) + _ -> + send_update(socket) + socket + |> Emails.opponent_moved_email(game) + |> Mailer.deliver_later + end + end + def track_presence(socket) do {:ok, _} = Presence.track(socket, socket.assigns.user_id, %{ user_id: socket.assigns.user_id, diff --git a/lib/chess_web/channels/user_socket.ex b/lib/chess_web/channels/user_socket.ex index 6d7c3cd..c5dead9 100644 --- a/lib/chess_web/channels/user_socket.ex +++ b/lib/chess_web/channels/user_socket.ex @@ -29,7 +29,7 @@ defmodule ChessWeb.UserSocket do :error end end - def connect(%{}, socket), do: :error + def connect(%{}, _socket), do: :error # Socket id's are topics that allow you to identify all sockets for a given user: # diff --git a/lib/chess_web/controllers/game_controller.ex b/lib/chess_web/controllers/game_controller.ex index ff50a82..93e5e16 100644 --- a/lib/chess_web/controllers/game_controller.ex +++ b/lib/chess_web/controllers/game_controller.ex @@ -1,9 +1,10 @@ defmodule ChessWeb.GameController do use ChessWeb, :controller + alias Chess.Emails + alias Chess.Mailer alias Chess.Store.Game alias Chess.Store.User - alias Chess.Mailer import Chess.Auth, only: [current_user: 1] @@ -42,7 +43,7 @@ defmodule ChessWeb.GameController do |> case do {:ok, game} -> conn - |> Chess.Emails.new_game_email( + |> Emails.new_game_email( game |> Repo.preload(:user) |> Repo.preload(:opponent) diff --git a/test/chess/store/move_test.exs b/test/chess/store/move_test.exs index 01f40ca..5b61040 100644 --- a/test/chess/store/move_test.exs +++ b/test/chess/store/move_test.exs @@ -10,7 +10,6 @@ defmodule Chess.Store.MoveTest do alias Chess.Store.Move describe "move" do - test "move is valid with a game, a from, and a to" do user = insert(:user, %{email: "link@hyrule.com"}) opponent = insert(:user, %{email: "zelda@hyrule.com"}) diff --git a/test/features/moves_test.exs b/test/features/moves_test.exs index ab91262..30ea028 100644 --- a/test/features/moves_test.exs +++ b/test/features/moves_test.exs @@ -1,5 +1,6 @@ defmodule Chess.Features.MovesTest do use ChessWeb.FeatureCase + use Bamboo.Test, shared: true import Wallaby.Query @@ -31,6 +32,37 @@ defmodule Chess.Features.MovesTest do |> refute_has(square_containing("f4-r1", "white.pawn")) end + test "opponents recieves an email on move", %{session: session} do + user = insert(:user, %{ + name: "Link", + email: "link@hyrule.com", + password: "ilovezelda" + }) + opponent = insert(:user, %{ + name: "Zelda", + email: "zelda@hyrule.com", + password: "ganonsucks" + }) + + session + |> login("link@hyrule.com", "ilovezelda") + |> visit("/games") + |> click(link("New game")) + |> select("game[opponent_id]", option: "Zelda") + |> click(button("Create game")) + + assert_email_delivered_with(to: [{opponent.name, opponent.email}]) + + session + |> click(css("#f4-r1")) + |> click(css("#f4-r3")) + + assert_email_delivered_with( + to: [{opponent.name, opponent.email}], + subject: "[64squares] #{user.name} has moved." + ) + end + test "cannot move the opponents pieces", %{session: session} do insert(:user, %{ name: "Zelda",