defmodule StatusphereWeb.PageController do alias Statusphere.Repo alias Statusphere.Status require Logger import Ecto.Query, only: [from: 2] use StatusphereWeb, :controller def home(conn, _params) do query = from u in Status, order_by: [desc: u.indexed_at], limit: 10 statuses = Repo.all(query) status_identities = statuses |> Enum.map(fn %{author_did: did} -> case Atex.IdentityResolver.resolve(did) do {:ok, identity} -> {identity.did, identity.handle} {:error, _} -> {did, did} end end) |> Enum.into(%{}) with {:ok, client} <- Atex.XRPC.OAuthClient.from_conn(conn), {:ok, %{body: %{value: profile}}, client} <- Atex.XRPC.get(client, %Com.Atproto.Repo.GetRecord{ params: %{ repo: client.did, collection: "app.bsky.actor.profile", rkey: "self" } }) do conn |> Atex.XRPC.OAuthClient.update_plug(client) |> render(:home, profile: profile, did: client.did, statuses: statuses, status_identities: status_identities ) else {:error, err, client} -> Logger.error("Failed to fetch Bluesky profile for #{client.did}: #{inspect(err)}") conn |> Atex.XRPC.OAuthClient.update_plug(client) |> render(:home, profile: %{}, did: client.did, statuses: statuses, status_identities: status_identities ) _err -> render(conn, :home, profile: nil, did: nil, statuses: statuses, status_identities: status_identities ) end end def login(conn, _params) do render(conn, :login) end def logout(conn, _params) do conn |> delete_session(:atex_oauth) |> redirect(to: "/") end end