Aggiedit/lib/aggiedit_web/channels/post_channel.ex

38 lines
1.1 KiB
Elixir
Raw Normal View History

2022-04-14 15:56:10 -04:00
defmodule AggieditWeb.PostChannel do
use AggieditWeb, :channel
alias Aggiedit.Roles
alias Aggiedit.Repo
2022-04-14 15:56:10 -04:00
alias Aggiedit.Rooms
@impl true
def join("post:" <> post_id, _payload, socket) do
post = Rooms.get_post!(post_id)
if Roles.guard?(socket.assigns.current_user, :show, post) do
send(self(), :after_join)
{:ok, assign(socket, %{:post => post})}
2022-04-14 15:56:10 -04:00
else
{:error, "You do not have permission to view this post."}
end
end
@impl true
def handle_info(:after_join, socket) do
comments = socket.assigns.post
|> Repo.preload(comments: [:user])
|> Map.get(:comments)
|> Enum.map(fn c -> Aggiedit.Post.Comment.serialize(c) end)
push(socket, "initial-comments", %{:comments => comments})
broadcast!(socket, "join", %{user: socket.assigns.current_user.username})
{:noreply, socket}
end
@impl true
def handle_in("send", %{"body" => comment}, socket) do
{:ok, comment} = Rooms.comment_post(socket.assigns.post, socket.assigns.current_user, comment)
broadcast!(socket, "shout", Aggiedit.Post.Comment.serialize(comment))
2022-04-14 15:56:10 -04:00
{:reply, :ok, socket}
end
end