defmodule PostlandWeb.FollowingLive do
use PostlandWeb, :live_view
alias Postland.Actors
alias Postland.Follows
def render(assigns) do
~H"""
Following
You aren't following anyone.
<.profile_card
id={dom_id}
account={acct}
dom_id={dom_id}
status={if confirmed, do: :following_confirmed, else: :following_pending}
/>
"""
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)
|> Enum.reject(&is_nil(&1.account))
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
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
end