Skip to content
This repository has been archived by the owner on Jun 11, 2023. It is now read-only.

Commit

Permalink
Merge 197adf2 into d164a1c
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Nov 10, 2017
2 parents d164a1c + 197adf2 commit 39893ac
Show file tree
Hide file tree
Showing 58 changed files with 792 additions and 514 deletions.
18 changes: 17 additions & 1 deletion lib/account/websocket/channel/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ channel Helix.Account.Websocket.Channel.Account do
as: BootstrapRequest
alias Helix.Account.Websocket.Channel.Account.Requests.EmailReply,
as: EmailReplyRequest
alias Helix.Account.Websocket.Channel.Account.Requests.Logout,
as: LogoutRequest

join _, AccountJoin

Expand All @@ -19,7 +21,7 @@ channel Helix.Account.Websocket.Channel.Account do
Replies to a Storyline email.
Params:
*reply_id: Reply identifier.
*reply_id: Reply identifier.
Returns:
%{}
Expand All @@ -31,6 +33,20 @@ channel Helix.Account.Websocket.Channel.Account do
"""
topic "email.reply", EmailReplyRequest

@doc """
Logs out from the channel.
Params: nil
Returns: nil
**Channel will be closed**
Errors:
- internal
"""
topic "account.logout", LogoutRequest

@doc """
Intercepts and handles outgoing events.
"""
Expand Down
30 changes: 30 additions & 0 deletions lib/account/websocket/channel/account/requests/logout.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Helix.Websocket.Request

request Helix.Account.Websocket.Channel.Account.Requests.Logout do
@moduledoc """
Invalidates the session token and shuts down the socket.
"""

alias Helix.Websocket
alias Helix.Account.Websocket.Controller.Account, as: AccountController

def check_params(request, _socket),
do: reply_ok(request)

def check_permissions(request, _socket),
do: reply_ok(request)

def handle_request(request, socket) do

AccountController.logout(socket.assigns, %{})

socket
|> Websocket.id()
|> Helix.Endpoint.broadcast("disconnect", %{})

reply_ok(request)
end

def reply(_request, _socket),
do: {:stop, :shutdown}
end
22 changes: 0 additions & 22 deletions lib/account/websocket/routes.ex

This file was deleted.

3 changes: 1 addition & 2 deletions lib/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ defmodule Helix.Endpoint do

plug Corsica,
origins: Application.get_env(:helix, Helix.Endpoint)[:allowed_cors],
allow_headers: ["content-type", "x-request-id"],
expose_headers: ["X-Request-Id"]
allow_headers: ["content-type", "x-request-id"]

plug Plug.Static,
at: "/",
Expand Down
35 changes: 31 additions & 4 deletions lib/event/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ defmodule Helix.Event do

import HELL.Macros

alias Helix.Websocket.Request.Relay, as: RequestRelay
alias Helix.Process.Model.Process
alias Helix.Event.Dispatcher, as: HelixDispatcher
alias Helix.Event.Meta, as: EventMeta
alias Helix.Event.State.Timer, as: EventTimer
alias Helix.Process.Model.Process

@type t :: HELF.Event.t
@type source :: t | RequestRelay.t
@type relay :: source

@doc """
Top-level macro for an event.
Expand Down Expand Up @@ -58,6 +61,20 @@ defmodule Helix.Event do
to: EventMeta
end

@doc """
This is pure syntactic sugar for: set_{field}(event, get_{field}(source))
I.e. we get {field} from `source` and assign it to `event`.
"""
defmacro relay(event, field, source) do
quote do
event = unquote(event)
source = unquote(source)

unquote(:"set_#{field}")(event, unquote(:"get_#{field}")(source))
end
end

