Skip to content

Commit

Permalink
Super ultra update to Elixir 1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed Oct 10, 2017
1 parent 99acce3 commit b1adfbf
Show file tree
Hide file tree
Showing 43 changed files with 212 additions and 224 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: elixir

otp_release:
- 18.3
- 19.0
- 20.0
elixir:
- 1.2.6
- 1.3.0
- 1.5.1
sudo: false
env:
- MIX_ENV=test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ How do I speak 'poxa'?

Poxa is a standalone elixir server implementation of the Pusher protocol.

You need [Elixir](http://elixir-lang.org) 1.2.6 at least and Erlang 18.0
You need [Elixir](http://elixir-lang.org) 1.5 at least and Erlang 20.0

Clone this repository

Expand Down
2 changes: 1 addition & 1 deletion lib/poxa.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule Poxa do
{ '/apps/:app_id/channels/:channel_name/users', Poxa.UsersHandler, [] },
{ '/app/:app_key', Poxa.WebsocketHandler, [] } ] }
])
case load_config do
case load_config() do
{:ok, config} ->
Logger.info "Starting Poxa using app_key: #{config.app_key}, app_id: #{config.app_id}, app_secret: #{config.app_secret} on port #{config.port}"
{:ok, _} = :cowboy.start_http(:poxa, 100,
Expand Down
8 changes: 4 additions & 4 deletions lib/poxa/adapter/gproc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ defmodule Poxa.Adapter.GProc do
def channels(pid \\ :_) do
Ex2ms.fun do
{{:p, :l, {:pusher, channel}}, ^pid, _} -> channel
end |> select |> Enum.uniq
end |> select |> Enum.uniq_by(&(&1))
end

def unique_subscriptions(channel) do
for {_pid, {user_id, user_info}} <- lookup_values({:p, :l, {:pusher, channel}}) do
for {_pid, {user_id, user_info}} <- lookup_values({:p, :l, {:pusher, channel}}), into: %{} do
{user_id, user_info}
end |> Enum.uniq(fn {user_id, _} -> user_id end)
end
end

def fetch(key) do
get_value({:p, :l, {:pusher, key}})
end

def clean_up, do: goodbye
def clean_up, do: goodbye()
end
4 changes: 2 additions & 2 deletions lib/poxa/channels_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ defmodule Poxa.ChannelsHandler do
defp show(channel, attributes, req, state) do
occupied = Channel.occupied?(channel)
attribute_list = mount_attribute_list(attributes, channel)
{JSX.encode!([occupied: occupied] ++ attribute_list), req, state}
{Poison.encode!([occupied: occupied] ++ attribute_list), req, state}
end

defp mount_attribute_list(attributes, channel) do
Expand All @@ -101,7 +101,7 @@ defmodule Poxa.ChannelsHandler do

defp index(filter, attributes, req, state) do
channels = channels(filter, attributes)
{JSX.encode!(channels: channels), req, state}
{Poison.encode!(channels: channels), req, state}
end

defp channels(filter, attributes) do
Expand Down
4 changes: 2 additions & 2 deletions lib/poxa/console/console.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Poxa.Console do
import JSX, only: [encode!: 1]
import Poison, only: [encode!: 1]
use GenEvent

@doc false
Expand Down Expand Up @@ -62,7 +62,7 @@ defmodule Poxa.Console do
end

defp message(type, socket_id, details) do
%{ type: type, socket: socket_id, details: details, time: time }
%{ type: type, socket: socket_id, details: details, time: time() }
end

defp time do
Expand Down
4 changes: 2 additions & 2 deletions lib/poxa/console/ws_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule Poxa.Console.WSHandler do
{qs_vals, req} = :cowboy_req.qs_vals(req)

if Authentication.check(method, path, "", qs_vals) do
:ok = Poxa.Event.add_handler({Poxa.Console, self}, self)
:ok = Poxa.Event.add_handler({Poxa.Console, self()}, self())
{:ok, req, nil}
else
Logger.error "Failed to authenticate on Console Websocket"
Expand All @@ -31,7 +31,7 @@ defmodule Poxa.Console.WSHandler do

@doc false
def websocket_terminate(_reason, _req, _state) do
Poxa.Event.remove_handler({Poxa.Console, self}, self)
Poxa.Event.remove_handler({Poxa.Console, self()}, self())
:ok
end
end
8 changes: 4 additions & 4 deletions lib/poxa/event_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ defmodule Poxa.EventHandler do

def allowed_methods(req, state), do: {["POST"], req, state}

@invalid_event_json JSX.encode!(%{error: "Event must have channel(s), name, and data"})
@invalid_event_json Poison.encode!(%{error: "Event must have channel(s), name, and data"})

@doc """
If the body is not JSON or not a valid PusherEvent this function will
guarantee that 400 will be returned
"""
def malformed_request(req, state) do
{:ok, body, req} = :cowboy_req.body(req)
case JSX.decode(body) do
case Poison.decode(body) do
{:ok, data} ->
case PusherEvent.build(data) do
{:ok, event} -> {false, req, %{body: body, event: event}}
Expand All @@ -38,7 +38,7 @@ defmodule Poxa.EventHandler do
end
end

@authentication_error_json JSX.encode!(%{error: "Authentication error"})
@authentication_error_json Poison.encode!(%{error: "Authentication error"})

@doc """
More info http://pusher.com/docs/rest_api#authentication
Expand All @@ -56,7 +56,7 @@ defmodule Poxa.EventHandler do
{authorized, req, state}
end

@invalid_data_size_json JSX.encode!(%{error: "Data key must be smaller than 10KB"})
@invalid_data_size_json Poison.encode!(%{error: "Data key must be smaller than 10KB"})

@doc """
The event data can't be greater than 10KB
Expand Down
22 changes: 11 additions & 11 deletions lib/poxa/presence_subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ defmodule Poxa.PresenceSubscription do
More info at: http://pusher.com/docs/client_api_guide/client_presence_channels
"""
@type user_id :: integer | binary
@type user_info :: :jsx.json_term
@type user_info :: term
defstruct [:channel, :channel_data]
@type t :: %__MODULE__{channel: binary, channel_data: [{user_id, user_info}]}
@type t :: %__MODULE__{channel: binary, channel_data: %{user_id => user_info}}

alias Poxa.PusherEvent
alias Poxa.Event
Expand All @@ -22,18 +22,18 @@ defmodule Poxa.PresenceSubscription do
@doc """
Returns a PresenceSubscription struct
"""
@spec subscribe!(binary, :jsx.json_term) :: __MODULE__.t
@spec subscribe!(binary, term) :: __MODULE__.t
def subscribe!(channel, channel_data) do
decoded_channel_data = JSX.decode!(channel_data)
if member?(channel, self) do
Logger.info "Already subscribed #{inspect self} on channel #{channel}"
decoded_channel_data = Poison.decode!(channel_data)
if member?(channel, self()) do
Logger.info "Already subscribed #{inspect self()} on channel #{channel}"
else
Logger.info "Registering #{inspect self} to channel #{channel}"
Logger.info "Registering #{inspect self()} to channel #{channel}"
{user_id, user_info} = extract_userid_and_userinfo(decoded_channel_data)
unless Poxa.Channel.member?(channel, user_id) do
Event.notify(:member_added, %{channel: channel, user_id: user_id})
message = PusherEvent.presence_member_added(channel, user_id, user_info)
Poxa.registry.send!(message, channel, self)
Poxa.registry.send!(message, channel, self())
end
Poxa.registry.register!(channel, {user_id, user_info})
end
Expand All @@ -48,7 +48,7 @@ defmodule Poxa.PresenceSubscription do
end

defp sanitize_user_id(user_id) when is_binary(user_id), do: user_id
defp sanitize_user_id(user_id), do: JSX.encode!(user_id)
defp sanitize_user_id(user_id), do: Poison.encode!(user_id)

@doc """
Unsubscribe from a presence channel, possibly triggering `presence_member_removed`.
Expand All @@ -74,7 +74,7 @@ defmodule Poxa.PresenceSubscription do
"""
@spec check_and_remove :: non_neg_integer
def check_and_remove do
for [channel, user_id] <- Poxa.registry.subscriptions(self),
for [channel, user_id] <- Poxa.registry.subscriptions(self()),
presence?(channel), only_one_connection_on_user_id?(channel, user_id) do
presence_member_removed(channel, user_id)
end |> Enum.count
Expand All @@ -83,7 +83,7 @@ defmodule Poxa.PresenceSubscription do
defp presence_member_removed(channel, user_id) do
Event.notify(:member_removed, %{channel: channel, user_id: user_id})
message = PusherEvent.presence_member_removed(channel, user_id)
Poxa.registry.send!(message, channel, self)
Poxa.registry.send!(message, channel, self())
end

defp only_one_connection_on_user_id?(channel, user_id) do
Expand Down
6 changes: 3 additions & 3 deletions lib/poxa/pusher_event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Poxa.PusherEvent do
"""

alias Poxa.PresenceSubscription
import JSX, only: [encode!: 1]
import Poison, only: [encode!: 1]

@doc """
Return a JSON for an established connection using the `socket_id` parameter to
Expand Down Expand Up @@ -63,7 +63,7 @@ defmodule Poxa.PusherEvent do
end

def subscription_succeeded(%PresenceSubscription{channel: channel, channel_data: channel_data}) do
{ids, _Hash} = :lists.unzip(channel_data)
ids = Map.keys(channel_data)
count = Enum.count(ids)
data = %{presence: %{ids: ids, hash: channel_data, count: count}} |> encode!
%{event: "pusher_internal:subscription_succeeded",
Expand Down Expand Up @@ -192,7 +192,7 @@ defmodule Poxa.PusherEvent do

defp publish_event_to_channel(event, channel) do
message = build_message(event, channel) |> encode!
Poxa.registry.send!(message, channel, self, event.socket_id)
Poxa.registry.send!(message, channel, self(), event.socket_id)
end

defp build_message(event, channel) do
Expand Down
2 changes: 1 addition & 1 deletion lib/poxa/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ defmodule Poxa.Registry do
@doc """
Returns the unique subscriptions of the given channel.
"""
@callback unique_subscriptions(binary) :: list(tuple())
@callback unique_subscriptions(binary) :: Map.t

@doc """
Returns the value assigned with the given property.
Expand Down
2 changes: 1 addition & 1 deletion lib/poxa/socket_id.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Poxa.SocketId do
"""
@spec generate! :: binary
def generate! do
<<part1::32, part2::32>> = :crypto.rand_bytes(8)
<<part1::32, part2::32>> = :crypto.strong_rand_bytes(8)
"#{part1}.#{part2}"
end

Expand Down
12 changes: 6 additions & 6 deletions lib/poxa/subscription.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule Poxa.Subscription do
Returns {:ok, channel} to public and private channels and
a PresenceSubscription to a presence channel
"""
@spec subscribe!(:jsx.json_term, binary) :: {:ok, binary}
@spec subscribe!(term, binary) :: {:ok, binary}
| PresenceSubscription.t
| {:error, binary}
def subscribe!(data, socket_id) do
Expand Down Expand Up @@ -60,10 +60,10 @@ defmodule Poxa.Subscription do

defp subscribe_channel(channel) do
Logger.info "Subscribing to channel #{channel}"
if Channel.member?(channel, self) do
Logger.info "Already subscribed #{inspect self} on channel #{channel}"
if Channel.member?(channel, self()) do
Logger.info "Already subscribed #{inspect self()} on channel #{channel}"
else
Logger.info "Registering #{inspect self} to channel #{channel}"
Logger.info "Registering #{inspect self()} to channel #{channel}"
Poxa.registry.register!(channel)
end
{:ok, channel}
Expand All @@ -72,10 +72,10 @@ defmodule Poxa.Subscription do
@doc """
Unsubscribe from a channel always returning :ok
"""
@spec unsubscribe!(:jsx.json_term) :: {:ok, binary}
@spec unsubscribe!(term) :: {:ok, binary}
def unsubscribe!(data) do
channel = data["channel"]
if Channel.member?(channel, self) do
if Channel.member?(channel, self()) do
if Channel.presence?(channel) do
PresenceSubscription.unsubscribe!(channel);
end
Expand Down
2 changes: 1 addition & 1 deletion lib/poxa/users_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ defmodule Poxa.UsersHandler do
"""
def get_json(req, channel) do
response = PresenceChannel.users(channel) |> Enum.map(fn(id) -> [id: id] end)
{JSX.encode!(users: response), req, nil}
{Poison.encode!(users: response), req, nil}
end
end

2 changes: 1 addition & 1 deletion lib/poxa/web_hook/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ defmodule Poxa.WebHook.Dispatcher do

defp send_web_hook!(events) do
Logger.debug "Sending webhook request."
body = JSX.encode! %{time_ms: time_ms, events: events}
body = Poison.encode! %{time_ms: time_ms(), events: events}
{:ok, url} = Application.fetch_env(:poxa, :web_hook)
case HTTPoison.post(url, body, pusher_headers(body)) do
{:ok, _} -> :ok
Expand Down
4 changes: 2 additions & 2 deletions lib/poxa/web_hook/event_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Poxa.WebHook.EventTable do
def insert(events, delay) do
Enum.filter events, fn(event) ->
if delete_corresponding(event) == 0 do
:ets.insert(@table_name, {time_ms + delay, event})
:ets.insert(@table_name, {time_ms() + delay, event})
end
end
end
Expand All @@ -45,7 +45,7 @@ defmodule Poxa.WebHook.EventTable do
This function returns all events in the ETS table that are ready to be sent
for a given `timestamp`.
"""
def ready(timestamp \\ time_ms + 1), do: {timestamp, filter_events [{:<, :"$1", timestamp}]}
def ready(timestamp \\ time_ms() + 1), do: {timestamp, filter_events [{:<, :"$1", timestamp}]}

defp filter_events(filter) do
select(@table_name, [{{:"$1", :"$2"}, filter, [:"$2"]}])
Expand Down
12 changes: 6 additions & 6 deletions lib/poxa/websocket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ defmodule Poxa.WebsocketHandler do
case :application.get_env(:poxa, :app_key) do
{:ok, ^app_key} ->
if supported_protocol?(protocol) do
send self, :start
send self(), :start
{:ok, req, nil}
else
Logger.error "Protocol #{protocol} not supported"
send self, :start_error
send self(), :start_error
{:ok, req, {4007, "Unsupported protocol version"}}
end
{:ok, expected_app_key} ->
Logger.error "Invalid app_key, expected #{expected_app_key}, found #{app_key}"
send self, :start_error
send self(), :start_error
{:ok, req, {4001, "Application does not exist"}}
end
end
Expand All @@ -70,7 +70,7 @@ defmodule Poxa.WebsocketHandler do
More info: http://pusher.com/docs/pusher_protocol
"""
def websocket_handle({:text, json}, req, state) do
JSX.decode!(json) |> handle_pusher_event(req, state)
Poison.decode!(json) |> handle_pusher_event(req, state)
end
def websocket_handle({:ping, _}, req, state), do: { :ok, req, state }

Expand Down Expand Up @@ -104,7 +104,7 @@ defmodule Poxa.WebsocketHandler do
defp handle_pusher_event("client-" <> _event_name, decoded_json, req, %State{socket_id: socket_id} = state) do
{:ok, event} = PusherEvent.build_client_event(decoded_json, socket_id)
channel = List.first(event.channels)
if Channel.private_or_presence?(channel) and Channel.member?(channel, self) do
if Channel.private_or_presence?(channel) and Channel.member?(channel, self()) do
PusherEvent.publish(event)
Event.notify(:client_event_message, %{socket_id: socket_id, channels: event.channels, name: event.name, data: event.data})
end
Expand Down Expand Up @@ -160,7 +160,7 @@ defmodule Poxa.WebsocketHandler do
def websocket_terminate(_reason, _req, nil), do: :ok
def websocket_terminate(_reason, _req, %State{socket_id: socket_id, time: time}) do
duration = Time.stamp - time
channels = Channel.all(self)
channels = Channel.all(self())
PresenceSubscription.check_and_remove
Poxa.registry.clean_up
Event.notify(:disconnected, %{socket_id: socket_id, channels: channels, duration: duration})
Expand Down
Loading

0 comments on commit b1adfbf

Please sign in to comment.