postland/lib/postland_web/live/following_live.ex

82 lines
2 KiB
Elixir
Raw Normal View History

2024-11-03 16:17:42 +00:00
defmodule PostlandWeb.FollowingLive 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">Following</h3>
<p :if={@count == 0} class="text-gray-400">
You aren't following anyone.
</p>
<div :for={{dom_id, %{confirmed: confirmed, account: acct}} <- @streams.accounts} class="mt-2">
<.profile_card
id={dom_id}
account={acct}
dom_id={dom_id}
status={if confirmed, do: :following_confirmed, else: :following_pending}
/>
</div>
</div>
"""
end
def mount(_params, _session, socket) do
follows =
Follows.all_following()
|> Enum.map(fn follow ->
%{
id: follow.followee,
confirmed: !!follow.confirmed_at,
account: Actors.actor(follow.followee)
}
end)
2024-11-27 00:04:08 +00:00
|> Enum.reject(&is_nil(&1.account))
2024-11-03 16:17:42 +00:00
account_count = Enum.count(follows)
socket =
socket
|> stream(:accounts, follows)
|> assign(:count, account_count)
{:ok, socket}
end
def handle_event("withdraw", %{"id" => id, "dom-id" => dom_id}, socket) do
request = Follows.get(Postland.my_actor_id(), id)
socket =
case Follows.record_and_send_withdrawl(request) do
{:ok, _} ->
socket
|> stream_delete_by_dom_id(:accounts, dom_id)
|> assign(:count, socket.assigns.count - 1)
_ ->
put_flash(socket, :error, "An unexpected error occurred.")
end
{:noreply, socket}
end
2024-11-27 00:04:08 +00:00
def handle_event("unfollow", %{"id" => id, "dom-id" => dom_id}, socket) do
request = Follows.get(Postland.my_actor_id(), id)
socket =
case Follows.record_and_unfollow(request) do
{:ok, _} ->
socket
|> stream_delete_by_dom_id(:accounts, dom_id)
|> assign(:count, socket.assigns.count - 1)
_ ->
put_flash(socket, :error, "An unexpected error occurred.")
end
{:noreply, socket}
end
2024-11-03 16:17:42 +00:00
end