Commit 2762abe7 authored by Peter van der meulen's avatar Peter van der meulen

Change from booleans to integers for more option

parent 0754898d
This diff is collapsed.
......@@ -4,7 +4,7 @@ defmodule SnugglesApp.Accounts.User do
schema "users" do
field :access, :boolean, default: false
field :access, :integer, default: 0
field :auth_token, :string
field :character_id, :integer
field :character_name, :string
......
......@@ -11,9 +11,9 @@ defmodule SnugglesApp.Discord do
alias Nostrum.Api
@doc """
Starts the Discord Messages Post worker.
Starts the Discord Messages Post worker.
Messages coming from Discord will enter the application trough the worker,
and then be pushed off in to queues to be handled by external apps and Discord
and then be pushed off in to queues to be handled by external apps and Discord
post delivery.
This method is supposed to be ran once on application upstart.
......@@ -23,10 +23,9 @@ defmodule SnugglesApp.Discord do
{:ok, #PID<0.346.0>}
"""
def start_post_supervisor do
PostSupervisor.start
PostSupervisor.start()
end
@doc """
Adds or removes the basic role to given user's discord ID
......@@ -37,12 +36,12 @@ defmodule SnugglesApp.Discord do
"""
def manage_alliance_permissions(_, unchanged, unchanged), do: {:ok}
def manage_alliance_permissions(user, _, true) do
def manage_alliance_permissions(user, _, 2) do
add_default_role_to_user(user.discord_id)
update_user_name(user)
end
def manage_alliance_permissions(user, _, false) do
def manage_alliance_permissions(user, _, 1) do
remove_default_role_from_user(String.to_integer(user.discord_id))
update_user_name(user)
end
......
......@@ -18,10 +18,11 @@ defmodule SnugglesApp.EveService do
"""
def get_auth_token(token) do
response = Auth.get_token(token)
response_body = (Poison.decode!(response.body))
response_body = Poison.decode!(response.body)
case response.status_code do
200 -> {:ok, response_body["access_token"]}
_ -> {:error, "API error: #{response_body["error_description"]}"}
_ -> {:error, "API error: #{response_body["error_description"]}"}
end
end
......@@ -37,16 +38,18 @@ defmodule SnugglesApp.EveService do
def get_auth_character_info(auth_token) do
response = Auth.get_character_info(auth_token)
response_body = Poison.decode!(response.body)
case response.status_code do
200 -> {:ok, character_info_object(response_body)}
_ -> {:error, "API error: Problem fetching character info"}
200 -> {:ok, character_info_object(response_body)}
_ -> {:error, "API error: Problem fetching character info"}
end
end
defp character_info_object(response_body) do
%{
character_name: response_body["CharacterName"],
character_id: response_body["CharacterID"]
character_id: response_body["CharacterID"],
access: 0
}
end
......@@ -62,14 +65,20 @@ defmodule SnugglesApp.EveService do
def get_alliance_info(character_id) do
response = Character.get_info(character_id)
response_body = Poison.decode!(response.body)
case response.status_code do
200 -> {:ok, alliance_info_object(response_body)}
_ -> {:error, "API error: Problem fetching alliance info"}
200 -> {:ok, alliance_info_object(response_body)}
_ -> {:error, "API error: Problem fetching alliance info"}
end
end
defp alliance_info_object(response_body) do
access = response_body["alliance_id"] == Application.get_env(:snuggles_app, :alliance_id)
access =
case response_body["alliance_id"] == Application.get_env(:snuggles_app, :alliance_id) do
false -> 1
true -> 2
end
%{
corp_id: response_body["corporation_id"],
access: access
......@@ -88,15 +97,16 @@ defmodule SnugglesApp.EveService do
def get_corp_info(alliance_object) do
response = Corp.get_info(alliance_object.corp_id)
response_body = Poison.decode!(response.body)
case response.status_code do
200 -> {:ok, corp_info_object(alliance_object, response_body)}
_ -> {:error, "API error: Problem fetching corp info"}
200 -> {:ok, corp_info_object(alliance_object, response_body)}
_ -> {:error, "API error: Problem fetching corp info"}
end
end
defp corp_info_object(alliance_object, response_body) do
Map.merge alliance_object, %{
corp_ticker: response_body["ticker"],
}
Map.merge(alliance_object, %{
corp_ticker: response_body["ticker"]
})
end
end
......@@ -14,8 +14,8 @@ defmodule SnugglesAppWeb.UserController do
defp token do
length = 60
:crypto.strong_rand_bytes(length)
|> Base.encode64
:crypto.strong_rand_bytes(length)
|> Base.encode64
|> binary_part(0, length)
end
......@@ -39,7 +39,7 @@ defmodule SnugglesAppWeb.UserController do
defp create_user(conn, code) do
case Accounts.create_user(code) do
{:ok, user} -> render(conn, "thanks.html", auth_token: user.auth_token)
{:error, _} -> (conn |> put_flash(:info, "Something went wrong, lets try again") |> redirect(to: "/auth"))
# {:error, _} -> (conn |> put_flash(:info, "Something went wrong, lets try again") |> redirect(to: "/auth"))
end
end
......@@ -47,7 +47,7 @@ defmodule SnugglesAppWeb.UserController do
case get_req_header(conn, "authorization") do
["Token " <> @queue_token] ->
queue_all_users(conn)
_ ->
_ ->
conn
|> put_status(403)
|> text("Failed")
......@@ -58,7 +58,7 @@ defmodule SnugglesAppWeb.UserController do
case Accounts.queue_all_users do
{:ok} ->
text conn, "Success"
_ ->
_ ->
conn
|> put_status(500)
|> text("Failed")
......
defmodule SnugglesApp.Repo.Migrations.RemoveAccessFromUser do
use Ecto.Migration
def change do
alter table("users") do
remove(:access)
end
end
end
defmodule SnugglesApp.Repo.Migrations.AddAccessToUser do
use Ecto.Migration
def change do
alter table("users") do
add(:access, :integer, default: 0)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment