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

Commit

Permalink
Add LogForgeRequest for Create and Edit operations
Browse files Browse the repository at this point in the history
  • Loading branch information
renatomassaro committed Aug 13, 2018
1 parent 75ed1a6 commit 2ddbcef
Show file tree
Hide file tree
Showing 38 changed files with 1,500 additions and 107 deletions.
5 changes: 5 additions & 0 deletions lib/event/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ defmodule Helix.Event.Dispatcher do
event LogEvent.Log.Deleted
event LogEvent.Log.Revised

# Custom handlers
event LogEvent.Forge.Processed,
LogHandler.Log,
:log_forge_processed

##############################################################################
# Process events
##############################################################################
Expand Down
2 changes: 1 addition & 1 deletion lib/event/loggable/flow.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Helix.Event.Loggable.Flow do
alias Helix.Network.Query.Bounce, as: BounceQuery
alias Helix.Server.Model.Server

@typep log_entry ::
@type log_entry ::
{Server.id, Entity.id, Log.info}

@doc """
Expand Down
1 change: 1 addition & 0 deletions lib/event/notificable/notificable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defprotocol Helix.Event.Notificable do
| %{account_id: Account.id, server_id: Server.id}
| %{account_id: Entity.id, server_id: Server.id}
| Server.id
| :no_one

@spec get_notification_info(Event.t) ::
{Notification.class, Notification.code}
Expand Down
99 changes: 99 additions & 0 deletions lib/log/action/flow/forge.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# credo:disable-for-this-file Credo.Check.Refactor.FunctionArity
defmodule Helix.Log.Action.Flow.Forge do

alias Helix.Event
alias Helix.Entity.Model.Entity
alias Helix.Network.Model.Connection
alias Helix.Network.Model.Tunnel
alias Helix.Server.Model.Server
alias Helix.Software.Model.File
alias Helix.Log.Model.Log

alias Helix.Log.Process.Forge, as: LogForgeProcess

@spec create(
Server.t,
Server.t,
Log.info,
File.t,
{Tunnel.t, Connection.ssh} | nil,
Event.relay
) ::
term
def create(
gateway = %Server{},
endpoint = %Server{},
log_info,
forger = %File{software_type: :log_forger},
conn,
relay
) do
start_process(gateway, endpoint, nil, log_info, forger, nil, conn, relay)
end

@spec edit(
Server.t,
Server.t,
Log.t,
Log.info,
File.t,
Entity.id,
{Tunnel.t, Connection.ssh} | nil,
Event.relay
) ::
term
def edit(
gateway = %Server{},
endpoint = %Server{},
log = %Log{},
log_info,
forger = %File{software_type: :log_forger},
entity_id = %Entity.ID{},
conn,
relay
) do
start_process(
gateway, endpoint, log, log_info, forger, entity_id, conn, relay
)
end

defp start_process(
gateway = %Server{},
endpoint = %Server{},
log,
log_info,
forger = %File{software_type: :log_forger},
entity_id,
conn_info,
relay
) do
action =
if is_nil(log) do
:create
else
:edit
end

{network_id, ssh} =
if is_nil(conn_info) do
{nil, nil}
else
{tunnel, ssh} = conn_info
{tunnel.network_id, ssh}
end

params = %{log_info: log_info}

meta =
%{
forger: forger,
log: log,
action: action,
ssh: ssh,
entity_id: entity_id,
network_id: network_id
}

LogForgeProcess.execute(gateway, endpoint, params, meta, relay)
end
end
2 changes: 2 additions & 0 deletions lib/log/event/forge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ defmodule Helix.Log.Event.Forge do
achieved its objective and finished executing.
"""

alias Helix.Entity.Model.Entity
alias Helix.Process.Model.Process
alias Helix.Server.Model.Server
alias Helix.Log.Model.Log
alias Helix.Log.Process.Forge, as: LogForgeProcess

Expand Down
29 changes: 29 additions & 0 deletions lib/log/event/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ defmodule Helix.Log.Event.Log do
def whom_to_publish(event),
do: %{server: event.log.server_id}
end

notification do
@moduledoc """
When the created log is artificial a notification is sent to the player.
"""

@class :server
@code :log_created

def whom_to_notify(%_{log: log}) do
if Log.is_artificial?(log) do
%{account_id: log.revision.entity_id, server_id: log.server_id}
else
:no_one
end
end
end
end

event Revised do
Expand Down Expand Up @@ -86,6 +103,18 @@ defmodule Helix.Log.Event.Log do
def whom_to_publish(event),
do: %{server: event.log.server_id}
end

notification do
@moduledoc """
When the created log is artificial a notification is sent to the player.
"""

@class :server
@code :log_revised

def whom_to_notify(%_{log: log}),
do: %{account_id: log.revision.entity_id, server_id: log.server_id}
end
end

event Deleted do
Expand Down
5 changes: 3 additions & 2 deletions lib/log/henforcer/log/forge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ defmodule Helix.Log.Henforcer.Log.Forge do

import Helix.Henforcer

alias Helix.Server.Model.Server
alias Helix.Server.Henforcer.Server, as: ServerHenforcer
alias Helix.Server.Model.Server
alias Helix.Software.Henforcer.File, as: FileHenforcer
alias Helix.Log.Model.Log
alias Helix.Software.Model.File
alias Helix.Log.Henforcer.Log, as: LogHenforcer
alias Helix.Log.Model.Log

@type can_edit_relay :: %{log: Log.t, gateway: Server.t, forger: File.t}
@type can_edit_relay_partial :: map
Expand Down
17 changes: 11 additions & 6 deletions lib/log/model/log.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ defmodule Helix.Log.Model.Log do
import HELL.Ecto.Macros

