Skip to content

Instantly share code, notes, and snippets.

@mazz
Created March 21, 2024 03:35
Show Gist options
  • Save mazz/d9a9d34b36f5552b300f66f9e4a24814 to your computer and use it in GitHub Desktop.
Save mazz/d9a9d34b36f5552b300f66f9e4a24814 to your computer and use it in GitHub Desktop.
user channel
defmodule MarkablyWeb.UserChannel do
use Phoenix.Channel, hibernate_after: :infinity
alias Phoenix.Socket.Broadcast
alias Markably.Pipeline.Timing
@impl true
def join("user:" <> user_id, _payload, socket) do
dbg("join MarkablyWeb.UserChannel")
dbg(user_id)
# broadcast_test(socket)
# MarkablyWeb.Endpoint.subscribe("user:" <> user_id)
# MarkablyWeb.Endpoint.subscribe("called-task-sync-endpoint:")
{:ok, assign(socket, :user_id, user_id)}
end
@impl true
def handle_in("user:" <> user_id, payload, socket) do
dbg("handle_in MarkablyWeb.UserChannel")
dbg(payload)
dbg(user_id)
{:reply, {:ok, payload}, socket}
end
def handle_in("ding", payload, socket) do
dbg(payload)
{:reply, {:ok, payload}, socket}
end
intercept([
"task:insert",
"task:update",
"task:delete",
"task:delete:error",
"task:undelete",
"task:undelete:error",
"course_code:insert",
"course_code:update",
"user:membership:store",
"user:client_preferences:store",
"user:hard_delete_user:success",
"user:hard_delete_user:error",
"seat:insert",
"seat:update",
"notifications_updated"
])
@impl true
def handle_out("task:insert", payload, socket) do
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
dbg(payload)
Markably.Statix.histogram(
"pipeline_rest.store_mby_tasks_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("task:update", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.update_mby_tasks_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("task:delete", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.delete_mby_tasks_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("task:delete:error", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.delete_error_mby_tasks_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("task:undelete", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.undelete_mby_tasks_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("task:undelete:error", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.undelete_error_mby_tasks_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("course_code:insert", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.store_mby_course_codes_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("course_code:update", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.update_mby_course_codes_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("seat:insert", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
dbg(payload)
Markably.Statix.histogram(
"pipeline_rest.store_mby_seats_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("seat:update", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
Markably.Statix.histogram(
"pipeline_rest.update_mby_seats_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("user:membership:store", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
dbg(Timing.unix_ms_now())
dbg(payload.enqueued_at)
Markably.Statix.histogram(
"pipeline_rest.store_mby_membership_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("user:client_preferences:store", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
dbg(Timing.unix_ms_now())
dbg(payload.enqueued_at)
Markably.Statix.histogram(
"pipeline_rest.store_mby_client_preferences_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("user:hard_delete_user:success", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
dbg(Timing.unix_ms_now())
dbg(payload.enqueued_at)
Markably.Statix.histogram(
"pipeline_rest.store_mby_client_preferences_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
@impl true
def handle_out("user:hard_delete_user:error", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
dbg(Timing.unix_ms_now())
dbg(payload.enqueued_at)
Markably.Statix.histogram(
"pipeline_rest.store_mby_client_preferences_aggregator.push_delivered",
Timing.unix_ms_now() - payload.enqueued_at
)
{:noreply, socket}
end
# MarkablyWeb.Endpoint.broadcast("user:#{socket.assigns.current_user.id}", "notifications_updated", %{
# mby_event: "notifications_updated",
# mby_notification: user_notification,
# mby_notification_type: user_notification.type,
# mby_notification_id: user_notification.id,
# enqueued_at: nil
# })
@impl true
def handle_out("notifications_updated", payload, socket) do
dbg(payload)
push(
socket,
"user:" <> socket.assigns.user_id,
payload
)
{:noreply, socket}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment