summaryrefslogtreecommitdiff
path: root/lib/aggiedit_web/channels
diff options
context:
space:
mode:
authorLogan Hunt <loganhunt@simponic.xyz>2022-04-20 16:03:17 -0600
committerLogan Hunt <loganhunt@simponic.xyz>2022-04-20 16:03:17 -0600
commit763ea5331b1977dd949c776215f2c7719f81ee9a (patch)
treec457c73fbf0f73a6d1975ece6d1573f4dfc6a26a /lib/aggiedit_web/channels
parent3cf9f4a364ac91cca30799c8379a682139425e71 (diff)
downloadaggiedit-763ea5331b1977dd949c776215f2c7719f81ee9a.tar.gz
aggiedit-763ea5331b1977dd949c776215f2c7719f81ee9a.zip
Initial chat box; man phoenix does not have the best docs
Diffstat (limited to 'lib/aggiedit_web/channels')
-rw-r--r--lib/aggiedit_web/channels/post_channel.ex20
1 files changed, 17 insertions, 3 deletions
diff --git a/lib/aggiedit_web/channels/post_channel.ex b/lib/aggiedit_web/channels/post_channel.ex
index 308c6de..ea79d76 100644
--- a/lib/aggiedit_web/channels/post_channel.ex
+++ b/lib/aggiedit_web/channels/post_channel.ex
@@ -2,21 +2,35 @@ defmodule AggieditWeb.PostChannel do
use AggieditWeb, :channel
alias Aggiedit.Roles
+ alias Aggiedit.Repo
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
- {:ok, socket}
+ send(self(), :after_join)
+ {:ok, assign(socket, %{:post => post})}
else
{:error, "You do not have permission to view this post."}
end
end
@impl true
- def handle_in("send", body, socket) do
- broadcast!(socket, "shout", body)
+ 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})
+
+ {:noreply, socket}
+ end
+
+ @impl true
+ def handle_in("send", %{"body" => comment}=body, socket) do
+ {:ok, comment} = Rooms.comment_post(socket.assigns.post, socket.assigns.current_user, comment)
+ broadcast!(socket, "shout", Aggiedit.Post.Comment.serialize(comment))
{:reply, :ok, socket}
end
end \ No newline at end of file