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
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

View File

@ -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,

View File

@ -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:
#

View File

@ -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)

View File

@ -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"})

View File

@ -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",