defmodule PostlandWeb.OtherProfileLive do
use PostlandWeb, :live_view
alias Postland.Follows
def render(assigns) do
~H"""
<%= Map.get(@profile, "name") %> (<%= @acct %>)
<%= case @follow do %>
<% nil -> %>
<.button phx-click="follow">Follow
<% %{confirmed_at: nil} -> %>
<.button disabled>Pending
<% _ -> %>
<.button phx-click="unfollow">Unfollow
<% end %>
<.list>
<:item :for={attachment <- Map.get(@profile, "attachment")} title={attachment["name"]}>
<%= attachment["value"] %>
"""
end
def mount(params, _session, socket) do
{:ok, resource} = ActivityPub.Webfinger.lookup_resource(Map.get(params, "acct"))
%{"href" => json_profile} =
resource
|> Map.get("links")
|> Enum.find(fn %{"rel" => rel} = link ->
rel == "self" && String.contains?(Map.get(link, "type"), "json")
end)
{:ok, profile} =
ActivityPub.get(
json_profile,
Postland.my_actor_id(),
Postland.Accounts.solo_user().private_key
)
follow = Follows.get(Postland.my_actor_id(), json_profile)
if Phoenix.LiveView.connected?(socket) do
Phoenix.PubSub.subscribe(Postland.PubSub, "follows:#{json_profile}")
end
{:ok,
assign(socket,
acct: Map.get(params, "acct"),
actor_id: json_profile,
follow: follow,
profile: profile
)}
end
def handle_event("follow", _unsigned_params, socket) do
actor_id = socket.assigns.actor_id
case Follows.record_and_send_follow_request(actor_id) do
{:ok, _} ->
{:noreply,
socket
|> put_flash(:success, "Follow request sent.")
|> assign(follow: Follows.get(Postland.my_actor_id(), actor_id))}
_other ->
{:noreply,
socket
|> put_flash(:error, "An unexpected error has occurred.")}
end
end
def handle_info({:update, follow}, socket) do
{:noreply, assign(socket, follow: follow)}
end
end