@spec emit([t] | t, from: t) ::
term
@doc """
Expand Down Expand Up @@ -98,12 +115,21 @@ defmodule Helix.Event do
def emit_after(event, interval),
do: EventTimer.emit_after(event, interval)

@spec inherit(t, t) ::
@spec inherit(t, source) ::
t
docp """
The application wants to emit `event`, which is coming from `source`. On this
case, `event` will inherit the source's metadata according to the logic below.
Note that `source` may either be another event (`t`) or a request relay
(`RequestRelay.t`). If it's a RequestRelay, then this event is being emitted
as a result of a direct action from the player. On the other hand, if `source`
is an event, it means this event is a side-effect of another event.
"""
defp inherit(event, nil),
do: event
defp inherit(event, relay = %RequestRelay{}),
do: set_request_id(event, relay.request_id)
defp inherit(event, source) do
# Relay the `process_id`
event =
Expand All @@ -116,9 +142,10 @@ defmodule Helix.Event do

# Accumulate source event on the stacktrace, and save it on the next event
stack = get_stack(source) || []
new_stack = stack ++ [source.__struct__]
event = set_stack(event, stack ++ [source.__struct__])

event = set_stack(event, new_stack)
# Relay the request_id information
event = relay(event, :request_id, source)

# Everything has been inherited, we are ready to emit/1 the event.
event
Expand Down
12 changes: 6 additions & 6 deletions lib/event/loggable/flow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ defmodule Helix.Event.Loggable.Flow do
do: {server_id, entity_id, msg}

@spec save([log_entry] | log_entry) ::
term
[Event.t]
@doc """
Receives the list of generated entries, which is returned by each event
that implements the Loggable protocol, and inserts them into the game
database, emitting the relevant `LogCreatedEvent`
Receives the list of generated entries, which is returned by each event that
implements the Loggable protocol, and inserts them into the game database.
Accumulates the corresponding `LogCreatedEvent`s, which shall be emitted by
the caller.
"""
def save([]),
do: :ok
do: []
def save(log_entry = {_, _, _}),
do: save([log_entry])
def save(logs) do
Expand All @@ -155,6 +156,5 @@ defmodule Helix.Event.Loggable.Flow do
events
end)
|> List.flatten()
|> Enum.each(&Event.emit/1)
end
end
16 changes: 12 additions & 4 deletions lib/event/meta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ defmodule Helix.Event.Meta do

@type t :: %{
event_id: HETypes.uuid | nil,
process_id: Process.id | nil
process_id: Process.id | nil,
stack: [Event.t] | nil,
request_id: binary | nil
}

@type rendered :: %{
event_id: String.t | nil,
process_id: String.t | nil
process_id: String.t | nil,
request_id: binary | nil
}

@meta_key :__meta__
Expand All @@ -36,7 +39,11 @@ defmodule Helix.Event.Meta do

# The `stack` field is a rudimentary stacktrace. Every time an event is
# emitted from another one, the previous event name is stored on this stack.
:stack
:stack,

# The `request_id` field associates which request was responsible for this
# event. Subsequent events will carry on (relay) this request_id as well.
:request_id
]

@doc """
Expand All @@ -59,7 +66,8 @@ defmodule Helix.Event.Meta do
def render(event) do
%{
event_id: get_event_id(event),
process_id: get_process_id(event) |> Utils.stringify()
process_id: get_process_id(event) |> Utils.stringify(),
request_id: get_request_id(event)
}
end

Expand Down
7 changes: 5 additions & 2 deletions lib/log/event/handler/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ defmodule Helix.Log.Event.Handler.Log do
Generic event handler for all Helix events. If the event implement the
Loggable protocol, it will guide it through the LoggableFlow, making sure
the relevant log entries are generated and saved
Emits `LogCreatedEvent`
"""
def handle_event(event) do
if Loggable.impl_for(event) do
event
|> Loggable.generate()
|> Loggable.Flow.save()
|> Event.emit(from: event)
end
end

Expand All @@ -33,7 +36,7 @@ defmodule Helix.Log.Event.Handler.Log do
|> LogQuery.fetch()
|> LogAction.revise(event.entity_id, event.message, event.version)

Event.emit(events)
Event.emit(events, from: event)
end

def log_forge_conclusion(event = %LogForgeCreateComplete{}) do
Expand All @@ -43,6 +46,6 @@ defmodule Helix.Log.Event.Handler.Log do
event.message,
event.version)

Event.emit(events)
Event.emit(events, from: event)
end
end
Loading

0 comments on commit 39893ac

Please sign in to comment.