grover
This commit is contained in:
@ -8,6 +8,10 @@ defmodule SomethingErlang.Application do
|
||||
@impl true
|
||||
def start(_type, _args) do
|
||||
children = [
|
||||
{Registry, [name: SomethingErlang.Registry.Grovers,
|
||||
keys: :unique]},
|
||||
{DynamicSupervisor, [name: SomethingErlang.Supervisor.Grovers,
|
||||
strategy: :one_for_one]},
|
||||
# Start the Ecto repository
|
||||
SomethingErlang.Repo,
|
||||
# Start the Telemetry supervisor
|
||||
|
57
lib/something_erlang/grover.ex
Normal file
57
lib/something_erlang/grover.ex
Normal file
@ -0,0 +1,57 @@
|
||||
defmodule SomethingErlang.Grover do
|
||||
use GenServer
|
||||
|
||||
require Logger
|
||||
|
||||
def mount(lv_pid, user, thread_id) do
|
||||
DynamicSupervisor.start_child(
|
||||
SomethingErlang.Supervisor.Grovers,
|
||||
{__MODULE__, [lv_pid, user, thread_id]}
|
||||
)
|
||||
end
|
||||
|
||||
def get_thread!(lv_pid, thread_id, page_number) do
|
||||
GenServer.call(via(lv_pid), {:show_thread, thread_id, page_number})
|
||||
end
|
||||
|
||||
def start_link([lv_pid, user, thread_id]) do
|
||||
GenServer.start_link(
|
||||
__MODULE__,
|
||||
[lv_pid, user, thread_id],
|
||||
name: via(lv_pid))
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init([pid, user, thread_id]) do
|
||||
%{bbuserid: userid, bbpassword: userhash} = user
|
||||
initial_state = %{
|
||||
lv_pid: pid,
|
||||
thread_id: thread_id,
|
||||
page_number: 1,
|
||||
user: %{id: userid, hash: userhash}
|
||||
}
|
||||
|
||||
Logger.debug "init #{userid} #{thread_id}"
|
||||
Process.monitor(pid)
|
||||
{:ok, initial_state}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call({:show_thread, thread_id, page_number}, _from, state) do
|
||||
thread = AwfulApi.parsed_thread(thread_id, page_number, state.user)
|
||||
{:reply, thread, %{state | page_number: page_number}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_info({:DOWN, _ref, :process, _object, reason}, state) do
|
||||
Logger.debug "received :DOWN from: #{inspect(state.lv_pid)} reason: #{inspect(reason)}"
|
||||
case reason do
|
||||
{:shutdown, _} -> {:stop, :normal, state}
|
||||
_ -> {:noreply, state}
|
||||
end
|
||||
|> IO.inspect()
|
||||
end
|
||||
|
||||
defp via(lv_pid),
|
||||
do: {:via, Registry, {SomethingErlang.Registry.Grovers, lv_pid}}
|
||||
end
|
@ -3,21 +3,49 @@ defmodule SomethingErlangWeb.ThreadLive.Show do
|
||||
on_mount SomethingErlangWeb.UserLiveAuth
|
||||
|
||||
alias SomethingErlang.Forums
|
||||
alias SomethingErlang.Grover
|
||||
|
||||
require Logger
|
||||
|
||||
@impl true
|
||||
def mount(_params, _session, socket) do
|
||||
socket |> IO.inspect
|
||||
def mount(%{"id" => id} = _params, _session, socket) do
|
||||
Grover.mount(self(), socket.assigns.current_user, id)
|
||||
{:ok, socket}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
def handle_params(%{"id" => id, "page" => page}, _, socket) do
|
||||
thread = Grover.get_thread!(self(), id, page |> String.to_integer())
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||
|> assign(:thread, Forums.get_thread!(id))}
|
||||
|> assign(:thread, thread)}
|
||||
end
|
||||
|
||||
defp page_title(:show), do: "Show Thread"
|
||||
defp page_title(:edit), do: "Edit Thread"
|
||||
@impl true
|
||||
def handle_params(%{"id" => id}, _, socket) do
|
||||
{:noreply, push_redirect(socket,
|
||||
to: Routes.thread_show_path(socket, :show, id, page: 1))}
|
||||
end
|
||||
|
||||
def post(assigns) do
|
||||
~H"""
|
||||
<div class="post">
|
||||
<.user info={@author} />
|
||||
<article class="grow-1 w-full">
|
||||
<%= raw @article %>
|
||||
</article>
|
||||
</div>
|
||||
"""
|
||||
end
|
||||
|
||||
def user(assigns) do
|
||||
~H"""
|
||||
<aside class="userinfo shrink-0 w-[13em]">
|
||||
<h3><%= @info.name %></h3>
|
||||
<div class="title">
|
||||
<%= raw @info.title %>
|
||||
</div>
|
||||
</aside>
|
||||
"""
|
||||
end
|
||||
end
|
||||
|
@ -1,5 +1,3 @@
|
||||
<h1>Show Thread</h1>
|
||||
|
||||
<%= if @live_action in [:edit] do %>
|
||||
<.modal return_to={Routes.thread_show_path(@socket, :show, @thread)}>
|
||||
<.live_component
|
||||
@ -13,19 +11,18 @@
|
||||
</.modal>
|
||||
<% end %>
|
||||
|
||||
<ul>
|
||||
<h2>
|
||||
<%= @thread.title %>
|
||||
</h2>
|
||||
|
||||
<li>
|
||||
<strong>Title:</strong>
|
||||
<%= @thread.title %>
|
||||
</li>
|
||||
<div class="thread my-8">
|
||||
<%= for post <- @thread.posts do %>
|
||||
<.post author={post.userinfo} article={post.postbody} />
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<li>
|
||||
<strong>Thread:</strong>
|
||||
<%= @thread.thread_id %>
|
||||
</li>
|
||||
|
||||
</ul>
|
||||
|
||||
<span><%= live_patch "Edit", to: Routes.thread_show_path(@socket, :edit, @thread), class: "button" %></span> |
|
||||
<span><%= live_redirect "Back", to: Routes.thread_index_path(@socket, :index) %></span>
|
||||
<span><%= @thread.page %></span>
|
||||
<span><%= live_redirect "Next Page", class: "link",
|
||||
to: Routes.thread_show_path(@socket, :show, @thread.id, page: @thread.page+1) %></span>
|
||||
|
Reference in New Issue
Block a user