postland/lib/postland_web/live/followers_live.ex
2024-12-26 21:26:10 -06:00

113 lines
2.8 KiB
Elixir

defmodule PostlandWeb.FollowersLive do
use PostlandWeb, :live_view
alias Postland.Actors
alias Postland.Follows
def render(assigns) do
~H"""
<div class="py-4">
<h3 class="text-base font-semibold">Followers</h3>
<p :if={@count == 0} class="text-gray-400">
No one follows you.
</p>
<div
:for={{dom_id, %{confirmed: confirmed, account: account}} <- @streams.accounts}
class="mt-2"
>
<.profile_card
id={dom_id}
dom_id={dom_id}
account={account}
status={if confirmed, do: :follower_confirmed, else: :follower_pending}
/>
</div>
</div>
"""
end
def mount(_params, _session, socket) do
followers =
Follows.all_followers()
|> Enum.map(fn follow ->
%{
id: follow.follower,
confirmed: !!follow.confirmed_at,
account: Actors.actor(follow.follower)
}
end)
|> Enum.reject(&is_nil(&1.account))
account_count = Enum.count(followers)
{:ok, socket |> stream(:accounts, followers) |> assign(count: account_count)}
end
def handle_event("confirm", %{"id" => id}, socket) do
request = Follows.get(id, Postland.my_actor_id())
socket =
case Follows.record_and_send_acceptance(request) do
{:ok, _} ->
request = Follows.get(id, Postland.my_actor_id())
acct = %{
id: request.follower,
confirmed: !!request.confirmed_at,
account: Actors.actor(request.follower)
}
socket
|> stream_insert(:accounts, acct)
_ ->
put_flash(socket, :error, "An unexpected error occurred.")
end
{:noreply, socket}
end
def handle_event("reject", %{"id" => id}, socket) do
request = Follows.get(id, Postland.my_actor_id())
socket =
case Follows.record_and_reject(request) do
{:ok, _} ->
acct = %{
id: request.follower,
confirmed: true,
account: Actors.actor(request.follower)
}
socket
|> stream_delete(:accounts, acct)
|> assign(count: socket.assigns.count - 1)
_ ->
put_flash(socket, :error, "An unexpected error occurred.")
end
{:noreply, socket}
end
def handle_event("ignore", %{"id" => id}, socket) do
request = Follows.get(id, Postland.my_actor_id())
case Postland.Repo.delete(request) do
{:ok, _} ->
acct = %{
id: request.follower,
confirmed: true,
account: Actors.actor(request.follower)
}
{:noreply,
socket
|> stream_delete(:accounts, acct)
|> assign(count: socket.assigns.count - 1)}
_ ->
{:noreply, put_flash(socket, :error, "An unexpected error occurred.")}
end
end
end