Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add websocket to spotlights #394

Merged
merged 2 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/safira/interaction/interaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ defmodule Safira.Interaction do
Repo.transaction(multi)
|> case do
{:ok, result} ->
Phoenix.PubSub.broadcast(Safira.PubSub, "spotlight", %{
"spotlight" => %{
"id" => result.update_company.badge_id,
"name" => result.update_company.name,
"badge_id" => result.update_company.badge_id,
"end" => result.upsert_spotlight.end
}
})

result

{:error, _failed_operation, changeset, _changes_so_far} ->
Expand Down
35 changes: 35 additions & 0 deletions lib/safira_web/channels/spotlight_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
defmodule SafiraWeb.SpotlightChannel do
@moduledoc """
Phoenix Channel to handle spotlight creations
"""
use Phoenix.Channel

alias Safira.Accounts
alias Safira.Interaction

def join("spotlight", _params, socket) do
Phoenix.PubSub.subscribe(Safira.PubSub, "spotlight")
send(self(), :after_join)
{:ok, socket}
end

def handle_info(:after_join, socket) do
spotlight = Interaction.get_spotlight()
company = Accounts.get_company_by_badge!(spotlight.badge_id)

data = %{
"id" => company.id,
"name" => company.name,
"badge_id" => company.badge_id,
"end" => spotlight.end
}

push(socket, "spotlight", data)
{:noreply, socket}
end

def handle_info(%{"spotlight" => body}, socket) do
push(socket, "spotlight", body)
{:noreply, socket}
end
end
2 changes: 1 addition & 1 deletion lib/safira_web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule SafiraWeb.UserSocket do
use Phoenix.Socket

## Channels
# channel "room:*", SafiraWeb.RoomChannel
channel "spotlight", SafiraWeb.SpotlightChannel

# Socket params are passed from the client and can
# be used to verify and authenticate a user. After
Expand Down
Loading