This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
165 changed files
with
7,136 additions
and
2,296 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
defmodule Helix.Account.Public.Account do | ||
|
||
alias Helix.Entity.Model.Entity | ||
alias Helix.Server.Public.Index, as: ServerIndex | ||
alias Helix.Account.Public.Index, as: AccountIndex | ||
|
||
@type bootstrap :: | ||
%{ | ||
account: AccountIndex.index, | ||
servers: ServerIndex.index | ||
} | ||
|
||
@doc """ | ||
Returns all data related to the player. Used by the client upon login (or sync | ||
request). | ||
The final result is a fractal-like format created by the indexes being used. | ||
""" | ||
@spec bootstrap(Entity.id) :: | ||
bootstrap | ||
def bootstrap(entity_id) do | ||
%{ | ||
account: AccountIndex.index(), | ||
servers: ServerIndex.index(entity_id) | ||
} | ||
end | ||
|
||
@doc """ | ||
Renders the bootstrap result by calling each index renderer. | ||
Similar in purpose to `ProcessViewable`. | ||
""" | ||
@spec render_bootstrap(bootstrap) :: | ||
%{ | ||
account: AccountIndex.rendered_index, | ||
servers: ServerIndex.rendered_index | ||
} | ||
def render_bootstrap(bootstrap) do | ||
%{ | ||
account: AccountIndex.render_index(bootstrap.account), | ||
servers: ServerIndex.render_index(bootstrap.servers), | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule Helix.Account.Public.Index do | ||
|
||
@type index :: term | ||
|
||
@type rendered_index :: term | ||
|
||
@spec index() :: | ||
index | ||
def index do | ||
%{} | ||
end | ||
|
||
@spec render_index(index) :: | ||
rendered_index | ||
def render_index(index) do | ||
index | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
defmodule Helix.Account.Websocket.Channel.Account.Join do | ||
@moduledoc """ | ||
Joinable implementation for the Account channel. | ||
There's only one way to subscribe to an account's channel, and only the owner | ||
of the account (authenticated on the Socket) can join this channel. | ||
Therefore, the requested account_id (specified on the Channel's topic) must | ||
belong to the authenticated user on the socket. | ||
""" | ||
|
||
require Helix.Websocket.Join | ||
|
||
Helix.Websocket.Join.register() | ||
|
||
defimpl Helix.Websocket.Joinable do | ||
|
||
alias Helix.Account.Model.Account | ||
|
||
def check_params(request, _socket) do | ||
account_id = get_id_from_topic(request.topic) | ||
|
||
with {:ok, account_id} <- Account.ID.cast(account_id) do | ||
params = %{ | ||
account_id: account_id | ||
} | ||
|
||
{:ok, %{request| params: params}} | ||
else | ||
_ -> | ||
{:error, %{message: "bad_request"}} | ||
end | ||
end | ||
|
||
@doc """ | ||
This is where we check the requested account is the same one currently | ||
authenticated. | ||
""" | ||
def check_permissions(request, socket) do | ||
account_id = socket.assigns.account.account_id | ||
|
||
if account_id == request.params.account_id do | ||
{:ok, request} | ||
else | ||
{:error, %{message: "access_denied"}} | ||
end | ||
end | ||
|
||
def join(_request, socket, _assign), | ||
do: {:ok, socket} | ||
|
||
defp get_id_from_topic(topic), | ||
do: List.last(String.split(topic, "account:")) | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
lib/account/websocket/channel/account/requests/bootstrap.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule Helix.Account.Websocket.Channel.Account.Requests.Bootstrap do | ||
|
||
require Helix.Websocket.Request | ||
|
||
Helix.Websocket.Request.register() | ||
|
||
defimpl Helix.Websocket.Requestable do | ||
|
||
alias Helix.Websocket.Utils, as: WebsocketUtils | ||
alias Helix.Account.Public.Account, as: AccountPublic | ||
|
||
def check_params(request, _socket), | ||
do: {:ok, request} | ||
|
||
def check_permissions(request, _socket), | ||
do: {:ok, request} | ||
|
||
def handle_request(request, socket) do | ||
entity_id = socket.assigns.entity_id | ||
|
||
meta = %{bootstrap: AccountPublic.bootstrap(entity_id)} | ||
|
||
{:ok, %{request| meta: meta}} | ||
end | ||
|
||
def reply(request, socket) do | ||
data = AccountPublic.render_bootstrap(request.meta.bootstrap) | ||
WebsocketUtils.reply_ok(data, socket) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.