Skip to content

Commit

Permalink
Merge pull request #556 from geekingfrog/user-updated-event
Browse files Browse the repository at this point in the history
User updated event
L-e-x-o-n authored Jan 19, 2025
2 parents d595bf4 + 50e7585 commit c690715
Showing 8 changed files with 153 additions and 12 deletions.
22 changes: 19 additions & 3 deletions lib/teiserver/player/tachyon_handler.ex
Original file line number Diff line number Diff line change
@@ -38,14 +38,30 @@ defmodule Teiserver.Player.TachyonHandler do
end

@impl Handler
@spec init(%{user: T.user()}) :: WebSock.handle_result()
@spec init(%{user: T.user()}) :: Handler.result()
def init(initial_state) do
# this is inside the process that maintain the connection
{:ok, session_pid} = setup_session(initial_state.user)
sess_monitor = Process.monitor(session_pid)

{:ok,
initial_state |> Map.put(:sess_monitor, sess_monitor) |> Map.put(:pending_responses, %{})}
state =
initial_state |> Map.put(:sess_monitor, sess_monitor) |> Map.put(:pending_responses, %{})

user = initial_state.user

event = %{
users: [
%{
userId: to_string(user.id),
username: user.name,
countryCode: user.country,
status: :menu,
clanId: user.clan_id
}
]
}

{:event, "user/updated", event, state}
end

@impl Handler
2 changes: 1 addition & 1 deletion lib/teiserver/tachyon/handler.ex
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ defmodule Teiserver.Tachyon.Handler do
@doc """
Same as `WebSock.init/1`
"""
@callback init(term()) :: WebSock.handle_result()
@callback init(term()) :: result()

@doc """
Same as `WebSock.handle_info/2`
89 changes: 89 additions & 0 deletions priv/tachyon/schema/user/updated/event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"$id": "https://schema.beyondallreason.dev/tachyon/user/updated/event.json",
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "UserUpdatedEvent",
"tachyon": {
"source": "server",
"target": "user",
"scopes": ["tachyon.lobby"]
},
"type": "object",
"properties": {
"type": { "const": "event" },
"messageId": { "type": "string" },
"commandId": { "const": "user/updated" },
"data": {
"title": "UserUpdatedEventData",
"type": "object",
"properties": {
"users": {
"type": "array",
"items": {
"type": "object",
"allOf": [
{
"type": "object",
"properties": {
"userId": {
"type": "string",
"examples": ["351"]
},
"username": { "type": "string" },
"displayName": { "type": "string" },
"clanId": {
"anyOf": [
{ "type": "string" },
{ "type": "null" }
]
},
"partyId": {
"anyOf": [
{ "type": "string" },
{ "type": "null" }
]
},
"scopes": {
"type": "array",
"items": { "type": "string" }
},
"countryCode": { "type": "string" },
"status": {
"enum": [
"offline",
"menu",
"playing",
"lobby"
]
}
}
},
{
"type": "object",
"properties": {
"friendIds": {
"type": "array",
"items": { "type": "string" }
},
"outgoingFriendRequestIds": {
"type": "array",
"items": { "type": "string" }
},
"incomingFriendRequestIds": {
"type": "array",
"items": { "type": "string" }
},
"ignoreIds": {
"type": "array",
"items": { "type": "string" }
}
}
}
]
}
}
},
"required": ["users"]
}
},
"required": ["type", "messageId", "commandId", "data"]
}
20 changes: 15 additions & 5 deletions test/support/tachyon.ex
Original file line number Diff line number Diff line change
@@ -15,18 +15,28 @@ defmodule Teiserver.Support.Tachyon do
@doc """
connects the given user and returns the ws client
"""
def connect(%Teiserver.OAuth.Token{} = token) do
opts = connect_options(token)
def connect(x, opts \\ [swallow_first_event: true])

def connect(%Teiserver.OAuth.Token{} = token, opts) do
connect_opts = connect_options(token)

{:ok, client} = WSC.connect(tachyon_url(), connect_opts)

# by default, swallow the user/updated event sent at login
swallow = Keyword.get(opts, :swallow_first_event, true)

if swallow and not is_nil(token.owner_id) do
{:ok, _user_updated} = recv_message(client)
end

{:ok, client} = WSC.connect(tachyon_url(), opts)
ExUnit.Callbacks.on_exit(fn -> WSC.disconnect(client) end)
client
end

def connect(user) do
def connect(user, opts) do
%{token: token} = OAuthFixtures.setup_token(user)

client = connect(token)
client = connect(token, opts)
%{client: client, token: token}
end

2 changes: 2 additions & 0 deletions test/teiserver/data/battle/lobby_chat_test.exs
Original file line number Diff line number Diff line change
@@ -4,6 +4,8 @@ defmodule Teiserver.Data.Battle.ChatLibTest do
import Teiserver.TeiserverTestLib, only: [new_user: 0]
alias Teiserver.Lobby.{ChatLib}

# https://github.com/beyond-all-reason/teiserver/actions/runs/12845418717/job/35819461341?pr=556
@tag :needs_attention
test "test lobby chat as bot" do
bot_user = new_user()
bot_user = CacheUser.update_user(%{bot_user | bot: true, roles: ["Bot"]})
2 changes: 1 addition & 1 deletion test/teiserver_web/tachyon/matchmaking_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Teiserver.Matchmaking.MatchmakingTest do
defmodule Teiserver.Tachyon.MatchmakingTest do
use TeiserverWeb.ConnCase
alias Teiserver.Support.Tachyon
alias Teiserver.OAuthFixtures
3 changes: 1 addition & 2 deletions test/teiserver_web/tachyon/session_test.exs
Original file line number Diff line number Diff line change
@@ -39,7 +39,6 @@ defmodule TeiserverWeb.Tachyon.SessionTest do
client: client,
token: token
} do
ensure_connected(client)
opts = Tachyon.connect_options(token)
{:ok, client2} = WSC.connect(Tachyon.tachyon_url(), opts)
ensure_connected(client2)
@@ -52,7 +51,6 @@ defmodule TeiserverWeb.Tachyon.SessionTest do
end

test "session dies after too long", %{client: client, user: user} do
ensure_connected(client)
Tachyon.abrupt_disconnect!(client)
assert {:error, :disconnected} = WSC.send_message(client, {:text, "test_ping"})
sess_pid = Player.SessionRegistry.lookup(user.id)
@@ -65,6 +63,7 @@ defmodule TeiserverWeb.Tachyon.SessionTest do

defp ensure_connected(client) do
WSC.send_message(client, {:text, "test_ping"})
Tachyon.recv_message!(client)
assert {:ok, {:text, "test_pong"}} == WSC.recv(client)
end
end
25 changes: 25 additions & 0 deletions test/teiserver_web/tachyon/user.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Teiserver.Tachyon.UserTest do
use TeiserverWeb.ConnCase, async: false
alias Teiserver.Support.Tachyon

describe "updated" do
test "sent after login" do
user = Central.Helpers.GeneralTestLib.make_user(%{"data" => %{"roles" => ["Verified"]}})

%{client: client} = Tachyon.connect(user, swallow_first_event: false)

{:ok,
%{
"commandId" => "user/updated",
"data" => %{
"users" => [userdata]
}
}} = Tachyon.recv_message(client)

assert userdata["userId"] == to_string(user.id)
assert userdata["username"] == user.name
assert userdata["clanId"] == user.clan_id
assert userdata["status"] == "menu"
end
end
end

0 comments on commit c690715

Please sign in to comment.