From 178c2bf72e0843fe493a0c687f1d19310ef57856 Mon Sep 17 00:00:00 2001 From: miraikumiko Date: Fri, 30 May 2025 14:16:51 +0200 Subject: [PATCH] Add UserController --- config/config.exs | 5 ++ lib/nulla_web/controllers/user_controller.ex | 64 +++++++++++++++++++ lib/nulla_web/controllers/user_html.ex | 10 +++ .../show.html.heex} | 0 lib/nulla_web/router.ex | 4 +- 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 lib/nulla_web/controllers/user_controller.ex create mode 100644 lib/nulla_web/controllers/user_html.ex rename lib/nulla_web/controllers/{page_html/profile.html.heex => user_html/show.html.heex} (100%) diff --git a/config/config.exs b/config/config.exs index 52e443a..2c52b8a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -61,6 +61,11 @@ config :logger, :console, # Use Jason for JSON parsing in Phoenix config :phoenix, :json_library, Jason +config :mime, :types, %{ + "application/activity+json" => ["activity+json"], + "application/ld+json" => ["ld+json"] +} + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{config_env()}.exs" diff --git a/lib/nulla_web/controllers/user_controller.ex b/lib/nulla_web/controllers/user_controller.ex new file mode 100644 index 0000000..b152141 --- /dev/null +++ b/lib/nulla_web/controllers/user_controller.ex @@ -0,0 +1,64 @@ +defmodule NullaWeb.UserController do + use NullaWeb, :controller + + def show(conn, %{"username" => username}) do + accept = get_req_header(conn, "accept") |> List.first() || "" + + if String.contains?(accept, "application/activity+json") or String.contains?(accept, "application/ld+json") do + conn + |> put_resp_content_type("application/activity+json") + |> json(%{ + id: "https://localhost/@#{username}", + type: "Person", + following: "https://localhost/@#{username}/following", + followers: "https://localhost/@#{username}/followers", + inbox: "https://localhost/@#{username}/inbox", + outbox: "https://localhost/@#{username}/outbox", + featured: "https://localhost/@#{username}/collections/featured", + preferredUsername: "miraikumiko", + name: "Mirai Kumiko", + summary: "Lol Kek Cheburek", + url: "https://localhost/@#{username}", + manuallyApprovesFollowers: false, + discoverable: true, + indexable: true, + published: "2025-05-05T00:00:00Z", + memorial: false, + publicKey: %{ + id: "https://localhost/@#{username}#main-key", + owner: "https://localhost/@#{username}", + publicKeyPem: "public key" + }, + tag: [ + %{ + type: "Hashtag", + href: "https://localhost/tags/linux", + name: "#linux" + } + ], + attachment: [ + %{ + type: "PropertyValue", + name: "Website", + value: "https://miraikumiko.com" + } + ], + endpoints: %{ + sharedInbox: "https://localhost/inbox" + }, + icon: %{ + type: "Image", + mediaType: "image/jpeg", + url: "url" + }, + image: %{ + type: "Image", + mediaType: "image/jpeg", + url: "url" + } + }) + else + render(conn, :show, username: username, layout: false) + end + end +end diff --git a/lib/nulla_web/controllers/user_html.ex b/lib/nulla_web/controllers/user_html.ex new file mode 100644 index 0000000..3c0c6ba --- /dev/null +++ b/lib/nulla_web/controllers/user_html.ex @@ -0,0 +1,10 @@ +defmodule NullaWeb.UserHTML do + @moduledoc """ + This module contains pages rendered by UserController. + + See the `user_html` directory for all templates available. + """ + use NullaWeb, :html + + embed_templates "user_html/*" +end diff --git a/lib/nulla_web/controllers/page_html/profile.html.heex b/lib/nulla_web/controllers/user_html/show.html.heex similarity index 100% rename from lib/nulla_web/controllers/page_html/profile.html.heex rename to lib/nulla_web/controllers/user_html/show.html.heex diff --git a/lib/nulla_web/router.ex b/lib/nulla_web/router.ex index 5fb2edb..46a699c 100644 --- a/lib/nulla_web/router.ex +++ b/lib/nulla_web/router.ex @@ -2,7 +2,7 @@ defmodule NullaWeb.Router do use NullaWeb, :router pipeline :browser do - plug :accepts, ["html"] + plug :accepts, ["html", "activity+json", "ld+json"] plug :fetch_session plug :fetch_live_flash plug :put_root_layout, html: {NullaWeb.Layouts, :root} @@ -18,7 +18,7 @@ defmodule NullaWeb.Router do pipe_through :browser get "/", PageController, :home - get "/@:username", PageController, :profile + get "/@:username", UserController, :show end # Other scopes may use custom stacks.