1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00

Send opponent an email if they're offline

This commit is contained in:
Daniel Barber 2018-09-08 16:03:52 -04:00
parent 3f1241e3a7
commit 6ee029fa23
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
6 changed files with 81 additions and 7 deletions

View File

@ -2,6 +2,10 @@ defmodule Chess.Emails do
@moduledoc false @moduledoc false
import Bamboo.Email import Bamboo.Email
import ChessWeb.GameView, only: [opponent: 2]
alias Chess.Repo
alias Chess.Store.User
def new_game_email(conn, game) do def new_game_email(conn, game) do
new_email() new_email()
@ -14,4 +18,19 @@ defmodule Chess.Emails do
Game link: #{ChessWeb.Router.Helpers.game_url(conn, :show, game)} Game link: #{ChessWeb.Router.Helpers.game_url(conn, :show, game)}
""") """)
end 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 end

View File

@ -6,6 +6,8 @@ defmodule ChessWeb.GameChannel do
import ChessWeb.GameView, only: [player: 2, opponent: 2] import ChessWeb.GameView, only: [player: 2, opponent: 2]
alias Chess.Board alias Chess.Board
alias Chess.Emails
alias Chess.Mailer
alias Chess.MoveList alias Chess.MoveList
alias Chess.Moves alias Chess.Moves
alias Chess.Repo.Queries alias Chess.Repo.Queries
@ -42,14 +44,17 @@ defmodule ChessWeb.GameChannel do
end end
def handle_in("game:move", params, socket) do 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) move_params = convert_params(params)
socket.assigns.user_id game
|> Queries.game_with_moves(socket.assigns.game_id)
|> Moves.make_move(move_params) |> Moves.make_move(move_params)
|> case do |> case do
{:ok, _} -> {:ok, _} ->
send_update(socket) update_opponent(socket, game)
{:noreply, socket} {:noreply, socket}
{:error, :game, changeset, _} -> {:error, :game, changeset, _} ->
@ -80,6 +85,24 @@ defmodule ChessWeb.GameChannel do
{:reply, {:ok, reply}, socket} {:reply, {:ok, reply}, socket}
end 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 def track_presence(socket) do
{:ok, _} = Presence.track(socket, socket.assigns.user_id, %{ {:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
user_id: socket.assigns.user_id, user_id: socket.assigns.user_id,

View File

@ -29,7 +29,7 @@ defmodule ChessWeb.UserSocket do
:error :error
end end
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: # Socket id's are topics that allow you to identify all sockets for a given user:
# #

View File

@ -1,9 +1,10 @@
defmodule ChessWeb.GameController do defmodule ChessWeb.GameController do
use ChessWeb, :controller use ChessWeb, :controller
alias Chess.Emails
alias Chess.Mailer
alias Chess.Store.Game alias Chess.Store.Game
alias Chess.Store.User alias Chess.Store.User
alias Chess.Mailer
import Chess.Auth, only: [current_user: 1] import Chess.Auth, only: [current_user: 1]
@ -42,7 +43,7 @@ defmodule ChessWeb.GameController do
|> case do |> case do
{:ok, game} -> {:ok, game} ->
conn conn
|> Chess.Emails.new_game_email( |> Emails.new_game_email(
game game
|> Repo.preload(:user) |> Repo.preload(:user)
|> Repo.preload(:opponent) |> Repo.preload(:opponent)

View File

@ -10,7 +10,6 @@ defmodule Chess.Store.MoveTest do
alias Chess.Store.Move alias Chess.Store.Move
describe "move" do describe "move" do
test "move is valid with a game, a from, and a to" do test "move is valid with a game, a from, and a to" do
user = insert(:user, %{email: "link@hyrule.com"}) user = insert(:user, %{email: "link@hyrule.com"})
opponent = insert(:user, %{email: "zelda@hyrule.com"}) opponent = insert(:user, %{email: "zelda@hyrule.com"})

View File

@ -1,5 +1,6 @@
defmodule Chess.Features.MovesTest do defmodule Chess.Features.MovesTest do
use ChessWeb.FeatureCase use ChessWeb.FeatureCase
use Bamboo.Test, shared: true
import Wallaby.Query import Wallaby.Query
@ -31,6 +32,37 @@ defmodule Chess.Features.MovesTest do
|> refute_has(square_containing("f4-r1", "white.pawn")) |> refute_has(square_containing("f4-r1", "white.pawn"))
end 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 test "cannot move the opponents pieces", %{session: session} do
insert(:user, %{ insert(:user, %{
name: "Zelda", name: "Zelda",