Skip to content

Commit

Permalink
broadcast messages on invite cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
geekingfrog committed Feb 27, 2025
1 parent 6534181 commit 2fc3b90
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
59 changes: 59 additions & 0 deletions lib/teiserver/protocols/spring/spring_party_in.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,52 @@ defmodule Teiserver.Protocols.Spring.PartyIn do
SpringOut.reply(:okay, "c.party.leave_current_party", msg_id, Map.put(state, :party_id, nil))
end

def do_handle("cancel_invite_to_party", data, msg_id, state) do
cmd_id = "c.party.cancel_invite_to_party"

with [username] <- String.split(data) |> Enum.map(&String.trim/1),
user when not is_nil(user) <- Teiserver.Account.get_user_by_name(username),
:ok <- if(state.party_id != nil, do: :ok, else: :not_in_party),
:ok <- if(Account.client_exists?(user.id), do: :ok, else: :no_client) do
party_id = state.party_id
Account.cancel_party_invite(party_id, user.id)

PubSub.broadcast!(
Teiserver.PubSub,
"teiserver_client_messages:#{user.id}",
%{
channel: "teiserver_party:#{party_id}",
event: {:invite_cancelled, user.id},
party_id: party_id
}
)

SpringOut.reply(:okay, cmd_id, msg_id, state)
else
nil ->
SpringOut.reply(:no, {cmd_id, "msg=no user found"}, msg_id, state)

:not_in_party ->
SpringOut.reply(
:no,
{cmd_id, "msg=cannot cancel invite when not in party"},
msg_id,
state
)

:no_client ->
SpringOut.reply(:no, {cmd_id, "msg=user not connected"}, msg_id, state)

_ ->
SpringOut.reply(
:no,
{cmd_id, "msg=expected username in argument but could not parse"},
msg_id,
state
)
end
end

def do_handle(msg, _, _msg_id, state) do
Logger.debug("Unhandled party message: #{msg}")
state
Expand Down Expand Up @@ -170,6 +216,19 @@ defmodule Teiserver.Protocols.Spring.PartyIn do
end
end

def handle_event(
%{event: {:invite_cancelled, user_id}, party_id: party_id},
state
) do
case Teiserver.Account.get_user_by_id(user_id) do
nil ->
state

user ->
SpringOut.reply(:party, :invite_cancelled, {party_id, user.name}, message_id(), state)
end
end

def handle_event(event, state) do
Logger.debug("Unhandled party event: #{inspect(event)}")
state
Expand Down
33 changes: 33 additions & 0 deletions test/teiserver/protocols/spring/spring_party_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ defmodule Teiserver.Protocols.Spring.SpringPartyTest do
accept_invite(sock2, party_id)
assert {"NO", _, _} = _recv_until(sock2) |> parse_in_message()
end

test "cancel invite, bad user", ctx do
cancel_invite(ctx.socket1, "definitely-not-a-valid-username")
assert {"NO", _, _} = _recv_until(ctx.socket1) |> parse_in_message()
end

test "must be in party to cancel invite", ctx do
cancel_invite(ctx.socket2, ctx.user2.name)
assert {"NO", _, _} = _recv_until(ctx.socket2) |> parse_in_message()
end

test "cancel invite", ctx do
username2 = ctx.user2.name
cancel_invite!(ctx.socket1, username2)
party_id = ctx.party_id

assert {"s.party.invite_cancelled", [^party_id, ^username2], _} =
_recv_until(ctx.socket2) |> parse_in_message()
end
end

describe "leaving parties" do
Expand Down Expand Up @@ -145,6 +164,7 @@ defmodule Teiserver.Protocols.Spring.SpringPartyTest do
Teiserver.Account.leave_party(ctx.party_id, ctx.user2.id)
party_id = ctx.party_id
user2 = ctx.user2.name

assert {"s.party.left_party", [^party_id, ^user2], _} =
_recv_until(ctx.socket1) |> parse_in_message()

Expand Down Expand Up @@ -205,6 +225,19 @@ defmodule Teiserver.Protocols.Spring.SpringPartyTest do
assert {"OK", _, _} = _recv_until(socket) |> parse_in_message()
end

defp cancel_invite(socket, username) do
msg_id = :rand.uniform(1_000_000) |> to_string()
_send_raw(socket, "##{msg_id} c.party.cancel_invite_to_party #{username}\n")
end

defp cancel_invite!(socket, username) do
cancel_invite(socket, username)

[ok, cancelled] = _recv_until(socket) |> parse_in_messages()
assert {"OK", _, _} = ok
assert {"s.party.invite_cancelled", _, _} = cancelled
end

defp leave_party(socket) do
msg_id = :rand.uniform(1_000_000) |> to_string()
_send_raw(socket, "##{msg_id} c.party.leave_current_party\n")
Expand Down

0 comments on commit 2fc3b90

Please sign in to comment.