alias Ecto.Changeset
alias Helix.Entity.Model.Entity
alias Helix.Server.Model.Server
alias Helix.Log.Model.LogType
alias Helix.Log.Model.Revision
Expand All @@ -28,11 +27,7 @@ defmodule Helix.Log.Model.Log do
@type data :: LogType.data
@type info :: {type, data}

@type creation_params ::
%{
server_id: Server.id,
entity_id: Entity.id
}
@type creation_params :: %{server_id: Server.id}

@creation_fields [:server_id]
@required_fields [:server_id, :revision_id, :creation_time, :log_id]
Expand Down Expand Up @@ -131,6 +126,16 @@ defmodule Helix.Log.Model.Log do
{:recover, changeset}
end

@spec is_artificial?(Log.t) ::
boolean
@doc """
Returns whether the log is artificial or not.
"""
def is_artificial?(%Log{revision: %{forge_version: nil}}),
do: false
def is_artificial?(%Log{}),
do: true

@spec build_heritage(creation_params) ::
Helix.ID.heritage
defp build_heritage(params),
Expand Down
103 changes: 72 additions & 31 deletions lib/log/model/log_type/macros.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,44 @@ defmodule Helix.Log.Model.LogType.Macros do

defenum LogEnum, @logs

# @spec exists?(term) ::
# boolean
def exists?(log) do
Enum.any?(@logs, fn {valid_log, _} -> valid_log == log end)
end

def new(type, data_params) do
@spec exists?(atom) ::
boolean
def exists?(log_type),
do: Enum.any?(@logs, fn {valid_type, _} -> valid_type == log_type end)

@spec new(type, map) ::
struct
@doc """
Creates a new struct for the given log `type`.
"""
def new(type, data_params),
do: dispatch(type, :new, data_params)

@spec parse(type, map) ::
{:ok, struct}
| :error
@doc """
Attempts to parse the potentially unsafe input into a valid LogData.
"""
def parse(type, unsafe_data_params),
do: dispatch(type, :parse, unsafe_data_params)

@spec dispatch(type, atom, term) ::
term
defp dispatch(type, method, param) when not is_list(param),
do: dispatch(type, method, [param])
defp dispatch(type, method, params) do
type
|> get_type_module()
|> apply(:new, [data_params])
|> apply(method, params)
end

end
end

@doc """
Top-level macro used to define a LogType and its underlying LogData.
"""
defmacro log(name, enum_id, do: block) do
module_name =
__CALLER__.module
Expand All @@ -67,6 +90,9 @@ defmodule Helix.Log.Model.LogType.Macros do
end
end

@doc """
Converts the module into a LogData struct.
"""
defmacro data_struct(keys) do
quote do

Expand All @@ -76,17 +102,48 @@ defmodule Helix.Log.Model.LogType.Macros do
end
end

defmacro new(args, do: block) do
@doc """
Creates a new LogData from the given `data` map.
"""
defmacro new(data, do: block) do
quote do

@doc false
def new(unquote(args)) do
def new(unquote(data)) do
unquote(block)
end

end
end

@doc """
Attempts to parse the given unsafe input into a valid LogData.
"""
defmacro parse(data, do: block) do
quote do

@spec parse(term) ::
{:ok, data :: struct}
| :error
@doc false
def parse(map = unquote(data)) when is_map(map) do
try do
{:ok, unquote(block)}
rescue
RuntimeError ->
:error

KeyError ->
:error
end
end

def parse(not_map) when not is_map(not_map),
do: :error

end
end

@doc """
Generates the boilerplate for a n-field log type.
Expand Down Expand Up @@ -116,22 +173,6 @@ defmodule Helix.Log.Model.LogType.Macros do
defmacro gen3(p1, p2, p3),
do: do_gen3(p1, p2, p3)

defmacro parse(args, do: block) do
quote do

@doc false
def parse(unquote(args)) do
try do
{:ok, unquote(block)}
rescue
RuntimeError ->
:error
end
end

end
end

def validate(field_type, field_value) when is_atom(field_type) do
fun = Utils.concat_atom(:validate_, field_type)

Expand Down Expand Up @@ -222,9 +263,9 @@ defmodule Helix.Log.Model.LogType.Macros do
parse(unsafe) do
%__MODULE__{
unquote(f1) =>
validate(unquote(v_f1), Map.get(unsafe, unquote(str_f1))),
validate(unquote(v_f1), Map.fetch!(unsafe, unquote(str_f1))),
unquote(f2) =>
validate(unquote(v_f2), Map.get(unsafe, unquote(str_f2)))
validate(unquote(v_f2), Map.fetch!(unsafe, unquote(str_f2)))
}
end

Expand All @@ -249,11 +290,11 @@ defmodule Helix.Log.Model.LogType.Macros do
parse(unsafe) do
%__MODULE__{
unquote(f1) =>
validate(unquote(v_f1), Map.get(unsafe, unquote(str_f1))),
validate(unquote(v_f1), Map.fetch!(unsafe, unquote(str_f1))),
unquote(f2) =>
validate(unquote(v_f2), Map.get(unsafe, unquote(str_f2))),
validate(unquote(v_f2), Map.fetch!(unsafe, unquote(str_f2))),
unquote(f3) =>
validate(unquote(v_f3), Map.get(unsafe, unquote(str_f3)))
validate(unquote(v_f3), Map.fetch!(unsafe, unquote(str_f3)))
}
end

Expand Down
Loading

0 comments on commit 2ddbcef

Please sign in to comment.